Path: blob/main/test/tests/components/ws-manager/dotfiles_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package wsmanager56import (7"context"8"encoding/json"9"fmt"10"os"11"strings"12"testing"13"time"1415corev1 "k8s.io/api/core/v1"16"sigs.k8s.io/e2e-framework/klient"17"sigs.k8s.io/e2e-framework/pkg/envconf"18"sigs.k8s.io/e2e-framework/pkg/features"1920csapi "github.com/gitpod-io/gitpod/content-service/api"21agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"22"github.com/gitpod-io/gitpod/test/pkg/integration"23wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"24)2526func TestDotfiles(t *testing.T) {27userToken, _ := os.LookupEnv("USER_TOKEN")28integration.SkipWithoutUsername(t, username)29integration.SkipWithoutUserToken(t, userToken)3031f := features.New("dotfiles").WithLabel("component", "ws-manager").Assess("ensure dotfiles are loaded", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {32t.Parallel()3334ctx, cancel := context.WithTimeout(testCtx, 5*time.Minute)35defer cancel()3637api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())38t.Cleanup(func() {39api.Done(t)40})4142userId, err := api.CreateUser(username, userToken)43if err != nil {44t.Fatal(err)45}4647// Scopes should larger than https://github.com/gitpod-io/gitpod/blob/main/components/supervisor/pkg/serverapi/publicapi.go#L99-L10948tokenId, err := api.CreateOAuth2Token(username, []string{49"function:getToken",50"function:openPort",51"function:getOpenPorts",52"function:guessGitTokenScopes",53"function:getWorkspace",54"function:sendHeartBeat",55"function:trackEvent",56"resource:token::*::get",57})58if err != nil {59t.Fatal(err)60}6162swr := func(req *wsmanapi.StartWorkspaceRequest) error {63req.Spec.Envvars = append(req.Spec.Envvars,64&wsmanapi.EnvironmentVariable{65Name: "SUPERVISOR_DOTFILE_REPO",66Value: "https://github.com/gitpod-io/test-dotfiles-support",67},68&wsmanapi.EnvironmentVariable{69Name: "THEIA_SUPERVISOR_TOKENS",70Value: fmt.Sprintf(`[{71"token": "%v",72"kind": "gitpod",73"host": "%v",74"scope": ["function:getToken", "function:openPort", "function:sendHeartBeat", "function:getOpenPorts", "function:guessGitTokenScopes", "function:getWorkspace", "function:trackEvent", "resource:token::*::get"],75"expiryDate": "2026-10-26T10:38:05.232Z",76"reuse": 477}]`, tokenId, getHostUrl(ctx, t, cfg.Client(), cfg.Namespace())),78},79)8081req.Spec.Initializer = &csapi.WorkspaceInitializer{82Spec: &csapi.WorkspaceInitializer_Git{83Git: &csapi.GitInitializer{84RemoteUri: "https://github.com/gitpod-io/empty",85CheckoutLocation: "empty",86Config: &csapi.GitConfig{},87},88},89}9091req.Metadata.Owner = userId92req.Spec.WorkspaceLocation = "empty"93return nil94}9596ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(swr))97if err != nil {98t.Fatal(err)99}100101defer func() {102sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)103defer scancel()104105sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())106defer sapi.Done(t)107108_, err = stopWs(true, sapi)109if err != nil {110t.Errorf("cannot stop workspace: %q", err)111}112}()113114rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),115integration.WithInstanceID(ws.Req.Id),116integration.WithContainer("workspace"),117integration.WithWorkspacekitLift(true),118)119if err != nil {120t.Fatal(err)121}122123integration.DeferCloser(t, closer)124defer rsa.Close()125126assertDotfiles(t, rsa)127128return testCtx129}).Feature()130131testEnv.Test(t, f)132}133134func getHostUrl(ctx context.Context, t *testing.T, k8sClient klient.Client, namespace string) string {135var configmap corev1.ConfigMap136if err := k8sClient.Resources().Get(ctx, "server-config", namespace, &configmap); err != nil {137t.Fatal(err)138}139140config, ok := configmap.Data["config.json"]141if !ok {142t.Fatal("server config map does not contain config.json")143}144145c := make(map[string]json.RawMessage)146if err := json.Unmarshal([]byte(config), &c); err != nil {147t.Fatal(err)148}149150hostUrlRaw, ok := c["hostUrl"]151if !ok {152t.Fatal("server config map does not contain host url")153}154155return strings.TrimPrefix(strings.Trim(string(hostUrlRaw), "\""), "https://")156}157158func assertDotfiles(t *testing.T, rsa *integration.RpcClient) error {159var ls agent.ListDirResponse160err := rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{161Dir: "/home/gitpod/.dotfiles",162}, &ls)163164if err != nil {165t.Fatal(err)166}167168dotfiles := map[string]bool{169"bash_aliases": false,170"git": false,171}172173for _, dir := range ls.Files {174delete(dotfiles, dir)175}176177if len(dotfiles) > 0 {178var cat agent.ExecResponse179err := rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{180Dir: "/",181Command: "cat",182Args: []string{"/home/gitpod/.dotfiles.log"},183}, &cat)184if err == nil {185t.Fatalf("dotfiles were not installed successfully: %+v, .dotfiles.log: %s", dotfiles, cat.Stdout)186}187t.Fatalf("dotfiles were not installed successfully: %+v", dotfiles)188}189190return nil191}192193194