-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
contexts_test.go
223 lines (193 loc) · 5.93 KB
/
contexts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package workspace
import (
"context"
"os"
"testing"
"time"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
"github.com/gitpod-io/gitpod/test/pkg/integration"
"github.com/gitpod-io/gitpod/test/pkg/integration/common"
)
type ContextTest struct {
Skip bool
Name string
ContextURL string
WorkspaceRoot string
ExpectedBranch string
ExpectedBranchFunc func(username string) string
IgnoreError bool
}
func TestGitHubContexts(t *testing.T) {
tests := []ContextTest{
{
Name: "open repository",
ContextURL: "github.com/gitpod-io/template-golang-cli",
WorkspaceRoot: "/workspace/template-golang-cli",
ExpectedBranch: "main",
},
{
Name: "open branch",
ContextURL: "github.com/gitpod-io/gitpod-test-repo/tree/integration-test-1",
WorkspaceRoot: "/workspace/gitpod-test-repo",
ExpectedBranch: "integration-test-1",
},
{
// Branch name decisions are not tested in the workspace as it is the server side logic
Name: "open issue",
ContextURL: "github.com/gitpod-io/gitpod-test-repo/issues/88",
WorkspaceRoot: "/workspace/gitpod-test-repo",
},
{
Name: "open tag",
ContextURL: "github.com/gitpod-io/gitpod-test-repo/tree/integration-test-context-tag",
WorkspaceRoot: "/workspace/gitpod-test-repo",
ExpectedBranch: "HEAD",
},
{
Name: "Git LFS support",
ContextURL: "github.com/atduarte/lfs-test",
WorkspaceRoot: "/workspace/lfs-test",
ExpectedBranch: "main",
},
{
Name: "empty repo",
ContextURL: "github.com/gitpod-io/empty",
WorkspaceRoot: "/workspace/empty",
ExpectedBranch: "HEAD",
IgnoreError: true,
},
}
runContextTests(t, tests)
}
func TestGitLabContexts(t *testing.T) {
if !gitlab {
t.Skip("Skipping gitlab integration tests")
}
tests := []ContextTest{
{
Name: "open repository",
ContextURL: "gitlab.com/AlexTugarev/gp-test",
WorkspaceRoot: "/workspace/gp-test",
ExpectedBranch: "master",
},
{
Name: "open branch",
ContextURL: "gitlab.com/AlexTugarev/gp-test/tree/wip",
WorkspaceRoot: "/workspace/gp-test",
ExpectedBranch: "wip",
},
{
Name: "open issue",
ContextURL: "gitlab.com/AlexTugarev/gp-test/issues/1",
WorkspaceRoot: "/workspace/gp-test",
},
{
Name: "open tag",
ContextURL: "gitlab.com/AlexTugarev/gp-test/merge_requests/2",
WorkspaceRoot: "/workspace/gp-test",
ExpectedBranch: "wip2",
},
}
runContextTests(t, tests)
}
func runContextTests(t *testing.T, tests []ContextTest) {
userToken, _ := os.LookupEnv("USER_TOKEN")
integration.SkipWithoutUsername(t, username)
integration.SkipWithoutUserToken(t, userToken)
parallelLimiter := make(chan struct{}, 2)
f := features.New("context").
WithLabel("component", "server").
Assess("should run context tests", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
ffs := []struct {
Name string
FF string
}{
{Name: "classic"},
{Name: "pvc", FF: "persistent_volume_claim"},
}
for _, ff := range ffs {
func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
defer api.Done(t)
username := username + ff.Name
userId, err := api.CreateUser(username, userToken)
if err != nil {
t.Fatal(err)
}
if err := api.UpdateUserFeatureFlag(userId, ff.FF); err != nil {
t.Fatal(err)
}
}()
}
for _, ff := range ffs {
for _, test := range tests {
t.Run(test.ContextURL+"_"+ff.Name, func(t *testing.T) {
if test.Skip {
t.SkipNow()
}
t.Logf("Waiting %s", test.ContextURL+"_"+ff.Name)
t.Parallel()
parallelLimiter <- struct{}{}
defer func() {
<-parallelLimiter
}()
t.Logf("Running %s", test.ContextURL+"_"+ff.Name)
username := username + ff.Name
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
defer cancel()
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
defer api.Done(t)
nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, test.ContextURL, username, api)
if err != nil {
t.Fatal(err)
}
defer func() {
sctx, scancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer scancel()
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
defer sapi.Done(t)
_, err := stopWs(true, sapi)
if err != nil {
t.Fatal(err)
}
}()
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.LatestInstance.ID))
if err != nil {
t.Fatal(err)
}
defer rsa.Close()
integration.DeferCloser(t, closer)
if test.ExpectedBranch == "" && test.ExpectedBranchFunc == nil {
return
}
// get actual from workspace
git := common.Git(rsa)
err = git.ConfigSafeDirectory()
if err != nil {
t.Fatal(err)
}
actBranch, err := git.GetBranch(test.WorkspaceRoot, test.IgnoreError)
if err != nil {
t.Fatal(err)
}
expectedBranch := test.ExpectedBranch
if test.ExpectedBranchFunc != nil {
expectedBranch = test.ExpectedBranchFunc(username)
}
if actBranch != expectedBranch {
t.Fatalf("expected branch '%s', got '%s'!", expectedBranch, actBranch)
}
})
}
}
return ctx
}).
Feature()
testEnv.Test(t, f)
}