github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/buildEnv/modify_gcb_test.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package inspect
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util/stringslice"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
    30  	"github.com/GoogleContainerTools/skaffold/proto/v1"
    31  	"github.com/GoogleContainerTools/skaffold/testutil"
    32  )
    33  
    34  func TestModifyGcbBuildEnv(t *testing.T) {
    35  	tests := []struct {
    36  		description     string
    37  		profile         string
    38  		modules         []string
    39  		buildEnvOpts    inspect.BuildEnvOptions
    40  		expectedConfigs []string
    41  		errCode         proto.StatusCode
    42  		strict          bool
    43  	}{
    44  		{
    45  			description:  "modify default pipeline; strict true",
    46  			buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project2"},
    47  			strict:       true,
    48  			expectedConfigs: []string{
    49  				`apiVersion: ""
    50  kind: ""
    51  metadata:
    52    name: cfg1
    53  requires:
    54  - path: path/to/cfg2
    55  build:
    56    googleCloudBuild:
    57      projectId: project2
    58  profiles:
    59  - name: p1
    60    build:
    61      googleCloudBuild:
    62        projectId: project1
    63  - name: p2
    64    build:
    65      cluster: {}
    66  `, ``,
    67  			},
    68  		},
    69  		{
    70  			description:  "modify profile pipeline; strict true",
    71  			strict:       true,
    72  			profile:      "p1",
    73  			buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project2"},
    74  			expectedConfigs: []string{
    75  				`apiVersion: ""
    76  kind: ""
    77  metadata:
    78    name: cfg1
    79  requires:
    80  - path: path/to/cfg2
    81  build:
    82    googleCloudBuild:
    83      projectId: project1
    84  profiles:
    85  - name: p1
    86    build:
    87      googleCloudBuild:
    88        projectId: project2
    89  - name: p2
    90    build:
    91      cluster: {}
    92  `, `apiVersion: ""
    93  kind: ""
    94  metadata:
    95    name: cfg2
    96  build:
    97    local: {}
    98  profiles:
    99  - name: p1
   100    build:
   101      googleCloudBuild:
   102        projectId: project2
   103  `,
   104  			},
   105  		},
   106  		{
   107  			description:  "add to non-existing profile; strict true",
   108  			strict:       true,
   109  			profile:      "p3",
   110  			buildEnvOpts: inspect.BuildEnvOptions{MachineType: "machine2", Concurrency: 2},
   111  			errCode:      proto.StatusCode_INSPECT_PROFILE_NOT_FOUND_ERR,
   112  		},
   113  		{
   114  			description:  "add to profile with wrong build env type; strict true",
   115  			strict:       true,
   116  			profile:      "p2",
   117  			buildEnvOpts: inspect.BuildEnvOptions{MachineType: "machine2", Concurrency: 2},
   118  			errCode:      proto.StatusCode_INSPECT_BUILD_ENV_INCORRECT_TYPE_ERR,
   119  		},
   120  		{
   121  			description:  "modify default pipeline; strict false",
   122  			buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project2"},
   123  			strict:       false,
   124  			expectedConfigs: []string{
   125  				`apiVersion: ""
   126  kind: ""
   127  metadata:
   128    name: cfg1
   129  requires:
   130  - path: path/to/cfg2
   131  build:
   132    googleCloudBuild:
   133      projectId: project2
   134  profiles:
   135  - name: p1
   136    build:
   137      googleCloudBuild:
   138        projectId: project1
   139  - name: p2
   140    build:
   141      cluster: {}
   142  `, ``,
   143  			},
   144  		},
   145  		{
   146  			description:  "modify profile pipeline; strict false",
   147  			strict:       false,
   148  			profile:      "p1",
   149  			buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project2"},
   150  			expectedConfigs: []string{
   151  				`apiVersion: ""
   152  kind: ""
   153  metadata:
   154    name: cfg1
   155  requires:
   156  - path: path/to/cfg2
   157  build:
   158    googleCloudBuild:
   159      projectId: project1
   160  profiles:
   161  - name: p1
   162    build:
   163      googleCloudBuild:
   164        projectId: project2
   165  - name: p2
   166    build:
   167      cluster: {}
   168  `, `apiVersion: ""
   169  kind: ""
   170  metadata:
   171    name: cfg2
   172  build:
   173    local: {}
   174  profiles:
   175  - name: p1
   176    build:
   177      googleCloudBuild:
   178        projectId: project2
   179  `,
   180  			},
   181  		},
   182  		{
   183  			description:  "add to non-existing profile; strict false",
   184  			strict:       false,
   185  			profile:      "p3",
   186  			buildEnvOpts: inspect.BuildEnvOptions{MachineType: "machine2", Concurrency: 2},
   187  			errCode:      proto.StatusCode_INSPECT_PROFILE_NOT_FOUND_ERR,
   188  		},
   189  		{
   190  			description:  "add to profile with wrong build env type; strict false",
   191  			strict:       false,
   192  			profile:      "p2",
   193  			buildEnvOpts: inspect.BuildEnvOptions{MachineType: "machine2", Concurrency: 2},
   194  			expectedConfigs: []string{
   195  				`apiVersion: ""
   196  kind: ""
   197  metadata:
   198    name: cfg1
   199  requires:
   200  - path: path/to/cfg2
   201  build:
   202    googleCloudBuild:
   203      projectId: project1
   204  profiles:
   205  - name: p1
   206    build:
   207      googleCloudBuild:
   208        projectId: project1
   209  - name: p2
   210    build:
   211      cluster: {}
   212  `, `apiVersion: ""
   213  kind: ""
   214  metadata:
   215    name: cfg2
   216  build:
   217    local: {}
   218  profiles:
   219  - name: p1
   220    build:
   221      googleCloudBuild:
   222        projectId: project1
   223  `,
   224  			},
   225  		},
   226  	}
   227  	for _, test := range tests {
   228  		testutil.Run(t, test.description, func(t *testutil.T) {
   229  			configSet := parser.SkaffoldConfigSet{
   230  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{
   231  					Metadata:     latest.Metadata{Name: "cfg1"},
   232  					Dependencies: []latest.ConfigDependency{{Path: pathToCfg2}},
   233  					Pipeline:     latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{ProjectID: "project1"}}}},
   234  					Profiles: []latest.Profile{
   235  						{Name: "p1", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{ProjectID: "project1"}}}}},
   236  						{Name: "p2", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{Cluster: &latest.ClusterDetails{}}}}},
   237  					}}, SourceFile: pathToCfg1, IsRootConfig: true, SourceIndex: 0},
   238  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{
   239  					Metadata: latest.Metadata{Name: "cfg2"},
   240  					Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}},
   241  					Profiles: []latest.Profile{
   242  						{Name: "p1", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{ProjectID: "project1"}}}}},
   243  					}}, SourceFile: pathToCfg2, SourceIndex: 0},
   244  			}
   245  			t.Override(&inspect.GetConfigSet, func(ctx context.Context, opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) {
   246  				if len(opts.ConfigurationFilter) == 0 || stringslice.Contains(opts.ConfigurationFilter, "cfg1") {
   247  					return configSet, nil
   248  				}
   249  				if stringslice.Contains(opts.ConfigurationFilter, "cfg2") {
   250  					return parser.SkaffoldConfigSet{configSet[0]}, nil
   251  				}
   252  				return nil, nil
   253  			})
   254  			t.Override(&inspect.ReadFileFunc, func(filename string) ([]byte, error) {
   255  				if filename == pathToCfg1 {
   256  					return yaml.MarshalWithSeparator([]*latest.SkaffoldConfig{configSet[0].SkaffoldConfig})
   257  				} else if filename == pathToCfg2 {
   258  					return yaml.MarshalWithSeparator([]*latest.SkaffoldConfig{configSet[1].SkaffoldConfig})
   259  				}
   260  				t.FailNow()
   261  				return nil, nil
   262  			})
   263  			var actualCfg1, actualCfg2 string
   264  			t.Override(&inspect.WriteFileFunc, func(filename string, data []byte) error {
   265  				switch filename {
   266  				case pathToCfg1:
   267  					actualCfg1 = string(data)
   268  				case pathToCfg2:
   269  					actualCfg2 = string(data)
   270  				default:
   271  					t.FailNow()
   272  				}
   273  				return nil
   274  			})
   275  
   276  			var buf bytes.Buffer
   277  			err := ModifyGcbBuildEnv(context.Background(), &buf, inspect.Options{OutFormat: "json", Modules: test.modules, Profile: test.profile, BuildEnvOptions: test.buildEnvOpts, Strict: test.strict})
   278  			t.CheckNoError(err)
   279  			if test.errCode == proto.StatusCode_OK {
   280  				t.CheckDeepEqual(test.expectedConfigs[0], actualCfg1, testutil.YamlObj(t.T))
   281  				t.CheckDeepEqual(test.expectedConfigs[1], actualCfg2, testutil.YamlObj(t.T))
   282  			} else {
   283  				t.CheckContains(test.errCode.String(), buf.String())
   284  			}
   285  		})
   286  	}
   287  }