Browse code

Move build cache tests to integration-cli

Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)

LK4D4 authored on 2014/06/01 05:49:50
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+
5 6
 	"os"
6 7
 	"os/exec"
7 8
 	"path/filepath"
... ...
@@ -765,6 +766,338 @@ func TestBuildEntrypoint(t *testing.T) {
765 765
 	logDone("build - entrypoint")
766 766
 }
767 767
 
768
-// TODO: TestCaching
768
+func TestBuildWithCache(t *testing.T) {
769
+	name := "testbuildwithcache"
770
+	defer deleteImages(name)
771
+	id1, err := buildImage(name,
772
+		`FROM scratch
773
+		MAINTAINER dockerio
774
+		EXPOSE 5432
775
+        ENTRYPOINT ["/bin/echo"]`,
776
+		true)
777
+	if err != nil {
778
+		t.Fatal(err)
779
+	}
780
+	id2, err := buildImage(name,
781
+		`FROM scratch
782
+		MAINTAINER dockerio
783
+		EXPOSE 5432
784
+        ENTRYPOINT ["/bin/echo"]`,
785
+		true)
786
+	if err != nil {
787
+		t.Fatal(err)
788
+	}
789
+	if id1 != id2 {
790
+		t.Fatal("The cache should have been used but hasn't.")
791
+	}
792
+	logDone("build - with cache")
793
+}
769 794
 
770
-// TODO: TestADDCacheInvalidation
795
+func TestBuildWithoutCache(t *testing.T) {
796
+	name := "testbuildwithoutcache"
797
+	defer deleteImages(name)
798
+	id1, err := buildImage(name,
799
+		`FROM scratch
800
+		MAINTAINER dockerio
801
+		EXPOSE 5432
802
+        ENTRYPOINT ["/bin/echo"]`,
803
+		true)
804
+	if err != nil {
805
+		t.Fatal(err)
806
+	}
807
+	id2, err := buildImage(name,
808
+		`FROM scratch
809
+		MAINTAINER dockerio
810
+		EXPOSE 5432
811
+        ENTRYPOINT ["/bin/echo"]`,
812
+		false)
813
+	if err != nil {
814
+		t.Fatal(err)
815
+	}
816
+	if id1 == id2 {
817
+		t.Fatal("The cache should have been invalided but hasn't.")
818
+	}
819
+	logDone("build - without cache")
820
+}
821
+
822
+func TestBuildADDLocalFileWithCache(t *testing.T) {
823
+	name := "testbuildaddlocalfilewithcache"
824
+	defer deleteImages(name)
825
+	dockerfile := `
826
+		FROM busybox
827
+        MAINTAINER dockerio
828
+        ADD foo /usr/lib/bla/bar
829
+		RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
830
+	ctx, err := fakeContext(dockerfile, map[string]string{
831
+		"foo": "hello",
832
+	})
833
+	defer ctx.Close()
834
+	if err != nil {
835
+		t.Fatal(err)
836
+	}
837
+	id1, err := buildImageFromContext(name, ctx, true)
838
+	if err != nil {
839
+		t.Fatal(err)
840
+	}
841
+	id2, err := buildImageFromContext(name, ctx, true)
842
+	if err != nil {
843
+		t.Fatal(err)
844
+	}
845
+	if id1 != id2 {
846
+		t.Fatal("The cache should have been used but hasn't.")
847
+	}
848
+	logDone("build - add local file with cache")
849
+}
850
+
851
+func TestBuildADDLocalFileWithoutCache(t *testing.T) {
852
+	name := "testbuildaddlocalfilewithoutcache"
853
+	defer deleteImages(name)
854
+	dockerfile := `
855
+		FROM busybox
856
+        MAINTAINER dockerio
857
+        ADD foo /usr/lib/bla/bar
858
+		RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
859
+	ctx, err := fakeContext(dockerfile, map[string]string{
860
+		"foo": "hello",
861
+	})
862
+	defer ctx.Close()
863
+	if err != nil {
864
+		t.Fatal(err)
865
+	}
866
+	id1, err := buildImageFromContext(name, ctx, true)
867
+	if err != nil {
868
+		t.Fatal(err)
869
+	}
870
+	id2, err := buildImageFromContext(name, ctx, false)
871
+	if err != nil {
872
+		t.Fatal(err)
873
+	}
874
+	if id1 == id2 {
875
+		t.Fatal("The cache should have been invalided but hasn't.")
876
+	}
877
+	logDone("build - add local file without cache")
878
+}
879
+
880
+func TestBuildADDCurrentDirWithCache(t *testing.T) {
881
+	name := "testbuildaddcurrentdirwithcache"
882
+	defer deleteImages(name)
883
+	dockerfile := `
884
+        FROM scratch
885
+        MAINTAINER dockerio
886
+        ADD . /usr/lib/bla`
887
+	ctx, err := fakeContext(dockerfile, map[string]string{
888
+		"foo": "hello",
889
+	})
890
+	defer ctx.Close()
891
+	if err != nil {
892
+		t.Fatal(err)
893
+	}
894
+	id1, err := buildImageFromContext(name, ctx, true)
895
+	if err != nil {
896
+		t.Fatal(err)
897
+	}
898
+	// Check that adding file invalidate cache of "ADD ."
899
+	if err := ctx.Add("bar", "hello2"); err != nil {
900
+		t.Fatal(err)
901
+	}
902
+	id2, err := buildImageFromContext(name, ctx, true)
903
+	if err != nil {
904
+		t.Fatal(err)
905
+	}
906
+	if id1 == id2 {
907
+		t.Fatal("The cache should have been invalided but hasn't.")
908
+	}
909
+	// Check that changing file invalidate cache of "ADD ."
910
+	if err := ctx.Add("foo", "hello1"); err != nil {
911
+		t.Fatal(err)
912
+	}
913
+	id3, err := buildImageFromContext(name, ctx, true)
914
+	if err != nil {
915
+		t.Fatal(err)
916
+	}
917
+	if id2 == id3 {
918
+		t.Fatal("The cache should have been invalided but hasn't.")
919
+	}
920
+	// Check that changing file to same content invalidate cache of "ADD ."
921
+	time.Sleep(1 * time.Second) // wait second because of mtime precision
922
+	if err := ctx.Add("foo", "hello1"); err != nil {
923
+		t.Fatal(err)
924
+	}
925
+	id4, err := buildImageFromContext(name, ctx, true)
926
+	if err != nil {
927
+		t.Fatal(err)
928
+	}
929
+	if id3 == id4 {
930
+		t.Fatal("The cache should have been invalided but hasn't.")
931
+	}
932
+	id5, err := buildImageFromContext(name, ctx, true)
933
+	if err != nil {
934
+		t.Fatal(err)
935
+	}
936
+	if id4 != id5 {
937
+		t.Fatal("The cache should have been used but hasn't.")
938
+	}
939
+	logDone("build - add current directory with cache")
940
+}
941
+
942
+func TestBuildADDCurrentDirWithoutCache(t *testing.T) {
943
+	name := "testbuildaddcurrentdirwithoutcache"
944
+	defer deleteImages(name)
945
+	dockerfile := `
946
+        FROM scratch
947
+        MAINTAINER dockerio
948
+        ADD . /usr/lib/bla`
949
+	ctx, err := fakeContext(dockerfile, map[string]string{
950
+		"foo": "hello",
951
+	})
952
+	defer ctx.Close()
953
+	if err != nil {
954
+		t.Fatal(err)
955
+	}
956
+	id1, err := buildImageFromContext(name, ctx, true)
957
+	if err != nil {
958
+		t.Fatal(err)
959
+	}
960
+	id2, err := buildImageFromContext(name, ctx, false)
961
+	if err != nil {
962
+		t.Fatal(err)
963
+	}
964
+	if id1 == id2 {
965
+		t.Fatal("The cache should have been invalided but hasn't.")
966
+	}
967
+	logDone("build - add current directory without cache")
968
+}
969
+
970
+func TestBuildADDRemoteFileWithCache(t *testing.T) {
971
+	name := "testbuildaddremotefilewithcache"
972
+	defer deleteImages(name)
973
+	server, err := fakeStorage(map[string]string{
974
+		"baz": "hello",
975
+	})
976
+	if err != nil {
977
+		t.Fatal(err)
978
+	}
979
+	defer server.Close()
980
+	id1, err := buildImage(name,
981
+		fmt.Sprintf(`FROM scratch
982
+        MAINTAINER dockerio
983
+        ADD %s/baz /usr/lib/baz/quux`, server.URL),
984
+		true)
985
+	if err != nil {
986
+		t.Fatal(err)
987
+	}
988
+	id2, err := buildImage(name,
989
+		fmt.Sprintf(`FROM scratch
990
+        MAINTAINER dockerio
991
+        ADD %s/baz /usr/lib/baz/quux`, server.URL),
992
+		true)
993
+	if err != nil {
994
+		t.Fatal(err)
995
+	}
996
+	if id1 != id2 {
997
+		t.Fatal("The cache should have been used but hasn't.")
998
+	}
999
+	logDone("build - add remote file with cache")
1000
+}
1001
+
1002
+func TestBuildADDRemoteFileWithoutCache(t *testing.T) {
1003
+	name := "testbuildaddremotefilewithoutcache"
1004
+	defer deleteImages(name)
1005
+	server, err := fakeStorage(map[string]string{
1006
+		"baz": "hello",
1007
+	})
1008
+	if err != nil {
1009
+		t.Fatal(err)
1010
+	}
1011
+	defer server.Close()
1012
+	id1, err := buildImage(name,
1013
+		fmt.Sprintf(`FROM scratch
1014
+        MAINTAINER dockerio
1015
+        ADD %s/baz /usr/lib/baz/quux`, server.URL),
1016
+		true)
1017
+	if err != nil {
1018
+		t.Fatal(err)
1019
+	}
1020
+	id2, err := buildImage(name,
1021
+		fmt.Sprintf(`FROM scratch
1022
+        MAINTAINER dockerio
1023
+        ADD %s/baz /usr/lib/baz/quux`, server.URL),
1024
+		false)
1025
+	if err != nil {
1026
+		t.Fatal(err)
1027
+	}
1028
+	if id1 == id2 {
1029
+		t.Fatal("The cache should have been invalided but hasn't.")
1030
+	}
1031
+	logDone("build - add remote file without cache")
1032
+}
1033
+
1034
+func TestBuildADDLocalAndRemoteFilesWithCache(t *testing.T) {
1035
+	name := "testbuildaddlocalandremotefilewithcache"
1036
+	defer deleteImages(name)
1037
+	server, err := fakeStorage(map[string]string{
1038
+		"baz": "hello",
1039
+	})
1040
+	if err != nil {
1041
+		t.Fatal(err)
1042
+	}
1043
+	defer server.Close()
1044
+	ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
1045
+        MAINTAINER dockerio
1046
+        ADD foo /usr/lib/bla/bar
1047
+        ADD %s/baz /usr/lib/baz/quux`, server.URL),
1048
+		map[string]string{
1049
+			"foo": "hello world",
1050
+		})
1051
+	if err != nil {
1052
+		t.Fatal(err)
1053
+	}
1054
+	defer ctx.Close()
1055
+	id1, err := buildImageFromContext(name, ctx, true)
1056
+	if err != nil {
1057
+		t.Fatal(err)
1058
+	}
1059
+	id2, err := buildImageFromContext(name, ctx, true)
1060
+	if err != nil {
1061
+		t.Fatal(err)
1062
+	}
1063
+	if id1 != id2 {
1064
+		t.Fatal("The cache should have been used but hasn't.")
1065
+	}
1066
+	logDone("build - add local and remote file with cache")
1067
+}
1068
+
1069
+func TestBuildADDLocalAndRemoteFilesWithoutCache(t *testing.T) {
1070
+	name := "testbuildaddlocalandremotefilewithoutcache"
1071
+	defer deleteImages(name)
1072
+	server, err := fakeStorage(map[string]string{
1073
+		"baz": "hello",
1074
+	})
1075
+	if err != nil {
1076
+		t.Fatal(err)
1077
+	}
1078
+	defer server.Close()
1079
+	ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
1080
+        MAINTAINER dockerio
1081
+        ADD foo /usr/lib/bla/bar
1082
+        ADD %s/baz /usr/lib/baz/quux`, server.URL),
1083
+		map[string]string{
1084
+			"foo": "hello world",
1085
+		})
1086
+	if err != nil {
1087
+		t.Fatal(err)
1088
+	}
1089
+	defer ctx.Close()
1090
+	id1, err := buildImageFromContext(name, ctx, true)
1091
+	if err != nil {
1092
+		t.Fatal(err)
1093
+	}
1094
+	id2, err := buildImageFromContext(name, ctx, false)
1095
+	if err != nil {
1096
+		t.Fatal(err)
1097
+	}
1098
+	if id1 == id2 {
1099
+		t.Fatal("The cache should have been invalided but hasn't.")
1100
+	}
1101
+	logDone("build - add local and remote file without cache")
1102
+}
... ...
@@ -445,226 +445,6 @@ func TestBuildEntrypointRunCleanup(t *testing.T) {
445 445
 	}
446 446
 }
447 447
 
448
-func checkCacheBehavior(t *testing.T, template testContextTemplate, expectHit bool) (imageId string) {
449
-	eng := NewTestEngine(t)
450
-	defer nuke(mkDaemonFromEngine(eng, t))
451
-
452
-	img, err := buildImage(template, t, eng, true)
453
-	if err != nil {
454
-		t.Fatal(err)
455
-	}
456
-
457
-	imageId = img.ID
458
-
459
-	img, err = buildImage(template, t, eng, expectHit)
460
-	if err != nil {
461
-		t.Fatal(err)
462
-	}
463
-
464
-	if hit := imageId == img.ID; hit != expectHit {
465
-		t.Fatalf("Cache misbehavior, got hit=%t, expected hit=%t: (first: %s, second %s)", hit, expectHit, imageId, img.ID)
466
-	}
467
-	return
468
-}
469
-
470
-func checkCacheBehaviorFromEngime(t *testing.T, template testContextTemplate, expectHit bool, eng *engine.Engine) (imageId string) {
471
-	img, err := buildImage(template, t, eng, true)
472
-	if err != nil {
473
-		t.Fatal(err)
474
-	}
475
-
476
-	imageId = img.ID
477
-
478
-	img, err = buildImage(template, t, eng, expectHit)
479
-	if err != nil {
480
-		t.Fatal(err)
481
-	}
482
-
483
-	if hit := imageId == img.ID; hit != expectHit {
484
-		t.Fatalf("Cache misbehavior, got hit=%t, expected hit=%t: (first: %s, second %s)", hit, expectHit, imageId, img.ID)
485
-	}
486
-	return
487
-}
488
-
489
-func TestBuildImageWithCache(t *testing.T) {
490
-	template := testContextTemplate{`
491
-        from {IMAGE}
492
-        maintainer dockerio
493
-        `,
494
-		nil, nil}
495
-	checkCacheBehavior(t, template, true)
496
-}
497
-
498
-func TestBuildExposeWithCache(t *testing.T) {
499
-	template := testContextTemplate{`
500
-        from {IMAGE}
501
-        maintainer dockerio
502
-	expose 80
503
-	run echo hello
504
-        `,
505
-		nil, nil}
506
-	checkCacheBehavior(t, template, true)
507
-}
508
-
509
-func TestBuildImageWithoutCache(t *testing.T) {
510
-	template := testContextTemplate{`
511
-        from {IMAGE}
512
-        maintainer dockerio
513
-        `,
514
-		nil, nil}
515
-	checkCacheBehavior(t, template, false)
516
-}
517
-
518
-func TestBuildADDLocalFileWithCache(t *testing.T) {
519
-	template := testContextTemplate{`
520
-        from {IMAGE}
521
-        maintainer dockerio
522
-        run echo "first"
523
-        add foo /usr/lib/bla/bar
524
-	run [ "$(cat /usr/lib/bla/bar)" = "hello" ]
525
-        run echo "second"
526
-	add . /src/
527
-	run [ "$(cat /src/foo)" = "hello" ]
528
-        `,
529
-		[][2]string{
530
-			{"foo", "hello"},
531
-		},
532
-		nil}
533
-	eng := NewTestEngine(t)
534
-	defer nuke(mkDaemonFromEngine(eng, t))
535
-
536
-	id1 := checkCacheBehaviorFromEngime(t, template, true, eng)
537
-	template.files = append(template.files, [2]string{"bar", "hello2"})
538
-	id2 := checkCacheBehaviorFromEngime(t, template, true, eng)
539
-	if id1 == id2 {
540
-		t.Fatal("The cache should have been invalided but hasn't.")
541
-	}
542
-	id3 := checkCacheBehaviorFromEngime(t, template, true, eng)
543
-	if id2 != id3 {
544
-		t.Fatal("The cache should have been used but hasn't.")
545
-	}
546
-	template.files[1][1] = "hello3"
547
-	id4 := checkCacheBehaviorFromEngime(t, template, true, eng)
548
-	if id3 == id4 {
549
-		t.Fatal("The cache should have been invalided but hasn't.")
550
-	}
551
-	template.dockerfile += `
552
-	add ./bar /src2/
553
-	run ls /src2/bar
554
-	`
555
-	id5 := checkCacheBehaviorFromEngime(t, template, true, eng)
556
-	if id4 == id5 {
557
-		t.Fatal("The cache should have been invalided but hasn't.")
558
-	}
559
-	template.files[1][1] = "hello4"
560
-	id6 := checkCacheBehaviorFromEngime(t, template, true, eng)
561
-	if id5 == id6 {
562
-		t.Fatal("The cache should have been invalided but hasn't.")
563
-	}
564
-
565
-	template.dockerfile += `
566
-	add bar /src2/bar2
567
-	add /bar /src2/bar3
568
-	run ls /src2/bar2 /src2/bar3
569
-	`
570
-	id7 := checkCacheBehaviorFromEngime(t, template, true, eng)
571
-	if id6 == id7 {
572
-		t.Fatal("The cache should have been invalided but hasn't.")
573
-	}
574
-	template.files[1][1] = "hello5"
575
-	id8 := checkCacheBehaviorFromEngime(t, template, true, eng)
576
-	if id7 == id8 {
577
-		t.Fatal("The cache should have been invalided but hasn't.")
578
-	}
579
-}
580
-
581
-func TestBuildADDLocalFileWithoutCache(t *testing.T) {
582
-	template := testContextTemplate{`
583
-        from {IMAGE}
584
-        maintainer dockerio
585
-        run echo "first"
586
-        add foo /usr/lib/bla/bar
587
-        run echo "second"
588
-        `,
589
-		[][2]string{{"foo", "hello"}},
590
-		nil}
591
-	checkCacheBehavior(t, template, false)
592
-}
593
-
594
-func TestBuildADDCurrentDirectoryWithCache(t *testing.T) {
595
-	template := testContextTemplate{`
596
-        from {IMAGE}
597
-        maintainer dockerio
598
-        add . /usr/lib/bla
599
-        `,
600
-		nil, nil}
601
-	checkCacheBehavior(t, template, true)
602
-}
603
-
604
-func TestBuildADDCurrentDirectoryWithoutCache(t *testing.T) {
605
-	template := testContextTemplate{`
606
-        from {IMAGE}
607
-        maintainer dockerio
608
-        add . /usr/lib/bla
609
-        `,
610
-		nil, nil}
611
-	checkCacheBehavior(t, template, false)
612
-}
613
-
614
-func TestBuildADDRemoteFileWithCache(t *testing.T) {
615
-	template := testContextTemplate{`
616
-        from {IMAGE}
617
-        maintainer dockerio
618
-        run echo "first"
619
-        add http://{SERVERADDR}/baz /usr/lib/baz/quux
620
-        run echo "second"
621
-        `,
622
-		nil,
623
-		[][2]string{{"/baz", "world!"}}}
624
-	checkCacheBehavior(t, template, true)
625
-}
626
-
627
-func TestBuildADDRemoteFileWithoutCache(t *testing.T) {
628
-	template := testContextTemplate{`
629
-        from {IMAGE}
630
-        maintainer dockerio
631
-        run echo "first"
632
-        add http://{SERVERADDR}/baz /usr/lib/baz/quux
633
-        run echo "second"
634
-        `,
635
-		nil,
636
-		[][2]string{{"/baz", "world!"}}}
637
-	checkCacheBehavior(t, template, false)
638
-}
639
-
640
-func TestBuildADDLocalAndRemoteFilesWithCache(t *testing.T) {
641
-	template := testContextTemplate{`
642
-        from {IMAGE}
643
-        maintainer dockerio
644
-        run echo "first"
645
-        add foo /usr/lib/bla/bar
646
-        add http://{SERVERADDR}/baz /usr/lib/baz/quux
647
-        run echo "second"
648
-        `,
649
-		[][2]string{{"foo", "hello"}},
650
-		[][2]string{{"/baz", "world!"}}}
651
-	checkCacheBehavior(t, template, true)
652
-}
653
-
654
-func TestBuildADDLocalAndRemoteFilesWithoutCache(t *testing.T) {
655
-	template := testContextTemplate{`
656
-        from {IMAGE}
657
-        maintainer dockerio
658
-        run echo "first"
659
-        add foo /usr/lib/bla/bar
660
-        add http://{SERVERADDR}/baz /usr/lib/baz/quux
661
-        run echo "second"
662
-        `,
663
-		[][2]string{{"foo", "hello"}},
664
-		[][2]string{{"/baz", "world!"}}}
665
-	checkCacheBehavior(t, template, false)
666
-}
667
-
668 448
 func TestForbiddenContextPath(t *testing.T) {
669 449
 	eng := NewTestEngine(t)
670 450
 	defer nuke(mkDaemonFromEngine(eng, t))