Skip to content

test: Refactor content tests #12950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 144 additions & 115 deletions test/tests/components/ws-manager/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package wsmanager

import (
"context"
"fmt"
"path/filepath"
"reflect"
"testing"
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}).
Expand Down
Loading