Path: blob/main/components/content-service-api/go/initializer_test.go
2498 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 api_test56import (7"path/filepath"8"strings"9"testing"1011"github.com/gitpod-io/gitpod/content-service/api"12"github.com/google/go-cmp/cmp"13"github.com/google/go-cmp/cmp/cmpopts"14"google.golang.org/protobuf/proto"15)1617func TestGetCheckoutLocationsFromInitializer(t *testing.T) {18var init []*api.WorkspaceInitializer19init = append(init, &api.WorkspaceInitializer{20Spec: &api.WorkspaceInitializer_Git{21Git: &api.GitInitializer{22CheckoutLocation: "/foo",23CloneTaget: "head",24Config: &api.GitConfig{25Authentication: api.GitAuthMethod_NO_AUTH,26},27RemoteUri: "somewhere-else",28TargetMode: api.CloneTargetMode_LOCAL_BRANCH,29},30},31})32init = append(init, &api.WorkspaceInitializer{33Spec: &api.WorkspaceInitializer_Git{34Git: &api.GitInitializer{35CheckoutLocation: "/bar",36CloneTaget: "head",37Config: &api.GitConfig{38Authentication: api.GitAuthMethod_NO_AUTH,39},40RemoteUri: "somewhere-else",41TargetMode: api.CloneTargetMode_LOCAL_BRANCH,42},43},44})4546tests := []struct {47Name string48Initializer *api.WorkspaceInitializer49Expectation string50}{51{52Name: "single git initializer",53Initializer: &api.WorkspaceInitializer{54Spec: &api.WorkspaceInitializer_Git{55Git: &api.GitInitializer{56CheckoutLocation: "/foo",57CloneTaget: "head",58Config: &api.GitConfig{59Authentication: api.GitAuthMethod_NO_AUTH,60},61RemoteUri: "somewhere-else",62TargetMode: api.CloneTargetMode_LOCAL_BRANCH,63},64},65},66Expectation: "/foo",67},68{69Name: "multiple git initializer",70Initializer: &api.WorkspaceInitializer{71Spec: &api.WorkspaceInitializer_Composite{72Composite: &api.CompositeInitializer{73Initializer: init,74},75},76},77Expectation: "/foo,/bar",78},79{80Name: "backup initializer",81Initializer: &api.WorkspaceInitializer{82Spec: &api.WorkspaceInitializer_Backup{83Backup: &api.FromBackupInitializer{84CheckoutLocation: "/foobar",85},86},87},88Expectation: "/foobar",89},90{91Name: "prebuild initializer",92Initializer: &api.WorkspaceInitializer{93Spec: &api.WorkspaceInitializer_Prebuild{94Prebuild: &api.PrebuildInitializer{95Git: []*api.GitInitializer{96{CheckoutLocation: "/foo"},97{CheckoutLocation: "/bar"},98},99},100},101},102Expectation: "/foo,/bar",103},104{105Name: "nil initializer",106},107{108Name: "snapshot initializer",109Initializer: &api.WorkspaceInitializer{110Spec: &api.WorkspaceInitializer_Snapshot{111Snapshot: &api.SnapshotInitializer{112Snapshot: "foo",113FromVolumeSnapshot: true,114},115},116},117},118}119120for _, test := range tests {121t.Run(test.Name, func(t *testing.T) {122locations := strings.Join(api.GetCheckoutLocationsFromInitializer(test.Initializer), ",")123if locations != test.Expectation {124t.Errorf("expected %s, got %s", test.Expectation, locations)125}126})127}128}129130func TestExtractInjectSecretsFromInitializer(t *testing.T) {131tests := []struct {132Name string133Input *api.WorkspaceInitializer134Expectation map[string]string135}{136{137Name: "git initializer",138Input: &api.WorkspaceInitializer{139Spec: &api.WorkspaceInitializer_Git{140Git: &api.GitInitializer{141Config: &api.GitConfig{142AuthPassword: "foobar",143},144},145},146},147Expectation: map[string]string{148"initializer.git": "foobar",149},150},151{152Name: "no secret git initializer",153Input: &api.WorkspaceInitializer{154Spec: &api.WorkspaceInitializer_Git{155Git: &api.GitInitializer{156Config: &api.GitConfig{},157},158},159},160Expectation: map[string]string{},161},162{163Name: "prebuild initializer",164Input: &api.WorkspaceInitializer{165Spec: &api.WorkspaceInitializer_Prebuild{166Prebuild: &api.PrebuildInitializer{167Git: []*api.GitInitializer{168{169Config: &api.GitConfig{170AuthPassword: "foobar",171},172},173{174Config: &api.GitConfig{175AuthPassword: "some value",176},177},178},179},180},181},182Expectation: map[string]string{183"initializer.prebuild.0.git": "foobar",184"initializer.prebuild.1.git": "some value",185},186},187}188189for _, test := range tests {190t.Run(test.Name, func(t *testing.T) {191original := proto.Clone(test.Input)192act := api.GatherSecretsFromInitializer(test.Input)193if diff := cmp.Diff(test.Expectation, act); diff != "" {194t.Errorf("unexpected GatherSecretsFromInitializer (-want +got):\n%s", diff)195}196197ignoreUnexported := []interface{}{198api.WorkspaceInitializer{},199api.WorkspaceInitializer_Git{},200api.GitInitializer{},201api.GitConfig{},202api.PrebuildInitializer{},203}204if diff := cmp.Diff(original, test.Input, cmpopts.IgnoreUnexported(ignoreUnexported...)); diff != "" {205t.Errorf("unexpected alteration from GatherSecretsFromInitializer (-want +got):\n%s", diff)206}207208act = api.ExtractAndReplaceSecretsFromInitializer(test.Input)209if diff := cmp.Diff(test.Expectation, act); diff != "" {210t.Errorf("unexpected ExtractSecretsFromInitializer (-want +got):\n%s", diff)211}212213_ = api.WalkInitializer(nil, test.Input, func(path []string, init *api.WorkspaceInitializer) error {214git, ok := init.Spec.(*api.WorkspaceInitializer_Git)215if !ok {216return nil217}218if pwd := git.Git.Config.AuthPassword; pwd != "" && !strings.HasPrefix(pwd, "extracted-secret/") {219t.Errorf("expected authPassword to be extracted, but got %s at %s", pwd, filepath.Join(path...))220}221222return nil223})224225injection := make(map[string][]byte, len(act))226for k, v := range act {227injection[k] = []byte(v)228}229230err := api.InjectSecretsToInitializer(test.Input, injection)231if err != nil {232t.Fatal(err)233}234235_ = api.WalkInitializer(nil, test.Input, func(path []string, init *api.WorkspaceInitializer) error {236git, ok := init.Spec.(*api.WorkspaceInitializer_Git)237if !ok {238return nil239}240if pwd := git.Git.Config.AuthPassword; pwd != "" && strings.HasPrefix(pwd, "extracted-secret/") {241t.Errorf("expected authPassword to be injected, but got %s at %s", pwd, filepath.Join(path...))242}243244return nil245})246})247}248}249250251