Skip to content

Commit

Permalink
test: add new test TestPrebuildDisablePVCAndEnablePVCOnExistingProject
Browse files Browse the repository at this point in the history
Signed-off-by: JenTing Hsiao <[email protected]>
  • Loading branch information
jenting committed Sep 27, 2022
1 parent 7bfaf3a commit 966a262
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions test/pkg/integration/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,3 +1148,8 @@ func (c *ComponentAPI) portFwdWithRetry(ctx context.Context, portFwdF portFwdFun
}
}
}

func (c *ComponentAPI) IsPVCExist(pvcName string) bool {
var pvc corev1.PersistentVolumeClaim
return c.client.Resources().Get(context.Background(), pvcName, c.namespace, &pvc) == nil
}
103 changes: 102 additions & 1 deletion test/tests/components/ws-manager/prebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
const (
prebuildLogPath string = "/workspace/.gitpod"
prebuildLog string = "'🤙 This task ran as a workspace prebuild'"
initTask string = "echo \"some output\" > someFile; sleep 90;"
initTask string = "echo \"some output\" > someFile; sleep 90; exit 0;"
)

// TestOpenWorkspaceFromPrebuild
Expand Down Expand Up @@ -491,6 +491,107 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
testEnv.Test(t, f)
}

// TestPrebuildDisablePVCAndEnablePVCOnExistingProject
// - create a prebuild without PVC
// - make sure there is no PVC object created
// - create a prebuild with PVC
// - make sure there is a PVC object created
func TestPrebuildDisablePVCAndEnablePVCOnExistingProject(t *testing.T) {
f := features.New("prebuild").
WithLabel("component", "ws-manager").
Assess("it should create a prebuild and succeed the defined tasks without and with PVC", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
var (
contextURL string = "https://github.com/gitpod-io/empty"
checkoutLocation string = "/workspace/empty"
)

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)
})

// create a prebuild without PVC and stop workspace
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
// which is client -> server -> ws-manager rather than client -> ws-manager directly
prebuildWs, prebuildStopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
req.Type = wsmanapi.WorkspaceType_PREBUILD
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
Name: "GITPOD_TASKS",
Value: `[{ "init": "echo \"some output\" > someFile; sleep 90; exit 0;" }]`,
})
req.Spec.Initializer = &csapi.WorkspaceInitializer{
Spec: &csapi.WorkspaceInitializer_Git{
Git: &csapi.GitInitializer{
RemoteUri: contextURL,
CheckoutLocation: checkoutLocation,
Config: &csapi.GitConfig{},
},
},
}
req.Spec.WorkspaceLocation = checkoutLocation
return nil
}))
if err != nil {
t.Fatalf("cannot launch a workspace: %q", err)
}

// check the PVC object not exist
pvcName := "prebuild-" + prebuildWs.Req.Id
if api.IsPVCExist(pvcName) {
t.Fatal("prebuild PVC object should not exist")
}

// stop workspace
if err := stopWorkspace(t, cfg, prebuildStopWs); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}

// trigger the prebuild with PVC and stop workspace
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
// which is client -> server -> ws-manager rather than client -> ws-manager directly
prebuildWs1, prebuildStopWs1, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
req.Type = wsmanapi.WorkspaceType_PREBUILD
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
Name: "GITPOD_TASKS",
Value: `[{ "init": "echo \"some output\" > someFile; sleep 90; exit 0;" }]`,
})
req.Spec.FeatureFlags = []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM}
req.Spec.Initializer = &csapi.WorkspaceInitializer{
Spec: &csapi.WorkspaceInitializer_Git{
Git: &csapi.GitInitializer{
RemoteUri: contextURL,
CheckoutLocation: checkoutLocation,
Config: &csapi.GitConfig{},
},
},
}
req.Spec.WorkspaceLocation = checkoutLocation
return nil
}))
if err != nil {
t.Fatalf("cannot launch a workspace: %q", err)
}

// check the PVC object exists
pvcName = "prebuild-" + prebuildWs1.Req.Id
if !api.IsPVCExist(pvcName) {
t.Fatalf("prebuild PVC %s should exist", pvcName)
}

// stop workspace in defer function to prevent we forget to stop the workspace
if err := stopWorkspace(t, cfg, prebuildStopWs1); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
return ctx
}).
Feature()

testEnv.Test(t, f)
}

func stopWorkspaceAndFindSnapshot(StopWorkspaceFunc integration.StopWorkspaceFunc, api *integration.ComponentAPI) (string, *wsmanapi.VolumeSnapshotInfo, error) {
lastStatus, err := StopWorkspaceFunc(true, api)
if err != nil {
Expand Down

0 comments on commit 966a262

Please sign in to comment.