@@ -20,9 +20,6 @@ package test
20
20
21
21
import (
22
22
"fmt"
23
- "io/ioutil"
24
- "regexp"
25
- "strings"
26
23
"testing"
27
24
"time"
28
25
@@ -34,32 +31,41 @@ import (
34
31
"golang.org/x/xerrors"
35
32
corev1 "k8s.io/api/core/v1"
36
33
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37
- "k8s.io/client-go/kubernetes"
38
34
knativetest "knative.dev/pkg/test"
39
35
)
40
36
41
37
const (
42
- kanikoTaskName = "kanikotask"
43
- kanikoTaskRunName = "kanikotask-run"
44
- kanikoResourceName = "go-example-git"
38
+ kanikoTaskName = "kanikotask"
39
+ kanikoTaskRunName = "kanikotask-run"
40
+ kanikoGitResourceName = "go-example-git"
41
+ kanikoImageResourceName = "go-example-image"
45
42
)
46
43
47
44
func getGitResource (namespace string ) * v1alpha1.PipelineResource {
48
- return tb .PipelineResource (kanikoResourceName , namespace , tb .PipelineResourceSpec (
45
+ return tb .PipelineResource (kanikoGitResourceName , namespace , tb .PipelineResourceSpec (
49
46
v1alpha1 .PipelineResourceTypeGit ,
50
47
tb .PipelineResourceSpecParam ("Url" , "https://github.com/GoogleContainerTools/kaniko" ),
51
48
))
52
49
}
53
50
51
+ func getImageResource (namespace , repo string ) * v1alpha1.PipelineResource {
52
+ return tb .PipelineResource (kanikoImageResourceName , namespace , tb .PipelineResourceSpec (
53
+ v1alpha1 .PipelineResourceTypeImage ,
54
+ tb .PipelineResourceSpecParam ("url" , repo ),
55
+ ))
56
+ }
57
+
54
58
func getTask (repo , namespace string , withSecretConfig bool ) * v1alpha1.Task {
55
59
taskSpecOps := []tb.TaskSpecOp {
56
60
tb .TaskInputs (tb .InputsResource ("gitsource" , v1alpha1 .PipelineResourceTypeGit )),
61
+ tb .TaskOutputs (tb .OutputsResource ("builtImage" , v1alpha1 .PipelineResourceTypeImage )),
57
62
}
58
63
stepOps := []tb.StepOp {
59
64
tb .StepArgs (
60
65
"--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label" ,
61
66
fmt .Sprintf ("--destination=%s" , repo ),
62
67
"--context=/workspace/gitsource" ,
68
+ "--oci-layout-path=/builder/home/image-outputs/builtImage" ,
63
69
),
64
70
}
65
71
if withSecretConfig {
@@ -73,7 +79,7 @@ func getTask(repo, namespace string, withSecretConfig bool) *v1alpha1.Task {
73
79
},
74
80
})))
75
81
}
76
- step := tb .Step ("kaniko" , "gcr.io/kaniko-project/executor:v0.9 .0" , stepOps ... )
82
+ step := tb .Step ("kaniko" , "gcr.io/kaniko-project/executor:v0.13 .0" , stepOps ... )
77
83
taskSpecOps = append (taskSpecOps , step )
78
84
79
85
return tb .Task (kanikoTaskName , namespace , tb .TaskSpec (taskSpecOps ... ))
@@ -83,7 +89,8 @@ func getTaskRun(namespace string) *v1alpha1.TaskRun {
83
89
return tb .TaskRun (kanikoTaskRunName , namespace , tb .TaskRunSpec (
84
90
tb .TaskRunTaskRef (kanikoTaskName ),
85
91
tb .TaskRunTimeout (2 * time .Minute ),
86
- tb .TaskRunInputs (tb .TaskRunInputsResource ("gitsource" , tb .TaskResourceBindingRef (kanikoResourceName ))),
92
+ tb .TaskRunInputs (tb .TaskRunInputsResource ("gitsource" , tb .TaskResourceBindingRef (kanikoGitResourceName ))),
93
+ tb .TaskRunOutputs (tb .TaskRunOutputsResource ("builtImage" , tb .TaskResourceBindingRef (kanikoImageResourceName ))),
87
94
))
88
95
}
89
96
@@ -101,9 +108,14 @@ func TestKanikoTaskRun(t *testing.T) {
101
108
t .Fatalf ("Expected to create kaniko creds: %v" , err )
102
109
}
103
110
104
- t .Logf ("Creating Git PipelineResource %s" , kanikoResourceName )
111
+ t .Logf ("Creating Git PipelineResource %s" , kanikoGitResourceName )
105
112
if _ , err := c .PipelineResourceClient .Create (getGitResource (namespace )); err != nil {
106
- t .Fatalf ("Failed to create Pipeline Resource `%s`: %s" , kanikoResourceName , err )
113
+ t .Fatalf ("Failed to create Pipeline Resource `%s`: %s" , kanikoGitResourceName , err )
114
+ }
115
+
116
+ t .Logf ("Creating Image PipelineResource %s" , repo )
117
+ if _ , err := c .PipelineResourceClient .Create (getImageResource (namespace , repo )); err != nil {
118
+ t .Fatalf ("Failed to create Pipeline Resource `%s`: %s" , kanikoGitResourceName , err )
107
119
}
108
120
109
121
t .Logf ("Creating Task %s" , kanikoTaskName )
@@ -117,32 +129,28 @@ func TestKanikoTaskRun(t *testing.T) {
117
129
}
118
130
119
131
// Verify status of TaskRun (wait for it)
120
- var podName string
132
+
121
133
if err := WaitForTaskRunState (c , kanikoTaskRunName , func (tr * v1alpha1.TaskRun ) (bool , error ) {
122
- podName = tr .Status .PodName
123
134
return TaskRunSucceed (kanikoTaskRunName )(tr )
124
135
}, "TaskRunCompleted" ); err != nil {
125
136
t .Errorf ("Error waiting for TaskRun %s to finish: %s" , kanikoTaskRunName , err )
126
137
}
127
138
128
- // There will be a Pod with the expected name.
129
- if _ , err := c .KubeClient .Kube .CoreV1 ().Pods (namespace ).Get (podName , metav1.GetOptions {}); err != nil {
130
- t .Fatalf ("Error getting build pod: %v" , err )
131
- }
132
-
133
- logs , err := getAllLogsFromPod (c .KubeClient .Kube , podName , namespace )
139
+ tr , err := c .TaskRunClient .Get (kanikoTaskRunName , metav1.GetOptions {})
134
140
if err != nil {
135
- t .Fatalf ("Expected to get logs from pod %s: %v" , podName , err )
141
+ t .Errorf ("Error retrieving taskrun: %s" , err )
142
+ }
143
+ digest := ""
144
+ for _ , rr := range tr .Status .ResourcesResult {
145
+ if rr .Key == "digest" {
146
+ digest = rr .Value
147
+ }
136
148
}
137
- // make sure the pushed digest matches the one we pushed
138
- re := regexp .MustCompile (`digest: (sha256:\w+)` )
139
- match := re .FindStringSubmatch (logs )
140
- // make sure we found a match and it has the capture group
141
- if len (match ) != 2 {
142
- t .Fatalf ("Expected to find an image digest in the build output: %s" , logs )
149
+ if digest == "" {
150
+ t .Errorf ("Digest not found in TaskRun.Status: %v" , tr .Status )
143
151
}
152
+
144
153
// match the local digest, which is first capture group against the remote image
145
- digest := match [1 ]
146
154
remoteDigest , err := getRemoteDigest (repo )
147
155
if err != nil {
148
156
t .Fatalf ("Expected to get digest for remote image %s" , repo )
@@ -152,40 +160,6 @@ func TestKanikoTaskRun(t *testing.T) {
152
160
}
153
161
}
154
162
155
- func getContainerLogs (c kubernetes.Interface , pod , namespace string , containers ... string ) (string , error ) {
156
- sb := strings.Builder {}
157
- for _ , container := range containers {
158
- req := c .CoreV1 ().Pods (namespace ).GetLogs (pod , & corev1.PodLogOptions {Follow : true , Container : container })
159
- rc , err := req .Stream ()
160
- if err != nil {
161
- return "" , err
162
- }
163
- bs , err := ioutil .ReadAll (rc )
164
- if err != nil {
165
- return "" , err
166
- }
167
- sb .Write (bs )
168
- }
169
- return sb .String (), nil
170
- }
171
-
172
- func getAllLogsFromPod (c kubernetes.Interface , pod , namespace string ) (string , error ) {
173
- p , err := c .CoreV1 ().Pods (namespace ).Get (pod , metav1.GetOptions {})
174
- if err != nil {
175
- return "" , err
176
- }
177
-
178
- var containers []string
179
- for _ , initContainer := range p .Spec .InitContainers {
180
- containers = append (containers , initContainer .Name )
181
- }
182
- for _ , container := range p .Spec .Containers {
183
- containers = append (containers , container .Name )
184
- }
185
-
186
- return getContainerLogs (c , pod , namespace , containers ... )
187
- }
188
-
189
163
func getRemoteDigest (image string ) (string , error ) {
190
164
ref , err := name .ParseReference (image , name .WeakValidation )
191
165
if err != nil {
0 commit comments