github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/container/registry_test.go (about) 1 package container 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 11 ) 12 13 func TestReplaceTaggedRefDomain(t *testing.T) { 14 var namedTaggedTestCases = []struct { 15 defaultRegistry string 16 name string 17 expected string 18 }{ 19 {"myreg.com", "gcr.io/foo/bar:deadbeef", "myreg.com/gcr.io_foo_bar"}, 20 {"aws_account_id.dkr.ecr.region.amazonaws.com/bar", "gcr.io/baz/foo/bar:deadbeef", "aws_account_id.dkr.ecr.region.amazonaws.com/bar/gcr.io_baz_foo_bar"}, 21 } 22 23 for i, tc := range namedTaggedTestCases { 24 t.Run(fmt.Sprintf("Test Case #%d", i), func(t *testing.T) { 25 reg := &v1alpha1.RegistryHosting{Host: tc.defaultRegistry} 26 assertReplaceRegistryForLocal(t, reg, tc.name, tc.expected) 27 }) 28 } 29 } 30 31 func TestReplaceNamed(t *testing.T) { 32 var namedTestCases = []struct { 33 defaultRegistry string 34 name string 35 expected string 36 }{ 37 {"myreg.com", "gcr.io/foo/bar", "myreg.com/gcr.io_foo_bar"}, 38 {"myreg.com", "gcr.io/foo/bar", "myreg.com/gcr.io_foo_bar"}, 39 {"aws_account_id.dkr.ecr.region.amazonaws.com/bar", "gcr.io/baz/foo/bar", "aws_account_id.dkr.ecr.region.amazonaws.com/bar/gcr.io_baz_foo_bar"}, 40 {"gcr.io/foo", "docker.io/library/busybox", "gcr.io/foo/busybox"}, 41 {"gcr.io/foo", "bar", "gcr.io/foo/bar"}, 42 {"myreg.com", "myreg.com/bar", "myreg.com/bar"}, 43 {"myreg.com:5000", "myreg.com:5000/bar", "myreg.com:5000/bar"}, 44 {"myreg.com:5000", "myreg.com/bar", "myreg.com:5000/myreg.com_bar"}, 45 {"myreg.com", "myreg.com:5000/bar", "myreg.com/myreg.com_5000_bar"}, 46 } 47 48 for i, tc := range namedTestCases { 49 t.Run(fmt.Sprintf("Test case #%d", i), func(t *testing.T) { 50 reg := &v1alpha1.RegistryHosting{Host: tc.defaultRegistry} 51 assertReplaceRegistryForLocal(t, reg, tc.name, tc.expected) 52 }) 53 } 54 } 55 56 func TestReplaceRefForCluster(t *testing.T) { 57 var refForClusterCases = []struct { 58 host string 59 clusterHost string 60 name string 61 expectedLocal string 62 expectedCluster string 63 }{ 64 {"myreg.com", "", "gcr.io/foo/bar", "myreg.com/gcr.io_foo_bar", "myreg.com/gcr.io_foo_bar"}, 65 {"myreg.com", "myreg.com", "gcr.io/foo/bar", "myreg.com/gcr.io_foo_bar", "myreg.com/gcr.io_foo_bar"}, 66 {"localhost:1234", "registry:1234", "gcr.io/foo/bar", "localhost:1234/gcr.io_foo_bar", "registry:1234/gcr.io_foo_bar"}, 67 } 68 69 for i, tc := range refForClusterCases { 70 t.Run(fmt.Sprintf("Test case #%d", i), func(t *testing.T) { 71 reg := &v1alpha1.RegistryHosting{ 72 Host: tc.host, 73 HostFromContainerRuntime: tc.clusterHost, 74 } 75 assertReplaceRegistryForLocal(t, reg, tc.name, tc.expectedLocal) 76 assertReplaceRegistryForCluster(t, reg, tc.name, tc.expectedCluster) 77 }) 78 } 79 } 80 81 func TestRegistryFromCluster(t *testing.T) { 82 registryHosting := func(host string) *v1alpha1.RegistryHosting { 83 return &v1alpha1.RegistryHosting{ 84 Host: host, 85 HostFromClusterNetwork: "localhost:12345/cluster-network", 86 HostFromContainerRuntime: "localhost:12345/container-runtime", 87 Help: "fake-help", 88 } 89 } 90 91 tests := []struct { 92 name string 93 cluster v1alpha1.Cluster 94 expectedHost string 95 expectedSingleName string 96 expectedLocal bool 97 }{ 98 {name: "Empty"}, 99 { 100 name: "DefaultNoLocal", 101 cluster: v1alpha1.Cluster{ 102 Spec: v1alpha1.ClusterSpec{ 103 DefaultRegistry: &v1alpha1.RegistryHosting{ 104 Host: "registry.example.com", 105 SingleName: "fake-repo", 106 }, 107 }, 108 }, 109 expectedHost: "registry.example.com", 110 expectedSingleName: "fake-repo", 111 }, 112 { 113 name: "DefaultWithLocal", 114 cluster: v1alpha1.Cluster{ 115 Spec: v1alpha1.ClusterSpec{ 116 DefaultRegistry: &v1alpha1.RegistryHosting{ 117 Host: "registry.example.com", 118 SingleName: "fake-repo", 119 }, 120 }, 121 Status: v1alpha1.ClusterStatus{ 122 Registry: registryHosting("localhost:12345"), 123 }, 124 }, 125 expectedHost: "localhost:12345", 126 expectedLocal: true, 127 }, 128 { 129 name: "LocalNoDefault", 130 cluster: v1alpha1.Cluster{ 131 Status: v1alpha1.ClusterStatus{ 132 Registry: registryHosting("localhost:7890"), 133 }, 134 }, 135 expectedHost: "localhost:7890", 136 expectedLocal: true, 137 }, 138 } 139 140 for _, tt := range tests { 141 t.Run(tt.name, func(t *testing.T) { 142 reg, err := RegistryFromCluster(&tt.cluster) 143 require.NoError(t, err, "Registry error") 144 if tt.expectedHost == "" { 145 return 146 } 147 148 require.NotNil(t, reg) 149 require.Equal(t, tt.expectedHost, reg.Host, "Registry host") 150 require.Equal(t, tt.expectedSingleName, reg.SingleName, "Registry single name") 151 if tt.expectedLocal { 152 require.Equal(t, "localhost:12345/container-runtime", reg.HostFromContainerRuntime, 153 "Registry host from container runtime") 154 } 155 }) 156 } 157 } 158 159 func TestRegistryFromCluster_Error(t *testing.T) { 160 cluster := v1alpha1.Cluster{ 161 Status: v1alpha1.ClusterStatus{Error: "fake cluster error"}, 162 } 163 reg, err := RegistryFromCluster(&cluster) 164 require.EqualError(t, err, "cluster not ready: fake cluster error") 165 require.Nil(t, reg) 166 } 167 168 func assertReplaceRegistryForLocal(t *testing.T, reg *v1alpha1.RegistryHosting, orig string, expected string) { 169 rs := NewRefSelector(MustParseNamed(orig)) 170 actual, err := ReplaceRegistryForLocalRef(rs, reg) 171 require.NoError(t, err) 172 assert.Equal(t, expected, actual.String()) 173 } 174 175 func assertReplaceRegistryForCluster(t *testing.T, reg *v1alpha1.RegistryHosting, orig string, expected string) { 176 rs := NewRefSelector(MustParseNamed(orig)) 177 actual, err := ReplaceRegistryForContainerRuntimeRef(rs, reg) 178 require.NoError(t, err) 179 assert.Equal(t, expected, actual.String()) 180 }