Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/components/ws-manager/git_status_test.go
2500 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 wsmanager
6
7
import (
8
"context"
9
"fmt"
10
"testing"
11
"time"
12
13
"google.golang.org/protobuf/proto"
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
// TestGitStatus tests that the git status is reported after a workspace is stopped.
24
func TestGitStatus(t *testing.T) {
25
f := features.New("git-status").
26
WithLabel("component", "ws-manager").
27
Assess("it should report the git status of a workspace when it stops", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
28
tests := []struct {
29
Name string
30
ContextURL string
31
WorkspaceRoot string
32
CheckoutLocation string
33
}{
34
{
35
Name: "classic",
36
ContextURL: "https://github.com/gitpod-io/empty",
37
WorkspaceRoot: "/workspace/empty",
38
CheckoutLocation: "empty",
39
},
40
}
41
for _, test := range tests {
42
test := test
43
t.Run(test.Name, func(t *testing.T) {
44
t.Parallel()
45
46
ctx, cancel := context.WithTimeout(testCtx, time.Duration(10*len(tests))*time.Minute)
47
defer cancel()
48
49
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
50
t.Cleanup(func() {
51
api.Done(t)
52
})
53
54
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
55
// which is client -> server -> ws-manager rather than client -> ws-manager directly
56
ws1, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {
57
w.Spec.Initializer = &csapi.WorkspaceInitializer{
58
Spec: &csapi.WorkspaceInitializer_Git{
59
Git: &csapi.GitInitializer{
60
RemoteUri: test.ContextURL,
61
CheckoutLocation: test.CheckoutLocation,
62
Config: &csapi.GitConfig{},
63
},
64
},
65
}
66
w.Spec.WorkspaceLocation = test.CheckoutLocation
67
return nil
68
}))
69
if err != nil {
70
t.Fatal(err)
71
}
72
t.Cleanup(func() {
73
if err != nil {
74
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
75
defer scancel()
76
77
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
78
defer sapi.Done(t)
79
80
_, err = stopWs(true, sapi)
81
if err != nil {
82
t.Fatal(err)
83
}
84
}
85
})
86
87
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),
88
integration.WithInstanceID(ws1.Req.Id),
89
integration.WithContainer("workspace"),
90
integration.WithWorkspacekitLift(true),
91
)
92
if err != nil {
93
t.Fatal(err)
94
}
95
integration.DeferCloser(t, closer)
96
97
var resp agent.WriteFileResponse
98
err = rsa.Call("WorkspaceAgent.WriteFile", &agent.WriteFileRequest{
99
Path: fmt.Sprintf("%s/foobar.txt", test.WorkspaceRoot),
100
Content: []byte("hello world"),
101
Mode: 0644,
102
}, &resp)
103
rsa.Close()
104
if err != nil {
105
if _, serr := stopWs(true, api); serr != nil {
106
t.Errorf("cannot stop workspace: %q", serr)
107
}
108
t.Fatal(err)
109
}
110
111
lastStatus, err := stopWs(true, api)
112
if err != nil {
113
t.Fatal(err)
114
}
115
116
t.Logf("last status: %v", lastStatus)
117
expected := &csapi.GitStatus{
118
Branch: "main",
119
UntrackedFiles: []string{"foobar.txt"},
120
TotalUntrackedFiles: 1,
121
}
122
if !proto.Equal(lastStatus.Repo, expected) {
123
t.Fatalf("unexpected git status: expected \"%+q\", got \"%+q\"", expected, lastStatus.Repo)
124
}
125
})
126
}
127
return testCtx
128
}).
129
Feature()
130
131
testEnv.Test(t, f)
132
133
}
134
135