Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions compiler/native/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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")
Expand All @@ -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)
}
}
}

Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
114 changes: 0 additions & 114 deletions compiler/native/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}
12 changes: 4 additions & 8 deletions compiler/types/yaml/yaml/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 4 additions & 40 deletions compiler/types/yaml/yaml/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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: "",
Expand Down
4 changes: 0 additions & 4 deletions compiler/types/yaml/yaml/testdata/ruleset_collide.yml

This file was deleted.

8 changes: 0 additions & 8 deletions compiler/types/yaml/yaml/testdata/ruleset_collide_adv.yml

This file was deleted.

4 changes: 0 additions & 4 deletions compiler/types/yaml/yaml/testdata/ruleset_unknown_field.yml

This file was deleted.

Loading