11package validation
22
33import (
4+ "github.com/devfile/api/v2/pkg/attributes"
45 "testing"
56
67 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
@@ -30,9 +31,10 @@ func generateDummyExecCommand(name, component string, group *v1alpha2.CommandGro
3031}
3132
3233// generateDummyExecCommand returns a dummy apply command for testing
33- func generateDummyApplyCommand (name , component string , group * v1alpha2.CommandGroup ) v1alpha2.Command {
34+ func generateDummyApplyCommand (name , component string , group * v1alpha2.CommandGroup , cmdAttributes attributes. Attributes ) v1alpha2.Command {
3435 return v1alpha2.Command {
35- Id : name ,
36+ Attributes : cmdAttributes ,
37+ Id : name ,
3638 CommandUnion : v1alpha2.CommandUnion {
3739 Apply : & v1alpha2.ApplyCommand {
3840 LabeledCommand : v1alpha2.LabeledCommand {
@@ -76,6 +78,10 @@ func TestValidateCommands(t *testing.T) {
7678 multipleDefaultCmdErr := ".*there should be exactly one default command, currently there is more than one default command"
7779 invalidCmdErr := ".*command does not map to a container component"
7880
81+ parentOverridesFromMainDevfile := attributes.Attributes {}.PutString (ImportSourceAttribute ,
82+ "uri: http://127.0.0.1:8080" ).PutString (ParentOverrideAttribute , "main devfile" )
83+ invalidCmdErrWithImportAttributes := ".*command does not map to a container component, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
84+
7985 tests := []struct {
8086 name string
8187 commands []v1alpha2.Command
@@ -131,7 +137,7 @@ func TestValidateCommands(t *testing.T) {
131137 {
132138 name : "Invalid Apply command with wrong component" ,
133139 commands : []v1alpha2.Command {
134- generateDummyApplyCommand ("command" , "invalidComponent" , nil ),
140+ generateDummyApplyCommand ("command" , "invalidComponent" , nil , attributes. Attributes {} ),
135141 },
136142 wantErr : & invalidCmdErr ,
137143 },
@@ -144,6 +150,13 @@ func TestValidateCommands(t *testing.T) {
144150 generateDummyCompositeCommand ("composite-a" , []string {"basic-exec" }, nil ),
145151 },
146152 },
153+ {
154+ name : "Invalid command with import source attribute" ,
155+ commands : []v1alpha2.Command {
156+ generateDummyApplyCommand ("command" , "invalidComponent" , nil , parentOverridesFromMainDevfile ),
157+ },
158+ wantErr : & invalidCmdErrWithImportAttributes ,
159+ },
147160 }
148161 for _ , tt := range tests {
149162 t .Run (tt .name , func (t * testing.T ) {
@@ -193,11 +206,11 @@ func TestValidateCommandComponent(t *testing.T) {
193206 },
194207 {
195208 name : "Valid Apply Command" ,
196- command : generateDummyApplyCommand ("command" , component , nil ),
209+ command : generateDummyApplyCommand ("command" , component , nil , attributes. Attributes {} ),
197210 },
198211 {
199212 name : "Invalid Apply Command with wrong component" ,
200- command : generateDummyApplyCommand ("command" , invalidComponent , & v1alpha2.CommandGroup {Kind : runGroup }),
213+ command : generateDummyApplyCommand ("command" , invalidComponent , & v1alpha2.CommandGroup {Kind : runGroup }, attributes. Attributes {} ),
201214 wantErr : & invalidCmdErr ,
202215 },
203216 }
@@ -307,7 +320,13 @@ func TestValidateGroup(t *testing.T) {
307320 component := "alias1"
308321
309322 noDefaultCmdErr := ".*there should be exactly one default command, currently there is no default command"
310- multipleDefaultCmdErr := ".*there should be exactly one default command, currently there is more than one default command"
323+ multipleDefaultError := ".*there should be exactly one default command, currently there is more than one default command"
324+ multipleDefaultCmdErr := multipleDefaultError + "; command: run command; command: customcommand"
325+
326+ parentOverridesFromMainDevfile := attributes.Attributes {}.PutString (ImportSourceAttribute ,
327+ "uri: http://127.0.0.1:8080" ).PutString (ParentOverrideAttribute , "main devfile" )
328+ multipleDefaultCmdErrWithImportAttributes := multipleDefaultError +
329+ "; command: run command; command: customcommand, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
311330
312331 tests := []struct {
313332 name string
@@ -322,6 +341,14 @@ func TestValidateGroup(t *testing.T) {
322341 },
323342 wantErr : & multipleDefaultCmdErr ,
324343 },
344+ {
345+ name : "Two default run commands with import source attribute" ,
346+ commands : []v1alpha2.Command {
347+ generateDummyExecCommand ("run command" , component , & v1alpha2.CommandGroup {Kind : runGroup , IsDefault : true }),
348+ generateDummyApplyCommand ("customcommand" , component , & v1alpha2.CommandGroup {Kind : runGroup , IsDefault : true }, parentOverridesFromMainDevfile ),
349+ },
350+ wantErr : & multipleDefaultCmdErrWithImportAttributes ,
351+ },
325352 {
326353 name : "No default for more than one build commands" ,
327354 commands : []v1alpha2.Command {
0 commit comments