Path: blob/main/test/tests/ide/ssh/ssh_gateway_test.go
2506 views
// Copyright (c) 2020 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 ide56import (7"context"8"io"9"log"10"net/url"11"os"12"strings"13"testing"14"time"1516"github.com/helloyi/go-sshclient"17"sigs.k8s.io/e2e-framework/pkg/envconf"18"sigs.k8s.io/e2e-framework/pkg/features"1920gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"21"github.com/gitpod-io/gitpod/test/pkg/integration"22)2324func TestSSHGatewayConnection(t *testing.T) {25userToken, _ := os.LookupEnv("USER_TOKEN")26integration.SkipWithoutUsername(t, username)27integration.SkipWithoutUserToken(t, userToken)2829f := features.New("TestSSHGatewayConnection").30WithLabel("component", "server").31Assess("it can connect to a workspace via SSH gateway", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {32ctx, cancel := context.WithTimeout(testCtx, 10*time.Minute)33defer cancel()3435api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())36t.Cleanup(func() {37api.Done(t)38})3940_, err := api.CreateUser(username, userToken)41if err != nil {42t.Fatal(err)43}4445serverOpts := []integration.GitpodServerOpt{integration.WithGitpodUser(username)}46server, err := api.GitpodServer(serverOpts...)47if err != nil {48t.Fatal(err)49}5051// This env var caused an incident https://www.gitpodstatus.com/incidents/26gwnhcpvqqx before52// Which was introduced by PR https://github.com/gitpod-io/gitpod/pull/1382253// And fixed by PR https://github.com/gitpod-io/gitpod/pull/1385854_ = server.SetEnvVar(ctx, &gitpod.UserEnvVarValue{55Name: "TEST",56RepositoryPattern: "*/*",57Value: "\\\"test space\\\"",58})5960_ = server.SetEnvVar(ctx, &gitpod.UserEnvVarValue{61Name: "TEST_MULTIPLE_LINES",62RepositoryPattern: "*/*",63Value: `Hello64World`,65})6667nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "github.com/gitpod-io/empty", username, api)68if err != nil {69t.Fatal(err)70}71defer func() {72sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)73defer scancel()7475sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())76defer sapi.Done(t)7778stopWs(true, sapi)79}()8081wsUrl, err := url.Parse(nfo.LatestInstance.IdeURL)82if err != nil {83t.Fatal(err)84}8586wId := nfo.Workspace.ID87ownerToken, err := server.GetOwnerToken(ctx, wId)88if err != nil {89t.Fatal(err)90}9192urlComponents := strings.Split(wsUrl.Host, ".")93connUrl := []string{urlComponents[0], "ssh"}94connUrl = append(connUrl, urlComponents[1:]...)95connUrlStr := strings.Join(connUrl, ".")9697cli, err := sshclient.DialWithPasswd(connUrlStr+":22", wId, ownerToken)98if err != nil {99t.Fatal(err)100}101102output, err := cli.Cmd("gp info").Output()103if err != nil && err != io.EOF {104log.Println("[error]", err)105time.Sleep(1 * time.Second)106}107108t.Log(string(output))109110return testCtx111}).112Feature()113114testEnv.Test(t, f)115}116117118