@@ -19,71 +19,34 @@ import (
1919 "strings"
2020 "testing"
2121
22- dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
23- "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
24- "github.com/devfile/devworkspace-operator/pkg/common"
2522 "github.com/devfile/devworkspace-operator/pkg/constants"
2623 "github.com/stretchr/testify/assert"
2724 corev1 "k8s.io/api/core/v1"
2825 "k8s.io/apimachinery/pkg/api/resource"
2926)
3027
31- func TestDefaultAndValidateHomeInitContainer (t * testing.T ) {
32- workspace := & common.DevWorkspaceWithConfig {
33- DevWorkspace : & dw.DevWorkspace {
34- Spec : dw.DevWorkspaceSpec {
35- Template : dw.DevWorkspaceTemplateSpec {
36- DevWorkspaceTemplateSpecContent : dw.DevWorkspaceTemplateSpecContent {
37- Components : []dw.Component {
38- {
39- Name : "main-container" ,
40- ComponentUnion : dw.ComponentUnion {
41- Container : & dw.ContainerComponent {
42- Container : dw.Container {
43- Image : "test-image:latest" ,
44- },
45- },
46- },
47- },
48- },
49- },
50- },
51- },
52- },
53- Config : & v1alpha1.OperatorConfiguration {
54- Workspace : & v1alpha1.WorkspaceConfig {},
55- },
56- }
57-
28+ func TestValidateInitContainer (t * testing.T ) {
5829 tests := []struct {
5930 name string
6031 container corev1.Container
6132 expectError bool
6233 errorMsg string
63- validate func (t * testing.T , result corev1.Container )
6434 }{
6535 {
66- name : "Defaults image when empty " ,
36+ name : "Accepts container with only args ( image and command will be filled by merge) " ,
6737 container : corev1.Container {
6838 Name : constants .HomeInitComponentName ,
6939 Args : []string {"echo 'test'" },
7040 },
7141 expectError : false ,
72- validate : func (t * testing.T , result corev1.Container ) {
73- assert .Equal (t , "test-image:latest" , result .Image )
74- },
7542 },
7643 {
77- name : "Defaults command when empty " ,
44+ name : "Accepts container with only image ( command and args will be filled by merge) " ,
7845 container : corev1.Container {
7946 Name : constants .HomeInitComponentName ,
8047 Image : "custom-image:latest" ,
81- Args : []string {"echo 'test'" },
8248 },
8349 expectError : false ,
84- validate : func (t * testing.T , result corev1.Container ) {
85- assert .Equal (t , []string {"/bin/sh" , "-c" }, result .Command )
86- },
8750 },
8851 {
8952 name : "Accepts valid command" ,
@@ -100,7 +63,18 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
10063 container : corev1.Container {
10164 Name : constants .HomeInitComponentName ,
10265 Image : "custom-image:latest" ,
103- Command : []string {"/bin/bash" },
66+ Command : []string {"/bin/sh" },
67+ Args : []string {"echo 'test'" },
68+ },
69+ expectError : true ,
70+ errorMsg : "command must be exactly [/bin/sh, -c]" ,
71+ },
72+ {
73+ name : "Rejects empty command" ,
74+ container : corev1.Container {
75+ Name : constants .HomeInitComponentName ,
76+ Image : "custom-image:latest" ,
77+ Command : []string {},
10478 Args : []string {"echo 'test'" },
10579 },
10680 expectError : true ,
@@ -131,7 +105,6 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
131105 container : corev1.Container {
132106 Name : constants .HomeInitComponentName ,
133107 Image : "custom-image:latest" ,
134- Args : []string {"echo 'test'" },
135108 VolumeMounts : []corev1.VolumeMount {
136109 {
137110 Name : "custom-volume" ,
@@ -142,44 +115,29 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
142115 expectError : true ,
143116 errorMsg : "volumeMounts are not allowed for init-persistent-home" ,
144117 },
145- {
146- name : "Injects persistent-home volumeMount" ,
147- container : corev1.Container {
148- Name : constants .HomeInitComponentName ,
149- Image : "custom-image:latest" ,
150- Args : []string {"echo 'test'" },
151- },
152- expectError : false ,
153- validate : func (t * testing.T , result corev1.Container ) {
154- assert .Len (t , result .VolumeMounts , 1 )
155- assert .Equal (t , constants .HomeVolumeName , result .VolumeMounts [0 ].Name )
156- assert .Equal (t , constants .HomeUserDirectory , result .VolumeMounts [0 ].MountPath )
157- },
158- },
159118 {
160119 name : "Allows env variables" ,
161120 container : corev1.Container {
162121 Name : constants .HomeInitComponentName ,
163122 Image : "custom-image:latest" ,
164- Args : []string {"echo 'test'" },
165123 Env : []corev1.EnvVar {
166- {Name : "TEST_VAR" , Value : "test-value" },
124+ {
125+ Name : "TEST_VAR" ,
126+ Value : "test-var" ,
127+ },
167128 },
168129 },
169130 expectError : false ,
170- validate : func (t * testing.T , result corev1.Container ) {
171- assert .Len (t , result .Env , 1 )
172- assert .Equal (t , "TEST_VAR" , result .Env [0 ].Name )
173- },
174131 },
175132 {
176133 name : "Rejects ports" ,
177134 container : corev1.Container {
178135 Name : constants .HomeInitComponentName ,
179136 Image : "custom-image:latest" ,
180- Args : []string {"echo 'test'" },
181137 Ports : []corev1.ContainerPort {
182- {ContainerPort : 8080 },
138+ {
139+ ContainerPort : 8080 ,
140+ },
183141 },
184142 },
185143 expectError : true ,
@@ -190,10 +148,11 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
190148 container : corev1.Container {
191149 Name : constants .HomeInitComponentName ,
192150 Image : "custom-image:latest" ,
193- Args : []string {"echo 'test'" },
194151 LivenessProbe : & corev1.Probe {
195152 ProbeHandler : corev1.ProbeHandler {
196- HTTPGet : & corev1.HTTPGetAction {Path : "/health" },
153+ HTTPGet : & corev1.HTTPGetAction {
154+ Path : "/health" ,
155+ },
197156 },
198157 },
199158 },
@@ -205,7 +164,6 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
205164 container : corev1.Container {
206165 Name : constants .HomeInitComponentName ,
207166 Image : "custom-image:latest" ,
208- Args : []string {"echo 'test'" },
209167 SecurityContext : & corev1.SecurityContext {
210168 RunAsUser : new (int64 ),
211169 },
@@ -218,7 +176,6 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
218176 container : corev1.Container {
219177 Name : constants .HomeInitComponentName ,
220178 Image : "custom-image:latest" ,
221- Args : []string {"echo 'test'" },
222179 Resources : corev1.ResourceRequirements {
223180 Limits : corev1.ResourceList {
224181 corev1 .ResourceMemory : resource .MustParse ("128Mi" ),
@@ -233,7 +190,6 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
233190 container : corev1.Container {
234191 Name : constants .HomeInitComponentName ,
235192 Image : "custom-image:latest" ,
236- Args : []string {"echo 'test'" },
237193 WorkingDir : "/tmp" ,
238194 },
239195 expectError : true ,
@@ -243,8 +199,7 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
243199 name : "Rejects image with whitespace" ,
244200 container : corev1.Container {
245201 Name : constants .HomeInitComponentName ,
246- Image : "nginx\n malicious" ,
247- Args : []string {"echo 'test'" },
202+ Image : "nginx\t malicious" ,
248203 },
249204 expectError : true ,
250205 errorMsg : "invalid image reference" ,
@@ -253,7 +208,7 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
253208
254209 for _ , tt := range tests {
255210 t .Run (tt .name , func (t * testing.T ) {
256- result , err := defaultAndValidateHomeInitContainer (tt .container , workspace )
211+ err := validateHomeInitContainer (tt .container )
257212
258213 if tt .expectError {
259214 assert .Error (t , err )
@@ -262,47 +217,11 @@ func TestDefaultAndValidateHomeInitContainer(t *testing.T) {
262217 }
263218 } else {
264219 assert .NoError (t , err )
265- if tt .validate != nil {
266- tt .validate (t , result )
267- }
268220 }
269221 })
270222 }
271223}
272224
273- func TestDefaultAndValidateHomeInitContainer_NoWorkspaceImage (t * testing.T ) {
274- workspaceNoImage := & common.DevWorkspaceWithConfig {
275- DevWorkspace : & dw.DevWorkspace {
276- Spec : dw.DevWorkspaceSpec {
277- Template : dw.DevWorkspaceTemplateSpec {
278- DevWorkspaceTemplateSpecContent : dw.DevWorkspaceTemplateSpecContent {
279- Components : []dw.Component {
280- {
281- Name : "volume-component" ,
282- ComponentUnion : dw.ComponentUnion {
283- Volume : & dw.VolumeComponent {},
284- },
285- },
286- },
287- },
288- },
289- },
290- },
291- Config : & v1alpha1.OperatorConfiguration {
292- Workspace : & v1alpha1.WorkspaceConfig {},
293- },
294- }
295-
296- container := corev1.Container {
297- Name : constants .HomeInitComponentName ,
298- Args : []string {"echo 'test'" },
299- }
300-
301- _ , err := defaultAndValidateHomeInitContainer (container , workspaceNoImage )
302- assert .Error (t , err )
303- assert .Contains (t , err .Error (), "unable to infer workspace image" )
304- }
305-
306225func TestValidateImageReference (t * testing.T ) {
307226 tests := []struct {
308227 name string
0 commit comments