Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BasicVerifier for Tasks #1324

Merged
merged 4 commits into from
Feb 12, 2020
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
4 changes: 2 additions & 2 deletions pkg/engine/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Build(task *v1beta1.Task) (Tasker, error) {
func newApply(task *v1beta1.Task) (Tasker, error) {
// validate ApplyTask
if len(task.Spec.ResourceTaskSpec.Resources) == 0 {
return nil, errors.New("task validation error: apply task has an empty resource list. if that's what you need, use a Dummy task instead")
return nil, fmt.Errorf("task validation error: apply task '%s' has an empty resource list. if that's what you need, use a Dummy task instead", task.Name)
}

return ApplyTask{
Expand All @@ -85,7 +85,7 @@ func newApply(task *v1beta1.Task) (Tasker, error) {
func newDelete(task *v1beta1.Task) (Tasker, error) {
// validate DeleteTask
if len(task.Spec.ResourceTaskSpec.Resources) == 0 {
return nil, errors.New("task validation error: delete task has an empty resource list. if that's what you need, use a Dummy task instead")
return nil, fmt.Errorf("task validation error: delete task '%s' has an empty resource list. if that's what you need, use a Dummy task instead", task.Name)
}

return DeleteTask{
Expand Down
1 change: 1 addition & 0 deletions pkg/kudoctl/cmd/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var verifiers = []verifier.PackageVerifier{
DuplicateVerifier{},
InvalidCharVerifier{";,"},
K8sVersionVerifier{},
task.BuildVerifier{},
task.ReferenceVerifier{},
template.ParametersVerifier{},
template.ReferenceVerifier{},
Expand Down
29 changes: 29 additions & 0 deletions pkg/kudoctl/packages/verifier/task/verify_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package task

import (
"github.com/kudobuilder/kudo/pkg/engine/task"

"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/verifier"
)

// BuildVerifier verifies tasks producing errors for tasks referenced in plans that do not exist and warnings for tasks which are not used in a plan
type BuildVerifier struct{}

func (BuildVerifier) Verify(pf *packages.Files) verifier.Result {
res := verifier.NewResult()
res.Merge(tasksWellDefined(pf))
return res
}

func tasksWellDefined(pf *packages.Files) verifier.Result {
result := verifier.NewResult()
for _, tt := range pf.Operator.Tasks {
tt := tt

if _, err := task.Build(&tt); err != nil {
result.AddErrors(err.Error())
}
}
return result
}
108 changes: 108 additions & 0 deletions pkg/kudoctl/packages/verifier/task/verify_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package task

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
)

func TestTaskBasicsVerifier(t *testing.T) {
tests := []struct {
name string
task v1beta1.Task
errors []string
warnings []string
}{
{name: "An Apply task without resources", task: v1beta1.Task{
Name: "Task",
Kind: "Apply",
Spec: v1beta1.TaskSpec{},
}, errors: []string{"task validation error: apply task 'Task' has an empty resource list. if that's what you need, use a Dummy task instead"}, warnings: []string{}},
{name: "An Apply task with resources", task: v1beta1.Task{
Name: "Task",
Kind: "Apply",
Spec: v1beta1.TaskSpec{
ResourceTaskSpec: v1beta1.ResourceTaskSpec{Resources: []string{"someResource"}},
},
}, errors: []string{}, warnings: []string{}},
{name: "An Delete task without resources", task: v1beta1.Task{
Name: "Task",
Kind: "Delete",
Spec: v1beta1.TaskSpec{},
}, errors: []string{"task validation error: delete task 'Task' has an empty resource list. if that's what you need, use a Dummy task instead"}, warnings: []string{}},
{name: "An Delete task with resources", task: v1beta1.Task{
Name: "Task",
Kind: "Delete",
Spec: v1beta1.TaskSpec{
ResourceTaskSpec: v1beta1.ResourceTaskSpec{Resources: []string{"someResource"}},
},
}, errors: []string{}, warnings: []string{}},

// More detailed tests are in engine/task/task_test.go
{name: "An empty pipe task", task: v1beta1.Task{
Name: "Task",
Kind: "Pipe",
Spec: v1beta1.TaskSpec{},
}, errors: []string{"task validation error: pipe task has an empty pipe files list"}, warnings: []string{}},
{name: "A valid pipe task", task: v1beta1.Task{
Name: "Task",
Kind: "Pipe",
Spec: v1beta1.TaskSpec{
PipeTaskSpec: v1beta1.PipeTaskSpec{
Pod: "",
Pipe: []v1beta1.PipeSpec{
{
Kind: "ConfigMap",
File: "someFile",
Key: "someKey",
},
},
},
},
}, errors: []string{}, warnings: []string{}},
}
verifier := BuildVerifier{}

for _, test := range tests {
test := test
pf := packageFilesFromWithTask(test.task)
res := verifier.Verify(&pf)

assert.Equal(t, test.errors, res.Errors, test.name)
assert.Equal(t, test.warnings, res.Warnings, test.name)
}
}

func packageFilesFromWithTask(task v1beta1.Task) packages.Files {
steps := []v1beta1.Step{{
Name: "cat-in-hat",
Tasks: []string{"thingOne"},
}, {
Name: "mayham",
Tasks: []string{"thingThree"},
}}

phases := []v1beta1.Phase{{
Name: "parents leave",
Strategy: "serial",
Steps: steps,
}}

plans := make(map[string]v1beta1.Plan)
plans["boring-rainy"] = v1beta1.Plan{
Strategy: "serial",
Phases: phases,
}

operator := packages.OperatorFile{
Tasks: []v1beta1.Task{task},
Plans: plans,
}

return packages.Files{
Operator: &operator,
}
}