github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/modules/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 TestPrintModulesList(t *testing.T) {
    36  	tests := []struct {
    37  		description string
    38  		configSet   parser.SkaffoldConfigSet
    39  		includeAll  bool
    40  		err         error
    41  		expected    string
    42  	}{
    43  		{
    44  			description: "print modules",
    45  			configSet: parser.SkaffoldConfigSet{
    46  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: ""}}, SourceFile: "path/to/cfg1", SourceIndex: 0, IsRootConfig: true},
    47  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg1"}}, SourceFile: "path/to/cfg1", SourceIndex: 1, IsRootConfig: true},
    48  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg2"}}, SourceFile: "path/to/cfg2", SourceIndex: 0, IsRemote: true},
    49  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: ""}}, SourceFile: "path/to/cfg3", SourceIndex: 0},
    50  			},
    51  			expected: `{"modules":[{"name":"cfg1","path":"path/to/cfg1","isRoot":true},{"name":"cfg2","path":"path/to/cfg2","isRemote":true}]}` + "\n",
    52  		},
    53  		{
    54  			description: "print modules; include all",
    55  			configSet: parser.SkaffoldConfigSet{
    56  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: ""}}, SourceFile: "path/to/cfg1", SourceIndex: 0, IsRootConfig: true},
    57  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg1"}}, SourceFile: "path/to/cfg1", SourceIndex: 1, IsRootConfig: true},
    58  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: "cfg2"}}, SourceFile: "path/to/cfg2", SourceIndex: 0, IsRemote: true},
    59  				&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{Metadata: latest.Metadata{Name: ""}}, SourceFile: "path/to/cfg3", SourceIndex: 0},
    60  			},
    61  			includeAll: true,
    62  			expected: `{"modules":[` +
    63  				`{"name":"__config_0","path":"path/to/cfg1","isRoot":true},` +
    64  				`{"name":"cfg1","path":"path/to/cfg1","isRoot":true},` +
    65  				`{"name":"cfg2","path":"path/to/cfg2","isRemote":true},` +
    66  				`{"name":"__config_0","path":"path/to/cfg3"}]}` + "\n",
    67  		},
    68  		{
    69  			description: "actionable error",
    70  			err:         sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")),
    71  			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",
    72  		},
    73  		{
    74  			description: "generic error",
    75  			err:         errors.New("some error occurred"),
    76  			expected:    `{"errorCode":"INSPECT_UNKNOWN_ERR","errorMessage":"some error occurred"}` + "\n",
    77  		},
    78  	}
    79  
    80  	for _, test := range tests {
    81  		testutil.Run(t, test.description, func(t *testutil.T) {
    82  			t.Override(&inspect.GetConfigSet, func(ctx context.Context, opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) {
    83  				if len(opts.ConfigurationFilter) == 0 {
    84  					return test.configSet, test.err
    85  				}
    86  				var set parser.SkaffoldConfigSet
    87  				if stringslice.Contains(opts.ConfigurationFilter, "cfg1") {
    88  					set = append(set, test.configSet[0])
    89  				}
    90  				if stringslice.Contains(opts.ConfigurationFilter, "cfg2") {
    91  					set = append(set, test.configSet[1])
    92  				}
    93  				return set, test.err
    94  			})
    95  			var buf bytes.Buffer
    96  			err := PrintModulesList(context.Background(), &buf, inspect.Options{OutFormat: "json", ModulesOptions: inspect.ModulesOptions{IncludeAll: test.includeAll}})
    97  			t.CheckError(test.err != nil, err)
    98  			t.CheckDeepEqual(test.expected, buf.String())
    99  		})
   100  	}
   101  }