Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service-api/go/initializer_test.go
2498 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 api_test
6
7
import (
8
"path/filepath"
9
"strings"
10
"testing"
11
12
"github.com/gitpod-io/gitpod/content-service/api"
13
"github.com/google/go-cmp/cmp"
14
"github.com/google/go-cmp/cmp/cmpopts"
15
"google.golang.org/protobuf/proto"
16
)
17
18
func TestGetCheckoutLocationsFromInitializer(t *testing.T) {
19
var init []*api.WorkspaceInitializer
20
init = append(init, &api.WorkspaceInitializer{
21
Spec: &api.WorkspaceInitializer_Git{
22
Git: &api.GitInitializer{
23
CheckoutLocation: "/foo",
24
CloneTaget: "head",
25
Config: &api.GitConfig{
26
Authentication: api.GitAuthMethod_NO_AUTH,
27
},
28
RemoteUri: "somewhere-else",
29
TargetMode: api.CloneTargetMode_LOCAL_BRANCH,
30
},
31
},
32
})
33
init = append(init, &api.WorkspaceInitializer{
34
Spec: &api.WorkspaceInitializer_Git{
35
Git: &api.GitInitializer{
36
CheckoutLocation: "/bar",
37
CloneTaget: "head",
38
Config: &api.GitConfig{
39
Authentication: api.GitAuthMethod_NO_AUTH,
40
},
41
RemoteUri: "somewhere-else",
42
TargetMode: api.CloneTargetMode_LOCAL_BRANCH,
43
},
44
},
45
})
46
47
tests := []struct {
48
Name string
49
Initializer *api.WorkspaceInitializer
50
Expectation string
51
}{
52
{
53
Name: "single git initializer",
54
Initializer: &api.WorkspaceInitializer{
55
Spec: &api.WorkspaceInitializer_Git{
56
Git: &api.GitInitializer{
57
CheckoutLocation: "/foo",
58
CloneTaget: "head",
59
Config: &api.GitConfig{
60
Authentication: api.GitAuthMethod_NO_AUTH,
61
},
62
RemoteUri: "somewhere-else",
63
TargetMode: api.CloneTargetMode_LOCAL_BRANCH,
64
},
65
},
66
},
67
Expectation: "/foo",
68
},
69
{
70
Name: "multiple git initializer",
71
Initializer: &api.WorkspaceInitializer{
72
Spec: &api.WorkspaceInitializer_Composite{
73
Composite: &api.CompositeInitializer{
74
Initializer: init,
75
},
76
},
77
},
78
Expectation: "/foo,/bar",
79
},
80
{
81
Name: "backup initializer",
82
Initializer: &api.WorkspaceInitializer{
83
Spec: &api.WorkspaceInitializer_Backup{
84
Backup: &api.FromBackupInitializer{
85
CheckoutLocation: "/foobar",
86
},
87
},
88
},
89
Expectation: "/foobar",
90
},
91
{
92
Name: "prebuild initializer",
93
Initializer: &api.WorkspaceInitializer{
94
Spec: &api.WorkspaceInitializer_Prebuild{
95
Prebuild: &api.PrebuildInitializer{
96
Git: []*api.GitInitializer{
97
{CheckoutLocation: "/foo"},
98
{CheckoutLocation: "/bar"},
99
},
100
},
101
},
102
},
103
Expectation: "/foo,/bar",
104
},
105
{
106
Name: "nil initializer",
107
},
108
{
109
Name: "snapshot initializer",
110
Initializer: &api.WorkspaceInitializer{
111
Spec: &api.WorkspaceInitializer_Snapshot{
112
Snapshot: &api.SnapshotInitializer{
113
Snapshot: "foo",
114
FromVolumeSnapshot: true,
115
},
116
},
117
},
118
},
119
}
120
121
for _, test := range tests {
122
t.Run(test.Name, func(t *testing.T) {
123
locations := strings.Join(api.GetCheckoutLocationsFromInitializer(test.Initializer), ",")
124
if locations != test.Expectation {
125
t.Errorf("expected %s, got %s", test.Expectation, locations)
126
}
127
})
128
}
129
}
130
131
func TestExtractInjectSecretsFromInitializer(t *testing.T) {
132
tests := []struct {
133
Name string
134
Input *api.WorkspaceInitializer
135
Expectation map[string]string
136
}{
137
{
138
Name: "git initializer",
139
Input: &api.WorkspaceInitializer{
140
Spec: &api.WorkspaceInitializer_Git{
141
Git: &api.GitInitializer{
142
Config: &api.GitConfig{
143
AuthPassword: "foobar",
144
},
145
},
146
},
147
},
148
Expectation: map[string]string{
149
"initializer.git": "foobar",
150
},
151
},
152
{
153
Name: "no secret git initializer",
154
Input: &api.WorkspaceInitializer{
155
Spec: &api.WorkspaceInitializer_Git{
156
Git: &api.GitInitializer{
157
Config: &api.GitConfig{},
158
},
159
},
160
},
161
Expectation: map[string]string{},
162
},
163
{
164
Name: "prebuild initializer",
165
Input: &api.WorkspaceInitializer{
166
Spec: &api.WorkspaceInitializer_Prebuild{
167
Prebuild: &api.PrebuildInitializer{
168
Git: []*api.GitInitializer{
169
{
170
Config: &api.GitConfig{
171
AuthPassword: "foobar",
172
},
173
},
174
{
175
Config: &api.GitConfig{
176
AuthPassword: "some value",
177
},
178
},
179
},
180
},
181
},
182
},
183
Expectation: map[string]string{
184
"initializer.prebuild.0.git": "foobar",
185
"initializer.prebuild.1.git": "some value",
186
},
187
},
188
}
189
190
for _, test := range tests {
191
t.Run(test.Name, func(t *testing.T) {
192
original := proto.Clone(test.Input)
193
act := api.GatherSecretsFromInitializer(test.Input)
194
if diff := cmp.Diff(test.Expectation, act); diff != "" {
195
t.Errorf("unexpected GatherSecretsFromInitializer (-want +got):\n%s", diff)
196
}
197
198
ignoreUnexported := []interface{}{
199
api.WorkspaceInitializer{},
200
api.WorkspaceInitializer_Git{},
201
api.GitInitializer{},
202
api.GitConfig{},
203
api.PrebuildInitializer{},
204
}
205
if diff := cmp.Diff(original, test.Input, cmpopts.IgnoreUnexported(ignoreUnexported...)); diff != "" {
206
t.Errorf("unexpected alteration from GatherSecretsFromInitializer (-want +got):\n%s", diff)
207
}
208
209
act = api.ExtractAndReplaceSecretsFromInitializer(test.Input)
210
if diff := cmp.Diff(test.Expectation, act); diff != "" {
211
t.Errorf("unexpected ExtractSecretsFromInitializer (-want +got):\n%s", diff)
212
}
213
214
_ = api.WalkInitializer(nil, test.Input, func(path []string, init *api.WorkspaceInitializer) error {
215
git, ok := init.Spec.(*api.WorkspaceInitializer_Git)
216
if !ok {
217
return nil
218
}
219
if pwd := git.Git.Config.AuthPassword; pwd != "" && !strings.HasPrefix(pwd, "extracted-secret/") {
220
t.Errorf("expected authPassword to be extracted, but got %s at %s", pwd, filepath.Join(path...))
221
}
222
223
return nil
224
})
225
226
injection := make(map[string][]byte, len(act))
227
for k, v := range act {
228
injection[k] = []byte(v)
229
}
230
231
err := api.InjectSecretsToInitializer(test.Input, injection)
232
if err != nil {
233
t.Fatal(err)
234
}
235
236
_ = api.WalkInitializer(nil, test.Input, func(path []string, init *api.WorkspaceInitializer) error {
237
git, ok := init.Spec.(*api.WorkspaceInitializer_Git)
238
if !ok {
239
return nil
240
}
241
if pwd := git.Git.Config.AuthPassword; pwd != "" && strings.HasPrefix(pwd, "extracted-secret/") {
242
t.Errorf("expected authPassword to be injected, but got %s at %s", pwd, filepath.Join(path...))
243
}
244
245
return nil
246
})
247
})
248
}
249
}
250
251