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