Skip to content

Commit

Permalink
test: Refactor prebuild_test
Browse files Browse the repository at this point in the history
  • Loading branch information
utam0k authored and roboquat committed Sep 15, 2022
1 parent 7deee9f commit 312b6b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
69 changes: 49 additions & 20 deletions test/tests/components/ws-manager/prebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package wsmanager

import (
"context"
"encoding/json"
"testing"
"time"

"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"

csapi "github.com/gitpod-io/gitpod/content-service/api"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/test/pkg/integration"
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
)
Expand All @@ -21,48 +23,75 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
f := features.New("prebuild").
WithLabel("component", "ws-manager").
Assess("it should create a prebuild and succeed the defined tasks", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
t.Cleanup(func() {
api.Done(t)
})

tests := []struct {
Name string
FF []wsmanapi.WorkspaceFeatureFlag
Name string
ContextURL string
WorkspaceRoot string
CheckoutLocation string
Task []gitpod.TasksItems
FF []wsmanapi.WorkspaceFeatureFlag
}{
{Name: "classic"},
{Name: "pvc", FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM}},
{
Name: "classic",
ContextURL: "https://github.com/gitpod-io/empty",
CheckoutLocation: "empty",
WorkspaceRoot: "/workspace/empty",
Task: []gitpod.TasksItems{
{Init: "echo \"some output\" > someFile; sleep 20; exit 0;"},
},
},
{
Name: "pvc",
ContextURL: "https://github.com/gitpod-io/empty",
CheckoutLocation: "empty",
WorkspaceRoot: "/workspace/empty",
Task: []gitpod.TasksItems{
{Init: "echo \"some output\" > someFile; sleep 20; exit 0;"},
},
FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM},
},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*len(tests))*time.Minute)
defer cancel()

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
t.Cleanup(func() {
api.Done(t)
})

_, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
req.Type = wsmanapi.WorkspaceType_PREBUILD

tasks, err := json.Marshal(test.Task)
if err != nil {
return err
}
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
Name: "GITPOD_TASKS",
Value: `[{ "init": "echo \"some output\" > someFile; sleep 20; exit 0;" }]`,
Value: string(tasks),
})

req.Spec.FeatureFlags = test.FF
req.Spec.Initializer = &csapi.WorkspaceInitializer{
Spec: &csapi.WorkspaceInitializer_Git{
Git: &csapi.GitInitializer{
RemoteUri: "https://github.com/gitpod-io/empty.git",
RemoteUri: test.ContextURL,
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
CloneTaget: "main",
CheckoutLocation: "empty",
CheckoutLocation: test.CheckoutLocation,
Config: &csapi.GitConfig{},
},
},
}
req.Spec.WorkspaceLocation = "empty"
req.Spec.WorkspaceLocation = test.CheckoutLocation
return nil
}))
if err != nil {
t.Fatalf("cannot launch a workspace: %q", err)
}
t.Cleanup(func() {
defer func() {
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer scancel()

Expand All @@ -73,7 +102,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
})
}()
})
}
return ctx
Expand Down Expand Up @@ -109,7 +138,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
t.Fatalf("cannot start workspace: %q", err)
}

t.Cleanup(func() {
defer func() {
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer scancel()

Expand All @@ -120,7 +149,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
if err != nil {
t.Fatal(err)
}
})
}()

_, err = integration.WaitForWorkspace(ctx, api, ws.Req.Id, func(status *wsmanapi.WorkspaceStatus) bool {
if status.Phase != wsmanapi.WorkspacePhase_STOPPED {
Expand Down
1 change: 0 additions & 1 deletion test/tests/workspace/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestWorkspaceInstrumentation(t *testing.T) {
WorkspaceRoot string
}{
{

Name: "example",
ContextURL: "https://github.com/gitpod-io/empty",
WorkspaceRoot: "/workspace/empty",
Expand Down

0 comments on commit 312b6b8

Please sign in to comment.