Skip to content

Commit

Permalink
update apply command validation rule (#649)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
yangcao77 authored Oct 18, 2021
1 parent 9d4037b commit 84c44e5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
15 changes: 11 additions & 4 deletions pkg/validation/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,20 @@ func validateCommandComponent(command v1alpha2.Command, components []v1alpha2.Co
commandComponent = command.Apply.Component
}

// must map to a container component
// exec command must map to a container component
// apply command must map to a container/kubernetes/openshift/image component
for _, component := range components {
if component.Container != nil && commandComponent == component.Name {
return nil
if commandComponent == component.Name {
if component.Container != nil {
return nil
}
if command.Apply != nil && (component.Image != nil || component.Kubernetes != nil || component.Openshift != nil) {
return nil
}
break
}
}
return &InvalidCommandError{commandId: command.Id, reason: "command does not map to a container component"}
return &InvalidCommandError{commandId: command.Id, reason: "command does not map to a valid component"}
}

// validateCompositeCommand checks that the specified composite command is valid. The command:
Expand Down
75 changes: 60 additions & 15 deletions pkg/validation/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func TestValidateCommands(t *testing.T) {
duplicateKeyErr := "duplicate key: somecommand1"
noDefaultCmdErr := ".*there should be exactly one default command, currently there is no default command"
multipleDefaultCmdErr := ".*there should be exactly one default command, currently there are multiple default commands"
invalidCmdErr := ".*command does not map to a container component"
invalidCmdErr := ".*command does not map to a valid component"
nonExistCmdInComposite := "the command .* mentioned in the composite command does not exist in the devfile"

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

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

func TestValidateCommandComponent(t *testing.T) {

component := "alias1"
invalidComponent := "garbagealias"
containerComponent := "alias1"
kubeComponent := "alias2"
openshiftComponent := "alias3"
imageComponent := "alias4"
volumeComponent := "alias5"
nonexistComponent := "garbagealias"

components := []v1alpha2.Component{
generateDummyContainerComponent(component, nil, nil, nil),
generateDummyContainerComponent(containerComponent, nil, nil, nil),
generateDummyKubernetesComponent(kubeComponent, nil, ""),
generateDummyOpenshiftComponent(openshiftComponent, nil, ""),
generateDummyImageComponent(imageComponent, v1alpha2.DockerfileSrc{}),
generateDummyVolumeComponent(volumeComponent, ""),
}

invalidCmdErr := ".*command does not map to a container component"
invalidCmdErr := ".*command does not map to a valid component"

tests := []struct {
name string
Expand All @@ -195,29 +203,66 @@ func TestValidateCommandComponent(t *testing.T) {
}{
{
name: "Valid Exec Command",
command: generateDummyExecCommand("command", component, &v1alpha2.CommandGroup{Kind: runGroup}),
command: generateDummyExecCommand("command", containerComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
},
{
name: "Invalid Exec Command with missing component",
command: generateDummyExecCommand("command", "", &v1alpha2.CommandGroup{Kind: runGroup}),
wantErr: &invalidCmdErr,
},
{
name: "Valid Exec Command with invalid component",
command: generateDummyExecCommand("command", invalidComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
name: "Exec Command with non-exist component",
command: generateDummyExecCommand("command", nonexistComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
wantErr: &invalidCmdErr,
},
{
name: "Exec Command with image component",
command: generateDummyExecCommand("command", imageComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
wantErr: &invalidCmdErr,
},
{
name: "Exec Command with kubernetes component",
command: generateDummyExecCommand("command", kubeComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
wantErr: &invalidCmdErr,
},
{
name: "Exec Command with openshift component",
command: generateDummyExecCommand("command", openshiftComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
wantErr: &invalidCmdErr,
},
{
name: "Exec Command with volume component",
command: generateDummyExecCommand("command", volumeComponent, &v1alpha2.CommandGroup{Kind: runGroup}),
wantErr: &invalidCmdErr,
},
{
name: "Valid Exec Command with Group nil",
command: generateDummyExecCommand("command", component, nil),
command: generateDummyExecCommand("command", containerComponent, nil),
},
{
name: "Valid Apply Command with container component",
command: generateDummyApplyCommand("command", containerComponent, nil, attributes.Attributes{}),
},
{
name: "Valid Apply Command with image component",
command: generateDummyApplyCommand("command", imageComponent, nil, attributes.Attributes{}),
},
{
name: "Valid Apply Command",
command: generateDummyApplyCommand("command", component, nil, attributes.Attributes{}),
name: "Valid Apply Command with kubernetes component",
command: generateDummyApplyCommand("command", kubeComponent, nil, attributes.Attributes{}),
},
{
name: "Valid Apply Command with openshift component",
command: generateDummyApplyCommand("command", openshiftComponent, nil, attributes.Attributes{}),
},
{
name: "Apply Command with non-exist component",
command: generateDummyApplyCommand("command", nonexistComponent, &v1alpha2.CommandGroup{Kind: runGroup}, attributes.Attributes{}),
wantErr: &invalidCmdErr,
},
{
name: "Invalid Apply Command with wrong component",
command: generateDummyApplyCommand("command", invalidComponent, &v1alpha2.CommandGroup{Kind: runGroup}, attributes.Attributes{}),
name: "Apply Command with volume component",
command: generateDummyApplyCommand("command", volumeComponent, &v1alpha2.CommandGroup{Kind: runGroup}, attributes.Attributes{}),
wantErr: &invalidCmdErr,
},
}
Expand Down Expand Up @@ -245,7 +290,7 @@ func TestValidateCompositeCommand(t *testing.T) {
generateDummyContainerComponent(component, nil, nil, nil),
}

invalidCmdErr := ".*command does not map to a container component"
invalidCmdErr := ".*command does not map to a valid component"
missingCmdErr := ".*the command .* mentioned in the composite command does not exist in the devfile"
selfRefCmdErr := ".*composite command cannot reference itself"
indirectRefCmdErr := "composite command cannot indirectly reference itself"
Expand Down
5 changes: 3 additions & 2 deletions pkg/validation/validation-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Since network is shared in the same pod, endpoint ports should be unique across
- Should not reference itself via a subcommand
- Should not indirectly reference itself via a subcommand which is a composite command
- Should reference a valid devfile command
3. exec and apply command should: map to a valid container component
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.
3. exec command should: map to a valid container component
4. apply command should: map to a valid container/kubernetes/openshift/image component
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.

### Components:
Common rules for all components types:
Expand Down

0 comments on commit 84c44e5

Please sign in to comment.