github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/docker/tracker/tracker_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 tracker
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    23  	"github.com/GoogleContainerTools/skaffold/testutil"
    24  )
    25  
    26  type ArtifactIDPair struct {
    27  	artifact graph.Artifact
    28  	id       string
    29  }
    30  
    31  func TestDeployedContainers(t *testing.T) {
    32  	tests := []struct {
    33  		name               string
    34  		containers         []ArtifactIDPair
    35  		expectedContainers []string
    36  	}{
    37  		{
    38  			name:               "one container",
    39  			containers:         []ArtifactIDPair{{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"}},
    40  			expectedContainers: []string{"deadbeef"},
    41  		},
    42  		{
    43  			name: "two containers",
    44  			containers: []ArtifactIDPair{
    45  				{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"},
    46  				{artifact: graph.Artifact{ImageName: "image2"}, id: "foobar"},
    47  			},
    48  			expectedContainers: []string{"deadbeef", "foobar"},
    49  		},
    50  		{
    51  			name: "adding the same artifact overwrites previous ID",
    52  			containers: []ArtifactIDPair{
    53  				{artifact: graph.Artifact{ImageName: "image1"}, id: "this will be ignored"},
    54  				{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"},
    55  				{artifact: graph.Artifact{ImageName: "image2"}, id: "foobar"},
    56  			},
    57  			expectedContainers: []string{"deadbeef", "foobar"},
    58  		},
    59  		{
    60  			name: "tags are ignored (keyed only on image name)",
    61  			containers: []ArtifactIDPair{
    62  				{artifact: graph.Artifact{ImageName: "image1", Tag: "image1:tag1"}, id: "this will be ignored"},
    63  				{artifact: graph.Artifact{ImageName: "image1", Tag: "image1:tag2"}, id: "deadbeef"},
    64  				{artifact: graph.Artifact{ImageName: "image2"}, id: "foobar"},
    65  			},
    66  			expectedContainers: []string{"deadbeef", "foobar"},
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		testutil.Run(t, test.name, func(t *testutil.T) {
    72  			tracker := NewContainerTracker()
    73  			for _, pair := range test.containers {
    74  				tracker.Add(pair.artifact, Container{ID: pair.id})
    75  			}
    76  			deployedContainers := tracker.DeployedContainers()
    77  
    78  			// ensure each container exists in the returned map
    79  			for _, c := range test.expectedContainers {
    80  				found := false
    81  				for _, container := range deployedContainers {
    82  					if container.ID == c {
    83  						found = true
    84  					}
    85  				}
    86  				if !found {
    87  					t.Fail()
    88  				}
    89  			}
    90  		})
    91  	}
    92  }
    93  
    94  func TestDeployedContainerForImage(t *testing.T) {
    95  	tests := []struct {
    96  		name       string
    97  		containers []ArtifactIDPair
    98  		target     string
    99  		expected   string
   100  	}{
   101  		{
   102  			name:       "one container",
   103  			containers: []ArtifactIDPair{{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"}},
   104  			target:     "image1",
   105  			expected:   "deadbeef",
   106  		},
   107  		{
   108  			name: "two containers, retrieve the second",
   109  			containers: []ArtifactIDPair{
   110  				{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"},
   111  				{artifact: graph.Artifact{ImageName: "image2"}, id: "foobar"},
   112  			},
   113  			target:   "image2",
   114  			expected: "foobar",
   115  		},
   116  		{
   117  			name: "adding the same artifact overwrites previous ID",
   118  			containers: []ArtifactIDPair{
   119  				{artifact: graph.Artifact{ImageName: "image1"}, id: "this will be ignored"},
   120  				{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"},
   121  				{artifact: graph.Artifact{ImageName: "image2"}, id: "foobar"},
   122  			},
   123  			target:   "image1",
   124  			expected: "deadbeef",
   125  		},
   126  		{
   127  			name: "tags are ignored (keyed only on image name)",
   128  			containers: []ArtifactIDPair{
   129  				{artifact: graph.Artifact{ImageName: "image1", Tag: "image1:tag1"}, id: "this will be ignored"},
   130  				{artifact: graph.Artifact{ImageName: "image1", Tag: "image1:tag2"}, id: "deadbeef"},
   131  				{artifact: graph.Artifact{ImageName: "image2"}, id: "foobar"},
   132  			},
   133  			target:   "image1",
   134  			expected: "deadbeef",
   135  		},
   136  		{
   137  			name:       "untracked image returns nothing",
   138  			containers: []ArtifactIDPair{{artifact: graph.Artifact{ImageName: "image1"}, id: "deadbeef"}},
   139  			target:     "bogus",
   140  		},
   141  	}
   142  
   143  	for _, test := range tests {
   144  		testutil.Run(t, test.name, func(t *testutil.T) {
   145  			tracker := NewContainerTracker()
   146  			for _, pair := range test.containers {
   147  				tracker.Add(pair.artifact, Container{ID: pair.id})
   148  			}
   149  			container, _ := tracker.ContainerForImage(test.target)
   150  			t.CheckDeepEqual(test.expected, container.ID)
   151  		})
   152  	}
   153  }