Skip to content

test: Put logging of workspace starts and stops #12901

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 3 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
133 changes: 82 additions & 51 deletions test/pkg/integration/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"io"
"sync"
"testing"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -90,10 +91,12 @@ type LaunchWorkspaceDirectlyResult struct {
LastStatus *wsmanapi.WorkspaceStatus
}

type stopWorkspaceFunc = func(waitForStop bool) (*wsmanapi.WorkspaceStatus, error)

// 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) (*wsmanapi.WorkspaceStatus, error), error) {
func LaunchWorkspaceDirectly(t *testing.T, ctx context.Context, api *ComponentAPI, opts ...LaunchWorkspaceDirectlyOpt) (*LaunchWorkspaceDirectlyResult, stopWorkspaceFunc, error) {
options := launchWorkspaceDirectlyOptions{
BaseImage: "docker.io/gitpod/workspace-full:latest",
}
Expand Down Expand Up @@ -181,57 +184,33 @@ func LaunchWorkspaceDirectly(ctx context.Context, api *ComponentAPI, opts ...Lau
sctx, scancel := context.WithTimeout(ctx, perCallTimeout)
defer scancel()

t.Log("prepare for a connection with ws-manager")
wsm, err := api.WorkspaceManager()
if err != nil {
return nil, nil, xerrors.Errorf("cannot start workspace manager: %q", err)
}
t.Log("established a connection with ws-manager")

t.Logf("attemp to start up the workspace directly: %s, %s", instanceID, workspaceID)
sresp, err := wsm.StartWorkspace(sctx, req)
if err != nil {
return nil, nil, xerrors.Errorf("cannot start workspace: %q", err)
}
t.Log("successfully sent workspace start request")

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 nil, err
}
break
}

if !waitForStop {
return nil, nil
}
for {
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 lastStatus, err
}
break
}
return lastStatus, err
}
stopWs := stopWsF(t, req.Id, api)
defer func() {
if err != nil {
stopWs(false)
}
}()

t.Log("wait for workspace to be fully up and running")
lastStatus, err := WaitForWorkspaceStart(ctx, instanceID.String(), api, options.WaitForOpts...)
if err != nil {
return nil, nil, xerrors.Errorf("cannot wait for workspace start: %q", err)
}
t.Log("successful launch of the workspace")

return &LaunchWorkspaceDirectlyResult{
Req: req,
Expand All @@ -245,19 +224,23 @@ func LaunchWorkspaceDirectly(ctx context.Context, api *ComponentAPI, opts ...Lau
// fail the test.
//
// When possible, prefer the less complex LaunchWorkspaceDirectly.
func LaunchWorkspaceFromContextURL(ctx context.Context, contextURL string, username string, api *ComponentAPI, serverOpts ...GitpodServerOpt) (*protocol.WorkspaceInfo, func(waitForStop bool), error) {
func LaunchWorkspaceFromContextURL(t *testing.T, ctx context.Context, contextURL string, username string, api *ComponentAPI, serverOpts ...GitpodServerOpt) (*protocol.WorkspaceInfo, stopWorkspaceFunc, error) {
var defaultServerOpts []GitpodServerOpt
if username != "" {
defaultServerOpts = []GitpodServerOpt{WithGitpodUser(username)}
}

t.Log("prepare for a connection with gitpod server")
server, err := api.GitpodServer(append(defaultServerOpts, serverOpts...)...)
if err != nil {
return nil, nil, xerrors.Errorf("cannot start server: %q", err)
}
t.Log("established a connection with gitpod server")

cctx, ccancel := context.WithTimeout(context.Background(), perCallTimeout)
defer ccancel()

t.Logf("attemp to create the workspace: %s", contextURL)
resp, err := server.CreateWorkspace(cctx, &protocol.CreateWorkspaceOptions{
ContextURL: contextURL,
Mode: "force-new",
Expand All @@ -266,42 +249,90 @@ func LaunchWorkspaceFromContextURL(ctx context.Context, contextURL string, usern
return nil, nil, xerrors.Errorf("cannot start workspace: %q", err)
}

nfo, err := server.GetWorkspace(ctx, resp.CreatedWorkspaceID)
t.Logf("attemp to get the workspace information: %s", resp.CreatedWorkspaceID)
wi, err := server.GetWorkspace(ctx, resp.CreatedWorkspaceID)
if err != nil {
return nil, nil, xerrors.Errorf("cannot get workspace: %q", err)
}
if nfo.LatestInstance == nil {
if wi.LatestInstance == nil {
return nil, nil, xerrors.Errorf("CreateWorkspace did not start the workspace")
}
t.Logf("got the workspace information: %s", wi.Workspace.ID)

// GetWorkspace might receive an instance before we seen the first event
// from ws-manager, in which case IdeURL is not set
nfo.LatestInstance.IdeURL = resp.WorkspaceURL

stopWs := func(waitForStop bool) {
sctx, scancel := context.WithTimeout(ctx, perCallTimeout)
_ = server.StopWorkspace(sctx, resp.CreatedWorkspaceID)
scancel()
wi.LatestInstance.IdeURL = resp.WorkspaceURL

if waitForStop {
_, _ = WaitForWorkspaceStop(ctx, api, nfo.LatestInstance.ID)
}
}
stopWs := stopWsF(t, wi.LatestInstance.ID, api)
defer func() {
if err != nil {
stopWs(false)
_, _ = stopWs(false)
}
}()
// it.t.Logf("created workspace: workspaceID=%s url=%s", resp.CreatedWorkspaceID, resp.WorkspaceURL)

_, err = WaitForWorkspaceStart(ctx, nfo.LatestInstance.ID, api)
t.Log("wait for workspace to be fully up and running")
_, err = WaitForWorkspaceStart(ctx, wi.LatestInstance.ID, api)
if err != nil {
return nil, nil, xerrors.Errorf("cannot start workspace: %q", err)
return nil, nil, xerrors.Errorf("failed to wait for the workspace to start up: %w", err)
}
t.Log("successful launch of the workspace")

// it.t.Logf("workspace is running: instanceID=%s", nfo.LatestInstance.ID)
return wi, stopWs, nil
}

func stopWsF(t *testing.T, instanceID string, api *ComponentAPI) stopWorkspaceFunc {
return func(waitForStop bool) (*wsmanapi.WorkspaceStatus, error) {
sctx, scancel := context.WithTimeout(context.Background(), perCallTimeout)
defer scancel()

return nfo, stopWs, nil
var err error
for {
t.Logf("attemp to delete the workspace: %s", instanceID)
err = DeleteWorkspace(sctx, api, instanceID)
if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable {
t.Logf("got %v when deleting workspace", st)
time.Sleep(5 * time.Second)
continue
} else if err != nil {
return nil, err
}
break
}

if !waitForStop {
wm, err := api.WorkspaceManager()
if err != nil {
return nil, err
}

dr, err := wm.DescribeWorkspace(sctx, &wsmanapi.DescribeWorkspaceRequest{
Id: instanceID,
})
if err != nil {
return nil, err
}

s, ok := status.FromError(err)
if ok && s.Code() == codes.NotFound {
return nil, err
}
return dr.Status, err
}

for {
t.Logf("waiting for stopping the workspace: %s", instanceID)
lastStatus, err := WaitForWorkspaceStop(sctx, api, instanceID)
if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable {
t.Logf("got %v during waiting for stopping the workspace", st)
time.Sleep(5 * time.Second)
continue
} else if err != nil {
return lastStatus, err
}
break
}
return nil, err
}
}

// WaitForWorkspaceOpt configures a WaitForWorkspace call
Expand Down
16 changes: 8 additions & 8 deletions test/tests/components/ws-manager/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestBackup(t *testing.T) {
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {
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{
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestBackup(t *testing.T) {
t.Fatal(err)
}

ws2, stopWs2, err := integration.LaunchWorkspaceDirectly(ctx, api,
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
Expand All @@ -116,12 +116,12 @@ func TestBackup(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
defer func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we change to use defer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_, err = stopWs2(true)
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(ws2.Req.Id),
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestExistingWorkspaceEnablePVC(t *testing.T) {
})

// Create a new workspace without the PVC feature flag
ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {
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{
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestExistingWorkspaceEnablePVC(t *testing.T) {
}

// Relaunch the workspace and enable the PVC feature flag
ws2, stopWs2, err := integration.LaunchWorkspaceDirectly(ctx, api,
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
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestMissingBackup(t *testing.T) {
api.Done(t)
})

ws, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api)
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestMissingBackup(t *testing.T) {
}
for _, test := range tests {
t.Run(test.Name+"_backup_init", func(t *testing.T) {
testws, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {
testws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(w *wsmanapi.StartWorkspaceRequest) error {
w.ServicePrefix = ws.Req.ServicePrefix
w.Metadata.MetaId = ws.Req.Metadata.MetaId
w.Metadata.Owner = ws.Req.Metadata.Owner
Expand Down
5 changes: 4 additions & 1 deletion test/tests/components/ws-manager/multi_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestMultiRepoWorkspaceSuccess(t *testing.T) {
return nil
}

ws, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(multiRepoInit))
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(multiRepoInit))
if err != nil {
t.Fatal(err)
}
Expand All @@ -113,6 +113,9 @@ func TestMultiRepoWorkspaceSuccess(t *testing.T) {
integration.WithContainer("workspace"),
integration.WithWorkspacekitLift(true),
)
if err != nil {
t.Fatal(err)
}

integration.DeferCloser(t, closer)
defer rsa.Close()
Expand Down
6 changes: 3 additions & 3 deletions test/tests/components/ws-manager/prebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
_, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
_, stopWs, 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",
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
api.Done(t)
})

ws, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
ws, stopWs, 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",
Expand All @@ -106,7 +106,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
t.Cleanup(func() {
_, err = stopWs(true)
if err != nil {
t.Errorf("cannot stop workspace: %q", err)
t.Fatal(err)
}
})

Expand Down
5 changes: 2 additions & 3 deletions test/tests/components/ws-manager/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import (
func TestRegularWorkspaceTasks(t *testing.T) {
testRepo := "https://github.com/gitpod-io/empty"
testRepoName := "empty"
wsLoc := "/workspace/empty"

wsLoc := fmt.Sprintf("/workspace/%s", testRepoName)
tests := []struct {
Name string
Task []gitpod.TasksItems
Expand Down Expand Up @@ -92,7 +91,7 @@ func TestRegularWorkspaceTasks(t *testing.T) {
return nil
}

nfo, stopWs, err := integration.LaunchWorkspaceDirectly(ctx, api, integration.WithRequestModifier(addInitTask))
nfo, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(addInitTask))
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions test/tests/ide/jetbrains/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/test/pkg/integration"
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
"github.com/google/go-github/v42/github"
)

Expand Down Expand Up @@ -107,10 +108,10 @@ func TestJetBrainsGatewayWorkspace(t *testing.T) {

t.Logf("starting workspace")
var nfo *protocol.WorkspaceInfo
var stopWs func(waitForStop bool)
var stopWs func(waitForStop bool) (*wsmanapi.WorkspaceStatus, error)

for i := 0; i < 3; i++ {
nfo, stopWs, err = integration.LaunchWorkspaceFromContextURL(ctx, "referrer:jetbrains-gateway:"+ideName+"/"+repo, username, api)
nfo, stopWs, err = integration.LaunchWorkspaceFromContextURL(t, ctx, "referrer:jetbrains-gateway:"+ideName+"/"+repo, username, api)
if err != nil {
if strings.Contains(err.Error(), "code 429 message: too many requests") {
t.Log(err)
Expand Down
2 changes: 1 addition & 1 deletion test/tests/ide/vscode/python_ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestPythonExtWorkspace(t *testing.T) {
t.Fatalf("cannot set ide to vscode insiders: %q", err)
}

nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(ctx, "github.com/gitpod-io/python-test-workspace", username, api)
nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "github.com/gitpod-io/python-test-workspace", username, api)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading