Skip to content

Commit 565f6e0

Browse files
committed
check for attribute and print out all default command and it's import reference if any
Signed-off-by: Stephanie <[email protected]>
1 parent 4ba7837 commit 565f6e0

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

pkg/validation/commands.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func ValidateCommands(commands []v1alpha2.Command, components []v1alpha2.Compone
2525
parentCommands := make(map[string]string)
2626
err = validateCommand(command, parentCommands, commandMap, components)
2727
if err != nil {
28-
return resolveErrorMessageWithImportArrtibutes(err, command.Attributes)
28+
return resolveErrorMessageWithImportAttributes(err, command.Attributes)
2929
}
3030

3131
commandGroup := getGroup(command)
@@ -68,11 +68,12 @@ func validateCommand(command v1alpha2.Command, parentCommands map[string]string,
6868
// 2. with more than one default, err out
6969
func validateGroup(commands []v1alpha2.Command) error {
7070
defaultCommandCount := 0
71-
71+
var defaultCommands []v1alpha2.Command
7272
if len(commands) > 1 {
7373
for _, command := range commands {
7474
if getGroup(command).IsDefault {
7575
defaultCommandCount++
76+
defaultCommands = append(defaultCommands, command)
7677
}
7778
}
7879
} else {
@@ -82,7 +83,13 @@ func validateGroup(commands []v1alpha2.Command) error {
8283
if defaultCommandCount == 0 {
8384
return fmt.Errorf("there should be exactly one default command, currently there is no default command")
8485
} else if defaultCommandCount > 1 {
85-
return fmt.Errorf("there should be exactly one default command, currently there is more than one default command")
86+
var commandsReference string
87+
for _, command := range defaultCommands {
88+
commandsReference += resolveErrorMessageWithImportAttributes(fmt.Errorf("; command: %s", command.Id), command.Attributes).Error()
89+
}
90+
// example: there should be exactly one default command, currently there is more than one default command;
91+
// command: <id1>; command: <id2>, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
92+
return fmt.Errorf("there should be exactly one default command, currently there is more than one default command%s", commandsReference)
8693
}
8794

8895
return nil

pkg/validation/commands_test.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,10 @@ func TestValidateGroup(t *testing.T) {
332332
component := "alias1"
333333

334334
noDefaultCmdErr := ".*there should be exactly one default command, currently there is no default command"
335-
multipleDefaultCmdErr := ".*there should be exactly one default command, currently there is more than one default command"
335+
multipleDefaultError := ".*there should be exactly one default command, currently there is more than one default command"
336+
multipleDefaultCmdErr := multipleDefaultError + "; command: run command; command: customcommand"
337+
invalidCmdErrWithImportAttributes := multipleDefaultError +
338+
"; command: run command; command: customcommand, imported from uri: http://127.0.0.1:8080, in parent overrides from main devfile"
336339

337340
tests := []struct {
338341
name string
@@ -347,6 +350,30 @@ func TestValidateGroup(t *testing.T) {
347350
},
348351
wantErr: &multipleDefaultCmdErr,
349352
},
353+
{
354+
name: "Two default run commands with import source attribute",
355+
commands: []v1alpha2.Command{
356+
generateDummyExecCommand("run command", component, &v1alpha2.CommandGroup{Kind: runGroup, IsDefault: true}),
357+
{
358+
Attributes: attributes.Attributes{}.PutString(ImportSourceAttribute,
359+
"uri: http://127.0.0.1:8080").PutString(ParentOverrideAttribute, "main devfile"),
360+
Id: "customcommand",
361+
CommandUnion: v1alpha2.CommandUnion{
362+
Exec: &v1alpha2.ExecCommand{
363+
LabeledCommand: v1alpha2.LabeledCommand{
364+
BaseCommand: v1alpha2.BaseCommand{
365+
Group: &v1alpha2.CommandGroup{Kind: runGroup, IsDefault: true},
366+
},
367+
},
368+
CommandLine: "command",
369+
Component: component,
370+
WorkingDir: "workDir",
371+
},
372+
},
373+
},
374+
},
375+
wantErr: &invalidCmdErrWithImportAttributes,
376+
},
350377
{
351378
name: "No default for more than one build commands",
352379
commands: []v1alpha2.Command{

pkg/validation/components.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func ValidateComponents(components []v1alpha2.Component) error {
5454

5555
err := validateEndpoints(component.Container.Endpoints, processedEndPointPort, processedEndPointName)
5656
if err != nil {
57-
return resolveErrorMessageWithImportArrtibutes(err, component.Attributes)
57+
return resolveErrorMessageWithImportAttributes(err, component.Attributes)
5858
}
5959
case component.Volume != nil:
6060
processedVolumes[component.Name] = true
@@ -64,37 +64,37 @@ func ValidateComponents(components []v1alpha2.Component) error {
6464
// https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
6565
if _, err := resource.ParseQuantity(component.Volume.Size); err != nil {
6666
invalidVolErr := &InvalidVolumeError{name: component.Name, reason: fmt.Sprintf("size %s for volume component is invalid, %v. Example - 2Gi, 1024Mi", component.Volume.Size, err)}
67-
return resolveErrorMessageWithImportArrtibutes(invalidVolErr, component.Attributes)
67+
return resolveErrorMessageWithImportAttributes(invalidVolErr, component.Attributes)
6868
}
6969
}
7070
case component.Openshift != nil:
7171
if component.Openshift.Uri != "" {
7272
err := ValidateURI(component.Openshift.Uri)
7373
if err != nil {
74-
return resolveErrorMessageWithImportArrtibutes(err, component.Attributes)
74+
return resolveErrorMessageWithImportAttributes(err, component.Attributes)
7575
}
7676
}
7777

7878
err := validateEndpoints(component.Openshift.Endpoints, processedEndPointPort, processedEndPointName)
7979
if err != nil {
80-
return resolveErrorMessageWithImportArrtibutes(err, component.Attributes)
80+
return resolveErrorMessageWithImportAttributes(err, component.Attributes)
8181
}
8282
case component.Kubernetes != nil:
8383
if component.Kubernetes.Uri != "" {
8484
err := ValidateURI(component.Kubernetes.Uri)
8585
if err != nil {
86-
return resolveErrorMessageWithImportArrtibutes(err, component.Attributes)
86+
return resolveErrorMessageWithImportAttributes(err, component.Attributes)
8787
}
8888
}
8989
err := validateEndpoints(component.Kubernetes.Endpoints, processedEndPointPort, processedEndPointName)
9090
if err != nil {
91-
return resolveErrorMessageWithImportArrtibutes(err, component.Attributes)
91+
return resolveErrorMessageWithImportAttributes(err, component.Attributes)
9292
}
9393
case component.Plugin != nil:
9494
if component.Plugin.RegistryUrl != "" {
9595
err := ValidateURI(component.Plugin.RegistryUrl)
9696
if err != nil {
97-
return resolveErrorMessageWithImportArrtibutes(err, component.Attributes)
97+
return resolveErrorMessageWithImportAttributes(err, component.Attributes)
9898
}
9999
}
100100
}
@@ -107,7 +107,7 @@ func ValidateComponents(components []v1alpha2.Component) error {
107107
for _, volumeMountName := range volumeMountNames {
108108
if !processedVolumes[volumeMountName] {
109109
missingVolumeMountErr := fmt.Errorf("\nvolume mount %s belonging to the container component %s", volumeMountName, componentName)
110-
newErr := resolveErrorMessageWithImportArrtibutes(missingVolumeMountErr, processedComponentWithVolumeMounts[componentName].Attributes)
110+
newErr := resolveErrorMessageWithImportAttributes(missingVolumeMountErr, processedComponentWithVolumeMounts[componentName].Attributes)
111111
invalidVolumeMountsErr += newErr.Error()
112112
}
113113
}

pkg/validation/errors.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ func (e *InvalidComponentError) Error() string {
9090
return fmt.Sprintf("the component %q is invalid - %s", e.componentName, e.reason)
9191
}
9292

93-
// resolveErrorMessageWithImportArrtibutes returns an updated error message
93+
// resolveErrorMessageWithImportAttributes returns an updated error message
9494
// with detailed information on the imported and overriden resource.
9595
// example:
9696
// "the component <compName> is invalid - <reason>, imported from Uri: http://example.com/devfile.yaml, in parent overrides from main devfile"
97-
func resolveErrorMessageWithImportArrtibutes(validationErr error, attributes attributesAPI.Attributes) error {
97+
func resolveErrorMessageWithImportAttributes(validationErr error, attributes attributesAPI.Attributes) error {
9898
var findKeyErr error
9999
importReference := attributes.Get(ImportSourceAttribute, &findKeyErr)
100100

pkg/validation/projects.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) error {
2424
switch len(gitSource.Remotes) {
2525
case 0:
2626
starterProjectErr := fmt.Errorf("\nstarterProject %s should have at least one remote", starterProject.Name)
27-
newErr := resolveErrorMessageWithImportArrtibutes(starterProjectErr, starterProject.Attributes)
27+
newErr := resolveErrorMessageWithImportAttributes(starterProjectErr, starterProject.Attributes)
2828
errString += newErr.Error()
2929
case 1:
3030
if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
3131
err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, starterProject.Name)
3232
if err != nil {
33-
newErr := resolveErrorMessageWithImportArrtibutes(err, starterProject.Attributes)
33+
newErr := resolveErrorMessageWithImportAttributes(err, starterProject.Attributes)
3434
errString += fmt.Sprintf("\n%s", newErr.Error())
3535
}
3636
}
3737
default: // len(gitSource.Remotes) >= 2
3838
starterProjectErr := fmt.Errorf("\nstarterProject %s should have one remote only", starterProject.Name)
39-
newErr := resolveErrorMessageWithImportArrtibutes(starterProjectErr, starterProject.Attributes)
39+
newErr := resolveErrorMessageWithImportAttributes(starterProjectErr, starterProject.Attributes)
4040
errString += newErr.Error()
4141
}
4242
}
@@ -67,24 +67,24 @@ func ValidateProjects(projects []v1alpha2.Project) error {
6767
switch len(gitSource.Remotes) {
6868
case 0:
6969
projectErr := fmt.Errorf("\nprojects %s should have at least one remote", project.Name)
70-
newErr := resolveErrorMessageWithImportArrtibutes(projectErr, project.Attributes)
70+
newErr := resolveErrorMessageWithImportAttributes(projectErr, project.Attributes)
7171
errString += newErr.Error()
7272
case 1:
7373
if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
7474
if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil {
75-
newErr := resolveErrorMessageWithImportArrtibutes(err, project.Attributes)
75+
newErr := resolveErrorMessageWithImportAttributes(err, project.Attributes)
7676
errString += fmt.Sprintf("\n%s", newErr.Error())
7777
}
7878
}
7979
default: // len(gitSource.Remotes) >= 2
8080
if gitSource.CheckoutFrom == nil || gitSource.CheckoutFrom.Remote == "" {
8181
projectErr := fmt.Errorf("\nproject %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
82-
newErr := resolveErrorMessageWithImportArrtibutes(projectErr, project.Attributes)
82+
newErr := resolveErrorMessageWithImportAttributes(projectErr, project.Attributes)
8383
errString += newErr.Error()
8484
continue
8585
}
8686
if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil {
87-
newErr := resolveErrorMessageWithImportArrtibutes(err, project.Attributes)
87+
newErr := resolveErrorMessageWithImportAttributes(err, project.Attributes)
8888
errString += fmt.Sprintf("\n%s", newErr.Error())
8989
}
9090
}

0 commit comments

Comments
 (0)