Path: blob/main/test/tests/components/ws-manager/multi_repo_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package wsmanager56import (7"context"8"path/filepath"9"strings"10"testing"11"time"1213"sigs.k8s.io/e2e-framework/pkg/envconf"14"sigs.k8s.io/e2e-framework/pkg/features"1516csapi "github.com/gitpod-io/gitpod/content-service/api"17agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"18"github.com/gitpod-io/gitpod/test/pkg/integration"19wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"20)2122var repos = []struct {23RemoteUri string24CloneTarget string25ExpectedBranch string26CheckoutLocation string27}{28{29RemoteUri: "https://github.com/gitpod-io/gitpod",30CloneTarget: "main",31ExpectedBranch: "main",32CheckoutLocation: "gitpod",33},34{35RemoteUri: "https://github.com/gitpod-io/gitpod",36CloneTarget: "master",37ExpectedBranch: "main",38CheckoutLocation: "gitpod",39},40{41RemoteUri: "https://github.com/gitpod-io/workspace-images",42CloneTarget: "main",43ExpectedBranch: "main",44CheckoutLocation: "workspace-images",45},46{47RemoteUri: "https://github.com/gitpod-io/dazzle",48CloneTarget: "main",49ExpectedBranch: "main",50CheckoutLocation: "dazzle",51},52{53RemoteUri: "https://github.com/gitpod-io/leeway",54CloneTarget: "main",55ExpectedBranch: "main",56CheckoutLocation: "leeway",57},58{59RemoteUri: "https://github.com/gitpod-io/ws-manager-integration-test",60CloneTarget: "master", // default branch is main61ExpectedBranch: "master",62CheckoutLocation: "ws-manager-integration-test",63},64}6566func TestMultiRepoWorkspaceSuccess(t *testing.T) {67f := 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 {68t.Parallel()6970ctx, cancel := context.WithTimeout(testCtx, 5*time.Minute)71defer cancel()7273api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())74t.Cleanup(func() {75api.Done(t)76})7778multiRepoInit := func(swr *wsmanapi.StartWorkspaceRequest) error {79composite := &csapi.CompositeInitializer{}80initializers := []*csapi.WorkspaceInitializer{}8182for _, repo := range repos {83init := &csapi.WorkspaceInitializer{84Spec: &csapi.WorkspaceInitializer_Git{85Git: &csapi.GitInitializer{86RemoteUri: repo.RemoteUri,87TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,88CloneTaget: repo.CloneTarget,89CheckoutLocation: repo.CheckoutLocation,90Config: &csapi.GitConfig{},91},92},93}9495initializers = append(initializers, init)96}9798composite.Initializer = initializers99swr.Spec.Initializer = &csapi.WorkspaceInitializer{100Spec: &csapi.WorkspaceInitializer_Composite{101Composite: &csapi.CompositeInitializer{102Initializer: initializers,103},104},105}106swr.Spec.WorkspaceLocation = "gitpod"107return nil108}109110ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(multiRepoInit))111if err != nil {112t.Fatal(err)113}114115t.Cleanup(func() {116sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)117defer scancel()118119sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())120defer sapi.Done(t)121122_, err = stopWs(true, sapi)123if err != nil {124t.Errorf("cannot stop workspace: %q", err)125}126})127128rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),129integration.WithInstanceID(ws.Req.Id),130integration.WithContainer("workspace"),131integration.WithWorkspacekitLift(true),132)133if err != nil {134t.Fatal(err)135}136137integration.DeferCloser(t, closer)138defer rsa.Close()139140assertRepositories(t, rsa)141142return testCtx143}).Feature()144145testEnv.Test(t, f)146}147148func assertRepositories(t *testing.T, rsa *integration.RpcClient) {149var ls agent.ListDirResponse150err := rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{151Dir: "/workspace",152}, &ls)153154if err != nil {155t.Fatal(err)156}157158expected := make(map[string]*struct {159Cloned bool160Branch string161})162for _, r := range repos {163expected[r.CheckoutLocation] = &struct {164Cloned bool165Branch string166}{167Cloned: false,168Branch: r.ExpectedBranch,169}170}171172for _, dir := range ls.Files {173if strings.HasPrefix(dir, ".") {174continue175}176if _, ok := expected[dir]; ok {177expected[dir].Cloned = true178} else {179t.Fatalf("unexpected repository %s", dir)180}181}182183git := integration.Git(rsa)184185for k, v := range expected {186if !v.Cloned {187t.Fatalf("repository %s has not been cloned", k)188}189190branch, err := git.GetBranch(filepath.Join("/workspace", k), false)191if err != nil {192t.Fatal(err)193}194195if branch != v.Branch {196t.Fatalf("expected branch %s, but got %s", v.Branch, branch)197}198}199}200201202