Path: blob/main/components/supervisor/pkg/config/gitpod-config_test.go
2500 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 config56import (7"context"8"fmt"9"os"10"testing"11"time"1213"github.com/google/go-cmp/cmp"14"golang.org/x/sync/errgroup"1516gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"17)1819func TestGitpodConfig(t *testing.T) {20tests := []struct {21Desc string22Content string23Expectation *gitpod.GitpodConfig24}{25{26Desc: "parsing",27Content: `28image: eu.gcr.io/gitpod-core-dev/dev/dev-environment:clu-yq4.129workspaceLocation: gitpod/gitpod-ws.code-workspace30checkoutLocation: gitpod31ports:32- port: 133733onOpen: open-preview34- port: 300035onOpen: ignore36tasks:37- before: scripts/branch-namespace.sh38init: yarn --network-timeout 100000 && yarn build39- name: Go40init: leeway exec --filter-type go -v -- go get -v ./...41openMode: split-right42vscode:43extensions:44- [email protected]:UATTe2sTFfCYWQ3jw4IRsw==45- [email protected]:ZnPmyF/Pb8AIWeCqc83gPw==`,46Expectation: &gitpod.GitpodConfig{47Image: "eu.gcr.io/gitpod-core-dev/dev/dev-environment:clu-yq4.1",48WorkspaceLocation: "gitpod/gitpod-ws.code-workspace",49CheckoutLocation: "gitpod",50Ports: []*gitpod.PortsItems{51{52Port: 1337,53OnOpen: "open-preview",54}, {55Port: 3000,56OnOpen: "ignore",57},58},59Tasks: []*gitpod.TasksItems{60{61Before: "scripts/branch-namespace.sh",62Init: "yarn --network-timeout 100000 && yarn build",63},64{65Name: "Go",66Init: "leeway exec --filter-type go -v -- go get -v ./...",67OpenMode: "split-right",68},69},70Vscode: &gitpod.Vscode{71Extensions: []string{72"[email protected]:UATTe2sTFfCYWQ3jw4IRsw==",73"[email protected]:ZnPmyF/Pb8AIWeCqc83gPw==",74},75},76},77},78}79for _, test := range tests {80t.Run(test.Desc, func(t *testing.T) {81tempDir, err := os.MkdirTemp("", "test-gitpod-config-*")82if err != nil {83t.Fatal(err)84}85defer os.RemoveAll(tempDir)8687locationReady := make(chan struct{})88configService := NewConfigService(tempDir+"/.gitpod.yml", locationReady)89ctx, cancel := context.WithCancel(context.Background())90defer cancel()91close(locationReady)9293go configService.Watch(ctx)9495var listeners []<-chan *gitpod.GitpodConfig96for i := 0; i < 10; i++ {97listeners = append(listeners, configService.Observe(ctx))98}99100for i := 0; i < 2; i++ {101eg, _ := errgroup.WithContext(ctx)102for _, listener := range listeners {103l := listener104eg.Go(func() error {105config := <-l106if diff := cmp.Diff((*gitpod.GitpodConfig)(nil), config); diff != "" {107return fmt.Errorf("unexpected output (-want +got):\n%s", diff)108}109return nil110})111}112err = eg.Wait()113if err != nil {114t.Fatal(err)115}116117err = os.WriteFile(configService.configLocation, []byte(test.Content), 0o600)118if err != nil {119t.Fatal(err)120}121122eg, _ = errgroup.WithContext(ctx)123for _, listener := range listeners {124l := listener125eg.Go(func() error {126config := <-l127if diff := cmp.Diff(test.Expectation, config); diff != "" {128return fmt.Errorf("unexpected output (-want +got):\n%s", diff)129}130return nil131})132}133err = eg.Wait()134if err != nil {135t.Fatal(err)136}137138err = os.Remove(configService.configLocation)139if err != nil {140t.Fatal(err)141}142}143})144}145}146147func TestInvalidGitpodConfig(t *testing.T) {148tempDir, err := os.MkdirTemp("", "test-gitpod-config-*")149if err != nil {150t.Fatal(err)151}152defer os.RemoveAll(tempDir)153154locationReady := make(chan struct{})155configService := NewConfigService(tempDir+"/.gitpod.yml", locationReady)156ctx, cancel := context.WithCancel(context.Background())157defer cancel()158close(locationReady)159160go configService.Watch(ctx)161162listener := configService.Observe(ctx)163164config := <-listener165if diff := cmp.Diff((*gitpod.GitpodConfig)(nil), config); diff != "" {166t.Errorf("unexpected output (-want +got):\n%s", diff)167}168169err = os.WriteFile(configService.configLocation, []byte(`170ports:171- port: 8080172173tasks:174- command: echo "Hello World"175176vscode:177extensions:178- foo.bar179`), 0o600)180if err != nil {181t.Fatal(err)182}183184config = <-listener185if diff := cmp.Diff(&gitpod.GitpodConfig{186Ports: []*gitpod.PortsItems{{Port: 8080}},187Tasks: []*gitpod.TasksItems{{Command: "echo \"Hello World\""}},188Vscode: &gitpod.Vscode{Extensions: []string{"foo.bar"}},189}, config); diff != "" {190t.Errorf("unexpected output (-want +got):\n%s", diff)191}192193err = os.WriteFile(configService.configLocation, []byte(`194ports:195- port:196visibility: private197- port: 8080198199tasks:200- before:201- command: echo "Hello World"202203vscode:204extensions:205-206`), 0o600)207if err != nil {208t.Fatal(err)209}210211time.Sleep(configService.configWatcher.debounceDuration * 10)212213err = os.WriteFile(configService.configLocation, []byte(`214ports:215- port: 8081216`), 0o600)217if err != nil {218t.Fatal(err)219}220221config = <-listener222if diff := cmp.Diff(&gitpod.GitpodConfig{223Ports: []*gitpod.PortsItems{{Port: 8081}},224}, config); diff != "" {225t.Errorf("unexpected output (-want +got):\n%s", diff)226}227}228229func TestWatchImageFile(t *testing.T) {230tempDir, err := os.MkdirTemp("", "test-gitpod-config-*")231if err != nil {232t.Fatal(err)233}234defer os.RemoveAll(tempDir)235236locationReady := make(chan struct{})237configService := NewConfigService(tempDir+"/.gitpod.yml", locationReady)238ctx, cancel := context.WithCancel(context.Background())239defer cancel()240close(locationReady)241242go configService.Watch(ctx)243244listener := configService.ObserveImageFile(ctx)245246err = os.WriteFile(configService.configLocation, []byte(`247image:248file: Dockerfile249`), 0o600)250if err != nil {251t.Fatal(err)252}253254changes := <-listener255if diff := cmp.Diff((*struct{})(nil), changes); diff != "" {256t.Errorf("unexpected output (-want +got):\n%s", diff)257}258259imageLocation := tempDir + "/Dockerfile"260err = os.WriteFile(imageLocation, []byte(`261FROM ubuntu262`), 0o600)263if err != nil {264t.Fatal(err)265}266267changes = <-listener268if diff := cmp.Diff(&struct{}{}, changes); diff != "" {269t.Errorf("unexpected output (-want +got):\n%s", diff)270}271272err = os.WriteFile(configService.configLocation, []byte(`273image: ubuntu274`), 0o600)275if err != nil {276t.Fatal(err)277}278279changes = <-listener280if diff := cmp.Diff((*struct{})(nil), changes); diff != "" {281t.Errorf("unexpected output (-want +got):\n%s", diff)282}283284err = os.WriteFile(configService.configLocation, []byte(`285image:286file: Dockerfile287`), 0o600)288if err != nil {289t.Fatal(err)290}291292changes = <-listener293if diff := cmp.Diff(&struct{}{}, changes); diff != "" {294t.Errorf("unexpected output (-want +got):\n%s", diff)295}296297err = os.WriteFile(imageLocation, []byte(`298FROM node299`), 0o600)300if err != nil {301t.Fatal(err)302}303304changes = <-listener305if diff := cmp.Diff(&struct{}{}, changes); diff != "" {306t.Errorf("unexpected output (-want +got):\n%s", diff)307}308}309310311