Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/components/ws-manager/multi_repo_test.go
2500 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 wsmanager
6
7
import (
8
"context"
9
"path/filepath"
10
"strings"
11
"testing"
12
"time"
13
14
"sigs.k8s.io/e2e-framework/pkg/envconf"
15
"sigs.k8s.io/e2e-framework/pkg/features"
16
17
csapi "github.com/gitpod-io/gitpod/content-service/api"
18
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
19
"github.com/gitpod-io/gitpod/test/pkg/integration"
20
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
21
)
22
23
var repos = []struct {
24
RemoteUri string
25
CloneTarget string
26
ExpectedBranch string
27
CheckoutLocation string
28
}{
29
{
30
RemoteUri: "https://github.com/gitpod-io/gitpod",
31
CloneTarget: "main",
32
ExpectedBranch: "main",
33
CheckoutLocation: "gitpod",
34
},
35
{
36
RemoteUri: "https://github.com/gitpod-io/gitpod",
37
CloneTarget: "master",
38
ExpectedBranch: "main",
39
CheckoutLocation: "gitpod",
40
},
41
{
42
RemoteUri: "https://github.com/gitpod-io/workspace-images",
43
CloneTarget: "main",
44
ExpectedBranch: "main",
45
CheckoutLocation: "workspace-images",
46
},
47
{
48
RemoteUri: "https://github.com/gitpod-io/dazzle",
49
CloneTarget: "main",
50
ExpectedBranch: "main",
51
CheckoutLocation: "dazzle",
52
},
53
{
54
RemoteUri: "https://github.com/gitpod-io/leeway",
55
CloneTarget: "main",
56
ExpectedBranch: "main",
57
CheckoutLocation: "leeway",
58
},
59
{
60
RemoteUri: "https://github.com/gitpod-io/ws-manager-integration-test",
61
CloneTarget: "master", // default branch is main
62
ExpectedBranch: "master",
63
CheckoutLocation: "ws-manager-integration-test",
64
},
65
}
66
67
func TestMultiRepoWorkspaceSuccess(t *testing.T) {
68
f := features.New("multi-repo").WithLabel("component", "ws-manager").Assess("can create multi repo workspace", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
69
t.Parallel()
70
71
ctx, cancel := context.WithTimeout(testCtx, 5*time.Minute)
72
defer cancel()
73
74
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
75
t.Cleanup(func() {
76
api.Done(t)
77
})
78
79
multiRepoInit := func(swr *wsmanapi.StartWorkspaceRequest) error {
80
composite := &csapi.CompositeInitializer{}
81
initializers := []*csapi.WorkspaceInitializer{}
82
83
for _, repo := range repos {
84
init := &csapi.WorkspaceInitializer{
85
Spec: &csapi.WorkspaceInitializer_Git{
86
Git: &csapi.GitInitializer{
87
RemoteUri: repo.RemoteUri,
88
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
89
CloneTaget: repo.CloneTarget,
90
CheckoutLocation: repo.CheckoutLocation,
91
Config: &csapi.GitConfig{},
92
},
93
},
94
}
95
96
initializers = append(initializers, init)
97
}
98
99
composite.Initializer = initializers
100
swr.Spec.Initializer = &csapi.WorkspaceInitializer{
101
Spec: &csapi.WorkspaceInitializer_Composite{
102
Composite: &csapi.CompositeInitializer{
103
Initializer: initializers,
104
},
105
},
106
}
107
swr.Spec.WorkspaceLocation = "gitpod"
108
return nil
109
}
110
111
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(multiRepoInit))
112
if err != nil {
113
t.Fatal(err)
114
}
115
116
t.Cleanup(func() {
117
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
118
defer scancel()
119
120
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
121
defer sapi.Done(t)
122
123
_, err = stopWs(true, sapi)
124
if err != nil {
125
t.Errorf("cannot stop workspace: %q", err)
126
}
127
})
128
129
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),
130
integration.WithInstanceID(ws.Req.Id),
131
integration.WithContainer("workspace"),
132
integration.WithWorkspacekitLift(true),
133
)
134
if err != nil {
135
t.Fatal(err)
136
}
137
138
integration.DeferCloser(t, closer)
139
defer rsa.Close()
140
141
assertRepositories(t, rsa)
142
143
return testCtx
144
}).Feature()
145
146
testEnv.Test(t, f)
147
}
148
149
func assertRepositories(t *testing.T, rsa *integration.RpcClient) {
150
var ls agent.ListDirResponse
151
err := rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{
152
Dir: "/workspace",
153
}, &ls)
154
155
if err != nil {
156
t.Fatal(err)
157
}
158
159
expected := make(map[string]*struct {
160
Cloned bool
161
Branch string
162
})
163
for _, r := range repos {
164
expected[r.CheckoutLocation] = &struct {
165
Cloned bool
166
Branch string
167
}{
168
Cloned: false,
169
Branch: r.ExpectedBranch,
170
}
171
}
172
173
for _, dir := range ls.Files {
174
if strings.HasPrefix(dir, ".") {
175
continue
176
}
177
if _, ok := expected[dir]; ok {
178
expected[dir].Cloned = true
179
} else {
180
t.Fatalf("unexpected repository %s", dir)
181
}
182
}
183
184
git := integration.Git(rsa)
185
186
for k, v := range expected {
187
if !v.Cloned {
188
t.Fatalf("repository %s has not been cloned", k)
189
}
190
191
branch, err := git.GetBranch(filepath.Join("/workspace", k), false)
192
if err != nil {
193
t.Fatal(err)
194
}
195
196
if branch != v.Branch {
197
t.Fatalf("expected branch %s, but got %s", v.Branch, branch)
198
}
199
}
200
}
201
202