diff --git a/test/tests/components/ws-manager/content_test.go b/test/tests/components/ws-manager/content_test.go index 77f9fb54f715bb..08762df9546cb8 100644 --- a/test/tests/components/ws-manager/content_test.go +++ b/test/tests/components/ws-manager/content_test.go @@ -6,6 +6,7 @@ package wsmanager import ( "context" + "fmt" "path/filepath" "reflect" "testing" @@ -25,37 +26,51 @@ func TestBackup(t *testing.T) { f := features.New("backup"). WithLabel("component", "ws-manager"). Assess("it should start a workspace, create a file and successfully create a backup", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context { - ctx, cancel := context.WithTimeout(context.Background(), 20*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 + FF []wsmanapi.WorkspaceFeatureFlag }{ - {Name: "classic"}, - {Name: "pvc", FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM}}, + { + Name: "classic", + ContextURL: "https://github.com/gitpod-io/empty", + WorkspaceRoot: "/workspace/empty", + CheckoutLocation: "empty", + }, + { + Name: "pvc", + ContextURL: "https://github.com/gitpod-io/empty", + WorkspaceRoot: "/workspace/empty", + CheckoutLocation: "empty", + FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM}, + }, } + ctx, cancel := context.WithTimeout(context.Background(), 20*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) + }) + ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error { w.Spec.FeatureFlags = test.FF w.Spec.Initializer = &csapi.WorkspaceInitializer{ Spec: &csapi.WorkspaceInitializer_Git{ Git: &csapi.GitInitializer{ - RemoteUri: "https://github.com/gitpod-io/gitpod.git", + RemoteUri: test.ContextURL, TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH, CloneTaget: "main", - CheckoutLocation: "gitpod", + CheckoutLocation: test.CheckoutLocation, Config: &csapi.GitConfig{}, }, }, } - w.Spec.WorkspaceLocation = "gitpod" + w.Spec.WorkspaceLocation = test.CheckoutLocation return nil })) if err != nil { @@ -77,13 +92,13 @@ func TestBackup(t *testing.T) { var resp agent.WriteFileResponse err = rsa.Call("WorkspaceAgent.WriteFile", &agent.WriteFileRequest{ - Path: "/workspace/gitpod/foobar.txt", + Path: fmt.Sprintf("%s/foobar.txt", test.WorkspaceRoot), Content: []byte("hello world"), Mode: 0644, }, &resp) rsa.Close() if err != nil { - if _, err := stopWs1(true, api); err != nil { + if _, err = stopWs1(true, api); err != nil { t.Errorf("cannot stop workspace: %q", err) } t.Fatal(err) @@ -139,12 +154,11 @@ func TestBackup(t *testing.T) { var ls agent.ListDirResponse err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{ - Dir: "/workspace/gitpod", + Dir: test.WorkspaceRoot, }, &ls) if err != nil { t.Fatal(err) } - rsa.Close() var found bool @@ -172,120 +186,135 @@ func TestExistingWorkspaceEnablePVC(t *testing.T) { f := features.New("backup"). WithLabel("component", "ws-manager"). Assess("it should enable PVC feature flag to the existing workspace without data loss", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context { + tests := []struct { + Name string + ContextURL string + WorkspaceRoot string + CheckoutLocation string + }{ + { + Name: "pvc", + ContextURL: "http://github.com/gitpod-io/template-golang-cli", + WorkspaceRoot: "/workspace/template-golang-cli", + CheckoutLocation: "template-golang-cli", + }, + } 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 { + api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client()) + t.Cleanup(func() { + api.Done(t) + }) - // Create a new workspace without the PVC feature flag - ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error { - w.Spec.Initializer = &csapi.WorkspaceInitializer{ - Spec: &csapi.WorkspaceInitializer_Git{ - Git: &csapi.GitInitializer{ - RemoteUri: "https://github.com/gitpod-io/gitpod.git", - TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH, - CloneTaget: "main", - CheckoutLocation: "gitpod", - Config: &csapi.GitConfig{}, + // Create a new workspace without the PVC feature flag + ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error { + w.Spec.Initializer = &csapi.WorkspaceInitializer{ + Spec: &csapi.WorkspaceInitializer_Git{ + Git: &csapi.GitInitializer{ + RemoteUri: test.ContextURL, + TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH, + CloneTaget: "main", + CheckoutLocation: test.CheckoutLocation, + Config: &csapi.GitConfig{}, + }, }, - }, + } + w.Spec.WorkspaceLocation = test.CheckoutLocation + return nil + })) + if err != nil { + t.Fatal(err) } - w.Spec.WorkspaceLocation = "gitpod" - return nil - })) - if err != nil { - t.Fatal(err) - } - rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), - integration.WithInstanceID(ws1.Req.Id), - integration.WithContainer("workspace"), - integration.WithWorkspacekitLift(true), - ) - if err != nil { - if _, err := stopWs1(true, api); err != nil { - t.Errorf("cannot stop workspace: %q", err) + rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), + integration.WithInstanceID(ws1.Req.Id), + integration.WithContainer("workspace"), + integration.WithWorkspacekitLift(true), + ) + if err != nil { + if _, err := stopWs1(true, api); err != nil { + t.Errorf("cannot stop workspace: %q", err) + } + t.Fatal(err) } - t.Fatal(err) - } - integration.DeferCloser(t, closer) - - var resp agent.WriteFileResponse - err = rsa.Call("WorkspaceAgent.WriteFile", &agent.WriteFileRequest{ - Path: "/workspace/gitpod/foobar.txt", - Content: []byte("hello world"), - Mode: 0644, - }, &resp) - rsa.Close() - if err != nil { - if _, err := stopWs1(true, api); err != nil { - t.Errorf("cannot stop workspace: %q", err) + integration.DeferCloser(t, closer) + + var resp agent.WriteFileResponse + err = rsa.Call("WorkspaceAgent.WriteFile", &agent.WriteFileRequest{ + Path: fmt.Sprintf("%s/foobar.txt", test.WorkspaceRoot), + Content: []byte("hello world"), + Mode: 0644, + }, &resp) + rsa.Close() + if err != nil { + if _, err := stopWs1(true, api); err != nil { + t.Errorf("cannot stop workspace: %q", err) + } + t.Fatal(err) } - t.Fatal(err) - } - _, err = stopWs1(true, api) - if err != nil { - t.Fatal(err) - } - - // Relaunch the workspace and enable the PVC feature flag - ws2, stopWs2, err := integration.LaunchWorkspaceDirectly(t, ctx, api, - integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error { - w.ServicePrefix = ws1.Req.ServicePrefix - w.Metadata.MetaId = ws1.Req.Metadata.MetaId - w.Metadata.Owner = ws1.Req.Metadata.Owner - w.Spec.FeatureFlags = []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM} - w.Spec.Initializer = ws1.Req.Spec.Initializer - w.Spec.WorkspaceLocation = ws1.Req.Spec.WorkspaceLocation - return nil - }), - ) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute) - defer scancel() + _, err = stopWs1(true, api) + if err != nil { + t.Fatal(err) + } - sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client()) - defer sapi.Done(t) - _, err = stopWs2(true, sapi) + // Relaunch the workspace and enable the PVC feature flag + ws2, stopWs2, err := integration.LaunchWorkspaceDirectly(t, ctx, api, + integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error { + w.ServicePrefix = ws1.Req.ServicePrefix + w.Metadata.MetaId = ws1.Req.Metadata.MetaId + w.Metadata.Owner = ws1.Req.Metadata.Owner + w.Spec.FeatureFlags = []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM} + w.Spec.Initializer = ws1.Req.Spec.Initializer + w.Spec.WorkspaceLocation = ws1.Req.Spec.WorkspaceLocation + return nil + }), + ) if err != nil { - t.Errorf("cannot stop workspace: %q", err) + t.Fatal(err) } - }) + t.Cleanup(func() { + sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer scancel() - rsa, closer, err = integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), - integration.WithInstanceID(ws2.Req.Id), - ) - if err != nil { - t.Fatal(err) - } - integration.DeferCloser(t, closer) + sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client()) + defer sapi.Done(t) + _, err = stopWs2(true, sapi) + if err != nil { + t.Errorf("cannot stop workspace: %q", err) + } + }) - var ls agent.ListDirResponse - err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{ - Dir: "/workspace/gitpod", - }, &ls) - if err != nil { - t.Fatal(err) - } + rsa, closer, err = integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), + integration.WithInstanceID(ws2.Req.Id), + ) + if err != nil { + t.Fatal(err) + } + integration.DeferCloser(t, closer) - rsa.Close() + var ls agent.ListDirResponse + err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{ + Dir: test.WorkspaceRoot, + }, &ls) + if err != nil { + t.Fatal(err) + } - var found bool - for _, f := range ls.Files { - if filepath.Base(f) == "foobar.txt" { - found = true - break + rsa.Close() + + var found bool + for _, f := range ls.Files { + if filepath.Base(f) == "foobar.txt" { + found = true + break + } + } + if !found { + t.Fatal("did not find foobar.txt from previous workspace instance") } - } - if !found { - t.Fatal("did not find foobar.txt from previous workspace instance") } return ctx }). diff --git a/test/tests/workspace/example_test.go b/test/tests/workspace/example_test.go index b17a1f4b2572ba..c735b097d4abaa 100644 --- a/test/tests/workspace/example_test.go +++ b/test/tests/workspace/example_test.go @@ -21,56 +21,73 @@ func TestWorkspaceInstrumentation(t *testing.T) { userToken, _ := os.LookupEnv("USER_TOKEN") integration.SkipWithoutUsername(t, username) integration.SkipWithoutUserToken(t, userToken) + tests := []struct { + Name string + ContextURL string + WorkspaceRoot string + }{ + { + + Name: "example", + ContextURL: "https://github.com/gitpod-io/empty", + WorkspaceRoot: "/workspace/empty", + }, + } f := features.New("instrumentation"). WithLabel("component", "server"). - Assess("it can instrument a workspace", 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) - }) - - _, err := api.CreateUser(username, userToken) - if err != nil { - t.Fatal(err) - } - - nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "github.com/gitpod-io/gitpod", username, api) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute) - defer scancel() - - sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client()) - defer sapi.Done(t) - - _, err = stopWs(true, sapi) - if 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.LatestInstance.ID)) - if err != nil { - t.Fatal(err) - } - defer rsa.Close() - integration.DeferCloser(t, closer) - - var ls agent.ListDirResponse - err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{ - Dir: "/workspace/gitpod", - }, &ls) - if err != nil { - t.Fatal(err) - } - for _, f := range ls.Files { - t.Log(f) + Assess("it can instrument a workspace", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + for _, test := range tests { + t.Run(test.ContextURL, func(t *testing.T) { + 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) + }) + + _, err := api.CreateUser(username, userToken) + if err != nil { + t.Fatal(err) + } + + nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, test.ContextURL, username, api) + if err != nil { + t.Fatal(err) + } + + defer func() { + sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer scancel() + + sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client()) + defer sapi.Done(t) + + _, err := stopWs(true, sapi) + if err != nil { + t.Fatal(err) + } + }() + + rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.LatestInstance.ID)) + if err != nil { + t.Fatal(err) + } + defer rsa.Close() + integration.DeferCloser(t, closer) + + var ls agent.ListDirResponse + err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{ + Dir: test.WorkspaceRoot, + }, &ls) + if err != nil { + t.Fatal(err) + } + for _, f := range ls.Files { + t.Log(f) + } + }) } return ctx @@ -97,18 +114,18 @@ func TestLaunchWorkspaceDirectly(t *testing.T) { t.Fatal(err) } - t.Cleanup(func() { + defer func() { sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute) defer scancel() sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client()) defer sapi.Done(t) - _, err = stopWs(true, sapi) + _, err := stopWs(true, sapi) if err != nil { - t.Errorf("cannot stop workspace: %q", err) + t.Fatal(err) } - }) + }() return ctx }).