@@ -195,21 +195,23 @@ func mkTestingFileServer(files [][2]string) (*httptest.Server, error) {
195195
196196func TestBuild (t * testing.T ) {
197197 for _ , ctx := range testContexts {
198- buildImage (ctx , t )
198+ buildImage (ctx , t , nil , true )
199199 }
200200}
201201
202- func buildImage (context testContextTemplate , t * testing.T ) * Image {
203- runtime , err := newTestRuntime ()
204- if err != nil {
205- t .Fatal (err )
206- }
207- defer nuke (runtime )
202+ func buildImage (context testContextTemplate , t * testing.T , srv * Server , useCache bool ) * Image {
203+ if srv == nil {
204+ runtime , err := newTestRuntime ()
205+ if err != nil {
206+ t .Fatal (err )
207+ }
208+ defer nuke (runtime )
208209
209- srv := & Server {
210- runtime : runtime ,
211- pullingPool : make (map [string ]struct {}),
212- pushingPool : make (map [string ]struct {}),
210+ srv = & Server {
211+ runtime : runtime ,
212+ pullingPool : make (map [string ]struct {}),
213+ pushingPool : make (map [string ]struct {}),
214+ }
213215 }
214216
215217 httpServer , err := mkTestingFileServer (context .remoteFiles )
@@ -224,10 +226,10 @@ func buildImage(context testContextTemplate, t *testing.T) *Image {
224226 }
225227 port := httpServer .URL [idx + 1 :]
226228
227- ip := runtime .networkManager .bridgeNetwork .IP
229+ ip := srv . runtime .networkManager .bridgeNetwork .IP
228230 dockerfile := constructDockerfile (context .dockerfile , ip , port )
229231
230- buildfile := NewBuildFile (srv , ioutil .Discard , false )
232+ buildfile := NewBuildFile (srv , ioutil .Discard , false , useCache )
231233 id , err := buildfile .Build (mkTestContext (dockerfile , context .files , t ))
232234 if err != nil {
233235 t .Fatal (err )
@@ -245,7 +247,7 @@ func TestVolume(t *testing.T) {
245247 from {IMAGE}
246248 volume /test
247249 cmd Hello world
248- ` , nil , nil }, t )
250+ ` , nil , nil }, t , nil , true )
249251
250252 if len (img .Config .Volumes ) == 0 {
251253 t .Fail ()
@@ -261,7 +263,7 @@ func TestBuildMaintainer(t *testing.T) {
261263 img := buildImage (testContextTemplate {`
262264 from {IMAGE}
263265 maintainer dockerio
264- ` , nil , nil }, t )
266+ ` , nil , nil }, t , nil , true )
265267
266268 if img .Author != "dockerio" {
267269 t .Fail ()
@@ -273,7 +275,7 @@ func TestBuildEnv(t *testing.T) {
273275 from {IMAGE}
274276 env port 4243
275277 ` ,
276- nil , nil }, t )
278+ nil , nil }, t , nil , true )
277279 hasEnv := false
278280 for _ , envVar := range img .Config .Env {
279281 if envVar == "port=4243" {
@@ -291,7 +293,7 @@ func TestBuildCmd(t *testing.T) {
291293 from {IMAGE}
292294 cmd ["/bin/echo", "Hello World"]
293295 ` ,
294- nil , nil }, t )
296+ nil , nil }, t , nil , true )
295297
296298 if img .Config .Cmd [0 ] != "/bin/echo" {
297299 t .Log (img .Config .Cmd [0 ])
@@ -308,7 +310,7 @@ func TestBuildExpose(t *testing.T) {
308310 from {IMAGE}
309311 expose 4243
310312 ` ,
311- nil , nil }, t )
313+ nil , nil }, t , nil , true )
312314
313315 if img .Config .PortSpecs [0 ] != "4243" {
314316 t .Fail ()
@@ -320,8 +322,70 @@ func TestBuildEntrypoint(t *testing.T) {
320322 from {IMAGE}
321323 entrypoint ["/bin/echo"]
322324 ` ,
323- nil , nil }, t )
325+ nil , nil }, t , nil , true )
324326
325327 if img .Config .Entrypoint [0 ] != "/bin/echo" {
326328 }
327329}
330+
331+ func TestBuildImageWithCache (t * testing.T ) {
332+ runtime , err := newTestRuntime ()
333+ if err != nil {
334+ t .Fatal (err )
335+ }
336+ defer nuke (runtime )
337+
338+ srv := & Server {
339+ runtime : runtime ,
340+ pullingPool : make (map [string ]struct {}),
341+ pushingPool : make (map [string ]struct {}),
342+ }
343+
344+ template := testContextTemplate {`
345+ from {IMAGE}
346+ maintainer dockerio
347+ ` ,
348+ nil , nil }
349+
350+ img := buildImage (template , t , srv , true )
351+ imageId := img .ID
352+
353+ img = nil
354+ img = buildImage (template , t , srv , true )
355+
356+ if imageId != img .ID {
357+ t .Logf ("Image ids should match: %s != %s" , imageId , img .ID )
358+ t .Fail ()
359+ }
360+ }
361+
362+ func TestBuildImageWithoutCache (t * testing.T ) {
363+ runtime , err := newTestRuntime ()
364+ if err != nil {
365+ t .Fatal (err )
366+ }
367+ defer nuke (runtime )
368+
369+ srv := & Server {
370+ runtime : runtime ,
371+ pullingPool : make (map [string ]struct {}),
372+ pushingPool : make (map [string ]struct {}),
373+ }
374+
375+ template := testContextTemplate {`
376+ from {IMAGE}
377+ maintainer dockerio
378+ ` ,
379+ nil , nil }
380+
381+ img := buildImage (template , t , srv , true )
382+ imageId := img .ID
383+
384+ img = nil
385+ img = buildImage (template , t , srv , false )
386+
387+ if imageId == img .ID {
388+ t .Logf ("Image ids should not match: %s == %s" , imageId , img .ID )
389+ t .Fail ()
390+ }
391+ }
0 commit comments