Skip to content

workspace integration test: refactor TestBackup #12861

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 1 commit into from
Sep 13, 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
1 change: 0 additions & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/go-sql-driver/mysql v1.5.0
github.com/google/go-github/v42 v42.0.0
github.com/google/uuid v1.3.0
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/prometheus/procfs v0.7.3
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
Expand Down
3 changes: 0 additions & 3 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 h1:nHHjmvjitIiyPlUHk/ofpgvBcNcawJLtf4PYHORLjAA=
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0/go.mod h1:YBCo4DoEeDndqvAn6eeu0vWM7QdXmHEeI9cFWplmBys=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
Expand Down Expand Up @@ -824,7 +822,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w=
golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
26 changes: 0 additions & 26 deletions test/pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import (
kubectlexec "k8s.io/kubectl/pkg/cmd/exec"
"sigs.k8s.io/e2e-framework/klient"

volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned"

"github.com/gitpod-io/gitpod/test/pkg/integration/common"
)

Expand Down Expand Up @@ -503,27 +501,3 @@ func isPodReady(s *corev1.PodStatus) bool {

return false
}

func FindVolumeSnapshot(ctx context.Context, instanceID, namespace string, client klient.Client) (string, string, error) {
vsClient, err := volumesnapshotv1.NewForConfig(client.RESTConfig())
if err != nil {
return "", "", err
}

volumeSnapshots, err := vsClient.SnapshotV1().VolumeSnapshots(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "workspaceID=" + instanceID,
})
if err != nil {
return "", "", xerrors.Errorf("cannot list volume snapshots: %w", err)
}
if len(volumeSnapshots.Items) == 0 {
return "", "", xerrors.Errorf("no volume snapshot for instance %s", instanceID)
}

var volumeSnapshot, volumeSnapshotContent string
volumeSnapshot = volumeSnapshots.Items[0].Name
if volumeSnapshots.Items[0].Status != nil && volumeSnapshots.Items[0].Status.BoundVolumeSnapshotContentName != nil {
volumeSnapshotContent = *volumeSnapshots.Items[0].Status.BoundVolumeSnapshotContentName
}
return volumeSnapshot, volumeSnapshotContent, nil
}
17 changes: 11 additions & 6 deletions test/pkg/integration/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type LaunchWorkspaceDirectlyResult struct {
// LaunchWorkspaceDirectly starts a workspace pod by talking directly to ws-manager.
// Whenever possible prefer this function over LaunchWorkspaceFromContextURL, because
// it has fewer prerequisites.
func LaunchWorkspaceDirectly(ctx context.Context, api *ComponentAPI, opts ...LaunchWorkspaceDirectlyOpt) (*LaunchWorkspaceDirectlyResult, func(waitForStop bool) error, error) {
func LaunchWorkspaceDirectly(ctx context.Context, api *ComponentAPI, opts ...LaunchWorkspaceDirectlyOpt) (*LaunchWorkspaceDirectlyResult, func(waitForStop bool) (*wsmanapi.WorkspaceStatus, error), error) {
options := launchWorkspaceDirectlyOptions{
BaseImage: "docker.io/gitpod/workspace-full:latest",
}
Expand Down Expand Up @@ -191,31 +191,36 @@ func LaunchWorkspaceDirectly(ctx context.Context, api *ComponentAPI, opts ...Lau
return nil, nil, xerrors.Errorf("cannot start workspace: %q", err)
}

stopWs := func(waitForStop bool) error {
stopWs := func(waitForStop bool) (*wsmanapi.WorkspaceStatus, error) {
tctx, tcancel := context.WithTimeout(context.Background(), perCallTimeout)
defer tcancel()

var lastStatus *wsmanapi.WorkspaceStatus
for {
err = DeleteWorkspace(tctx, api, req.Id)
if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable {
time.Sleep(5 * time.Second)
continue
} else if err != nil {
return err
return nil, err
}
break
}

if !waitForStop {
return nil, nil
}
for {
_, err = WaitForWorkspaceStop(tctx, api, req.Id)
lastStatus, err = WaitForWorkspaceStop(tctx, api, req.Id)
if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable {
time.Sleep(5 * time.Second)
continue
} else if err != nil {
return err
return lastStatus, err
}
break
}
return err
return lastStatus, err
}
defer func() {
if err != nil {
Expand Down
34 changes: 14 additions & 20 deletions test/tests/components/ws-manager/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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(), 10*time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
defer cancel()

api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
Expand All @@ -41,7 +41,7 @@ func TestBackup(t *testing.T) {
{Name: "pvc", FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM}},
}
for _, test := range tests {
t.Run(test.Name+"_backup", func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {
w.Spec.FeatureFlags = test.FF
w.Spec.Initializer = &csapi.WorkspaceInitializer{
Expand All @@ -68,7 +68,7 @@ func TestBackup(t *testing.T) {
integration.WithWorkspacekitLift(true),
)
if err != nil {
if err := stopWs1(true); err != nil {
if _, err := stopWs1(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
t.Fatal(err)
Expand All @@ -83,13 +83,13 @@ func TestBackup(t *testing.T) {
}, &resp)
rsa.Close()
if err != nil {
if err = stopWs1(true); err != nil {
if _, err := stopWs1(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
t.Fatal(err)
}

err = stopWs1(true)
lastStatusWs1, err := stopWs1(true)
if err != nil {
t.Fatal(err)
}
Expand All @@ -107,13 +107,8 @@ func TestBackup(t *testing.T) {
return nil
}

// PVC: find VolumeSnapshot by workspace ID
volumeSnapshot, volumeSnapshotContent, err := integration.FindVolumeSnapshot(ctx, ws1.Req.Id, cfg.Namespace(), cfg.Client())
if err != nil {
return err
}
if volumeSnapshot != "" && volumeSnapshotContent != "" {
w.Spec.VolumeSnapshot = &wsmanapi.VolumeSnapshotInfo{VolumeSnapshotName: volumeSnapshot, VolumeSnapshotHandle: volumeSnapshotContent}
if lastStatusWs1 != nil && lastStatusWs1.Conditions != nil && lastStatusWs1.Conditions.VolumeSnapshot != nil {
w.Spec.VolumeSnapshot = lastStatusWs1.Conditions.VolumeSnapshot
}
return nil
}),
Expand All @@ -122,7 +117,7 @@ func TestBackup(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() {
err = stopWs2(true)
_, err = stopWs2(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down Expand Up @@ -205,7 +200,7 @@ func TestExistingWorkspaceEnablePVC(t *testing.T) {
integration.WithWorkspacekitLift(true),
)
if err != nil {
if err := stopWs1(true); err != nil {
if _, err := stopWs1(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
t.Fatal(err)
Expand All @@ -220,13 +215,13 @@ func TestExistingWorkspaceEnablePVC(t *testing.T) {
}, &resp)
rsa.Close()
if err != nil {
if err = stopWs1(true); err != nil {
if _, err := stopWs1(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
t.Fatal(err)
}

err = stopWs1(true)
_, err = stopWs1(true)
if err != nil {
t.Fatal(err)
}
Expand All @@ -247,7 +242,7 @@ func TestExistingWorkspaceEnablePVC(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() {
err = stopWs2(true)
_, err = stopWs2(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down Expand Up @@ -308,8 +303,7 @@ func TestMissingBackup(t *testing.T) {

wsm, err := api.WorkspaceManager()
if err != nil {
err = stopWs(true)
if err != nil {
if _, err := stopWs(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
t.Fatal(err)
Expand Down Expand Up @@ -369,7 +363,7 @@ func TestMissingBackup(t *testing.T) {
return
}
if testws.LastStatus.Conditions.Failed == "" {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/components/ws-manager/multi_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestMultiRepoWorkspaceSuccess(t *testing.T) {
}

defer func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/tests/components/ws-manager/prebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
t.Fatalf("cannot launch a workspace: %q", err)
}
t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
}

t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/components/ws-manager/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestRegularWorkspaceTasks(t *testing.T) {
}

defer func() {
if err = stopWs(true); err != nil {
if _, err = stopWs(true); err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
}()
Expand Down
2 changes: 1 addition & 1 deletion test/tests/workspace/cgroup_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCgroupV2(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/workspace/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRunDocker(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/workspace/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestLaunchWorkspaceDirectly(t *testing.T) {
}

t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/workspace/k3s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestK3s(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/workspace/mount_proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestMountProc(t *testing.T) {
}

t.Cleanup(func() {
err = stopWs(true)
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
}
Expand Down