@@ -22,21 +22,6 @@ func loadFile(t *testing.T, relPath string) []byte {
2222 return data
2323}
2424
25- // Test legacy JSON composer format (for backward compatibility)
26- func TestValidComposerJSON (t * testing.T ) {
27- v := loadFile (t , "testdata/valid.json" )
28- if err := ValidateComposerJSON (v ); err != nil {
29- t .Errorf ("expected valid.json to pass, but got: %v" , err )
30- }
31- }
32-
33- func TestInvalidComposerJSON (t * testing.T ) {
34- v := loadFile (t , "testdata/invalid.json" )
35- if err := ValidateComposerJSON (v ); err == nil {
36- t .Errorf ("expected invalid.json to fail validation" )
37- }
38- }
39-
4025// Test new YAML image template format
4126func TestValidImageTemplate (t * testing.T ) {
4227 v := loadFile (t , "image-templates/azl3-x86_64-edge-raw.yml" )
@@ -54,7 +39,7 @@ func TestValidImageTemplate(t *testing.T) {
5439 t .Errorf ("json marshaling error: %v" , err )
5540 return
5641 }
57- if err := ValidateImageJSON (dataJSON ); err != nil {
42+ if err := ValidateImageTemplateJSON (dataJSON ); err != nil {
5843 t .Errorf ("expected image-templates/azl3-x86_64-edge-raw.yml to pass, but got: %v" , err )
5944 }
6045}
@@ -76,7 +61,7 @@ func TestInvalidImageTemplate(t *testing.T) {
7661 return
7762 }
7863
79- if err := ValidateImageJSON (dataJSON ); err == nil {
64+ if err := ValidateImageTemplateJSON (dataJSON ); err == nil {
8065 t .Errorf ("expected testdata/invalid-image.yml to pass, but got: %v" , err )
8166 }
8267}
@@ -117,36 +102,15 @@ func TestInvalidConfig(t *testing.T) {
117102
118103 if err := ValidateConfigJSON (dataJSON ); err == nil {
119104 t .Errorf ("expected invalid-config.json to fail validation: %v" , err )
120- } else {
121- t .Logf ("expected validation error: %v" , err )
122105 }
123106}
124107
125- // Test validation of template structure
108+ // Test validation of template structure using external test files
126109func TestImageTemplateStructure (t * testing.T ) {
127- // Test a minimal valid template
128- minimalTemplate := `image:
129- name: test-image
130- version: "1.0.0"
131-
132- target:
133- os: azure-linux
134- dist: azl3
135- arch: x86_64
136- imageType: raw
137-
138- systemConfigs:
139- - name: minimal
140- description: Minimal test configuration
141- packages:
142- - openssh-server
143- kernel:
144- version: "6.12"
145- cmdline: "quiet"
146- `
110+ v := loadFile (t , "testdata/complete-valid-template.yml" )
147111
148112 var raw interface {}
149- if err := yaml .Unmarshal ([] byte ( minimalTemplate ) , & raw ); err != nil {
113+ if err := yaml .Unmarshal (v , & raw ); err != nil {
150114 t .Fatalf ("failed to parse minimal template: %v" , err )
151115 }
152116
@@ -155,29 +119,16 @@ systemConfigs:
155119 t .Fatalf ("failed to marshal to JSON: %v" , err )
156120 }
157121
158- if err := ValidateImageJSON (dataJSON ); err != nil {
122+ if err := ValidateImageTemplateJSON (dataJSON ); err != nil {
159123 t .Errorf ("minimal template should be valid, but got: %v" , err )
160124 }
161125}
162126
163127func TestImageTemplateMissingFields (t * testing.T ) {
164- // Test template missing required fields
165- invalidTemplate := `image:
166- name: test-image
167-
168- target:
169- os: azure-linux
170- dist: azl3
171- arch: x86_64
172-
173- systemConfigs:
174- - name: incomplete
175- packages:
176- - openssh-server
177- `
128+ v := loadFile (t , "testdata/incomplete-template.yml" )
178129
179130 var raw interface {}
180- if err := yaml .Unmarshal ([] byte ( invalidTemplate ) , & raw ); err != nil {
131+ if err := yaml .Unmarshal (v , & raw ); err != nil {
181132 t .Fatalf ("failed to parse invalid template: %v" , err )
182133 }
183134
@@ -186,7 +137,71 @@ systemConfigs:
186137 t .Fatalf ("failed to marshal to JSON: %v" , err )
187138 }
188139
189- if err := ValidateImageJSON (dataJSON ); err == nil {
140+ if err := ValidateImageTemplateJSON (dataJSON ); err == nil {
190141 t .Errorf ("incomplete template should fail validation" )
191142 }
192143}
144+
145+ // Table-driven test for multiple template validation scenarios
146+ func TestImageTemplateValidation (t * testing.T ) {
147+ tests := []struct {
148+ name string
149+ file string
150+ shouldPass bool
151+ description string
152+ }{
153+ {
154+ name : "ValidComplete" ,
155+ file : "testdata/complete-valid-template.yml" ,
156+ shouldPass : true ,
157+ description : "complete template with all optional fields" ,
158+ },
159+ {
160+ name : "InvalidMissingImage" ,
161+ file : "testdata/missing-image-section.yml" ,
162+ shouldPass : false ,
163+ description : "template missing image section" ,
164+ },
165+ {
166+ name : "InvalidMissingTarget" ,
167+ file : "testdata/missing-target-section.yml" ,
168+ shouldPass : false ,
169+ description : "template missing target section" ,
170+ },
171+ {
172+ name : "InvalidMissingSystemConfigs" ,
173+ file : "testdata/missing-systemconfigs.yml" ,
174+ shouldPass : false ,
175+ description : "template missing systemConfigs section" ,
176+ },
177+ {
178+ name : "InvalidWrongTypes" ,
179+ file : "testdata/wrong-field-types.yml" ,
180+ shouldPass : false ,
181+ description : "template with incorrect field types" ,
182+ },
183+ }
184+
185+ for _ , tt := range tests {
186+ t .Run (tt .name , func (t * testing.T ) {
187+ v := loadFile (t , tt .file )
188+
189+ var raw interface {}
190+ if err := yaml .Unmarshal (v , & raw ); err != nil {
191+ t .Fatalf ("failed to parse template %s: %v" , tt .file , err )
192+ }
193+
194+ dataJSON , err := json .Marshal (raw )
195+ if err != nil {
196+ t .Fatalf ("failed to marshal to JSON: %v" , err )
197+ }
198+
199+ err = ValidateImageTemplateJSON (dataJSON )
200+ if tt .shouldPass && err != nil {
201+ t .Errorf ("expected %s to pass validation (%s), but got error: %v" , tt .file , tt .description , err )
202+ } else if ! tt .shouldPass && err == nil {
203+ t .Errorf ("expected %s to fail validation (%s), but it passed" , tt .file , tt .description )
204+ }
205+ })
206+ }
207+ }
0 commit comments