Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/workspace/ports_test.go
2496 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 workspace
6
7
import (
8
"context"
9
"encoding/json"
10
"fmt"
11
"net/http"
12
"os"
13
"reflect"
14
"strings"
15
"testing"
16
"time"
17
18
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
19
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
20
"github.com/gitpod-io/gitpod/test/pkg/integration"
21
"sigs.k8s.io/e2e-framework/pkg/envconf"
22
"sigs.k8s.io/e2e-framework/pkg/features"
23
)
24
25
func TestRegularWorkspacePorts(t *testing.T) {
26
userToken, _ := os.LookupEnv("USER_TOKEN")
27
integration.SkipWithoutUsername(t, username)
28
integration.SkipWithoutUserToken(t, userToken)
29
30
// This branch exposes a python server on port 3000 as part of the Gitpod tasks.
31
testRepo := "https://github.com/gitpod-io/gitpod-test-repo/tree/integration-test/ports"
32
testRepoName := "gitpod-test-repo"
33
wsLoc := fmt.Sprintf("/workspace/%s", testRepoName)
34
35
f := features.New("ports").
36
WithLabel("component", "workspace").
37
WithLabel("type", "ports").
38
Assess("it can open and access workspace ports", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
39
t.Parallel()
40
41
ctx, cancel := context.WithTimeout(testCtx, time.Duration(5*time.Minute))
42
defer cancel()
43
44
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
45
t.Cleanup(func() {
46
api.Done(t)
47
})
48
49
_, err := api.CreateUser(username, userToken)
50
if err != nil {
51
t.Fatal(err)
52
}
53
54
serverOpts := []integration.GitpodServerOpt{integration.WithGitpodUser(username)}
55
server, err := api.GitpodServer(serverOpts...)
56
if err != nil {
57
t.Fatal(err)
58
}
59
60
// Must change supervisor address from localhost to 10.0.5.2.
61
err = server.SetEnvVar(ctx, &gitpod.UserEnvVarValue{
62
Name: "SUPERVISOR_ADDR",
63
Value: `10.0.5.2:22999`,
64
RepositoryPattern: "gitpod-io/" + testRepoName,
65
})
66
if err != nil {
67
t.Fatal(err)
68
}
69
defer func() {
70
err := server.DeleteEnvVar(ctx, &gitpod.UserEnvVarValue{
71
Name: "SUPERVISOR_ADDR",
72
RepositoryPattern: "gitpod-io/" + testRepoName,
73
})
74
if err != nil {
75
t.Fatal(err)
76
}
77
}()
78
79
nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, testRepo, username, api, integration.WithGitpodUser(username))
80
if err != nil {
81
t.Fatal(err)
82
}
83
84
t.Cleanup(func() {
85
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
86
defer scancel()
87
88
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
89
defer sapi.Done(t)
90
91
if _, err = stopWs(true, sapi); err != nil {
92
t.Errorf("cannot stop workspace: %q", err)
93
}
94
})
95
96
instanceId := nfo.LatestInstance.ID
97
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(instanceId))
98
integration.DeferCloser(t, closer)
99
if err != nil {
100
t.Fatalf("unexpected error instrumenting workspace: %v", err)
101
}
102
defer rsa.Close()
103
104
type Port struct {
105
LocalPort int `json:"localPort,omitempty"`
106
Exposed struct {
107
Visibility string `json:"visibility,omitempty"`
108
Url string `json:"url,omitempty"`
109
} `json:"exposed,omitempty"`
110
}
111
var portsResp struct {
112
Result struct {
113
Ports []*Port `json:"ports,omitempty"`
114
} `json:"result"`
115
}
116
117
expectPort := func(expected Port) {
118
for i := 0; i < 10; i++ {
119
var res agent.ExecResponse
120
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
121
Dir: wsLoc,
122
Command: "curl",
123
// nftable rule only forwards to this ip address
124
Args: []string{"10.0.5.2:22999/_supervisor/v1/status/ports"},
125
}, &res)
126
if err != nil {
127
t.Fatal(err)
128
}
129
err = json.Unmarshal([]byte(res.Stdout), &portsResp)
130
if err != nil {
131
t.Fatalf("cannot decode supervisor ports status response: %s", err)
132
}
133
134
t.Logf("ports: %s (%d)", res.Stdout, res.ExitCode)
135
if len(portsResp.Result.Ports) != 1 {
136
t.Logf("expected one port to be open, but got %d, retrying", len(portsResp.Result.Ports))
137
time.Sleep(2 * time.Second)
138
continue
139
}
140
if !reflect.DeepEqual(expected, *portsResp.Result.Ports[0]) {
141
t.Logf("expected %v but got %v, retrying", expected, *portsResp.Result.Ports[0])
142
time.Sleep(2 * time.Second)
143
continue
144
}
145
146
// Got expected port.
147
return
148
}
149
150
// If we get here, we didn't get the expected port status after retrying.
151
t.Fatalf("did not get expected port status after 10 attempts")
152
}
153
154
t.Logf("checking that port has been auto-detected, and is private")
155
portUrl := fmt.Sprintf("https://%d-%s", 3000, strings.TrimPrefix(nfo.LatestInstance.IdeURL, "https://"))
156
expectPort(Port{
157
LocalPort: 3000,
158
Exposed: struct {
159
Visibility string `json:"visibility,omitempty"`
160
Url string `json:"url,omitempty"`
161
}{
162
Visibility: "private",
163
Url: portUrl,
164
},
165
})
166
167
t.Logf("checking that private port is not accessible from outside the workspace at %s", portUrl)
168
res, err := http.Get(portUrl)
169
if err != nil {
170
t.Fatal(err)
171
}
172
if res.StatusCode != http.StatusUnauthorized {
173
t.Fatalf("expected status code 401, but got %d", res.StatusCode)
174
}
175
176
// Make port public.
177
t.Logf("making port public via gp ports visibility")
178
var res1 agent.ExecResponse
179
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
180
Dir: wsLoc,
181
Command: "gp",
182
// nftable rule only forwards to this ip address
183
Args: []string{"ports", "visibility", "3000:public"},
184
}, &res1)
185
if err != nil {
186
t.Fatal(err)
187
}
188
t.Logf("debug: gp CLI output: %s, %s, %d", res1.Stdout, res1.Stderr, res1.ExitCode)
189
190
t.Logf("checking that port is now public")
191
expectPort(Port{
192
LocalPort: 3000,
193
Exposed: struct {
194
Visibility string `json:"visibility,omitempty"`
195
Url string `json:"url,omitempty"`
196
}{
197
Visibility: "public",
198
Url: portUrl,
199
},
200
})
201
202
t.Logf("checking if port is accessible from outside the workspace at %s", portUrl)
203
res, err = http.Get(portUrl)
204
if err != nil {
205
t.Fatal(err)
206
}
207
if res.StatusCode != http.StatusOK {
208
t.Fatalf("expected status code 200, but got %d", res.StatusCode)
209
}
210
211
return testCtx
212
}).
213
Feature()
214
215
testEnv.Test(t, f)
216
}
217
218