github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/ko/publisher_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 ko
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
    23  	"github.com/GoogleContainerTools/skaffold/testutil"
    24  )
    25  
    26  func TestPublishOptions(t *testing.T) {
    27  	tests := []struct {
    28  		description          string
    29  		ref                  string
    30  		repo                 string
    31  		insecureRegistries   map[string]bool
    32  		pushImages           bool
    33  		wantInsecureRegistry bool
    34  		wantTag              string
    35  	}{
    36  		{
    37  			description: "sideloaded image",
    38  			ref:         "registry.example.com/repository/myapp1:tag1",
    39  			repo:        "registry.example.com/repository/myapp1",
    40  			pushImages:  false,
    41  			wantTag:     "tag1",
    42  		},
    43  		{
    44  			description: "published image",
    45  			ref:         "registry.example.com/repository/myapp2:tag2",
    46  			repo:        "registry.example.com/repository/myapp2",
    47  			pushImages:  true,
    48  			wantTag:     "tag2",
    49  		},
    50  		{
    51  			description:          "insecure registry",
    52  			ref:                  "insecure.registry.example.com/repository/myapp3:tag3",
    53  			repo:                 "insecure.registry.example.com/repository/myapp3",
    54  			insecureRegistries:   map[string]bool{"insecure.registry.example.com": true},
    55  			pushImages:           true,
    56  			wantInsecureRegistry: true,
    57  			wantTag:              "tag3",
    58  		},
    59  		{
    60  			description:          "secure registry",
    61  			ref:                  "secure.registry.example.com/repository/myapp4:tag4",
    62  			repo:                 "secure.registry.example.com/repository/myapp4",
    63  			insecureRegistries:   map[string]bool{"insecure.registry.example.com": true},
    64  			pushImages:           true,
    65  			wantInsecureRegistry: false,
    66  			wantTag:              "tag4",
    67  		},
    68  	}
    69  	for _, test := range tests {
    70  		testutil.Run(t, test.description, func(t *testutil.T) {
    71  			dockerClient := fakeDockerAPIClient(test.ref, "imageID")
    72  			po, err := publishOptions(test.ref, test.pushImages, dockerClient, test.insecureRegistries)
    73  			t.CheckNoError(err)
    74  			if !po.Bare || po.BaseImportPaths || po.PreserveImportPaths {
    75  				t.Errorf("use ko's Bare naming option as it allow for arbitrary image names")
    76  			}
    77  			if po.DockerClient != dockerClient {
    78  				t.Errorf("use provided docker client")
    79  			}
    80  			if test.pushImages && po.DockerRepo != test.repo {
    81  				t.Errorf("wanted DockerRepo (%q), got (%q)", test.repo, po.DockerRepo)
    82  			}
    83  			if test.wantInsecureRegistry != po.InsecureRegistry {
    84  				t.Errorf("wanted InsecureRegistry (%v), got (%v)", test.wantInsecureRegistry, po.InsecureRegistry)
    85  			}
    86  			if !test.pushImages && po.LocalDomain != test.repo {
    87  				t.Errorf("wanted LocalDomain (%q), got (%q)", test.repo, po.DockerRepo)
    88  			}
    89  			if test.pushImages == po.Local {
    90  				t.Errorf("Local (%v) should be the inverse of pushImages (%v)", po.Local, test.pushImages)
    91  			}
    92  			if test.pushImages != po.Push {
    93  				t.Errorf("Push (%v) should match pushImages (%v)", po.Push, test.pushImages)
    94  			}
    95  			if po.Tags[0] != test.wantTag {
    96  				t.Errorf("wanted Tags (%+v), got (%+v)", []string{test.wantTag}, po.Tags)
    97  			}
    98  			if po.UserAgent != version.UserAgentWithClient() {
    99  				t.Errorf("wanted UserAgent (%s), got (%s)", version.UserAgentWithClient(), po.UserAgent)
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  func fakeDockerAPIClient(ref string, imageID string) *testutil.FakeAPIClient {
   106  	return (&testutil.FakeAPIClient{}).Add(ref, imageID)
   107  }