@@ -20,9 +20,6 @@ package test
2020
2121import (
2222 "fmt"
23- "io/ioutil"
24- "regexp"
25- "strings"
2623 "testing"
2724 "time"
2825
@@ -34,32 +31,41 @@ import (
3431 "golang.org/x/xerrors"
3532 corev1 "k8s.io/api/core/v1"
3633 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37- "k8s.io/client-go/kubernetes"
3834 knativetest "knative.dev/pkg/test"
3935)
4036
4137const (
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"
4542)
4643
4744func getGitResource (namespace string ) * v1alpha1.PipelineResource {
48- return tb .PipelineResource (kanikoResourceName , namespace , tb .PipelineResourceSpec (
45+ return tb .PipelineResource (kanikoGitResourceName , namespace , tb .PipelineResourceSpec (
4946 v1alpha1 .PipelineResourceTypeGit ,
5047 tb .PipelineResourceSpecParam ("Url" , "https://github.com/GoogleContainerTools/kaniko" ),
5148 ))
5249}
5350
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+
5458func getTask (repo , namespace string , withSecretConfig bool ) * v1alpha1.Task {
5559 taskSpecOps := []tb.TaskSpecOp {
5660 tb .TaskInputs (tb .InputsResource ("gitsource" , v1alpha1 .PipelineResourceTypeGit )),
61+ tb .TaskOutputs (tb .OutputsResource ("builtImage" , v1alpha1 .PipelineResourceTypeImage )),
5762 }
5863 stepOps := []tb.StepOp {
5964 tb .StepArgs (
6065 "--dockerfile=/workspace/gitsource/integration/dockerfiles/Dockerfile_test_label" ,
6166 fmt .Sprintf ("--destination=%s" , repo ),
6267 "--context=/workspace/gitsource" ,
68+ "--oci-layout-path=/builder/home/image-outputs/builtImage" ,
6369 ),
6470 }
6571 if withSecretConfig {
@@ -73,7 +79,7 @@ func getTask(repo, namespace string, withSecretConfig bool) *v1alpha1.Task {
7379 },
7480 })))
7581 }
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 ... )
7783 taskSpecOps = append (taskSpecOps , step )
7884
7985 return tb .Task (kanikoTaskName , namespace , tb .TaskSpec (taskSpecOps ... ))
@@ -83,7 +89,8 @@ func getTaskRun(namespace string) *v1alpha1.TaskRun {
8389 return tb .TaskRun (kanikoTaskRunName , namespace , tb .TaskRunSpec (
8490 tb .TaskRunTaskRef (kanikoTaskName ),
8591 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 ))),
8794 ))
8895}
8996
@@ -101,9 +108,14 @@ func TestKanikoTaskRun(t *testing.T) {
101108 t .Fatalf ("Expected to create kaniko creds: %v" , err )
102109 }
103110
104- t .Logf ("Creating Git PipelineResource %s" , kanikoResourceName )
111+ t .Logf ("Creating Git PipelineResource %s" , kanikoGitResourceName )
105112 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 )
107119 }
108120
109121 t .Logf ("Creating Task %s" , kanikoTaskName )
@@ -117,32 +129,28 @@ func TestKanikoTaskRun(t *testing.T) {
117129 }
118130
119131 // Verify status of TaskRun (wait for it)
120- var podName string
132+
121133 if err := WaitForTaskRunState (c , kanikoTaskRunName , func (tr * v1alpha1.TaskRun ) (bool , error ) {
122- podName = tr .Status .PodName
123134 return TaskRunSucceed (kanikoTaskRunName )(tr )
124135 }, "TaskRunCompleted" ); err != nil {
125136 t .Errorf ("Error waiting for TaskRun %s to finish: %s" , kanikoTaskRunName , err )
126137 }
127138
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 {})
134140 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+ }
136148 }
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 )
143151 }
152+
144153 // match the local digest, which is first capture group against the remote image
145- digest := match [1 ]
146154 remoteDigest , err := getRemoteDigest (repo )
147155 if err != nil {
148156 t .Fatalf ("Expected to get digest for remote image %s" , repo )
@@ -152,40 +160,6 @@ func TestKanikoTaskRun(t *testing.T) {
152160 }
153161}
154162
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-
189163func getRemoteDigest (image string ) (string , error ) {
190164 ref , err := name .ParseReference (image , name .WeakValidation )
191165 if err != nil {
0 commit comments