github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/tag/tagger_mux_test.go (about)

     1  /*
     2  Copyright 2020 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 tag
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    25  	"github.com/GoogleContainerTools/skaffold/testutil"
    26  )
    27  
    28  func TestCreateComponents(t *testing.T) {
    29  	runCtx := &runcontext.RunContext{}
    30  
    31  	digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
    32  	gitExample, _ := NewGitCommit("", "", false)
    33  	envExample, _ := NewEnvTemplateTagger("test")
    34  
    35  	tests := []struct {
    36  		description          string
    37  		customTemplateTagger *latest.CustomTemplateTagger
    38  		expected             map[string]Tagger
    39  		shouldErr            bool
    40  	}{
    41  		{
    42  			description: "correct component types",
    43  			customTemplateTagger: &latest.CustomTemplateTagger{
    44  				Components: []latest.TaggerComponent{
    45  					{Name: "FOO", Component: latest.TagPolicy{GitTagger: &latest.GitTagger{}}},
    46  					{Name: "FOE", Component: latest.TagPolicy{ShaTagger: &latest.ShaTagger{}}},
    47  					{Name: "BAR", Component: latest.TagPolicy{EnvTemplateTagger: &latest.EnvTemplateTagger{Template: "test"}}},
    48  					{Name: "BAT", Component: latest.TagPolicy{DateTimeTagger: &latest.DateTimeTagger{}}},
    49  					{Name: "BAS", Component: latest.TagPolicy{InputDigest: &latest.InputDigest{}}},
    50  				},
    51  			},
    52  			expected: map[string]Tagger{
    53  				"FOO": gitExample,
    54  				"FOE": &ChecksumTagger{},
    55  				"BAR": envExample,
    56  				"BAT": NewDateTimeTagger("", ""),
    57  				"BAS": digestExample,
    58  			},
    59  		},
    60  		{
    61  			description: "customTemplate is an invalid component",
    62  			customTemplateTagger: &latest.CustomTemplateTagger{
    63  				Components: []latest.TaggerComponent{
    64  					{Name: "FOO", Component: latest.TagPolicy{CustomTemplateTagger: &latest.CustomTemplateTagger{Template: "test"}}},
    65  				},
    66  			},
    67  			shouldErr: true,
    68  		},
    69  		{
    70  			description: "recurring names",
    71  			customTemplateTagger: &latest.CustomTemplateTagger{
    72  				Components: []latest.TaggerComponent{
    73  					{Name: "FOO", Component: latest.TagPolicy{GitTagger: &latest.GitTagger{}}},
    74  					{Name: "FOO", Component: latest.TagPolicy{GitTagger: &latest.GitTagger{}}},
    75  				},
    76  			},
    77  			shouldErr: true,
    78  		},
    79  		{
    80  			description: "unknown component",
    81  			customTemplateTagger: &latest.CustomTemplateTagger{
    82  				Components: []latest.TaggerComponent{
    83  					{Name: "FOO", Component: latest.TagPolicy{}},
    84  				},
    85  			},
    86  			shouldErr: true,
    87  		},
    88  	}
    89  	for _, test := range tests {
    90  		testutil.Run(t, test.description, func(t *testutil.T) {
    91  			components, err := CreateComponents(runCtx, test.customTemplateTagger)
    92  			t.CheckErrorAndDeepEqual(test.shouldErr, err, len(test.expected), len(components))
    93  			for k, v := range test.expected {
    94  				t.CheckTypeEquality(v, components[k])
    95  			}
    96  		})
    97  	}
    98  }