-
Notifications
You must be signed in to change notification settings - Fork 0
/
given.go
190 lines (171 loc) · 4.87 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package fixtures
import (
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/hydrator"
)
type Given struct {
t *testing.T
client v1alpha1.WorkflowInterface
wfebClient v1alpha1.WorkflowEventBindingInterface
wfTemplateClient v1alpha1.WorkflowTemplateInterface
cwfTemplateClient v1alpha1.ClusterWorkflowTemplateInterface
cronClient v1alpha1.CronWorkflowInterface
hydrator hydrator.Interface
wf *wfv1.Workflow
wfeb *wfv1.WorkflowEventBinding
wfTemplates []*wfv1.WorkflowTemplate
cwfTemplates []*wfv1.ClusterWorkflowTemplate
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 {
g.t.Helper()
g.wf = &wfv1.Workflow{}
g.readResource(text, g.wf)
g.checkImages(g.wf.Spec.Templates)
return g
}
func (g *Given) readResource(text string, v metav1.Object) {
g.t.Helper()
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()
}
{
file, err := ioutil.ReadFile(file)
if err != nil {
g.t.Fatal(err)
}
err = yaml.Unmarshal(file, v)
if err != nil {
g.t.Fatal(err)
}
g.checkLabels(v)
}
}
func (g *Given) checkImages(templates []wfv1.Template) {
g.t.Helper()
// Using an arbitrary image will result in slow and flakey tests as we can't really predict when they'll be
// downloaded or evicted. To keep tests fast and reliable you must use whitelisted images.
imageWhitelist := func(image string) bool {
return strings.Contains(image, "argoexec:") ||
image == "argoproj/argosay:v1" ||
image == "argoproj/argosay:v2" ||
image == "python:alpine3.6"
}
for _, t := range templates {
container := t.Container
if container != nil {
image := container.Image
if !imageWhitelist(image) {
g.t.Fatalf("non-whitelisted image used in test: %s", image)
}
}
}
}
func (g *Given) checkLabels(m metav1.Object) {
g.t.Helper()
if m.GetLabels()[Label] == "" {
g.t.Fatalf("%s%s does not have %s label", m.GetName(), m.GetGenerateName(), Label)
}
}
func (g *Given) WorkflowName(name string) *Given {
g.t.Helper()
g.workflowName = name
return g
}
func (g *Given) WorkflowEventBinding(text string) *Given {
g.t.Helper()
g.wfeb = &wfv1.WorkflowEventBinding{}
g.readResource(text, g.wfeb)
return g
}
func (g *Given) WorkflowTemplate(text string) *Given {
g.t.Helper()
wfTemplate := &wfv1.WorkflowTemplate{}
g.readResource(text, wfTemplate)
g.checkImages(wfTemplate.Spec.Templates)
g.wfTemplates = append(g.wfTemplates, wfTemplate)
return g
}
func (g *Given) CronWorkflow(text string) *Given {
g.t.Helper()
g.cronWf = &wfv1.CronWorkflow{}
g.readResource(text, g.cronWf)
g.checkImages(g.cronWf.Spec.WorkflowSpec.Templates)
return g
}
var NoError = func(t *testing.T, output string, err error) {
assert.NoError(t, err, output)
}
var OutputContains = func(contains string) func(t *testing.T, output string, err error) {
return func(t *testing.T, output string, err error) {
if assert.NoError(t, err, output) {
assert.Contains(t, output, contains)
}
}
}
func (g *Given) Exec(name string, args []string, block func(t *testing.T, output string, err error)) *Given {
g.t.Helper()
output, err := Exec(name, args...)
block(g.t, output, err)
if g.t.Failed() {
g.t.FailNow()
}
return g
}
func (g *Given) RunCli(args []string, block func(t *testing.T, output string, err error)) *Given {
return g.Exec("../../dist/argo", append([]string{"-n", Namespace}, args...), block)
}
func (g *Given) ClusterWorkflowTemplate(text string) *Given {
g.t.Helper()
cwfTemplate := &wfv1.ClusterWorkflowTemplate{}
g.readResource(text, cwfTemplate)
g.cwfTemplates = append(g.cwfTemplates, cwfTemplate)
return g
}
func (g *Given) When() *When {
return &When{
t: g.t,
wf: g.wf,
wfeb: g.wfeb,
wfTemplates: g.wfTemplates,
cwfTemplates: g.cwfTemplates,
cronWf: g.cronWf,
client: g.client,
wfebClient: g.wfebClient,
wfTemplateClient: g.wfTemplateClient,
cwfTemplateClient: g.cwfTemplateClient,
cronClient: g.cronClient,
hydrator: g.hydrator,
workflowName: g.workflowName,
kubeClient: g.kubeClient,
}
}