5
5
package integration_test
6
6
7
7
import (
8
+ "context"
8
9
"fmt"
10
+ "time"
9
11
10
12
. "github.com/onsi/ginkgo"
11
13
. "github.com/onsi/gomega"
12
- "knative.dev/pkg/apis"
13
14
14
15
"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
15
16
"github.com/shipwright-io/build/test"
17
+ corev1 "k8s.io/api/core/v1"
18
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
+ "k8s.io/apimachinery/pkg/watch"
20
+ "knative.dev/pkg/apis"
16
21
)
17
22
18
23
var _ = Describe ("Integration tests BuildRuns and TaskRuns" , func () {
@@ -26,7 +31,7 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
26
31
27
32
// Load the ClusterBuildStrategies before each test case
28
33
BeforeEach (func () {
29
- cbsObject , err = tb .Catalog .LoadCBSWithName (STRATEGY + tb .Namespace , []byte (test .ClusterBuildStrategySingleStep ))
34
+ cbsObject , err = tb .Catalog .LoadCBSWithName (STRATEGY + tb .Namespace , []byte (test .ClusterBuildStrategySingleStepKaniko ))
30
35
Expect (err ).To (BeNil ())
31
36
32
37
err = tb .CreateClusterBuildStrategy (cbsObject )
@@ -59,6 +64,174 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
59
64
}
60
65
})
61
66
67
+ Context ("when buildrun uses conditions" , func () {
68
+ var setupBuildAndBuildRun = func (buildDef []byte , buildRunDef []byte , strategy ... string ) (watch.Interface , * v1alpha1.Build , * v1alpha1.BuildRun ) {
69
+
70
+ var strategyName = STRATEGY + tb .Namespace
71
+ if len (strategy ) > 0 {
72
+ strategyName = strategy [0 ]
73
+ }
74
+
75
+ buildRunWitcher , err := tb .BuildClientSet .BuildV1alpha1 ().BuildRuns (tb .Namespace ).Watch (context .TODO (), metav1.ListOptions {})
76
+ Expect (err ).To (BeNil ())
77
+
78
+ buildObject , err = tb .Catalog .LoadBuildWithNameAndStrategy (BUILD + tb .Namespace , strategyName , buildDef )
79
+ Expect (err ).To (BeNil ())
80
+ Expect (tb .CreateBuild (buildObject )).To (BeNil ())
81
+
82
+ buildRunObject , err = tb .Catalog .LoadBRWithNameAndRef (BUILDRUN + tb .Namespace , BUILD + tb .Namespace , buildRunDef )
83
+ Expect (err ).To (BeNil ())
84
+ Expect (tb .CreateBR (buildRunObject )).To (BeNil ())
85
+
86
+ //TODO: consider how to deal with buildObject or buildRunObject
87
+ return buildRunWitcher , buildObject , buildRunObject
88
+ }
89
+
90
+ var WithCustomClusterBuildStrategy = func (data []byte , f func ()) {
91
+ customClusterBuildStrategy , err := tb .Catalog .LoadCBSWithName (STRATEGY + tb .Namespace + "custom" , data )
92
+ Expect (err ).To (BeNil ())
93
+
94
+ Expect (tb .CreateClusterBuildStrategy (customClusterBuildStrategy )).To (BeNil ())
95
+ f ()
96
+ Expect (tb .DeleteClusterBuildStrategy (customClusterBuildStrategy .Name )).To (BeNil ())
97
+ }
98
+
99
+ Context ("when condition status unknown" , func () {
100
+ It ("reflects a change from pending to running reason" , func () {
101
+ buildRunWitcher , _ , _ := setupBuildAndBuildRun ([]byte (test .BuildCBSMinimal ), []byte (test .MinimalBuildRun ))
102
+
103
+ var timeout = time .After (tb .TimeOut )
104
+ go func () {
105
+ <- timeout
106
+ buildRunWitcher .Stop ()
107
+ }()
108
+
109
+ var seq = []* v1alpha1.Condition {}
110
+ for event := range buildRunWitcher .ResultChan () {
111
+ condition := event .Object .(* v1alpha1.BuildRun ).Status .GetCondition (v1alpha1 .Succeeded )
112
+ if condition != nil {
113
+ seq = append (seq , condition )
114
+ }
115
+
116
+ // Pending -> Running
117
+ if condition != nil && condition .Reason == "Running" {
118
+ buildRunWitcher .Stop ()
119
+ }
120
+ }
121
+
122
+ Expect (len (seq )).To (Equal (2 ))
123
+ Expect (seq [0 ].Type ).To (Equal (v1alpha1 .Succeeded ))
124
+ Expect (seq [0 ].Status ).To (Equal (corev1 .ConditionUnknown ))
125
+ Expect (seq [0 ].Reason ).To (Equal ("Pending" ))
126
+ Expect (seq [1 ].Type ).To (Equal (v1alpha1 .Succeeded ))
127
+ Expect (seq [1 ].Reason ).To (Equal ("Running" ))
128
+ })
129
+ })
130
+
131
+ Context ("when condition status is false" , func () {
132
+ It ("reflects a timeout" , func () {
133
+ buildRunWitcher , build , buildRun := setupBuildAndBuildRun ([]byte (test .BuildCBSWithShortTimeOut ), []byte (test .MinimalBuildRun ))
134
+
135
+ var timeout = time .After (tb .TimeOut )
136
+ go func () {
137
+ <- timeout
138
+ buildRunWitcher .Stop ()
139
+ }()
140
+
141
+ var seq = []* v1alpha1.Condition {}
142
+ for event := range buildRunWitcher .ResultChan () {
143
+ condition := event .Object .(* v1alpha1.BuildRun ).Status .GetCondition (v1alpha1 .Succeeded )
144
+ if condition != nil {
145
+ seq = append (seq , condition )
146
+ }
147
+
148
+ // Pending -> Running
149
+ if condition != nil && condition .Status == corev1 .ConditionFalse {
150
+ buildRunWitcher .Stop ()
151
+ }
152
+ }
153
+
154
+ lastIdx := len (seq ) - 1
155
+ Expect (lastIdx ).To (BeNumerically (">" , 0 ))
156
+ Expect (seq [lastIdx ].Type ).To (Equal (v1alpha1 .Succeeded ))
157
+ Expect (seq [lastIdx ].Status ).To (Equal (corev1 .ConditionFalse ))
158
+ Expect (seq [lastIdx ].Reason ).To (Equal ("BuildRunTimeout" ))
159
+ Expect (seq [lastIdx ].Message ).To (Equal (fmt .Sprintf ("BuildRun %s failed to finish within %v" , buildRun .Name , build .Spec .Timeout .Duration )))
160
+ })
161
+
162
+ It ("reflects a failed reason" , func () {
163
+ WithCustomClusterBuildStrategy ([]byte (test .ClusterBuildStrategySingleStepKanikoError ), func () {
164
+ buildRunWitcher , _ , buildRun := setupBuildAndBuildRun ([]byte (test .BuildCBSMinimal ), []byte (test .MinimalBuildRun ), STRATEGY + tb .Namespace + "custom" )
165
+
166
+ var timeout = time .After (tb .TimeOut )
167
+ go func () {
168
+ <- timeout
169
+ buildRunWitcher .Stop ()
170
+ }()
171
+
172
+ var seq = []* v1alpha1.Condition {}
173
+ for event := range buildRunWitcher .ResultChan () {
174
+ condition := event .Object .(* v1alpha1.BuildRun ).Status .GetCondition (v1alpha1 .Succeeded )
175
+ if condition != nil {
176
+ seq = append (seq , condition )
177
+ }
178
+
179
+ if condition != nil && condition .Status == corev1 .ConditionFalse {
180
+ buildRunWitcher .Stop ()
181
+ }
182
+ }
183
+
184
+ buildRun , err = tb .GetBR (buildRun .Name )
185
+ Expect (err ).ToNot (HaveOccurred ())
186
+ Expect (buildRun .Status .CompletionTime ).ToNot (BeNil ())
187
+
188
+ taskRun , err := tb .GetTaskRunFromBuildRun (buildRun .Name )
189
+ Expect (err ).ToNot (HaveOccurred ())
190
+
191
+ lastIdx := len (seq ) - 1
192
+ Expect (lastIdx ).To (BeNumerically (">" , 0 ))
193
+ Expect (seq [lastIdx ].Type ).To (Equal (v1alpha1 .Succeeded ))
194
+ Expect (seq [lastIdx ].Status ).To (Equal (corev1 .ConditionFalse ))
195
+ Expect (seq [lastIdx ].Reason ).To (Equal ("Failed" ))
196
+ Expect (seq [lastIdx ].Message ).To (ContainSubstring ("buildrun step failed in pod %s" , taskRun .Status .PodName ))
197
+ })
198
+ })
199
+ })
200
+
201
+ Context ("when condition status true" , func () {
202
+ It ("should reflect the taskrun succeeded reason in the buildrun condition" , func () {
203
+ WithCustomClusterBuildStrategy ([]byte (test .ClusterBuildStrategyNoOp ), func () {
204
+ buildRunWitcher , _ , _ := setupBuildAndBuildRun ([]byte (test .BuildCBSMinimal ), []byte (test .MinimalBuildRun ), STRATEGY + tb .Namespace + "custom" )
205
+
206
+ var timeout = time .After (tb .TimeOut )
207
+ go func () {
208
+ <- timeout
209
+ buildRunWitcher .Stop ()
210
+ }()
211
+
212
+ var seq = []* v1alpha1.Condition {}
213
+ for event := range buildRunWitcher .ResultChan () {
214
+ condition := event .Object .(* v1alpha1.BuildRun ).Status .GetCondition (v1alpha1 .Succeeded )
215
+ if condition != nil {
216
+ seq = append (seq , condition )
217
+ }
218
+
219
+ if condition != nil && condition .Status == corev1 .ConditionTrue {
220
+ buildRunWitcher .Stop ()
221
+ }
222
+ }
223
+
224
+ lastIdx := len (seq ) - 1
225
+ Expect (lastIdx ).To (BeNumerically (">" , 0 ))
226
+ Expect (seq [lastIdx ].Type ).To (Equal (v1alpha1 .Succeeded ))
227
+ Expect (seq [lastIdx ].Status ).To (Equal (corev1 .ConditionTrue ))
228
+ Expect (seq [lastIdx ].Reason ).To (Equal ("Succeeded" ))
229
+ Expect (seq [lastIdx ].Message ).To (ContainSubstring ("All Steps have completed executing" ))
230
+ })
231
+ })
232
+ })
233
+ })
234
+
62
235
Context ("when a buildrun is created" , func () {
63
236
64
237
BeforeEach (func () {
0 commit comments