Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/workspace/contexts_test.go
2498 views
1
// Copyright (c) 2020 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 workspace
6
7
import (
8
"context"
9
"fmt"
10
"os"
11
"testing"
12
"time"
13
14
"sigs.k8s.io/e2e-framework/pkg/envconf"
15
"sigs.k8s.io/e2e-framework/pkg/features"
16
17
"github.com/gitpod-io/gitpod/test/pkg/integration"
18
"github.com/gitpod-io/gitpod/test/pkg/report"
19
)
20
21
type ContextTest struct {
22
Skip bool
23
Name string
24
ContextURL string
25
WorkspaceRoot string
26
ExpectedBranch string
27
ExpectedBranchFunc func(username string) string
28
IgnoreError bool
29
}
30
31
func TestGitHubContexts(t *testing.T) {
32
tests := []ContextTest{
33
{
34
Name: "open repository",
35
ContextURL: "github.com/gitpod-io/template-golang-cli",
36
WorkspaceRoot: "/workspace/template-golang-cli",
37
ExpectedBranch: "main",
38
},
39
{
40
Name: "open branch",
41
ContextURL: "github.com/gitpod-io/gitpod-test-repo/tree/integration-test-1",
42
WorkspaceRoot: "/workspace/gitpod-test-repo",
43
ExpectedBranch: "integration-test-1",
44
},
45
{
46
// Branch name decisions are not tested in the workspace as it is the server side logic
47
Name: "open issue",
48
ContextURL: "github.com/gitpod-io/gitpod-test-repo/issues/88",
49
WorkspaceRoot: "/workspace/gitpod-test-repo",
50
},
51
{
52
Name: "open tag",
53
ContextURL: "github.com/gitpod-io/gitpod-test-repo/tree/integration-test-context-tag",
54
WorkspaceRoot: "/workspace/gitpod-test-repo",
55
ExpectedBranch: "HEAD",
56
},
57
{
58
Name: "Git LFS support",
59
ContextURL: "github.com/atduarte/lfs-test",
60
WorkspaceRoot: "/workspace/lfs-test",
61
ExpectedBranch: "main",
62
},
63
{
64
Name: "empty repo",
65
ContextURL: "github.com/gitpod-io/empty",
66
WorkspaceRoot: "/workspace/empty",
67
ExpectedBranch: "HEAD",
68
IgnoreError: true,
69
},
70
}
71
runContextTests(t, tests)
72
}
73
74
func TestGitLabContexts(t *testing.T) {
75
if !gitlab {
76
t.Skip("Skipping gitlab integration tests")
77
}
78
tests := []ContextTest{
79
{
80
Name: "open repository",
81
ContextURL: "gitlab.com/AlexTugarev/gp-test",
82
WorkspaceRoot: "/workspace/gp-test",
83
ExpectedBranch: "master",
84
},
85
{
86
Name: "open branch",
87
ContextURL: "gitlab.com/AlexTugarev/gp-test/tree/wip",
88
WorkspaceRoot: "/workspace/gp-test",
89
ExpectedBranch: "wip",
90
},
91
{
92
Name: "open issue",
93
ContextURL: "gitlab.com/AlexTugarev/gp-test/issues/1",
94
WorkspaceRoot: "/workspace/gp-test",
95
},
96
{
97
Name: "open tag",
98
ContextURL: "gitlab.com/AlexTugarev/gp-test/merge_requests/2",
99
WorkspaceRoot: "/workspace/gp-test",
100
ExpectedBranch: "wip2",
101
},
102
}
103
runContextTests(t, tests)
104
}
105
106
func runContextTests(t *testing.T, tests []ContextTest) {
107
userToken, _ := os.LookupEnv("USER_TOKEN")
108
integration.SkipWithoutUsername(t, username)
109
integration.SkipWithoutUserToken(t, userToken)
110
111
f := features.New("context").
112
WithLabel("component", "server").
113
Assess("should run context tests", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
114
115
sctx, scancel := context.WithTimeout(testCtx, time.Duration(10*len(tests))*time.Minute)
116
defer scancel()
117
118
api := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
119
defer api.Done(t)
120
121
for _, test := range tests {
122
test := test
123
t.Run(test.ContextURL, func(t *testing.T) {
124
report.SetupReport(t, report.FeatureContentInit, fmt.Sprintf("Test to open %v", test.ContextURL))
125
if test.Skip {
126
t.SkipNow()
127
}
128
129
t.Parallel()
130
131
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*len(tests))*time.Minute)
132
defer cancel()
133
134
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
135
defer api.Done(t)
136
137
_, err := api.CreateUser(username, userToken)
138
if err != nil {
139
t.Fatal(err)
140
}
141
142
nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, test.ContextURL, username, api)
143
if err != nil {
144
t.Fatal(err)
145
}
146
147
t.Cleanup(func() {
148
sctx, scancel := context.WithTimeout(context.Background(), 10*time.Minute)
149
defer scancel()
150
151
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
152
defer sapi.Done(t)
153
154
_, err := stopWs(true, sapi)
155
if err != nil {
156
t.Fatal(err)
157
}
158
})
159
160
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.LatestInstance.ID))
161
if err != nil {
162
t.Fatal(err)
163
}
164
defer rsa.Close()
165
integration.DeferCloser(t, closer)
166
167
if test.ExpectedBranch == "" && test.ExpectedBranchFunc == nil {
168
return
169
}
170
171
// get actual from workspace
172
git := integration.Git(rsa)
173
err = git.ConfigSafeDirectory()
174
if err != nil {
175
t.Fatal(err)
176
}
177
actBranch, err := git.GetBranch(test.WorkspaceRoot, test.IgnoreError)
178
if err != nil {
179
t.Fatal(err)
180
}
181
182
expectedBranch := test.ExpectedBranch
183
if test.ExpectedBranchFunc != nil {
184
expectedBranch = test.ExpectedBranchFunc(username)
185
}
186
if actBranch != expectedBranch {
187
t.Fatalf("expected branch '%s', got '%s'!", expectedBranch, actBranch)
188
}
189
})
190
}
191
return testCtx
192
}).
193
Feature()
194
195
testEnv.Test(t, f)
196
}
197
198