Path: blob/main/components/ide/jetbrains/launcher/main_test.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package main56import (7"fmt"8"os"9"path/filepath"10"strings"11"testing"1213protocol "github.com/gitpod-io/gitpod/gitpod-protocol"14"github.com/google/go-cmp/cmp"15"github.com/google/go-cmp/cmp/cmpopts"16"github.com/stretchr/testify/assert"17)1819func TestGetProductConfig(t *testing.T) {20expectation := &protocol.JetbrainsProduct{}21actual := getProductConfig(&protocol.GitpodConfig{22Jetbrains: &protocol.Jetbrains{23Intellij: expectation,24},25}, "intellij")2627if diff := cmp.Diff(expectation, actual); diff != "" {28t.Errorf("unexpected output (-want +got):\n%s", diff)29}30}3132func TestParseGitpodConfig(t *testing.T) {33gitpodConfig, _ := parseGitpodConfig("testdata")34assert.Equal(t, 1, len(gitpodConfig.Jetbrains.Intellij.Plugins))35assert.Equal(t, "both", gitpodConfig.Jetbrains.Intellij.Prebuilds.Version)36assert.Equal(t, "-Xmx3g", gitpodConfig.Jetbrains.Intellij.Vmoptions)37assert.Equal(t, "-Xmx4096m -XX:MaxRAMPercentage=75", gitpodConfig.Jetbrains.Goland.Vmoptions)38}3940func TestUpdateVMOptions(t *testing.T) {41tests := []struct {42Desc string43Alias string44EnvVar map[string]string45Src string46Expectation string47}{48{"idea64.vmoptions (GITPOD_CPU_COUNT)", "intellij", map[string]string{"INTELLIJ_VMOPTIONS": "-Xmx4096m", "GITPOD_CPU_COUNT": "1"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx4096m\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-XX:ActiveProcessorCount=1\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true"},49{"idea64.vmoptions (GITPOD_CPU_COUNT 2)", "intellij", map[string]string{"INTELLIJ_VMOPTIONS": "-Xmx4096m", "GITPOD_CPU_COUNT": "12"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx4096m\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-XX:ActiveProcessorCount=12\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true"},50{"goland64.vmoptions", "goland", nil, "-Xms128m\n-Xmx750m\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx750m\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true"},51{"idea64.vmoptions", "intellij", nil, "-Xms128m\n-Xmx750m\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx750m\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true"},52{"idea64.vmoptions (INTELLIJ_VMOPTIONS env set)", "intellij", map[string]string{"INTELLIJ_VMOPTIONS": "-Xmx2048m"}, "-Xms128m\n-Xmx750m\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx2048m\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true"},53{"idea64.vmoptions (INTELLIJ_VMOPTIONS env set)", "intellij", map[string]string{"INTELLIJ_VMOPTIONS": "-Xmx4096m"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx4096m\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true"},54{"idea64.vmoptions (INTELLIJ_VMOPTIONS env set)", "intellij", map[string]string{"INTELLIJ_VMOPTIONS": "-Xmx4096m -XX:MaxRAMPercentage=75"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx4096m\n-XX:MaxRAMPercentage=75\n-Dsun.tools.attach.tmp.only=true\n-XX:+UseContainerSupport\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true"},55{"goland64.vmoptions (GOLAND_VMOPTIONS env set with conflicting options)", "goland", map[string]string{"GOLAND_VMOPTIONS": "-ea -XX:+IgnoreUnrecognizedVMOptions -XX:MaxRAMPercentage=75 -XX:MaxRAMPercentage=50"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-ea\n-XX:+IgnoreUnrecognizedVMOptions\n-XX:+UseContainerSupport\n-XX:MaxRAMPercentage=50"},56{"GITPOD_MEMORY with 1G", "intellij", map[string]string{"GITPOD_MEMORY": "1024"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true\n-XX:+UseContainerSupport\n"},57{"GITPOD_MEMORY with 4G", "intellij", map[string]string{"GITPOD_MEMORY": "4096"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx2457m\n-Dsun.tools.attach.tmp.only=true\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true\n-XX:+UseContainerSupport\n"},58{"GITPOD_MEMORY with 256G will use max 8G", "intellij", map[string]string{"GITPOD_MEMORY": "262144"}, "-Xms128m\n-Xmx2g\n-Dsun.tools.attach.tmp.only=true", "-Xms128m\n-Xmx8192m\n-Dsun.tools.attach.tmp.only=true\n-Dfreeze.reporter.profiling=false\n-Dgtw.disable.exit.dialog=true\n-Djdk.configure.existing=true\n-XX:+UseContainerSupport\n"},59}60os.Unsetenv("GITPOD_CPU_COUNT")61os.Unsetenv("GITPOD_MEMORY")62for _, test := range tests {63// compare vmoptions string content equality (i.e. split into slices and compare ignore order)64lessFunc := func(a, b string) bool { return a < b }6566t.Run(test.Desc, func(t *testing.T) {67for v := range test.EnvVar {68t.Setenv(v, test.EnvVar[v])69}70actual := updateVMOptions(nil, test.Alias, strings.Fields(test.Src))71if diff := cmp.Diff(strings.Fields(test.Expectation), actual, cmpopts.SortSlices(lessFunc)); diff != "" {72t.Errorf("unexpected output (-want +got):\n%s", diff)73}74})7576t.Run("updateVMOptions multiple time should be stable", func(t *testing.T) {77for v := range test.EnvVar {78t.Setenv(v, test.EnvVar[v])79}80actual := strings.Fields(test.Src)81for i := 0; i < 5; i++ {82actual = updateVMOptions(nil, test.Alias, actual)83if diff := cmp.Diff(strings.Fields(test.Expectation), actual, cmpopts.SortSlices(lessFunc)); diff != "" {84t.Errorf("unexpected output (-want +got):\n%s", diff)85}86}87})88}89}9091func TestUpdatePlatformProperties(t *testing.T) {92platformProperties := `#---------------------------------------------------------------------93# Uncomment this option if you want to customize a path to the settings directory.94#---------------------------------------------------------------------95# idea.config.path=${user.home}/.IntelliJIdea/config969798shared.indexes.download.auto.consent=true99`100configDir := "/workspace/.config/JetBrains/RemoteDev-IU"101systemDir := "/workspace/.cache/JetBrains/RemoteDev-IU"102103t.Run("updatePlatformProperties multiple time should be stable", func(t *testing.T) {104105updated, content := updatePlatformProperties(platformProperties, configDir, systemDir)106assert.Equal(t, true, updated)107assert.Equal(t, true, strings.Contains(content, "idea.config.path=/workspace/.config/JetBrains/RemoteDev-IU"))108for i := 0; i < 5; i++ {109updated, newContent := updatePlatformProperties(content, configDir, systemDir)110assert.Equal(t, false, updated)111assert.Equal(t, content, newContent)112}113})114}115116func Test_safeLink(t *testing.T) {117type args struct {118changeSource bool119}120t.Log("link folders twice")121tests := []struct {122name string123args args124wantErr bool125}{126{name: "happy path", args: args{changeSource: true}, wantErr: false},127{name: "happy path 2", args: args{changeSource: false}, wantErr: false},128}129for index, tt := range tests {130t.Run(tt.name, func(t *testing.T) {131source := createTempDir(t, fmt.Sprintf("source_%d", index))132target := createTempDir(t, fmt.Sprintf("target_%d", index))133safeLink(source, target)134os.RemoveAll(source)135if tt.args.changeSource {136source = createTempDir(t, fmt.Sprintf("source_new_%d", index))137} else {138source = createTempDir(t, fmt.Sprintf("source_%d", index))139}140if err := safeLink(source, target); (err != nil) != tt.wantErr {141t.Errorf("safeLink() error = %v, wantErr %v", err, tt.wantErr)142}143})144}145}146147func createTempDir(t *testing.T, dir string) string {148path := filepath.Join(t.TempDir(), dir)149if err := os.Mkdir(path, 0o755); err != nil {150t.Fatal(err)151}152return path153}154155156