Skip to content

Commit 6deb076

Browse files
Furistoroboquat
authored andcommitted
[workspace] Test dotfiles
1 parent 0f58438 commit 6deb076

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright (c) 2022 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 wsmanager
6+
7+
import (
8+
"context"
9+
"encoding/json"
10+
"fmt"
11+
"net/rpc"
12+
"os"
13+
"strings"
14+
"testing"
15+
"time"
16+
17+
corev1 "k8s.io/api/core/v1"
18+
"sigs.k8s.io/e2e-framework/klient"
19+
"sigs.k8s.io/e2e-framework/pkg/envconf"
20+
"sigs.k8s.io/e2e-framework/pkg/features"
21+
22+
csapi "github.com/gitpod-io/gitpod/content-service/api"
23+
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
24+
"github.com/gitpod-io/gitpod/test/pkg/integration"
25+
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
26+
)
27+
28+
func TestDotfiles(t *testing.T) {
29+
userToken, _ := os.LookupEnv("USER_TOKEN")
30+
integration.SkipWithoutUsername(t, username)
31+
integration.SkipWithoutUserToken(t, userToken)
32+
33+
f := features.New("dotfiles").WithLabel("component", "ws-manager").Assess("ensure dotfiles are loaded", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
34+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
35+
defer cancel()
36+
37+
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
38+
t.Cleanup(func() {
39+
api.Done(t)
40+
})
41+
42+
userId, err := api.CreateUser(username, userToken)
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
47+
tokenId, err := api.CreateOAuth2Token(username, []string{
48+
"function:getToken",
49+
"function:openPort",
50+
"function:getOpenPorts",
51+
"function:guessGitTokenScopes",
52+
"function:getWorkspace",
53+
"resource:token::*::get",
54+
})
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
swr := func(req *wsmanapi.StartWorkspaceRequest) error {
60+
req.Spec.Envvars = []*wsmanapi.EnvironmentVariable{
61+
{
62+
Name: "SUPERVISOR_DOTFILE_REPO",
63+
Value: "https://github.com/gitpod-io/test-dotfiles-support",
64+
},
65+
{
66+
Name: "THEIA_SUPERVISOR_TOKENS",
67+
Value: fmt.Sprintf(`[{
68+
"token": "%v",
69+
"kind": "gitpod",
70+
"host": "%v",
71+
"scope": ["function:getToken", "function:openPort", "function:getOpenPorts", "function:guessGitTokenScopes", "getWorkspace", "resource:token::*::get"],
72+
"expiryDate": "2022-10-26T10:38:05.232Z",
73+
"reuse": 4
74+
}]`, tokenId, getHostUrl(ctx, t, cfg.Client())),
75+
},
76+
}
77+
78+
req.Spec.Initializer = &csapi.WorkspaceInitializer{
79+
Spec: &csapi.WorkspaceInitializer_Git{
80+
Git: &csapi.GitInitializer{
81+
RemoteUri: "https://github.com/gitpod-io/empty",
82+
CheckoutLocation: "empty",
83+
Config: &csapi.GitConfig{},
84+
},
85+
},
86+
}
87+
88+
req.Metadata.Owner = userId
89+
req.Spec.WorkspaceLocation = "empty"
90+
return nil
91+
}
92+
93+
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(swr))
94+
if err != nil {
95+
t.Fatal(err)
96+
}
97+
98+
defer func() {
99+
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
100+
defer scancel()
101+
102+
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
103+
defer sapi.Done(t)
104+
105+
_, err = stopWs(true, sapi)
106+
if err != nil {
107+
t.Errorf("cannot stop workspace: %q", err)
108+
}
109+
}()
110+
111+
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),
112+
integration.WithInstanceID(ws.Req.Id),
113+
integration.WithContainer("workspace"),
114+
integration.WithWorkspacekitLift(true),
115+
)
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
120+
integration.DeferCloser(t, closer)
121+
defer rsa.Close()
122+
123+
assertDotfiles(t, rsa)
124+
125+
return ctx
126+
}).Feature()
127+
128+
testEnv.Test(t, f)
129+
}
130+
131+
func getHostUrl(ctx context.Context, t *testing.T, k8sClient klient.Client) string {
132+
var configmap corev1.ConfigMap
133+
if err := k8sClient.Resources().Get(ctx, "server-config", "default", &configmap); err != nil {
134+
t.Fatal(err)
135+
}
136+
137+
config, ok := configmap.Data["config.json"]
138+
if !ok {
139+
t.Fatal("server config map does not contain config.json")
140+
}
141+
142+
c := make(map[string]json.RawMessage)
143+
if err := json.Unmarshal([]byte(config), &c); err != nil {
144+
t.Fatal(err)
145+
}
146+
147+
hostUrlRaw, ok := c["hostUrl"]
148+
if !ok {
149+
t.Fatal("server config map does not contain host url")
150+
}
151+
152+
return strings.TrimPrefix(strings.Trim(string(hostUrlRaw), "\""), "https://")
153+
}
154+
155+
func assertDotfiles(t *testing.T, rsa *rpc.Client) error {
156+
var ls agent.ListDirResponse
157+
err := rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{
158+
Dir: "/home/gitpod/.dotfiles",
159+
}, &ls)
160+
161+
if err != nil {
162+
t.Fatal(err)
163+
}
164+
165+
dotfiles := map[string]bool{
166+
"bash_aliases": false,
167+
"git": false,
168+
}
169+
170+
for _, dir := range ls.Files {
171+
delete(dotfiles, dir)
172+
}
173+
174+
if len(dotfiles) > 0 {
175+
t.Fatalf("dotfiles were not installed successfully: %+v", dotfiles)
176+
}
177+
178+
return nil
179+
}

0 commit comments

Comments
 (0)