Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/ide/ssh/ssh_gateway_test.go
2506 views
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 ide
6
7
import (
8
"context"
9
"io"
10
"log"
11
"net/url"
12
"os"
13
"strings"
14
"testing"
15
"time"
16
17
"github.com/helloyi/go-sshclient"
18
"sigs.k8s.io/e2e-framework/pkg/envconf"
19
"sigs.k8s.io/e2e-framework/pkg/features"
20
21
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
22
"github.com/gitpod-io/gitpod/test/pkg/integration"
23
)
24
25
func TestSSHGatewayConnection(t *testing.T) {
26
userToken, _ := os.LookupEnv("USER_TOKEN")
27
integration.SkipWithoutUsername(t, username)
28
integration.SkipWithoutUserToken(t, userToken)
29
30
f := features.New("TestSSHGatewayConnection").
31
WithLabel("component", "server").
32
Assess("it can connect to a workspace via SSH gateway", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
33
ctx, cancel := context.WithTimeout(testCtx, 10*time.Minute)
34
defer cancel()
35
36
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
37
t.Cleanup(func() {
38
api.Done(t)
39
})
40
41
_, err := api.CreateUser(username, userToken)
42
if err != nil {
43
t.Fatal(err)
44
}
45
46
serverOpts := []integration.GitpodServerOpt{integration.WithGitpodUser(username)}
47
server, err := api.GitpodServer(serverOpts...)
48
if err != nil {
49
t.Fatal(err)
50
}
51
52
// This env var caused an incident https://www.gitpodstatus.com/incidents/26gwnhcpvqqx before
53
// Which was introduced by PR https://github.com/gitpod-io/gitpod/pull/13822
54
// And fixed by PR https://github.com/gitpod-io/gitpod/pull/13858
55
_ = server.SetEnvVar(ctx, &gitpod.UserEnvVarValue{
56
Name: "TEST",
57
RepositoryPattern: "*/*",
58
Value: "\\\"test space\\\"",
59
})
60
61
_ = server.SetEnvVar(ctx, &gitpod.UserEnvVarValue{
62
Name: "TEST_MULTIPLE_LINES",
63
RepositoryPattern: "*/*",
64
Value: `Hello
65
World`,
66
})
67
68
nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "github.com/gitpod-io/empty", username, api)
69
if err != nil {
70
t.Fatal(err)
71
}
72
defer func() {
73
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
74
defer scancel()
75
76
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
77
defer sapi.Done(t)
78
79
stopWs(true, sapi)
80
}()
81
82
wsUrl, err := url.Parse(nfo.LatestInstance.IdeURL)
83
if err != nil {
84
t.Fatal(err)
85
}
86
87
wId := nfo.Workspace.ID
88
ownerToken, err := server.GetOwnerToken(ctx, wId)
89
if err != nil {
90
t.Fatal(err)
91
}
92
93
urlComponents := strings.Split(wsUrl.Host, ".")
94
connUrl := []string{urlComponents[0], "ssh"}
95
connUrl = append(connUrl, urlComponents[1:]...)
96
connUrlStr := strings.Join(connUrl, ".")
97
98
cli, err := sshclient.DialWithPasswd(connUrlStr+":22", wId, ownerToken)
99
if err != nil {
100
t.Fatal(err)
101
}
102
103
output, err := cli.Cmd("gp info").Output()
104
if err != nil && err != io.EOF {
105
log.Println("[error]", err)
106
time.Sleep(1 * time.Second)
107
}
108
109
t.Log(string(output))
110
111
return testCtx
112
}).
113
Feature()
114
115
testEnv.Test(t, f)
116
}
117
118