Skip to content

Commit c7bf50b

Browse files
committed
[tests] Add basic GitHub context tests
1 parent fa95e37 commit c7bf50b

File tree

5 files changed

+181
-3
lines changed

5 files changed

+181
-3
lines changed

test/pkg/integration/integration.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ var (
5151
// NewTest produces a new integration test instance
5252
func NewTest(t *testing.T) *Test {
5353
flag.Parse()
54-
5554
kubeconfig := *cfgFlag
5655
namespaceOverride := *namespaceFlag
57-
t.Logf("kubeconfig: %s", kubeconfig)
58-
t.Logf("namespaceOverride: %s", namespaceOverride)
5956

6057
if kubeconfig == cfgFlagDefault {
6158
home, err := os.UserHomeDir()

test/tests/workspace/contexts_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package workspace_test
6+
7+
import (
8+
"context"
9+
"testing"
10+
"time"
11+
12+
"github.com/gitpod-io/gitpod/test/pkg/integration"
13+
git "github.com/gitpod-io/gitpod/test/tests/workspace/git"
14+
wsapi "github.com/gitpod-io/gitpod/ws-manager/api"
15+
)
16+
17+
type ContextTest struct {
18+
Name string
19+
ContextURL string
20+
WorkspaceRoot string
21+
ExpectedBranch string
22+
}
23+
24+
func TestGitHubContexts(t *testing.T) {
25+
tests := []ContextTest{
26+
{
27+
Name: "open repository",
28+
ContextURL: "github.com/gitpod-io/gitpod",
29+
WorkspaceRoot: "/workspace/gitpod",
30+
ExpectedBranch: "main",
31+
},
32+
{
33+
Name: "open issue",
34+
ContextURL: "github.com/gitpod-io/gitpod-test-repo/issues/88",
35+
WorkspaceRoot: "/workspace/gitpod-test-repo",
36+
ExpectedBranch: "main",
37+
},
38+
{
39+
Name: "open tag",
40+
ContextURL: "github.com/gitpod-io/gitpod-test-repo/tree/integration-test-context-tag",
41+
WorkspaceRoot: "/workspace/gitpod-test-repo",
42+
ExpectedBranch: "HEAD",
43+
},
44+
}
45+
runContextTests(t, tests)
46+
}
47+
48+
func runContextTests(t *testing.T, tests []ContextTest) {
49+
for _, test := range tests {
50+
t.Run(test.ContextURL, func(t *testing.T) {
51+
t.Parallel()
52+
53+
it := integration.NewTest(t)
54+
defer it.Done()
55+
56+
nfo := integration.LaunchWorkspaceFromContextURL(it, test.ContextURL)
57+
58+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
59+
defer cancel()
60+
it.WaitForWorkspace(ctx, nfo.LatestInstance.ID)
61+
62+
rsa, err := it.Instrument(integration.ComponentWorkspace, "workspace", integration.WithInstanceID(nfo.LatestInstance.ID))
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
defer rsa.Close()
67+
68+
// get actual from workspace
69+
actBranch := git.GetBranch(t, rsa, test.WorkspaceRoot)
70+
rsa.Close()
71+
72+
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
73+
defer cancel()
74+
_, err = it.API().WorkspaceManager().StopWorkspace(ctx, &wsapi.StopWorkspaceRequest{
75+
Id: nfo.LatestInstance.ID,
76+
})
77+
if err != nil {
78+
t.Fatal(err)
79+
return
80+
}
81+
82+
// perform actual comparion
83+
if actBranch != test.ExpectedBranch {
84+
t.Fatalf("expected branch '%s', got '%s'!", test.ExpectedBranch, actBranch)
85+
}
86+
})
87+
}
88+
}

test/tests/workspace/git/git.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package git
6+
7+
import (
8+
"fmt"
9+
"net/rpc"
10+
"testing"
11+
12+
agent "github.com/gitpod-io/gitpod/test/tests/workspace/workspace_agent/api"
13+
)
14+
15+
func GetBranch(t *testing.T, rsa *rpc.Client, workspaceRoot string) string {
16+
var resp agent.ExecResponse
17+
err := rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
18+
Dir: workspaceRoot,
19+
Command: "git",
20+
Args: []string{"rev-parse", "--abbrev-ref", "HEAD"},
21+
}, &resp)
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
if resp.ExitCode != 0 {
26+
t.Fatal(fmt.Errorf("getBranch returned rc: %d", resp.ExitCode))
27+
}
28+
return resp.Stdout
29+
}
30+
31+
func Commit(t *testing.T, rsa *rpc.Client, workspaceRoot string, message string, all bool) {
32+
args := []string{"commit", "-m", message}
33+
if all {
34+
args = append(args, "--all")
35+
}
36+
var resp agent.ExecResponse
37+
err := rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
38+
Dir: workspaceRoot,
39+
Command: "git",
40+
Args: args,
41+
}, &resp)
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
if resp.ExitCode != 0 {
46+
t.Fatal(fmt.Errorf("commit returned rc: %d", resp.ExitCode))
47+
}
48+
}

test/tests/workspace/workspace_agent/api/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ type WriteFileRequest struct {
2626
// WriteFileResponse is the response for WriteFile
2727
type WriteFileResponse struct {
2828
}
29+
30+
type ExecRequest struct {
31+
Dir string
32+
Command string
33+
Args []string
34+
}
35+
36+
type ExecResponse struct {
37+
ExitCode int
38+
Stdout string
39+
Stderr string
40+
}

test/tests/workspace/workspace_agent/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package main
66

77
import (
8+
"bytes"
89
"io/ioutil"
10+
"os/exec"
911

1012
"github.com/gitpod-io/gitpod/test/pkg/integration"
1113
"github.com/gitpod-io/gitpod/test/tests/workspace/workspace_agent/api"
@@ -43,3 +45,34 @@ func (*WorkspaceAgent) WriteFile(req *api.WriteFileRequest, resp *api.WriteFileR
4345
*resp = api.WriteFileResponse{}
4446
return
4547
}
48+
49+
// Exec writes a file in the workspace
50+
func (*WorkspaceAgent) Exec(req *api.ExecRequest, resp *api.ExecResponse) (err error) {
51+
cmd := exec.Command(req.Command, req.Args...)
52+
var (
53+
stdout bytes.Buffer
54+
stderr bytes.Buffer
55+
)
56+
if req.Dir != "" {
57+
cmd.Dir = req.Dir
58+
}
59+
cmd.Stdout = &stdout
60+
cmd.Stderr = &stderr
61+
err = cmd.Run()
62+
63+
var rc int
64+
if err != nil {
65+
exitError, ok := err.(*exec.ExitError)
66+
if !ok {
67+
return err
68+
}
69+
rc = exitError.ExitCode()
70+
}
71+
72+
*resp = api.ExecResponse{
73+
ExitCode: rc,
74+
Stdout: stdout.String(),
75+
Stderr: stderr.String(),
76+
}
77+
return
78+
}

0 commit comments

Comments
 (0)