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

     1  /*
     2  Copyright 2019 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  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/warnings"
    26  	"github.com/GoogleContainerTools/skaffold/testutil"
    27  )
    28  
    29  func TestEnvTemplateTagger_GenerateTag(t *testing.T) {
    30  	tests := []struct {
    31  		description      string
    32  		template         string
    33  		imageName        string
    34  		env              []string
    35  		expected         string
    36  		expectedWarnings []string
    37  		shouldErr        bool
    38  	}{
    39  		{
    40  			description: "empty env",
    41  			template:    "{{.IMAGE_NAME}}",
    42  			imageName:   "foo",
    43  			expected:    "foo",
    44  		},
    45  		{
    46  			description: "env",
    47  			template:    "{{.FOO}}-{{.BAZ}}:latest",
    48  			env:         []string{"FOO=BAR", "BAZ=BAT"},
    49  			imageName:   "foo",
    50  			expected:    "BAR-BAT:latest",
    51  		},
    52  		{
    53  			description: "missing env",
    54  			template:    "{{.FOO}}:latest",
    55  			shouldErr:   true,
    56  		},
    57  		{
    58  			description: "opts precedence",
    59  			template:    "{{.IMAGE_NAME}}-{{.FROM_ENV}}:latest",
    60  			env:         []string{"FROM_ENV=FOO", "IMAGE_NAME=BAT"},
    61  			imageName:   "image_name",
    62  			expected:    "image_name-FOO:latest",
    63  		},
    64  		{
    65  			description: "ignore @{{.DIGEST}} suffix",
    66  			template:    "{{.IMAGE_NAME}}:tag@{{.DIGEST}}",
    67  			imageName:   "foo",
    68  			shouldErr:   true,
    69  		},
    70  		{
    71  			description: "ignore @{{.DIGEST_ALGO}}:{{.DIGEST_HEX}} suffix",
    72  			template:    "{{.IMAGE_NAME}}:tag@{{.DIGEST_ALGO}}:{{.DIGEST_HEX}}",
    73  			imageName:   "image_name",
    74  			shouldErr:   true,
    75  		},
    76  		{
    77  			description: "digest is deprecated",
    78  			template:    "{{.IMAGE_NAME}}:{{.DIGEST}}",
    79  			imageName:   "foo",
    80  			shouldErr:   true,
    81  		},
    82  		{
    83  			description: "digest algo is deprecated",
    84  			template:    "{{.IMAGE_NAME}}:{{.DIGEST_ALGO}}",
    85  			imageName:   "foo",
    86  			shouldErr:   true,
    87  		},
    88  		{
    89  			description: "digest hex is deprecated",
    90  			template:    "{{.IMAGE_NAME}}:{{.DIGEST_HEX}}",
    91  			imageName:   "foo",
    92  			shouldErr:   true,
    93  		},
    94  	}
    95  	for _, test := range tests {
    96  		testutil.Run(t, test.description, func(t *testutil.T) {
    97  			fakeWarner := &warnings.Collect{}
    98  			t.Override(&warnings.Printf, fakeWarner.Warnf)
    99  			t.Override(&util.OSEnviron, func() []string { return test.env })
   100  
   101  			c, err := NewEnvTemplateTagger(test.template)
   102  			t.CheckNoError(err)
   103  
   104  			image := latest.Artifact{
   105  				ImageName: test.imageName,
   106  			}
   107  
   108  			got, err := c.GenerateTag(context.Background(), image)
   109  
   110  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, got)
   111  			t.CheckDeepEqual(test.expectedWarnings, fakeWarner.Warnings)
   112  		})
   113  	}
   114  }
   115  
   116  func TestNewEnvTemplateTagger(t *testing.T) {
   117  	tests := []struct {
   118  		description string
   119  		template    string
   120  		shouldErr   bool
   121  	}{
   122  		{
   123  			description: "valid template",
   124  			template:    "{{.FOO}}",
   125  		},
   126  		{
   127  			description: "invalid template",
   128  			template:    "{{.FOO",
   129  			shouldErr:   true,
   130  		},
   131  	}
   132  	for _, test := range tests {
   133  		testutil.Run(t, test.description, func(t *testutil.T) {
   134  			_, err := NewEnvTemplateTagger(test.template)
   135  
   136  			t.CheckError(test.shouldErr, err)
   137  		})
   138  	}
   139  }