-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtasks_test.go
179 lines (162 loc) · 5.45 KB
/
tasks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package wsmanager
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
csapi "github.com/gitpod-io/gitpod/content-service/api"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
supervisorapi "github.com/gitpod-io/gitpod/supervisor/api"
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
"github.com/gitpod-io/gitpod/test/pkg/integration"
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)
func TestRegularWorkspaceTasks(t *testing.T) {
testRepo := "https://github.com/gitpod-io/empty"
testRepoName := "empty"
wsLoc := "/workspace/empty"
tests := []struct {
Name string
Task []gitpod.TasksItems
LookForFile []string
FF []wsmanapi.WorkspaceFeatureFlag
}{
{
Name: "classic",
Task: []gitpod.TasksItems{
{Init: fmt.Sprintf("touch %s/init-ran; exit", wsLoc)},
{Before: fmt.Sprintf("touch %s/before-ran; exit", wsLoc)},
{Command: fmt.Sprintf("touch %s/command-ran; exit", wsLoc)},
},
LookForFile: []string{"init-ran", "before-ran", "command-ran"},
},
{
Name: "pvc",
Task: []gitpod.TasksItems{
{Init: fmt.Sprintf("touch %s/init-ran; exit", wsLoc)},
{Before: fmt.Sprintf("touch %s/before-ran; exit", wsLoc)},
{Command: fmt.Sprintf("touch %s/command-ran; exit", wsLoc)},
},
LookForFile: []string{"init-ran", "before-ran", "command-ran"},
FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM},
},
}
f := features.New("ws-manager").
WithLabel("component", "ws-manager").
WithLabel("type", "tasks").
Assess("it can run workspace tasks", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
t.Cleanup(func() {
api.Done(t)
})
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
addInitTask := func(swr *wsmanapi.StartWorkspaceRequest) error {
tasks, err := json.Marshal(test.Task)
if err != nil {
return err
}
swr.Spec.Envvars = append(swr.Spec.Envvars, &wsmanapi.EnvironmentVariable{
Name: "GITPOD_TASKS",
Value: string(tasks),
})
swr.Spec.FeatureFlags = test.FF
swr.Spec.Initializer = &csapi.WorkspaceInitializer{
Spec: &csapi.WorkspaceInitializer_Git{
Git: &csapi.GitInitializer{
RemoteUri: testRepo,
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
CloneTaget: "main",
CheckoutLocation: testRepoName,
Config: &csapi.GitConfig{},
},
},
}
swr.Spec.WorkspaceLocation = testRepoName
return nil
}
nfo, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(addInitTask))
if err != nil {
t.Fatal(err)
}
defer func() {
if _, err = stopWs(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
}()
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.Req.Id))
integration.DeferCloser(t, closer)
if err != nil {
t.Fatalf("unexpected error instrumenting workspace: %v", err)
}
defer rsa.Close()
var parsedResp struct {
Result struct {
Tasks []*struct {
State string `json:"state,omitempty"`
} `json:"tasks,omitempty"`
} `json:"result"`
}
supervisorTaskStatusCompleted := false
for i := 1; i < 10; i++ {
var res agent.ExecResponse
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
Dir: wsLoc,
Command: "curl",
// nftable rule only forwards to this ip address
Args: []string{"10.0.5.2:22999/_supervisor/v1/status/tasks"},
}, &res)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal([]byte(res.Stdout), &parsedResp)
if err != nil {
t.Fatalf("cannot decode supervisor status response: %s", err)
}
if len(parsedResp.Result.Tasks) != len(test.Task) {
t.Fatalf("expected one task to run, but got %d", len(parsedResp.Result.Tasks))
}
if parsedResp.Result.Tasks[0].State == supervisorapi.TaskState_name[int32(supervisorapi.TaskState_closed)] {
supervisorTaskStatusCompleted = true
break
}
// sleep before next attempt hoping that the task completed meanwhile
time.Sleep(6 * time.Second)
}
if !supervisorTaskStatusCompleted {
t.Fatal("tasks did not complete in time")
}
var ls agent.ListDirResponse
err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{
Dir: wsLoc,
}, &ls)
if err != nil {
t.Fatal(err)
}
for _, lff := range test.LookForFile {
var foundMaker bool
for _, f := range ls.Files {
if f == lff {
foundMaker = true
break
}
}
if !foundMaker {
t.Fatalf("task seems to have run, but cannot find %s it should have created", lff)
}
}
})
}
return ctx
}).
Feature()
testEnv.Test(t, f)
}