forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepaction_results_test.go
258 lines (231 loc) · 7.15 KB
/
stepaction_results_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//go:build e2e
// +build e2e
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/parse"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/system"
knativetest "knative.dev/pkg/test"
)
var (
ignoreProvenance = cmpopts.IgnoreFields(v1.TaskRunStatusFields{}, "Provenance")
requireEnableStepActionsGate = map[string]string{
"enable-step-actions": "true",
}
)
func TestStepResultsStepActions(t *testing.T) {
featureFlags := getFeatureFlagsBaseOnAPIFlag(t)
previousResultExtractionMethod := featureFlags.ResultExtractionMethod
type tests struct {
name string
taskRunFunc func(*testing.T, string) (*v1.TaskRun, *v1.TaskRun)
stepActionFunc func(*testing.T, string) *v1beta1.StepAction
}
tds := []tests{{
name: "step results",
taskRunFunc: getStepActionsTaskRun,
stepActionFunc: getStepAction,
}}
for _, td := range tds {
td := td
t.Run(td.name, func(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setUpStepActionsResults(ctx, t)
// reset configmap
knativetest.CleanupOnInterrupt(func() { resetStepActionsResults(ctx, t, c, previousResultExtractionMethod) }, t.Logf)
defer resetSidecarLogs(ctx, t, c, previousResultExtractionMethod)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
t.Logf("Setting up test resources for %q test in namespace %s", td.name, namespace)
taskRun, expectedResolvedTaskRun := td.taskRunFunc(t, namespace)
stepAction := td.stepActionFunc(t, namespace)
trName := taskRun.Name
_, err := c.V1beta1StepActionClient.Create(ctx, stepAction, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create StepAction : %s", err)
}
_, err = c.V1TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create TaskRun `%s`: %s", trName, err)
}
t.Logf("Waiting for TaskRun %s in namespace %s to complete", trName, namespace)
if err := WaitForTaskRunState(ctx, c, trName, TaskRunSucceed(trName), "TaskRunSuccess", v1Version); err != nil {
t.Fatalf("Error waiting for TaskRun %s to finish: %s", trName, err)
}
taskrun, _ := c.V1TaskRunClient.Get(ctx, trName, metav1.GetOptions{})
d := cmp.Diff(expectedResolvedTaskRun, taskrun,
ignoreTypeMeta,
ignoreObjectMeta,
ignoreCondition,
ignoreTaskRunStatus,
ignoreContainerStates,
ignoreProvenance,
ignoreSidecarState,
ignoreStepState,
)
if d != "" {
t.Fatalf(`The expected taskrun does not match created taskrun. Here is the diff: %v`, d)
}
t.Logf("Successfully finished test %q", td.name)
})
}
}
func getStepAction(t *testing.T, namespace string) *v1beta1.StepAction {
t.Helper()
return parse.MustParseV1beta1StepAction(t, fmt.Sprintf(`
metadata:
name: step-action
namespace: %s
spec:
results:
- name: result1
type: string
image: alpine
script: |
echo -n step-action >> $(step.results.result1.path)
`, namespace))
}
func getStepActionsTaskRun(t *testing.T, namespace string) (*v1.TaskRun, *v1.TaskRun) {
t.Helper()
taskRun := parse.MustParseV1TaskRun(t, fmt.Sprintf(`
metadata:
name: step-results-task-run
namespace: %s
spec:
taskSpec:
steps:
- name: step1
image: alpine
script: |
echo -n inlined-step >> $(step.results.result1.path)
results:
- name: result1
type: string
- name: step2
ref:
name: step-action
results:
- name: inlined-step-result
type: string
value: $(steps.step1.results.result1)
- name: step-action-result
type: string
value: $(steps.step2.results.result1)
`, namespace))
expectedTaskRun := parse.MustParseV1TaskRun(t, fmt.Sprintf(`
metadata:
name: step-results-task-run
namespace: %s
spec:
serviceAccountName: default
timeout: 1h
taskSpec:
steps:
- name: step1
image: alpine
script: |
echo -n inlined-step >> $(step.results.result1.path)
results:
- name: result1
type: string
- name: step2
ref:
name: step-action
results:
- name: inlined-step-result
type: string
value: $(steps.step1.results.result1)
- name: step-action-result
type: string
value: $(steps.step2.results.result1)
status:
conditions:
- type: "Succeeded"
status: "True"
reason: "Succeeded"
podName: step-results-task-run-pod
taskSpec:
steps:
- name: step1
image: alpine
results:
- name: result1
type: string
script: |
echo -n inlined-step >> /tekton/steps/step-step1/results/result1
- name: step2
image: alpine
results:
- name: result1
type: string
script: |
echo -n step-action >> /tekton/steps/step-step2/results/result1
results:
- name: inlined-step-result
type: string
value: $(steps.step1.results.result1)
- name: step-action-result
type: string
value: $(steps.step2.results.result1)
results:
- name: inlined-step-result
type: string
value: inlined-step
- name: step-action-result
type: string
value: step-action
steps:
- name: step1
container: step-step1
results:
- name: result1
type: string
value: inlined-step
- name: step2
container: step-step2
results:
- name: result1
type: string
value: step-action
`, namespace))
return taskRun, expectedTaskRun
}
func setUpStepActionsResults(ctx context.Context, t *testing.T) (*clients, string) {
t.Helper()
c, ns := setup(ctx, t, requireAllGates(requireEnableStepActionsGate))
configMapData := map[string]string{
"results-from": "termination-message",
}
if err := updateConfigMap(ctx, c.KubeClient, system.Namespace(), config.GetFeatureFlagsConfigName(), configMapData); err != nil {
t.Fatal(err)
}
return c, ns
}
func resetStepActionsResults(ctx context.Context, t *testing.T, c *clients, previousResultExtractionMethod string) {
t.Helper()
if err := updateConfigMap(ctx, c.KubeClient, system.Namespace(), config.GetFeatureFlagsConfigName(), map[string]string{"results-from": previousResultExtractionMethod}); err != nil {
t.Fatal(err)
}
}