diff --git a/pkg/workflow/errors.go b/pkg/workflow/errors.go new file mode 100644 index 0000000..7899e86 --- /dev/null +++ b/pkg/workflow/errors.go @@ -0,0 +1,10 @@ +package workflow + +import ( + "errors" +) + +var ( + ErrNoNodesFound = errors.New("no nodes found in the graph") + ErrEntryNodeNotFound = errors.New("\"entry\" node not found") +) diff --git a/pkg/workflow/graph.go b/pkg/workflow/graph.go index ba6d74a..983fb05 100644 --- a/pkg/workflow/graph.go +++ b/pkg/workflow/graph.go @@ -3,7 +3,6 @@ package workflow import ( "bytes" "encoding/base64" - "fmt" v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1" @@ -22,8 +21,8 @@ type Node struct { } func Build(entry string, nodes map[string]v1alpha13.NodeStatus) (*Graph, error) { - if nodes == nil || len(nodes) == 0 { - return nil, fmt.Errorf("no nodes found in the graph") + if len(nodes) == 0 { + return nil, ErrNoNodesFound } g := &Graph{ @@ -33,7 +32,7 @@ func Build(entry string, nodes map[string]v1alpha13.NodeStatus) (*Graph, error) e, ok := nodes[entry] if !ok { - return nil, fmt.Errorf("\"entry\" node not found") + return nil, ErrEntryNodeNotFound } err := g.bft(e) diff --git a/pkg/workflow/graph_test.go b/pkg/workflow/graph_test.go new file mode 100644 index 0000000..8079a69 --- /dev/null +++ b/pkg/workflow/graph_test.go @@ -0,0 +1,56 @@ +package workflow + +import ( + "testing" + + v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" +) + +func TestBuild(t *testing.T) { + type args struct { + entry string + nodes map[string]v1alpha13.NodeStatus + } + testmap := make(map[string]v1alpha13.NodeStatus) + testmap["dummy"] = v1alpha13.NodeStatus{} + + tests := []struct { + name string + args args + want *Graph + err error + }{ + // + // TODO: Add more test cases. + // + { + name: "testing nil nodes", + args: args{ + entry: "", + nodes: nil, + }, + want: nil, + err: ErrNoNodesFound, + }, + { + name: "testing unknown entry", + args: args{ + entry: "unknown", + nodes: testmap, + }, + want: nil, + err: ErrEntryNodeNotFound, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Build(tt.args.entry, tt.args.nodes) + if err != tt.err { + t.Errorf("Build() error = %v, want %v", err, tt.err) + } + if got != tt.want { + t.Errorf("Build() = %v, want %v", got, tt.want) + } + }) + } +}