|
| 1 | +/* |
| 2 | +Copyright 2024 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 k8sjob |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + |
| 24 | + "github.com/GoogleContainerTools/skaffold/v2/testutil" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPatchToK8sContainer(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + description string |
| 30 | + actionContainer corev1.Container |
| 31 | + k8sContainer corev1.Container |
| 32 | + expected corev1.Container |
| 33 | + }{ |
| 34 | + { |
| 35 | + description: "update all fields", |
| 36 | + actionContainer: corev1.Container{ |
| 37 | + Image: "my-image:latest", |
| 38 | + Command: []string{"/bin/bash"}, |
| 39 | + Args: []string{"-c", "echo hello world"}, |
| 40 | + Name: "my-container", |
| 41 | + Env: []corev1.EnvVar{ |
| 42 | + {Name: "FOO", Value: "BAR"}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + k8sContainer: corev1.Container{}, |
| 46 | + expected: corev1.Container{ |
| 47 | + Image: "my-image:latest", |
| 48 | + Command: []string{"/bin/bash"}, |
| 49 | + Args: []string{"-c", "echo hello world"}, |
| 50 | + Name: "my-container", |
| 51 | + Env: []corev1.EnvVar{ |
| 52 | + {Name: "FOO", Value: "BAR"}, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + description: "update image", |
| 58 | + actionContainer: corev1.Container{ |
| 59 | + Image: "my-new-image:latest", |
| 60 | + }, |
| 61 | + k8sContainer: corev1.Container{ |
| 62 | + Image: "my-image:latest", |
| 63 | + }, |
| 64 | + expected: corev1.Container{ |
| 65 | + Image: "my-new-image:latest", |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "update command", |
| 70 | + actionContainer: corev1.Container{ |
| 71 | + Command: []string{"/bin/ls"}, |
| 72 | + }, |
| 73 | + k8sContainer: corev1.Container{ |
| 74 | + Command: []string{"/bin/bash"}, |
| 75 | + }, |
| 76 | + expected: corev1.Container{ |
| 77 | + Command: []string{"/bin/ls"}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + description: "update args", |
| 82 | + actionContainer: corev1.Container{ |
| 83 | + Args: []string{"-l"}, |
| 84 | + }, |
| 85 | + k8sContainer: corev1.Container{ |
| 86 | + Args: []string{"-c", "echo hello world"}, |
| 87 | + }, |
| 88 | + expected: corev1.Container{ |
| 89 | + Args: []string{"-l"}, |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + description: "update name", |
| 94 | + actionContainer: corev1.Container{ |
| 95 | + Name: "my-new-container", |
| 96 | + }, |
| 97 | + k8sContainer: corev1.Container{ |
| 98 | + Name: "my-container", |
| 99 | + }, |
| 100 | + expected: corev1.Container{ |
| 101 | + Name: "my-new-container", |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + description: "update env", |
| 106 | + actionContainer: corev1.Container{ |
| 107 | + Env: []corev1.EnvVar{ |
| 108 | + {Name: "FOO", Value: "BARR"}, |
| 109 | + {Name: "BAZ", Value: "QUX"}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + k8sContainer: corev1.Container{ |
| 113 | + Env: []corev1.EnvVar{ |
| 114 | + {Name: "FOO", Value: "BAR"}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + // Duplicate name should be ok, as the last writer should win. |
| 118 | + expected: corev1.Container{ |
| 119 | + Env: []corev1.EnvVar{ |
| 120 | + {Name: "FOO", Value: "BAR"}, |
| 121 | + {Name: "FOO", Value: "BARR"}, |
| 122 | + {Name: "BAZ", Value: "QUX"}, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + for _, test := range tests { |
| 128 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 129 | + patchToK8sContainer(test.actionContainer, &test.k8sContainer) |
| 130 | + t.CheckDeepEqual(test.expected, test.k8sContainer) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments