forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
given.go
175 lines (164 loc) · 3.94 KB
/
given.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package fixtures
import (
"io/ioutil"
"strings"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
"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 Given struct {
t *testing.T
diagnostics *Diagnostics
client v1alpha1.WorkflowInterface
wfTemplateClient v1alpha1.WorkflowTemplateInterface
cronClient v1alpha1.CronWorkflowInterface
offloadNodeStatusRepo sqldb.OffloadNodeStatusRepo
wf *wfv1.Workflow
wfTemplates []*wfv1.WorkflowTemplate
cronWf *wfv1.CronWorkflow
workflowName string
kubeClient kubernetes.Interface
}
// creates a workflow based on the parameter, this may be:
//
// 1. A file name if it starts with "@"
// 2. Raw YAML.
func (g *Given) Workflow(text string) *Given {
var file string
if strings.HasPrefix(text, "@") {
file = strings.TrimPrefix(text, "@")
} else {
f, err := ioutil.TempFile("", "argo_e2e")
if err != nil {
g.t.Fatal(err)
}
_, err = f.Write([]byte(text))
if err != nil {
g.t.Fatal(err)
}
err = f.Close()
if err != nil {
g.t.Fatal(err)
}
file = f.Name()
}
// read the file in
{
file, err := ioutil.ReadFile(file)
if err != nil {
g.t.Fatal(err)
}
g.wf = &wfv1.Workflow{}
err = yaml.Unmarshal(file, g.wf)
if err != nil {
g.t.Fatal(err)
}
}
g.checkLabels(g.wf.ObjectMeta)
return g
}
func (g *Given) checkLabels(m metav1.ObjectMeta) {
if m.GetLabels()[Label] == "" && m.GetLabels()[LabelCron] == "" {
g.t.Fatalf("%s%s does not have one of {%s, %s} labels", m.Name, m.GenerateName, Label, LabelCron)
}
}
func (g *Given) WorkflowName(name string) *Given {
g.workflowName = name
return g
}
func (g *Given) WorkflowTemplate(text string) *Given {
var file string
if strings.HasPrefix(text, "@") {
file = strings.TrimPrefix(text, "@")
} else {
f, err := ioutil.TempFile("", "argo_e2e")
if err != nil {
g.t.Fatal(err)
}
_, err = f.Write([]byte(text))
if err != nil {
g.t.Fatal(err)
}
err = f.Close()
if err != nil {
g.t.Fatal(err)
}
file = f.Name()
}
// read the file in
{
file, err := ioutil.ReadFile(file)
if err != nil {
g.t.Fatal(err)
}
wfTemplate := &wfv1.WorkflowTemplate{}
err = yaml.Unmarshal(file, wfTemplate)
if err != nil {
g.t.Fatal(err)
}
g.checkLabels(wfTemplate.ObjectMeta)
g.wfTemplates = append(g.wfTemplates, wfTemplate)
}
return g
}
func (g *Given) CronWorkflow(text string) *Given {
var file string
if strings.HasPrefix(text, "@") {
file = strings.TrimPrefix(text, "@")
} else {
f, err := ioutil.TempFile("", "argo_e2e")
if err != nil {
g.t.Fatal(err)
}
_, err = f.Write([]byte(text))
if err != nil {
g.t.Fatal(err)
}
err = f.Close()
if err != nil {
g.t.Fatal(err)
}
file = f.Name()
}
// read the file in
{
file, err := ioutil.ReadFile(file)
if err != nil {
g.t.Fatal(err)
}
g.cronWf = &wfv1.CronWorkflow{}
err = yaml.Unmarshal(file, g.cronWf)
if err != nil {
g.t.Fatal(err)
}
if g.cronWf.GetLabels() == nil {
g.cronWf.SetLabels(map[string]string{})
}
g.checkLabels(g.cronWf.ObjectMeta)
}
return g
}
func (g *Given) RunCli(args []string, block func(t *testing.T, output string, err error)) *Given {
output, err := runCli(g.diagnostics, args)
block(g.t, output, err)
return g
}
func (g *Given) When() *When {
return &When{
t: g.t,
diagnostics: g.diagnostics,
wf: g.wf,
wfTemplates: g.wfTemplates,
cronWf: g.cronWf,
client: g.client,
wfTemplateClient: g.wfTemplateClient,
cronClient: g.cronClient,
offloadNodeStatusRepo: g.offloadNodeStatusRepo,
workflowName: g.workflowName,
kubeClient: g.kubeClient,
}
}