forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflowpod_test.go
124 lines (115 loc) · 3.33 KB
/
workflowpod_test.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
package controller
import (
"testing"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func unmarshalTemplate(yamlStr string) *wfv1.Template {
var tmpl wfv1.Template
err := yaml.Unmarshal([]byte(yamlStr), &tmpl)
if err != nil {
panic(err)
}
return &tmpl
}
// newWoc a new operation context suitable for testing
func newWoc(wfs ...wfv1.Workflow) *wfOperationCtx {
var wf *wfv1.Workflow
if len(wfs) == 0 {
wf = unmarshalWF(helloWorldWf)
} else {
wf = &wfs[0]
}
woc := wfOperationCtx{
wf: wf,
orig: wf.DeepCopyObject().(*wfv1.Workflow),
updated: false,
log: log.WithFields(log.Fields{
"workflow": wf.ObjectMeta.Name,
"namespace": wf.ObjectMeta.Namespace,
}),
controller: newController(),
completedPods: make(map[string]bool),
}
if woc.wf.Status.Nodes == nil {
woc.wf.Status.Nodes = make(map[string]wfv1.NodeStatus)
}
return &woc
}
// getPodName returns the podname of the created pod of a workflow
// Only applies to single pod workflows
func getPodName(wf *wfv1.Workflow) string {
if len(wf.Status.Nodes) != 1 {
panic("getPodName called against a multi-pod workflow")
}
for podName := range wf.Status.Nodes {
return podName
}
return ""
}
var scriptTemplateWithInputArtifact = `
name: script-with-input-artifact
inputs:
artifacts:
- name: kubectl
path: /bin/kubectl
http:
url: https://storage.googleapis.com/kubernetes-release/release/v1.8.0/bin/linux/amd64/kubectl
script:
image: alpine:latest
command: [sh]
source: |
ls /bin/kubectl
`
// TestScriptTemplateWithVolume ensure we can a script pod with input artifacts
func TestScriptTemplateWithVolume(t *testing.T) {
tmpl := unmarshalTemplate(scriptTemplateWithInputArtifact)
err := newWoc().executeScript(tmpl.Name, tmpl)
assert.Nil(t, err)
}
// TestServiceAccount verifies the ability to carry forward the service account name
// for the pod from workflow.spec.serviceAccountName.
func TestServiceAccount(t *testing.T) {
woc := newWoc()
woc.wf.Spec.ServiceAccountName = "foo"
err := woc.executeContainer(woc.wf.Spec.Entrypoint, &woc.wf.Spec.Templates[0])
assert.Nil(t, err)
podName := getPodName(woc.wf)
pod, err := woc.controller.kubeclientset.CoreV1().Pods("").Get(podName, metav1.GetOptions{})
assert.Nil(t, err)
assert.Equal(t, pod.Spec.ServiceAccountName, "foo")
}
// TestAffinity verifies the ability to carry forward affinity rules
func TestAffinity(t *testing.T) {
woc := newWoc()
woc.wf.Spec.Affinity = &apiv1.Affinity{
NodeAffinity: &apiv1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &apiv1.NodeSelector{
NodeSelectorTerms: []apiv1.NodeSelectorTerm{
{
MatchExpressions: []apiv1.NodeSelectorRequirement{
{
Key: "kubernetes.io/e2e-az-name",
Operator: apiv1.NodeSelectorOpIn,
Values: []string{
"e2e-az1",
"e2e-az2",
},
},
},
},
},
},
},
}
err := woc.executeContainer(woc.wf.Spec.Entrypoint, &woc.wf.Spec.Templates[0])
assert.Nil(t, err)
podName := getPodName(woc.wf)
pod, err := woc.controller.kubeclientset.CoreV1().Pods("").Get(podName, metav1.GetOptions{})
assert.Nil(t, err)
assert.NotNil(t, pod.Spec.Affinity)
}