Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide/jetbrains/launcher/main_test.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package main
6
7
import (
8
"fmt"
9
"os"
10
"path/filepath"
11
"strings"
12
"testing"
13
14
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
15
"github.com/google/go-cmp/cmp"
16
"github.com/google/go-cmp/cmp/cmpopts"
17
"github.com/stretchr/testify/assert"
18
)
19
20
func TestGetProductConfig(t *testing.T) {
21
expectation := &protocol.JetbrainsProduct{}
22
actual := getProductConfig(&protocol.GitpodConfig{
23
Jetbrains: &protocol.Jetbrains{
24
Intellij: expectation,
25
},
26
}, "intellij")
27
28
if diff := cmp.Diff(expectation, actual); diff != "" {
29
t.Errorf("unexpected output (-want +got):\n%s", diff)
30
}
31
}
32
33
func TestParseGitpodConfig(t *testing.T) {
34
gitpodConfig, _ := parseGitpodConfig("testdata")
35
assert.Equal(t, 1, len(gitpodConfig.Jetbrains.Intellij.Plugins))
36
assert.Equal(t, "both", gitpodConfig.Jetbrains.Intellij.Prebuilds.Version)
37
assert.Equal(t, "-Xmx3g", gitpodConfig.Jetbrains.Intellij.Vmoptions)
38
assert.Equal(t, "-Xmx4096m -XX:MaxRAMPercentage=75", gitpodConfig.Jetbrains.Goland.Vmoptions)
39
}
40
41
func TestUpdateVMOptions(t *testing.T) {
42
tests := []struct {
43
Desc string
44
Alias string
45
EnvVar map[string]string
46
Src string
47
Expectation string
48
}{
49
{"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"},
50
{"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"},
51
{"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"},
52
{"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"},
53
{"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"},
54
{"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"},
55
{"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"},
56
{"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"},
57
{"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"},
58
{"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"},
59
{"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"},
60
}
61
os.Unsetenv("GITPOD_CPU_COUNT")
62
os.Unsetenv("GITPOD_MEMORY")
63
for _, test := range tests {
64
// compare vmoptions string content equality (i.e. split into slices and compare ignore order)
65
lessFunc := func(a, b string) bool { return a < b }
66
67
t.Run(test.Desc, func(t *testing.T) {
68
for v := range test.EnvVar {
69
t.Setenv(v, test.EnvVar[v])
70
}
71
actual := updateVMOptions(nil, test.Alias, strings.Fields(test.Src))
72
if diff := cmp.Diff(strings.Fields(test.Expectation), actual, cmpopts.SortSlices(lessFunc)); diff != "" {
73
t.Errorf("unexpected output (-want +got):\n%s", diff)
74
}
75
})
76
77
t.Run("updateVMOptions multiple time should be stable", func(t *testing.T) {
78
for v := range test.EnvVar {
79
t.Setenv(v, test.EnvVar[v])
80
}
81
actual := strings.Fields(test.Src)
82
for i := 0; i < 5; i++ {
83
actual = updateVMOptions(nil, test.Alias, actual)
84
if diff := cmp.Diff(strings.Fields(test.Expectation), actual, cmpopts.SortSlices(lessFunc)); diff != "" {
85
t.Errorf("unexpected output (-want +got):\n%s", diff)
86
}
87
}
88
})
89
}
90
}
91
92
func TestUpdatePlatformProperties(t *testing.T) {
93
platformProperties := `#---------------------------------------------------------------------
94
# Uncomment this option if you want to customize a path to the settings directory.
95
#---------------------------------------------------------------------
96
# idea.config.path=${user.home}/.IntelliJIdea/config
97
98
99
shared.indexes.download.auto.consent=true
100
`
101
configDir := "/workspace/.config/JetBrains/RemoteDev-IU"
102
systemDir := "/workspace/.cache/JetBrains/RemoteDev-IU"
103
104
t.Run("updatePlatformProperties multiple time should be stable", func(t *testing.T) {
105
106
updated, content := updatePlatformProperties(platformProperties, configDir, systemDir)
107
assert.Equal(t, true, updated)
108
assert.Equal(t, true, strings.Contains(content, "idea.config.path=/workspace/.config/JetBrains/RemoteDev-IU"))
109
for i := 0; i < 5; i++ {
110
updated, newContent := updatePlatformProperties(content, configDir, systemDir)
111
assert.Equal(t, false, updated)
112
assert.Equal(t, content, newContent)
113
}
114
})
115
}
116
117
func Test_safeLink(t *testing.T) {
118
type args struct {
119
changeSource bool
120
}
121
t.Log("link folders twice")
122
tests := []struct {
123
name string
124
args args
125
wantErr bool
126
}{
127
{name: "happy path", args: args{changeSource: true}, wantErr: false},
128
{name: "happy path 2", args: args{changeSource: false}, wantErr: false},
129
}
130
for index, tt := range tests {
131
t.Run(tt.name, func(t *testing.T) {
132
source := createTempDir(t, fmt.Sprintf("source_%d", index))
133
target := createTempDir(t, fmt.Sprintf("target_%d", index))
134
safeLink(source, target)
135
os.RemoveAll(source)
136
if tt.args.changeSource {
137
source = createTempDir(t, fmt.Sprintf("source_new_%d", index))
138
} else {
139
source = createTempDir(t, fmt.Sprintf("source_%d", index))
140
}
141
if err := safeLink(source, target); (err != nil) != tt.wantErr {
142
t.Errorf("safeLink() error = %v, wantErr %v", err, tt.wantErr)
143
}
144
})
145
}
146
}
147
148
func createTempDir(t *testing.T, dir string) string {
149
path := filepath.Join(t.TempDir(), dir)
150
if err := os.Mkdir(path, 0o755); err != nil {
151
t.Fatal(err)
152
}
153
return path
154
}
155
156