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

Add unit tests for pkg/workflow package #334

Merged
merged 2 commits into from
Jun 23, 2021
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
10 changes: 10 additions & 0 deletions pkg/workflow/errors.go
Original file line number Diff line number Diff line change
@@ -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")
)
7 changes: 3 additions & 4 deletions pkg/workflow/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions pkg/workflow/graph_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}