forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_test.go
167 lines (157 loc) · 4.84 KB
/
agent_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
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
package controller
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
apiv1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
func TestExecuteTaskSet(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(`apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: http-template
namespace: default
spec:
entrypoint: main
templates:
- name: main
steps:
- - name: good
template: http
arguments:
parameters: [{name: url, value: "https://raw.githubusercontent.com/argoproj/argo-workflows/4e450e250168e6b4d51a126b784e90b11a0162bc/pkg/apis/workflow/v1alpha1/generated.swagger.json"}]
- - name: bad
template: http
continueOn:
failed: true
arguments:
parameters: [{name: url, value: "https://openlibrary.org/people/george08/nofound.json"}]
- name: http
inputs:
parameters:
- name: url
http:
url: "{{inputs.parameters.url}}"
`)
ctx := context.Background()
var ts wfv1.WorkflowTaskSet
wfv1.MustUnmarshal(`apiVersion: argoproj.io/v1alpha1
kind: WorkflowTaskSet
metadata:
name: http-template-1
namespace: default
spec:
tasks:
http-template-nxvtg-1265710817:
http:
url: https://openlibrary.org/people/george08/nofound.json
inputs:
parameters:
- name: url
value: https://openlibrary.org/people/george08/nofound.json
name: http
status:
nodes:
http-template-1-3690327077:
outputs:
parameters:
- name: result
value: |
{
"swagger": "2.0",
"info": {
"title": "pkg/apis/workflow/v1alpha1/generated.proto",
"version": "version not set"
},
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {},
"definitions": {}
}
phase: Succeeded
`, &ts)
t.Run("CreateTaskSet", func(t *testing.T) {
cancel, controller := newController(wf, ts)
defer cancel()
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
tslist, err := woc.controller.wfclientset.ArgoprojV1alpha1().WorkflowTaskSets("default").List(ctx, v1.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, tslist.Items)
assert.Len(t, tslist.Items, 1)
for _, ts := range tslist.Items {
assert.NotNil(t, ts)
assert.Equal(t, ts.Name, wf.Name)
assert.Equal(t, ts.Namespace, wf.Namespace)
assert.Len(t, ts.Spec.Tasks, 1)
}
pods, err := woc.controller.kubeclientset.CoreV1().Pods("default").List(ctx, v1.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, pods.Items)
assert.Len(t, pods.Items, 1)
for _, pod := range pods.Items {
assert.NotNil(t, pod)
assert.True(t, strings.HasSuffix(pod.Name, "-agent"))
}
})
t.Run("CreateTaskSetWithInstanceID", func(t *testing.T) {
cancel, controller := newController(wf, ts)
defer cancel()
controller.Config.InstanceID = "testID"
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
tslist, err := woc.controller.wfclientset.ArgoprojV1alpha1().WorkflowTaskSets("default").List(ctx, v1.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, tslist.Items)
assert.Len(t, tslist.Items, 1)
for _, ts := range tslist.Items {
assert.NotNil(t, ts)
assert.Equal(t, ts.Name, wf.Name)
assert.Equal(t, ts.Namespace, wf.Namespace)
assert.Len(t, ts.Spec.Tasks, 1)
}
pods, err := woc.controller.kubeclientset.CoreV1().Pods("default").List(ctx, v1.ListOptions{})
assert.NoError(t, err)
assert.NotEmpty(t, pods.Items)
assert.Len(t, pods.Items, 1)
for _, pod := range pods.Items {
assert.NotNil(t, pod)
assert.True(t, strings.HasSuffix(pod.Name, "-agent"))
assert.Equal(t, "testID", pod.ObjectMeta.Labels[common.LabelKeyControllerInstanceID])
}
})
}
func TestAssessAgentPodStatus(t *testing.T) {
t.Run("Failed", func(t *testing.T) {
pod1 := &apiv1.Pod{
Status: apiv1.PodStatus{Phase: apiv1.PodFailed},
}
nodeStatus, msg := assessAgentPodStatus(pod1)
assert.Equal(t, wfv1.WorkflowFailed, nodeStatus)
assert.Equal(t, "", msg)
})
t.Run("Running", func(t *testing.T) {
pod1 := &apiv1.Pod{
Status: apiv1.PodStatus{Phase: apiv1.PodRunning},
}
nodeStatus, msg := assessAgentPodStatus(pod1)
assert.Equal(t, wfv1.WorkflowPhase(""), nodeStatus)
assert.Equal(t, "", msg)
})
t.Run("Success", func(t *testing.T) {
pod1 := &apiv1.Pod{
Status: apiv1.PodStatus{Phase: apiv1.PodSucceeded},
}
nodeStatus, msg := assessAgentPodStatus(pod1)
assert.Equal(t, wfv1.WorkflowPhase(""), nodeStatus)
assert.Equal(t, "", msg)
})
}