forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_checkout_test.go
365 lines (310 loc) · 14.5 KB
/
git_checkout_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// +build e2e
/*
Copyright 2019 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 (
"strings"
"testing"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
tb "github.com/tektoncd/pipeline/test/builder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
)
const (
gitSourceResourceName = "git-source-resource"
gitTestTaskName = "git-check-task"
gitTestPipelineName = "git-check-pipeline"
gitTestPipelineRunName = "git-check-pipeline-run"
)
// TestGitPipelineRun is an integration test that will verify the source code is either fetched or pulled
// successfully under different revision inputs (branch, commitid, tag, ref)
func TestGitPipelineRun(t *testing.T) {
t.Parallel()
revisions := []string{"master", "c15aced0e5aaee6456fbe6f7a7e95e0b5b3b2b2f", "release-0.1", "v0.1.0", "refs/pull/347/head"}
for _, revision := range revisions {
t.Run(revision, func(t *testing.T) {
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)
t.Logf("Creating Git PipelineResource %s", gitSourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResource(revision, "", "true", "", "", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}
t.Logf("Creating Task %s", gitTestTaskName)
if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}
t.Logf("Creating Pipeline %s", gitTestPipelineName)
if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}
t.Logf("Creating PipelineRun %s", gitTestPipelineRunName)
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}
if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", gitTestPipelineRunName, err)
t.Fatalf("PipelineRun execution failed")
}
})
}
}
// Test revision fetching with refspec specified
func TestGitPipelineRunWithRefspec(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
description string
revision string
refspec string
}{{
description: "Fetch refs/tags/v0.1.0 alongside master and checkout the master branch",
revision: "master",
refspec: "refs/tags/v0.1.0:refs/tags/v0.1.0 refs/heads/master:refs/heads/master",
}, {
description: "Checkout specific revision from refs/pull/1009/head's commit chain",
revision: "968d5d37a61bfb85426c885dc1090c1cc4b33436",
refspec: "refs/pull/1009/head",
}, {
description: "Fetch refs/pull/1009/head into a named master branch and then check it out",
revision: "master",
refspec: "refs/pull/1009/head:refs/heads/master",
}} {
t.Run(tc.description, func(t *testing.T) {
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResource(tc.revision, tc.refspec, "true", "", "", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}
if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}
if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}
if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", gitTestPipelineRunName, err)
t.Fatalf("PipelineRun execution failed")
}
})
}
}
// TestGitPipelineRun_Disable_SSLVerify will verify the source code is retrieved even after disabling SSL certificates (sslVerify)
func TestGitPipelineRun_Disable_SSLVerify(t *testing.T) {
t.Parallel()
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)
t.Logf("Creating Git PipelineResource %s", gitSourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResource("master", "", "false", "", "", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}
t.Logf("Creating Task %s", gitTestTaskName)
if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}
t.Logf("Creating Pipeline %s", gitTestPipelineName)
if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}
t.Logf("Creating PipelineRun %s", gitTestPipelineRunName)
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}
if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", gitTestPipelineRunName, err)
t.Fatalf("PipelineRun execution failed")
}
}
// TestGitPipelineRunFail is a test to ensure that the code extraction from github fails as expected when
// an invalid revision is passed on the pipelineresource.
func TestGitPipelineRunFail(t *testing.T) {
t.Parallel()
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)
t.Logf("Creating Git PipelineResource %s", gitSourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResource("Idontexistrabbitmonkeydonkey", "", "true", "", "", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}
t.Logf("Creating Task %s", gitTestTaskName)
if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}
t.Logf("Creating Pipeline %s", gitTestPipelineName)
if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}
t.Logf("Creating PipelineRun %s", gitTestPipelineRunName)
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}
if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
taskruns, err := c.TaskRunClient.List(metav1.ListOptions{})
if err != nil {
t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err)
}
for _, tr := range taskruns.Items {
if tr.Status.PodName != "" {
p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
}
for _, stat := range p.Status.ContainerStatuses {
if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) {
if stat.State.Terminated != nil {
req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name})
logContent, err := req.Do().Raw()
if err != nil {
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace)
}
// Check for failure messages from fetch and pull in the log file
if strings.Contains(strings.ToLower(string(logContent)), "couldn't find remote ref idontexistrabbitmonkeydonkey") {
t.Logf("Found exepected errors when retrieving non-existent git revision")
} else {
t.Logf("Container `%s` log File: %s", stat.Name, logContent)
t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.")
}
}
}
}
}
}
} else {
t.Fatalf("PipelineRun succeeded when should have failed")
}
}
// TestGitPipelineRunFail_HTTPS_PROXY is a test to ensure that the code extraction from github fails as expected when
// an invalid HTTPS_PROXY is passed on the pipelineresource.
func TestGitPipelineRunFail_HTTPS_PROXY(t *testing.T) {
t.Parallel()
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)
t.Logf("Creating Git PipelineResource %s", gitSourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResource("master", "", "true", "", "invalid.https.proxy.com", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}
t.Logf("Creating Task %s", gitTestTaskName)
if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}
t.Logf("Creating Pipeline %s", gitTestPipelineName)
if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}
t.Logf("Creating PipelineRun %s", gitTestPipelineRunName)
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}
if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
taskruns, err := c.TaskRunClient.List(metav1.ListOptions{})
if err != nil {
t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err)
}
for _, tr := range taskruns.Items {
if tr.Status.PodName != "" {
p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
}
for _, stat := range p.Status.ContainerStatuses {
if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) {
if stat.State.Terminated != nil {
req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name})
logContent, err := req.Do().Raw()
if err != nil {
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace)
}
// Check for failure messages from fetch and pull in the log file
if strings.Contains(strings.ToLower(string(logContent)), "could not resolve proxy: invalid.https.proxy.com") {
t.Logf("Found exepected errors when using non-existent https proxy")
} else {
t.Logf("Container `%s` log File: %s", stat.Name, logContent)
t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.")
}
}
}
}
}
}
} else {
t.Fatalf("PipelineRun succeeded when should have failed")
}
}
func getGitPipelineResource(revision, refspec, sslverify, httpproxy, httpsproxy, noproxy string) *v1alpha1.PipelineResource {
return tb.PipelineResource(gitSourceResourceName, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("Url", "https://github.com/tektoncd/pipeline"),
tb.PipelineResourceSpecParam("Revision", revision),
tb.PipelineResourceSpecParam("Refspec", refspec),
tb.PipelineResourceSpecParam("sslVerify", sslverify),
tb.PipelineResourceSpecParam("httpProxy", httpproxy),
tb.PipelineResourceSpecParam("httpsProxy", httpsproxy),
tb.PipelineResourceSpecParam("noProxy", noproxy),
))
}
func getGitCheckTask(namespace string) *v1beta1.Task {
return &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: gitTestTaskName, Namespace: namespace},
Spec: v1beta1.TaskSpec{
Resources: &v1beta1.TaskResources{
Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{
Name: "gitsource", Type: resources.PipelineResourceTypeGit,
}}},
},
Steps: []v1beta1.Step{{Container: corev1.Container{
Image: "alpine/git",
Args: []string{"--git-dir=/workspace/gitsource/.git", "show"},
}}},
},
}
}
func getGitCheckPipeline(namespace string) *v1beta1.Pipeline {
return &v1beta1.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineName, Namespace: namespace},
Spec: v1beta1.PipelineSpec{
Resources: []v1beta1.PipelineDeclaredResource{{
Name: "git-repo", Type: resources.PipelineResourceTypeGit,
}},
Tasks: []v1beta1.PipelineTask{{
Name: "git-check",
TaskRef: &v1beta1.TaskRef{Name: gitTestTaskName},
Resources: &v1beta1.PipelineTaskResources{
Inputs: []v1beta1.PipelineTaskInputResource{{
Name: "gitsource",
Resource: "git-repo",
}},
},
}},
},
}
}
func getGitCheckPipelineRun(namespace string) *v1beta1.PipelineRun {
return &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName, Namespace: namespace},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: &v1beta1.PipelineRef{Name: gitTestPipelineName},
Resources: []v1beta1.PipelineResourceBinding{{
Name: "git-repo",
ResourceRef: &v1beta1.PipelineResourceRef{Name: gitSourceResourceName},
}},
},
}
}