github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/tiltfile/k8s_custom_deploy_test.go (about) 1 package tiltfile 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 10 "github.com/tilt-dev/tilt/pkg/model" 11 ) 12 13 func TestK8sCustomDeployLiveUpdateImageSelector(t *testing.T) { 14 f := newLiveUpdateFixture(t) 15 16 f.skipYAML = true 17 f.tiltfileCode = ` 18 default_registry('gcr.io/myrepo') 19 k8s_custom_deploy('foo', 'apply', 'delete', deps=['foo'], image_selector='foo-img', live_update=%s) 20 ` 21 f.init() 22 23 f.load("foo") 24 25 m := f.assertNextManifest("foo", cb(image("foo-img"), f.expectedLU)) 26 assert.True(t, m.ImageTargets[0].IsLiveUpdateOnly) 27 28 require.NoError(t, m.InferLiveUpdateSelectors(), "Failed to infer Live Update selectors") 29 luSpec := m.ImageTargets[0].LiveUpdateSpec 30 require.NotNil(t, luSpec.Selector.Kubernetes) 31 assert.Empty(t, luSpec.Selector.Kubernetes.ContainerName) 32 // NO registry rewriting should be applied here because Tilt isn't actually building the image 33 assert.Equal(t, "foo-img", luSpec.Selector.Kubernetes.Image) 34 } 35 36 func TestK8sCustomDeployLiveUpdateContainerNameSelector(t *testing.T) { 37 f := newLiveUpdateFixture(t) 38 39 f.skipYAML = true 40 f.tiltfileCode = ` 41 k8s_custom_deploy('foo', 'apply', 'delete', deps=['foo'], container_selector='bar', live_update=%s) 42 ` 43 f.init() 44 45 f.load("foo") 46 f.expectedLU.Selector.Kubernetes = &v1alpha1.LiveUpdateKubernetesSelector{ 47 ContainerName: "bar", 48 } 49 50 // NOTE: because there is no known image name, the manifest name is used to 51 // generate one since an image target without a ref is not valid 52 m := f.assertNextManifest("foo", cb(image("k8s_custom_deploy:foo"), f.expectedLU)) 53 assert.True(t, m.ImageTargets[0].IsLiveUpdateOnly) 54 55 require.NoError(t, m.InferLiveUpdateSelectors(), "Failed to infer Live Update selectors") 56 luSpec := m.ImageTargets[0].LiveUpdateSpec 57 require.NotNil(t, luSpec.Selector.Kubernetes) 58 assert.Empty(t, luSpec.Selector.Kubernetes.Image) 59 // NO registry rewriting should be applied here because Tilt isn't actually building the image 60 assert.Equal(t, "bar", luSpec.Selector.Kubernetes.ContainerName) 61 } 62 63 func TestK8sCustomDeployNoLiveUpdate(t *testing.T) { 64 f := newFixture(t) 65 66 f.file("Tiltfile", ` 67 k8s_custom_deploy('foo', 68 apply_cmd='apply', 69 delete_cmd='delete', 70 apply_dir='apply-dir', 71 delete_dir='delete-dir', 72 apply_env={'APPLY_KEY': '1'}, 73 delete_env={'DELETE_KEY': 'baz'}, 74 deps=['foo']) 75 `) 76 77 f.load("foo") 78 79 m := f.assertNextManifest("foo") 80 assert.Empty(t, m.ImageTargets, "No image targets should have been created") 81 82 spec := m.K8sTarget().KubernetesApplySpec 83 assertK8sApplyCmdEqual(f, 84 model.ToHostCmdInDirWithEnv("apply", "apply-dir", []string{"APPLY_KEY=1"}), 85 spec.ApplyCmd) 86 assertK8sApplyCmdEqual(f, 87 model.ToHostCmdInDirWithEnv("delete", "delete-dir", []string{"DELETE_KEY=baz"}), 88 spec.DeleteCmd) 89 } 90 91 func TestK8sCustomDeployImageDepsMissing(t *testing.T) { 92 f := newFixture(t) 93 94 f.file("Tiltfile", ` 95 k8s_custom_deploy('foo', 96 apply_cmd='apply', 97 delete_cmd='delete', 98 deps=[], 99 image_deps=['image-a']) 100 `) 101 102 f.loadErrString(`resource "foo": image build "image-a" not found`) 103 } 104 105 func TestK8sCustomDeployImageDepsMalformed(t *testing.T) { 106 f := newFixture(t) 107 108 f.file("Tiltfile", ` 109 k8s_custom_deploy('foo', 110 apply_cmd='apply', 111 delete_cmd='delete', 112 deps=[], 113 image_deps=['image a']) 114 `) 115 116 f.loadErrString(`k8s_custom_deploy: for parameter "image_deps": must be a valid image reference: invalid reference format`) 117 } 118 119 func TestK8sCustomDeployImageDepExists(t *testing.T) { 120 f := newFixture(t) 121 122 f.file("Dockerfile", "FROM golang:1.10") 123 f.file("Tiltfile", ` 124 k8s_custom_deploy('foo', 125 apply_cmd='apply', 126 delete_cmd='delete', 127 deps=[], 128 image_deps=['image-a']) 129 130 docker_build('image-a', '.') 131 `) 132 133 f.load() 134 m := f.assertNextManifest("foo") 135 assert.Equal(t, 1, len(m.ImageTargets)) 136 137 spec := m.K8sTarget().KubernetesApplySpec 138 assert.Equal(t, []string{"image-a"}, spec.ImageMaps) 139 } 140 141 func TestK8sCustomDeployConflict(t *testing.T) { 142 f := newFixture(t) 143 144 f.file("Tiltfile", ` 145 k8s_custom_deploy('foo', 146 apply_cmd='apply', 147 delete_cmd='delete', 148 deps=[]) 149 k8s_custom_deploy('foo', 150 apply_cmd='apply', 151 delete_cmd='delete', 152 deps=[]) 153 `) 154 155 f.loadErrString(`k8s_resource named "foo" already exists`) 156 } 157 158 func TestK8sCustomDeployLocalResourceConflict(t *testing.T) { 159 f := newFixture(t) 160 161 f.file("Tiltfile", ` 162 k8s_custom_deploy('foo', 163 apply_cmd='apply', 164 delete_cmd='delete', 165 deps=[]) 166 local_resource('foo', 'foo') 167 `) 168 169 f.loadErrString(`k8s_resource named "foo" already exists`) 170 } 171 172 func assertK8sApplyCmdEqual(f *fixture, expected model.Cmd, actual *v1alpha1.KubernetesApplyCmd) bool { 173 t := f.t 174 t.Helper() 175 if !assert.NotNil(t, actual, "KubernetesApplyCmd was nil") { 176 return false 177 } 178 result := true 179 result = assert.Equal(t, expected.Argv, actual.Args, "Args were not equal") && result 180 result = assert.Equal(t, expected.Env, actual.Env, "Env was not equal") && result 181 result = assert.Equal(t, f.JoinPath(expected.Dir), actual.Dir, "Working dir was not equal") && result 182 return result 183 }