diff --git a/compiler/native/validate.go b/compiler/native/validate.go index bd70d4579..77a8578f2 100644 --- a/compiler/native/validate.go +++ b/compiler/native/validate.go @@ -58,7 +58,7 @@ func (c *client) Validate(p *yaml.Build) error { } // validate the steps block provided - err = validateSteps(p.Steps, make(map[string]bool), "") + err = validateSteps(p.Steps) if err != nil { result = multierror.Append(result, err) } @@ -85,8 +85,6 @@ func validateServices(s yaml.ServiceSlice) error { // validateStages is a helper function that verifies the // stages block in the yaml configuration is valid. func validateStages(s yaml.StageSlice) error { - nameMap := make(map[string]bool) - for _, stage := range s { if len(stage.Name) == 0 { return fmt.Errorf("no name provided for stage") @@ -99,9 +97,24 @@ func validateStages(s yaml.StageSlice) error { } } - err := validateSteps(stage.Steps, nameMap, stage.Name) - if err != nil { - return err + for _, step := range stage.Steps { + if len(step.Name) == 0 { + return fmt.Errorf("no name provided for step for stage %s", stage.Name) + } + + if len(step.Image) == 0 { + return fmt.Errorf("no image provided for step %s for stage %s", step.Name, stage.Name) + } + + if step.Name == "clone" || step.Name == "init" { + continue + } + + if len(step.Commands) == 0 && len(step.Environment) == 0 && + len(step.Parameters) == 0 && len(step.Secrets) == 0 && + len(step.Template.Name) == 0 && !step.Detach { + return fmt.Errorf("no commands, environment, parameters, secrets or template provided for step %s for stage %s", step.Name, stage.Name) + } } } @@ -110,7 +123,7 @@ func validateStages(s yaml.StageSlice) error { // validateSteps is a helper function that verifies the // steps block in the yaml configuration is valid. -func validateSteps(s yaml.StepSlice, nameMap map[string]bool, stageName string) error { +func validateSteps(s yaml.StepSlice) error { reportCount := 0 reportMap := make(map[string]string) @@ -128,12 +141,6 @@ func validateSteps(s yaml.StepSlice, nameMap map[string]bool, stageName string) continue } - if _, ok := nameMap[stageName+"_"+step.Name]; ok { - return fmt.Errorf("step %s is already defined", step.Name) - } - - nameMap[stageName+"_"+step.Name] = true - if s, ok := reportMap[step.ReportAs]; ok { return fmt.Errorf("report_as to %s for step %s is already targeted by step %s", step.ReportAs, step.Name, s) } diff --git a/compiler/native/validate_test.go b/compiler/native/validate_test.go index 6f0d77c43..a5771e953 100644 --- a/compiler/native/validate_test.go +++ b/compiler/native/validate_test.go @@ -389,53 +389,6 @@ func TestNative_Validate_Stages_NoCommands(t *testing.T) { } } -func TestNative_Validate_Stages_StepNameConflict(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - set.String("clone-image", defaultCloneImage, "doc") - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - }, - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - }, - }, - } - - // run test - compiler, err := FromCLIContext(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - func TestNative_Validate_Stages_NeedsSelfReference(t *testing.T) { // setup types set := flag.NewFlagSet("test", 0) @@ -533,36 +486,6 @@ func TestNative_Validate_Steps_NoName(t *testing.T) { } } -func TestNative_Validate_Steps_NameCollision(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - set.String("clone-image", defaultCloneImage, "doc") - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := FromCLIContext(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - func TestNative_Validate_Steps_NoImage(t *testing.T) { // setup types set := flag.NewFlagSet("test", 0) @@ -701,40 +624,3 @@ func TestNative_Validate_MultiReportAs(t *testing.T) { t.Errorf("Validate should have returned err") } } - -func TestNative_Validate_Steps_StepNameConflict(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - set.String("clone-image", defaultCloneImage, "doc") - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - &yaml.Step{ - Commands: raw.StringSlice{"echo goodbye"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := FromCLIContext(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} diff --git a/compiler/types/yaml/yaml/ruleset.go b/compiler/types/yaml/yaml/ruleset.go index 4c5eaf3ce..a75433311 100644 --- a/compiler/types/yaml/yaml/ruleset.go +++ b/compiler/types/yaml/yaml/ruleset.go @@ -67,15 +67,11 @@ func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error { }) // attempt to unmarshal simple ruleset - err := unmarshal(simple) - if err != nil { - return err - } + //nolint:errcheck // intentionally not handling error + unmarshal(simple) // attempt to unmarshal advanced ruleset - err = unmarshal(advanced) - if err != nil { - return err - } + //nolint:errcheck // intentionally not handling error + unmarshal(advanced) // set ruleset `unless` to advanced `unless` rules r.Unless = advanced.Unless diff --git a/compiler/types/yaml/yaml/ruleset_test.go b/compiler/types/yaml/yaml/ruleset_test.go index ef76afb9b..cb4e0466f 100644 --- a/compiler/types/yaml/yaml/ruleset_test.go +++ b/compiler/types/yaml/yaml/ruleset_test.go @@ -10,7 +10,6 @@ import ( "gopkg.in/yaml.v3" "github.com/go-vela/server/compiler/types/pipeline" - "github.com/google/go-cmp/cmp" ) func TestYaml_Ruleset_ToPipeline(t *testing.T) { @@ -96,9 +95,8 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) { func TestYaml_Ruleset_UnmarshalYAML(t *testing.T) { // setup tests tests := []struct { - file string - want *Ruleset - wantErr bool + file string + want *Ruleset }{ { file: "testdata/ruleset_simple.yml", @@ -150,27 +148,6 @@ func TestYaml_Ruleset_UnmarshalYAML(t *testing.T) { Matcher: "regex", }, }, - { - file: "testdata/ruleset_unknown_field.yml", - want: &Ruleset{ - If: Rules{ - Branch: []string{"main"}, - Event: []string{"push"}, - }, - Matcher: "filepath", - Operator: "and", - }, - }, - { - file: "testdata/ruleset_collide.yml", - want: nil, - wantErr: true, - }, - { - file: "testdata/ruleset_collide_adv.yml", - want: nil, - wantErr: true, - }, } // run tests @@ -184,20 +161,12 @@ func TestYaml_Ruleset_UnmarshalYAML(t *testing.T) { err = yaml.Unmarshal(b, got) - if test.wantErr { - if err == nil { - t.Errorf("UnmarshalYAML should have returned err") - } - - continue - } - if err != nil { t.Errorf("UnmarshalYAML returned err: %v", err) } - if diff := cmp.Diff(got, test.want); diff != "" { - t.Errorf("UnmarshalYAML mismatch (-got +want):\n%s", diff) + if !reflect.DeepEqual(got, test.want) { + t.Errorf("UnmarshalYAML is %v, want %v", got, test.want) } } } @@ -278,11 +247,6 @@ func TestYaml_Rules_UnmarshalYAML(t *testing.T) { Target: []string{"production"}, }, }, - { - failure: true, - file: "testdata/ruleset_collide.yml", - want: nil, - }, { failure: true, file: "", diff --git a/compiler/types/yaml/yaml/testdata/ruleset_collide.yml b/compiler/types/yaml/yaml/testdata/ruleset_collide.yml deleted file mode 100644 index d560b601b..000000000 --- a/compiler/types/yaml/yaml/testdata/ruleset_collide.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -branch: main -branch: -event: push \ No newline at end of file diff --git a/compiler/types/yaml/yaml/testdata/ruleset_collide_adv.yml b/compiler/types/yaml/yaml/testdata/ruleset_collide_adv.yml deleted file mode 100644 index fd8cb469c..000000000 --- a/compiler/types/yaml/yaml/testdata/ruleset_collide_adv.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- -if: - event: push - branch: main - event: -unless: - tag: v3* - matcher: filepath \ No newline at end of file diff --git a/compiler/types/yaml/yaml/testdata/ruleset_unknown_field.yml b/compiler/types/yaml/yaml/testdata/ruleset_unknown_field.yml deleted file mode 100644 index db2a6296c..000000000 --- a/compiler/types/yaml/yaml/testdata/ruleset_unknown_field.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -event: push -branch: main -user: octocat \ No newline at end of file