github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/tag/tag_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  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/warnings"
    27  	"github.com/GoogleContainerTools/skaffold/testutil"
    28  )
    29  
    30  func TestTagger_GenerateFullyQualifiedImageName(t *testing.T) {
    31  	// This is for testing envTemplate
    32  	envTemplateExample, _ := NewEnvTemplateTagger("{{.FOO}}")
    33  	invalidEnvTemplate, _ := NewEnvTemplateTagger("{{.BAR}}")
    34  	env := []string{"FOO=BAR"}
    35  
    36  	// This is for testing dateTime
    37  	aLocalTimeStamp := time.Date(2015, 03, 07, 11, 06, 39, 123456789, time.Local)
    38  	dateTimeExample := &dateTimeTagger{
    39  		Format:   "2006-01-02",
    40  		TimeZone: "UTC",
    41  		timeFn:   func() time.Time { return aLocalTimeStamp },
    42  	}
    43  	dateTimeExpected := "2015-03-07"
    44  
    45  	customTemplateExample, _ := NewCustomTemplateTagger("{{.DATE}}_{{.SHA}}", map[string]Tagger{"DATE": dateTimeExample})
    46  
    47  	tests := []struct {
    48  		description      string
    49  		imageName        string
    50  		tagger           Tagger
    51  		expected         string
    52  		expectedWarnings []string
    53  		shouldErr        bool
    54  	}{
    55  		{
    56  			description: "sha256 w/o tag",
    57  			imageName:   "test",
    58  			tagger:      &ChecksumTagger{},
    59  			expected:    "test:latest",
    60  		},
    61  		{
    62  			description: "sha256 w/ tag",
    63  			imageName:   "test:tag",
    64  			tagger:      &ChecksumTagger{},
    65  			expected:    "test:tag",
    66  		},
    67  		{
    68  			description: "envTemplate",
    69  			imageName:   "test",
    70  			tagger:      envTemplateExample,
    71  			expected:    "test:BAR",
    72  		},
    73  		{
    74  			description: "undefined env variable",
    75  			imageName:   "test",
    76  			tagger:      invalidEnvTemplate,
    77  			shouldErr:   true,
    78  		},
    79  		{
    80  			description: "dateTime",
    81  			imageName:   "test",
    82  			tagger:      dateTimeExample,
    83  			expected:    "test:" + dateTimeExpected,
    84  		},
    85  		{
    86  			description: "dateTime",
    87  			imageName:   "test",
    88  			tagger: &dateTimeTagger{
    89  				Format:   "2006-01-02",
    90  				TimeZone: "FOO",
    91  				timeFn:   func() time.Time { return aLocalTimeStamp },
    92  			},
    93  			shouldErr: true,
    94  		},
    95  		{
    96  			description: "customTemplate",
    97  			imageName:   "test",
    98  			tagger:      customTemplateExample,
    99  			expected:    "test:" + dateTimeExpected + "_latest",
   100  		},
   101  		{
   102  			description: "error on invalid image tag",
   103  			imageName:   "test",
   104  			tagger:      &CustomTag{Tag: "bar:bar"},
   105  			shouldErr:   true,
   106  		},
   107  		{
   108  			description: "error on invalid image tag inside imageName",
   109  			imageName:   "test:bar:bar",
   110  			tagger:      &ChecksumTagger{},
   111  			shouldErr:   true,
   112  		},
   113  	}
   114  	for _, test := range tests {
   115  		testutil.Run(t, test.description, func(t *testutil.T) {
   116  			fakeWarner := &warnings.Collect{}
   117  			t.Override(&warnings.Printf, fakeWarner.Warnf)
   118  			t.Override(&util.OSEnviron, func() []string { return env })
   119  
   120  			image := latest.Artifact{
   121  				ImageName: test.imageName,
   122  			}
   123  
   124  			tag, err := GenerateFullyQualifiedImageName(context.Background(), test.tagger, image)
   125  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, tag)
   126  			t.CheckDeepEqual(test.expectedWarnings, fakeWarner.Warnings)
   127  		})
   128  	}
   129  }