@@ -18,12 +18,16 @@ package executor
18
18
19
19
import (
20
20
"fmt"
21
+ "net/http/httptest"
22
+ "net/url"
21
23
"os"
22
24
"path/filepath"
23
25
"strings"
24
26
"testing"
25
27
"time"
26
28
29
+ "github.com/google/go-containerregistry/pkg/registry"
30
+
27
31
"github.com/GoogleContainerTools/kaniko/pkg/config"
28
32
"github.com/GoogleContainerTools/kaniko/pkg/constants"
29
33
"github.com/GoogleContainerTools/kaniko/testutil"
@@ -37,8 +41,7 @@ func TestDoCacheProbe(t *testing.T) {
37
41
COPY foo/bar.txt copied/
38
42
`
39
43
os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
40
- // Populate the cache by doing an initial build
41
- cacheDir := t .TempDir ()
44
+ regCache := setupCacheRegistry (t )
42
45
opts := & config.KanikoOptions {
43
46
DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
44
47
SrcContext : filepath .Join (testDir , "workspace" ),
@@ -49,10 +52,10 @@ COPY foo/bar.txt copied/
49
52
},
50
53
CacheCopyLayers : true ,
51
54
CacheRunLayers : true ,
52
- CacheRepo : "oci:/" + cacheDir ,
55
+ CacheRepo : regCache + "/test" ,
53
56
}
54
57
_ , err := DoCacheProbe (opts )
55
- if err == nil || ! strings .Contains (err .Error (), "not supported in fake build " ) {
58
+ if err == nil || ! strings .Contains (err .Error (), "uncached command " ) {
56
59
t .Errorf ("unexpected error, got %v" , err )
57
60
}
58
61
})
@@ -64,7 +67,7 @@ COPY foo/bar.txt copied/
64
67
COPY foo/bar.txt copied/
65
68
`
66
69
os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
67
- cacheDir := t . TempDir ( )
70
+ regCache := setupCacheRegistry ( t )
68
71
opts := & config.KanikoOptions {
69
72
DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
70
73
SrcContext : filepath .Join (testDir , "workspace" ),
@@ -75,8 +78,9 @@ COPY foo/bar.txt copied/
75
78
},
76
79
CacheCopyLayers : true ,
77
80
CacheRunLayers : true ,
78
- CacheRepo : "oci:/" + cacheDir ,
81
+ CacheRepo : regCache + "/test" ,
79
82
}
83
+ // Populate the cache by doing an initial build
80
84
_ , err := DoBuild (opts )
81
85
testutil .CheckNoError (t , err )
82
86
opts .Reproducible = true
@@ -91,18 +95,18 @@ COPY foo/bar.txt copied/
91
95
COPY foo/bar.txt copied/
92
96
`
93
97
os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
94
- cacheDir := t . TempDir ( )
98
+ regCache := setupCacheRegistry ( t )
95
99
opts := & config.KanikoOptions {
96
100
DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
97
101
SrcContext : filepath .Join (testDir , "workspace" ),
98
- SnapshotMode : constants .SnapshotModeFull ,
102
+ SnapshotMode : constants .SnapshotModeRedo ,
99
103
Cache : true ,
100
104
CacheOptions : config.CacheOptions {
101
105
CacheTTL : time .Hour ,
102
106
},
103
107
CacheCopyLayers : true ,
104
108
CacheRunLayers : true ,
105
- CacheRepo : "oci:/" + cacheDir ,
109
+ CacheRepo : regCache + "/test" ,
106
110
}
107
111
_ , err := DoBuild (opts )
108
112
testutil .CheckNoError (t , err )
@@ -115,10 +119,61 @@ COPY foo/baz.txt copied/
115
119
`
116
120
os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
117
121
_ , err = DoCacheProbe (opts )
118
- if err == nil || ! strings .Contains (err .Error (), "not supported in fake build " ) {
122
+ if err == nil || ! strings .Contains (err .Error (), "uncached command " ) {
119
123
t .Errorf ("unexpected error, got %v" , err )
120
124
}
121
125
})
126
+
127
+ t .Run ("MultiStage" , func (t * testing.T ) {
128
+ t .Skip ("TODO: https://github.com/coder/envbuilder/issues/230" )
129
+ testDir , fn := setupMultistageTests (t )
130
+ defer fn ()
131
+ dockerFile := `
132
+ FROM scratch as first
133
+ COPY foo/bam.txt copied/
134
+ ENV test test
135
+
136
+ From scratch as second
137
+ COPY --from=first copied/bam.txt output/bam.txt`
138
+ os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
139
+ regCache := setupCacheRegistry (t )
140
+ opts := & config.KanikoOptions {
141
+ DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
142
+ SrcContext : filepath .Join (testDir , "workspace" ),
143
+ SnapshotMode : constants .SnapshotModeRedo ,
144
+ Cache : true ,
145
+ CacheOptions : config.CacheOptions {
146
+ CacheTTL : time .Hour ,
147
+ },
148
+ CacheCopyLayers : true ,
149
+ CacheRunLayers : true ,
150
+ CacheRepo : regCache + "/test" ,
151
+ }
152
+ _ , err := DoBuild (opts )
153
+ testutil .CheckNoError (t , err )
154
+ os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
155
+ opts .Reproducible = true
156
+ _ , err = DoCacheProbe (opts )
157
+ testutil .CheckNoError (t , err )
158
+ // Check Image has one layer bam.txt
159
+ files , err := readDirectory (filepath .Join (testDir , "output" ))
160
+ if err != nil {
161
+ t .Fatal (err )
162
+ }
163
+ testutil .CheckDeepEqual (t , 1 , len (files ))
164
+ testutil .CheckDeepEqual (t , files [0 ].Name (), "bam.txt" )
165
+ })
166
+ }
167
+
168
+ func setupCacheRegistry (t * testing.T ) string {
169
+ t .Helper ()
170
+ tempDir := t .TempDir ()
171
+ testReg := registry .New (registry .WithBlobHandler (registry .NewDiskBlobHandler (tempDir )))
172
+ regSrv := httptest .NewServer (testReg )
173
+ t .Cleanup (func () { regSrv .Close () })
174
+ regSrvURL , err := url .Parse (regSrv .URL )
175
+ testutil .CheckNoError (t , err )
176
+ return fmt .Sprintf ("localhost:%s" , regSrvURL .Port ())
122
177
}
123
178
124
179
func setupCacheProbeTests (t * testing.T ) (string , func ()) {
0 commit comments