Skip to content

Commit 84c44e5

Browse files
authored
update apply command validation rule (#649)
* update apply command validation rule Signed-off-by: Stephanie <[email protected]> * run go fmt Signed-off-by: Stephanie <[email protected]> * update based on suggestion Signed-off-by: Stephanie <[email protected]>
1 parent 9d4037b commit 84c44e5

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

pkg/validation/commands.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,20 @@ func validateCommandComponent(command v1alpha2.Command, components []v1alpha2.Co
128128
commandComponent = command.Apply.Component
129129
}
130130

131-
// must map to a container component
131+
// exec command must map to a container component
132+
// apply command must map to a container/kubernetes/openshift/image component
132133
for _, component := range components {
133-
if component.Container != nil && commandComponent == component.Name {
134-
return nil
134+
if commandComponent == component.Name {
135+
if component.Container != nil {
136+
return nil
137+
}
138+
if command.Apply != nil && (component.Image != nil || component.Kubernetes != nil || component.Openshift != nil) {
139+
return nil
140+
}
141+
break
135142
}
136143
}
137-
return &InvalidCommandError{commandId: command.Id, reason: "command does not map to a container component"}
144+
return &InvalidCommandError{commandId: command.Id, reason: "command does not map to a valid component"}
138145
}
139146

140147
// validateCompositeCommand checks that the specified composite command is valid. The command:

pkg/validation/commands_test.go

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ func TestValidateCommands(t *testing.T) {
7878
duplicateKeyErr := "duplicate key: somecommand1"
7979
noDefaultCmdErr := ".*there should be exactly one default command, currently there is no default command"
8080
multipleDefaultCmdErr := ".*there should be exactly one default command, currently there are multiple default commands"
81-
invalidCmdErr := ".*command does not map to a container component"
81+
invalidCmdErr := ".*command does not map to a valid component"
8282
nonExistCmdInComposite := "the command .* mentioned in the composite command does not exist in the devfile"
8383

8484
parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,
8585
"uri: http://127.0.0.1:8080").PutString(ParentOverrideAttribute, "main devfile")
86-
invalidCmdErrWithImportAttributes := ".*command does not map to a container component, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
86+
invalidCmdErrWithImportAttributes := ".*command does not map to a valid component, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
8787

8888
tests := []struct {
8989
name string
@@ -179,14 +179,22 @@ func TestValidateCommands(t *testing.T) {
179179

180180
func TestValidateCommandComponent(t *testing.T) {
181181

182-
component := "alias1"
183-
invalidComponent := "garbagealias"
182+
containerComponent := "alias1"
183+
kubeComponent := "alias2"
184+
openshiftComponent := "alias3"
185+
imageComponent := "alias4"
186+
volumeComponent := "alias5"
187+
nonexistComponent := "garbagealias"
184188

185189
components := []v1alpha2.Component{
186-
generateDummyContainerComponent(component, nil, nil, nil),
190+
generateDummyContainerComponent(containerComponent, nil, nil, nil),
191+
generateDummyKubernetesComponent(kubeComponent, nil, ""),
192+
generateDummyOpenshiftComponent(openshiftComponent, nil, ""),
193+
generateDummyImageComponent(imageComponent, v1alpha2.DockerfileSrc{}),
194+
generateDummyVolumeComponent(volumeComponent, ""),
187195
}
188196

189-
invalidCmdErr := ".*command does not map to a container component"
197+
invalidCmdErr := ".*command does not map to a valid component"
190198

191199
tests := []struct {
192200
name string
@@ -195,29 +203,66 @@ func TestValidateCommandComponent(t *testing.T) {
195203
}{
196204
{
197205
name: "Valid Exec Command",
198-
command: generateDummyExecCommand("command", component, &v1alpha2.CommandGroup{Kind: runGroup}),
206+
command: generateDummyExecCommand("command", containerComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
199207
},
200208
{
201209
name: "Invalid Exec Command with missing component",
202210
command: generateDummyExecCommand("command", "", &v1alpha2.CommandGroup{Kind: runGroup}),
203211
wantErr: &invalidCmdErr,
204212
},
205213
{
206-
name: "Valid Exec Command with invalid component",
207-
command: generateDummyExecCommand("command", invalidComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
214+
name: "Exec Command with non-exist component",
215+
command: generateDummyExecCommand("command", nonexistComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
216+
wantErr: &invalidCmdErr,
217+
},
218+
{
219+
name: "Exec Command with image component",
220+
command: generateDummyExecCommand("command", imageComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
221+
wantErr: &invalidCmdErr,
222+
},
223+
{
224+
name: "Exec Command with kubernetes component",
225+
command: generateDummyExecCommand("command", kubeComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
226+
wantErr: &invalidCmdErr,
227+
},
228+
{
229+
name: "Exec Command with openshift component",
230+
command: generateDummyExecCommand("command", openshiftComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
231+
wantErr: &invalidCmdErr,
232+
},
233+
{
234+
name: "Exec Command with volume component",
235+
command: generateDummyExecCommand("command", volumeComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
208236
wantErr: &invalidCmdErr,
209237
},
210238
{
211239
name: "Valid Exec Command with Group nil",
212-
command: generateDummyExecCommand("command", component, nil),
240+
command: generateDummyExecCommand("command", containerComponent, nil),
241+
},
242+
{
243+
name: "Valid Apply Command with container component",
244+
command: generateDummyApplyCommand("command", containerComponent, nil, attributes.Attributes{}),
245+
},
246+
{
247+
name: "Valid Apply Command with image component",
248+
command: generateDummyApplyCommand("command", imageComponent, nil, attributes.Attributes{}),
213249
},
214250
{
215-
name: "Valid Apply Command",
216-
command: generateDummyApplyCommand("command", component, nil, attributes.Attributes{}),
251+
name: "Valid Apply Command with kubernetes component",
252+
command: generateDummyApplyCommand("command", kubeComponent, nil, attributes.Attributes{}),
253+
},
254+
{
255+
name: "Valid Apply Command with openshift component",
256+
command: generateDummyApplyCommand("command", openshiftComponent, nil, attributes.Attributes{}),
257+
},
258+
{
259+
name: "Apply Command with non-exist component",
260+
command: generateDummyApplyCommand("command", nonexistComponent, &v1alpha2.CommandGroup{Kind: runGroup}, attributes.Attributes{}),
261+
wantErr: &invalidCmdErr,
217262
},
218263
{
219-
name: "Invalid Apply Command with wrong component",
220-
command: generateDummyApplyCommand("command", invalidComponent, &v1alpha2.CommandGroup{Kind: runGroup}, attributes.Attributes{}),
264+
name: "Apply Command with volume component",
265+
command: generateDummyApplyCommand("command", volumeComponent, &v1alpha2.CommandGroup{Kind: runGroup}, attributes.Attributes{}),
221266
wantErr: &invalidCmdErr,
222267
},
223268
}
@@ -245,7 +290,7 @@ func TestValidateCompositeCommand(t *testing.T) {
245290
generateDummyContainerComponent(component, nil, nil, nil),
246291
}
247292

248-
invalidCmdErr := ".*command does not map to a container component"
293+
invalidCmdErr := ".*command does not map to a valid component"
249294
missingCmdErr := ".*the command .* mentioned in the composite command does not exist in the devfile"
250295
selfRefCmdErr := ".*composite command cannot reference itself"
251296
indirectRefCmdErr := "composite command cannot indirectly reference itself"

pkg/validation/validation-rule.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ Since network is shared in the same pod, endpoint ports should be unique across
2323
- Should not reference itself via a subcommand
2424
- Should not indirectly reference itself via a subcommand which is a composite command
2525
- Should reference a valid devfile command
26-
3. exec and apply command should: map to a valid container component
27-
4. `{build, run, test, debug}`, each kind of group can only have one default command associated with it. If there are multiple commands of the same kind without a default, a warning will be displayed.
26+
3. exec command should: map to a valid container component
27+
4. apply command should: map to a valid container/kubernetes/openshift/image component
28+
5. `{build, run, test, debug, deploy}`, each kind of group can only have one default command associated with it. If there are multiple commands of the same kind without a default, a warning will be displayed.
2829

2930
### Components:
3031
Common rules for all components types:

0 commit comments

Comments
 (0)