forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
then.go
102 lines (90 loc) · 3.12 KB
/
then.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package fixtures
import (
"testing"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/argoproj/argo/persist/sqldb"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
)
type Then struct {
t *testing.T
diagnostics *Diagnostics
workflowName string
wfTemplateNames []string
cronWorkflowName string
client v1alpha1.WorkflowInterface
cronClient v1alpha1.CronWorkflowInterface
offloadNodeStatusRepo sqldb.OffloadNodeStatusRepo
kubeClient kubernetes.Interface
}
func (t *Then) ExpectWorkflow(block func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus)) *Then {
return t.expectWorkflow(t.workflowName, block)
}
func (t *Then) ExpectWorkflowName(workflowName string, block func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus)) *Then {
return t.expectWorkflow(workflowName, block)
}
func (t *Then) expectWorkflow(workflowName string, block func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus)) *Then {
if workflowName == "" {
t.t.Fatal("No workflow to test")
}
log.WithFields(log.Fields{"test": t.t.Name(), "workflow": workflowName}).Info("Checking expectation")
wf, err := t.client.Get(workflowName, metav1.GetOptions{})
if err != nil {
t.t.Fatal(err)
}
if wf.Status.IsOffloadNodeStatus() {
offloadedNodes, err := t.offloadNodeStatusRepo.Get(string(wf.UID), wf.GetOffloadNodeStatusVersion())
if err != nil {
t.t.Fatal(err)
}
wf.Status.Nodes = offloadedNodes
}
block(t.t, &wf.ObjectMeta, &wf.Status)
return t
}
func (t *Then) ExpectCron(block func(t *testing.T, cronWf *wfv1.CronWorkflow)) *Then {
if t.cronWorkflowName == "" {
t.t.Fatal("No cron workflow to test")
}
log.WithFields(log.Fields{"cronWorkflow": t.cronWorkflowName}).Info("Checking cron expectation")
cronWf, err := t.cronClient.Get(t.cronWorkflowName, metav1.GetOptions{})
if err != nil {
t.t.Fatal(err)
}
block(t.t, cronWf)
return t
}
func (t *Then) ExpectWorkflowList(listOptions metav1.ListOptions, block func(t *testing.T, wfList *wfv1.WorkflowList)) *Then {
log.Info("Listing workflows")
wfList, err := t.client.List(listOptions)
if err != nil {
t.t.Fatal(err)
}
log.Info("Checking expectation")
block(t.t, wfList)
return t
}
func (t *Then) ExpectAuditEvents(block func(*testing.T, *apiv1.EventList)) *Then {
if t.workflowName == "" {
t.t.Fatal("No workflow to test")
}
log.WithFields(log.Fields{"workflow": t.workflowName}).Info("Checking expectation")
wf, err := t.client.Get(t.workflowName, metav1.GetOptions{})
if err != nil {
t.t.Fatal(err)
}
eventList, err := t.kubeClient.CoreV1().Events(wf.ObjectMeta.Namespace).List(metav1.ListOptions{})
if err != nil {
t.t.Fatal(err)
}
block(t.t, eventList)
return t
}
func (t *Then) RunCli(args []string, block func(t *testing.T, output string, err error)) *Then {
output, err := runCli(t.diagnostics, args)
block(t.t, output, err)
return t
}