Skip to content

Commit da6e934

Browse files
committed
test: new integration test TestOpenWorkspaceFromPrebuild
Signed-off-by: JenTing Hsiao <[email protected]>
1 parent 3ac0c02 commit da6e934

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

test/tests/components/ws-manager/prebuild_test.go

+144
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package wsmanager
77
import (
88
"context"
99
"encoding/json"
10+
"fmt"
11+
"strings"
1012
"testing"
1113
"time"
1214

@@ -15,6 +17,7 @@ import (
1517

1618
csapi "github.com/gitpod-io/gitpod/content-service/api"
1719
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
20+
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
1821
"github.com/gitpod-io/gitpod/test/pkg/integration"
1922
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
2023
)
@@ -171,3 +174,144 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
171174

172175
testEnv.Test(t, f)
173176
}
177+
178+
const (
179+
prebuildLogPath string = "/workspace/.gitpod"
180+
prebuildLog string = "'🤙 This task ran as a workspace prebuild'"
181+
)
182+
183+
func TestOpenWorkspaceFromPrebuild(t *testing.T) {
184+
f := features.New("prebuild").
185+
WithLabel("component", "ws-manager").
186+
Assess("it should open workspace from prebuild successfully", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
187+
tests := []struct {
188+
Name string
189+
ContextURL string
190+
WorkspaceRoot string
191+
CheckoutLocation string
192+
FF []wsmanapi.WorkspaceFeatureFlag
193+
}{
194+
{
195+
Name: "classic",
196+
ContextURL: "https://github.com/gitpod-io/empty",
197+
CheckoutLocation: "empty",
198+
WorkspaceRoot: "/workspace/empty",
199+
},
200+
}
201+
202+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10*len(tests))*time.Minute)
203+
defer cancel()
204+
205+
for _, test := range tests {
206+
t.Run(test.Name, func(t *testing.T) {
207+
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
208+
t.Cleanup(func() {
209+
api.Done(t)
210+
})
211+
212+
// create a prebuild
213+
_, prebuildStopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
214+
req.Type = wsmanapi.WorkspaceType_PREBUILD
215+
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
216+
Name: "GITPOD_TASKS",
217+
Value: `[{ "init": "echo \"some output\" > someFile; sleep 20; exit 0;" }]`,
218+
})
219+
req.Spec.FeatureFlags = test.FF
220+
req.Spec.Initializer = &csapi.WorkspaceInitializer{
221+
Spec: &csapi.WorkspaceInitializer_Git{
222+
Git: &csapi.GitInitializer{
223+
RemoteUri: test.ContextURL,
224+
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
225+
CloneTaget: "main",
226+
CheckoutLocation: test.CheckoutLocation,
227+
Config: &csapi.GitConfig{},
228+
},
229+
},
230+
}
231+
req.Spec.WorkspaceLocation = test.CheckoutLocation
232+
return nil
233+
}))
234+
if err != nil {
235+
t.Fatalf("cannot launch a workspace: %q", err)
236+
}
237+
238+
prebuildLastStatus, err := prebuildStopWs(true, api)
239+
if err != nil {
240+
t.Errorf("cannot stop workspace: %q", err)
241+
}
242+
if prebuildLastStatus == nil || prebuildLastStatus.Conditions == nil {
243+
t.Fatal("cannot find the prebuild snapshot")
244+
}
245+
prebuildSnapshot := prebuildLastStatus.Conditions.Snapshot
246+
247+
// launch the workspace from prebuild
248+
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
249+
req.Spec.FeatureFlags = test.FF
250+
req.Spec.Initializer = &csapi.WorkspaceInitializer{
251+
Spec: &csapi.WorkspaceInitializer_Prebuild{
252+
Prebuild: &csapi.PrebuildInitializer{
253+
Prebuild: &csapi.SnapshotInitializer{Snapshot: prebuildSnapshot},
254+
Git: []*csapi.GitInitializer{
255+
{
256+
RemoteUri: test.ContextURL,
257+
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
258+
CloneTaget: "main",
259+
CheckoutLocation: test.CheckoutLocation,
260+
Config: &csapi.GitConfig{},
261+
},
262+
},
263+
},
264+
},
265+
}
266+
req.Spec.WorkspaceLocation = test.CheckoutLocation
267+
return nil
268+
}))
269+
if err != nil {
270+
t.Fatalf("cannot launch a workspace: %q", err)
271+
}
272+
273+
defer func() {
274+
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
275+
defer scancel()
276+
277+
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
278+
defer sapi.Done(t)
279+
280+
_, err = stopWs(true, sapi)
281+
if err != nil {
282+
t.Errorf("cannot stop workspace: %q", err)
283+
}
284+
}()
285+
286+
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(ws.Req.Id))
287+
if err != nil {
288+
t.Error(err)
289+
}
290+
integration.DeferCloser(t, closer)
291+
292+
var resp agent.ExecResponse
293+
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
294+
Dir: prebuildLogPath,
295+
Command: "bash",
296+
Args: []string{
297+
"-c",
298+
fmt.Sprintf("grep %s *", prebuildLog),
299+
},
300+
}, &resp)
301+
rsa.Close()
302+
if err != nil || resp.ExitCode != 0 {
303+
t.Errorf("cannot grep %s: exit code %d", prebuildLogPath, resp.ExitCode)
304+
return
305+
}
306+
if strings.Trim(resp.Stdout, " \t\n") == "" {
307+
t.Errorf("cannot found message %s in %s", prebuildLog, prebuildLogPath)
308+
return
309+
}
310+
})
311+
}
312+
return ctx
313+
}).
314+
Feature()
315+
316+
testEnv.Test(t, f)
317+
}

0 commit comments

Comments
 (0)