Path: blob/main/test/tests/components/ws-manager/git_status_test.go
2500 views
// Copyright (c) 2020 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"fmt"9"testing"10"time"1112"google.golang.org/protobuf/proto"13"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)2122// TestGitStatus tests that the git status is reported after a workspace is stopped.23func TestGitStatus(t *testing.T) {24f := features.New("git-status").25WithLabel("component", "ws-manager").26Assess("it should report the git status of a workspace when it stops", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {27tests := []struct {28Name string29ContextURL string30WorkspaceRoot string31CheckoutLocation string32}{33{34Name: "classic",35ContextURL: "https://github.com/gitpod-io/empty",36WorkspaceRoot: "/workspace/empty",37CheckoutLocation: "empty",38},39}40for _, test := range tests {41test := test42t.Run(test.Name, func(t *testing.T) {43t.Parallel()4445ctx, cancel := context.WithTimeout(testCtx, time.Duration(10*len(tests))*time.Minute)46defer cancel()4748api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())49t.Cleanup(func() {50api.Done(t)51})5253// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow54// which is client -> server -> ws-manager rather than client -> ws-manager directly55ws1, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {56w.Spec.Initializer = &csapi.WorkspaceInitializer{57Spec: &csapi.WorkspaceInitializer_Git{58Git: &csapi.GitInitializer{59RemoteUri: test.ContextURL,60CheckoutLocation: test.CheckoutLocation,61Config: &csapi.GitConfig{},62},63},64}65w.Spec.WorkspaceLocation = test.CheckoutLocation66return nil67}))68if err != nil {69t.Fatal(err)70}71t.Cleanup(func() {72if err != nil {73sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)74defer scancel()7576sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())77defer sapi.Done(t)7879_, err = stopWs(true, sapi)80if err != nil {81t.Fatal(err)82}83}84})8586rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),87integration.WithInstanceID(ws1.Req.Id),88integration.WithContainer("workspace"),89integration.WithWorkspacekitLift(true),90)91if err != nil {92t.Fatal(err)93}94integration.DeferCloser(t, closer)9596var resp agent.WriteFileResponse97err = rsa.Call("WorkspaceAgent.WriteFile", &agent.WriteFileRequest{98Path: fmt.Sprintf("%s/foobar.txt", test.WorkspaceRoot),99Content: []byte("hello world"),100Mode: 0644,101}, &resp)102rsa.Close()103if err != nil {104if _, serr := stopWs(true, api); serr != nil {105t.Errorf("cannot stop workspace: %q", serr)106}107t.Fatal(err)108}109110lastStatus, err := stopWs(true, api)111if err != nil {112t.Fatal(err)113}114115t.Logf("last status: %v", lastStatus)116expected := &csapi.GitStatus{117Branch: "main",118UntrackedFiles: []string{"foobar.txt"},119TotalUntrackedFiles: 1,120}121if !proto.Equal(lastStatus.Repo, expected) {122t.Fatalf("unexpected git status: expected \"%+q\", got \"%+q\"", expected, lastStatus.Repo)123}124})125}126return testCtx127}).128Feature()129130testEnv.Test(t, f)131132}133134135