github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/buildEnv/list_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  	"errors"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser"
    29  	sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/errors"
    30  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    31  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util/stringslice"
    32  	"github.com/GoogleContainerTools/skaffold/testutil"
    33  )
    34  
    35  func TestPrintBuildEnvsList(t *testing.T) {
    36  	tests := []struct {
    37  		description string
    38  		profiles    []string
    39  		module      []string
    40  		err         error
    41  		expected    string
    42  	}{
    43  		{
    44  			description: "print all build environments",
    45  			expected: `{"build_envs":[` +
    46  				`{"type":"local","path":"path/to/cfg1","module":"cfg1"},` +
    47  				`{"type":"googleCloudBuild","path":"path/to/cfg2","module":"cfg2"}` +
    48  				"]}\n",
    49  		},
    50  		{
    51  			description: "print all build environments for one module",
    52  			expected: `{"build_envs":[` +
    53  				`{"type":"local","path":"path/to/cfg1","module":"cfg1"}` +
    54  				"]}\n",
    55  			module: []string{"cfg1"},
    56  		},
    57  		{
    58  			description: "print all build environments for two activated profiles",
    59  
    60  			expected: `{"build_envs":[` +
    61  				`{"type":"cluster","path":"path/to/cfg1","module":"cfg1"},` +
    62  				`{"type":"local","path":"path/to/cfg2","module":"cfg2"}` +
    63  				"]}\n",
    64  			profiles: []string{"local", "cluster"},
    65  		},
    66  		{
    67  			description: "print all build environments for one module and an activated profile",
    68  
    69  			expected: `{"build_envs":[` +
    70  				`{"type":"cluster","path":"path/to/cfg1","module":"cfg1"}` +
    71  				"]}\n",
    72  			module:   []string{"cfg1"},
    73  			profiles: []string{"cluster"},
    74  		},
    75  		{
    76  			description: "actionable error",
    77  			err:         sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")),
    78  			expected:    `{"errorCode":"CONFIG_FILE_NOT_FOUND_ERR","errorMessage":"unable to find configuration file \"path/to/skaffold.yaml\": failed to read file : \"skaffold.yaml\". Check that the specified configuration file exists at \"path/to/skaffold.yaml\"."}` + "\n",
    79  		},
    80  		{
    81  			description: "generic error",
    82  			err:         errors.New("some error occurred"),
    83  			expected:    `{"errorCode":"INSPECT_UNKNOWN_ERR","errorMessage":"some error occurred"}` + "\n",
    84  		},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		testutil.Run(t, test.description, func(t *testutil.T) {
    89  			configSet := parser.SkaffoldConfigSet{
    90  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{
    91  					Metadata: latest.Metadata{Name: "cfg1"},
    92  					Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}},
    93  					Profiles: []latest.Profile{
    94  						{Name: "cluster", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{Cluster: &latest.ClusterDetails{}}}}},
    95  					}}, SourceFile: "path/to/cfg1"},
    96  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{
    97  					Metadata: latest.Metadata{Name: "cfg2"},
    98  					Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{GoogleCloudBuild: &latest.GoogleCloudBuild{}}}},
    99  					Profiles: []latest.Profile{
   100  						{Name: "local", Pipeline: latest.Pipeline{Build: latest.BuildConfig{BuildType: latest.BuildType{LocalBuild: &latest.LocalBuild{}}}}},
   101  					}}, SourceFile: "path/to/cfg2"},
   102  			}
   103  			t.Override(&inspect.GetConfigSet, func(ctx context.Context, opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) {
   104  				// mock profile activation
   105  				var set parser.SkaffoldConfigSet
   106  				for _, c := range configSet {
   107  					if len(opts.ConfigurationFilter) > 0 && !stringslice.Contains(opts.ConfigurationFilter, c.Metadata.Name) {
   108  						continue
   109  					}
   110  					for _, pName := range opts.Profiles {
   111  						for _, profile := range c.Profiles {
   112  							if profile.Name != pName {
   113  								continue
   114  							}
   115  							c.Build.BuildType = profile.Build.BuildType
   116  						}
   117  					}
   118  					set = append(set, c)
   119  				}
   120  				return set, test.err
   121  			})
   122  			var buf bytes.Buffer
   123  			err := PrintBuildEnvsList(context.Background(), &buf, inspect.Options{OutFormat: "json", Modules: test.module, Profiles: test.profiles})
   124  			t.CheckError(test.err != nil, err)
   125  			t.CheckDeepEqual(test.expected, buf.String())
   126  		})
   127  	}
   128  }