I've decided that custom asserts only hide the meaning of tests
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
| ... | ... |
@@ -10,25 +10,6 @@ import ( |
| 10 | 10 |
"time" |
| 11 | 11 |
) |
| 12 | 12 |
|
| 13 |
-func checkSimpleBuild(t *testing.T, dockerfile, name, inspectFormat, expected string) {
|
|
| 14 |
- buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-") |
|
| 15 |
- buildCmd.Stdin = strings.NewReader(dockerfile) |
|
| 16 |
- out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 17 |
- errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
|
|
| 18 |
- if err != nil || exitCode != 0 {
|
|
| 19 |
- t.Fatal("failed to build the image")
|
|
| 20 |
- } |
|
| 21 |
- inspectCmd := exec.Command(dockerBinary, "inspect", "-f", inspectFormat, name) |
|
| 22 |
- out, exitCode, err = runCommandWithOutput(inspectCmd) |
|
| 23 |
- if err != nil || exitCode != 0 {
|
|
| 24 |
- t.Fatalf("failed to inspect the image: %s", out)
|
|
| 25 |
- } |
|
| 26 |
- out = strings.TrimSpace(out) |
|
| 27 |
- if out != expected {
|
|
| 28 |
- t.Fatalf("From format %s expected %s, got %s", inspectFormat, expected, out)
|
|
| 29 |
- } |
|
| 30 |
-} |
|
| 31 |
- |
|
| 32 | 13 |
func TestBuildCacheADD(t *testing.T) {
|
| 33 | 14 |
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildCacheADD", "1") |
| 34 | 15 |
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcacheadd1", ".") |
| ... | ... |
@@ -606,126 +587,181 @@ func TestBuildRm(t *testing.T) {
|
| 606 | 606 |
logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
|
| 607 | 607 |
logDone("build - ensure --rm=false overrides the default")
|
| 608 | 608 |
} |
| 609 |
- |
|
| 610 |
-func TestBuildWithVolume(t *testing.T) {
|
|
| 611 |
- checkSimpleBuild(t, |
|
| 612 |
- ` |
|
| 613 |
- FROM scratch |
|
| 614 |
- VOLUME /test |
|
| 615 |
- `, |
|
| 616 |
- "testbuildimg", |
|
| 617 |
- "{{json .Config.Volumes}}",
|
|
| 618 |
- `{"/test":{}}`)
|
|
| 619 |
- |
|
| 620 |
- deleteImages("testbuildimg")
|
|
| 621 |
- logDone("build - with volume")
|
|
| 609 |
+func TestBuildWithVolumes(t *testing.T) {
|
|
| 610 |
+ name := "testbuildvolumes" |
|
| 611 |
+ expected := "map[/test1:map[] /test2:map[]]" |
|
| 612 |
+ defer deleteImages(name) |
|
| 613 |
+ _, err := buildImage(name, |
|
| 614 |
+ `FROM scratch |
|
| 615 |
+ VOLUME /test1 |
|
| 616 |
+ VOLUME /test2`, |
|
| 617 |
+ true) |
|
| 618 |
+ if err != nil {
|
|
| 619 |
+ t.Fatal(err) |
|
| 620 |
+ } |
|
| 621 |
+ res, err := inspectField(name, "Config.Volumes") |
|
| 622 |
+ if err != nil {
|
|
| 623 |
+ t.Fatal(err) |
|
| 624 |
+ } |
|
| 625 |
+ if res != expected {
|
|
| 626 |
+ t.Fatalf("Volumes %s, expected %s", res, expected)
|
|
| 627 |
+ } |
|
| 628 |
+ logDone("build - with volumes")
|
|
| 622 | 629 |
} |
| 623 | 630 |
|
| 624 | 631 |
func TestBuildMaintainer(t *testing.T) {
|
| 625 |
- checkSimpleBuild(t, |
|
| 626 |
- ` |
|
| 627 |
- FROM scratch |
|
| 628 |
- MAINTAINER dockerio |
|
| 629 |
- `, |
|
| 630 |
- "testbuildimg", |
|
| 631 |
- "{{json .Author}}",
|
|
| 632 |
- `"dockerio"`) |
|
| 633 |
- |
|
| 634 |
- deleteImages("testbuildimg")
|
|
| 632 |
+ name := "testbuildmaintainer" |
|
| 633 |
+ expected := "dockerio" |
|
| 634 |
+ defer deleteImages(name) |
|
| 635 |
+ _, err := buildImage(name, |
|
| 636 |
+ `FROM scratch |
|
| 637 |
+ MAINTAINER dockerio`, |
|
| 638 |
+ true) |
|
| 639 |
+ if err != nil {
|
|
| 640 |
+ t.Fatal(err) |
|
| 641 |
+ } |
|
| 642 |
+ res, err := inspectField(name, "Author") |
|
| 643 |
+ if err != nil {
|
|
| 644 |
+ t.Fatal(err) |
|
| 645 |
+ } |
|
| 646 |
+ if res != expected {
|
|
| 647 |
+ t.Fatalf("Maintainer %s, expected %s", res, expected)
|
|
| 648 |
+ } |
|
| 635 | 649 |
logDone("build - maintainer")
|
| 636 | 650 |
} |
| 637 | 651 |
|
| 638 | 652 |
func TestBuildUser(t *testing.T) {
|
| 639 |
- checkSimpleBuild(t, |
|
| 640 |
- ` |
|
| 641 |
- FROM busybox |
|
| 653 |
+ name := "testbuilduser" |
|
| 654 |
+ expected := "dockerio" |
|
| 655 |
+ defer deleteImages(name) |
|
| 656 |
+ _, err := buildImage(name, |
|
| 657 |
+ `FROM busybox |
|
| 642 | 658 |
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd |
| 643 | 659 |
USER dockerio |
| 644 |
- RUN [ $(whoami) = 'dockerio' ] |
|
| 645 |
- `, |
|
| 646 |
- "testbuildimg", |
|
| 647 |
- "{{json .Config.User}}",
|
|
| 648 |
- `"dockerio"`) |
|
| 649 |
- |
|
| 650 |
- deleteImages("testbuildimg")
|
|
| 660 |
+ RUN [ $(whoami) = 'dockerio' ]`, |
|
| 661 |
+ true) |
|
| 662 |
+ if err != nil {
|
|
| 663 |
+ t.Fatal(err) |
|
| 664 |
+ } |
|
| 665 |
+ res, err := inspectField(name, "Config.User") |
|
| 666 |
+ if err != nil {
|
|
| 667 |
+ t.Fatal(err) |
|
| 668 |
+ } |
|
| 669 |
+ if res != expected {
|
|
| 670 |
+ t.Fatalf("User %s, expected %s", res, expected)
|
|
| 671 |
+ } |
|
| 651 | 672 |
logDone("build - user")
|
| 652 | 673 |
} |
| 653 | 674 |
|
| 654 | 675 |
func TestBuildRelativeWorkdir(t *testing.T) {
|
| 655 |
- checkSimpleBuild(t, |
|
| 656 |
- ` |
|
| 657 |
- FROM busybox |
|
| 676 |
+ name := "testbuildrelativeworkdir" |
|
| 677 |
+ expected := "/test2/test3" |
|
| 678 |
+ defer deleteImages(name) |
|
| 679 |
+ _, err := buildImage(name, |
|
| 680 |
+ `FROM busybox |
|
| 658 | 681 |
RUN [ "$PWD" = '/' ] |
| 659 | 682 |
WORKDIR test1 |
| 660 | 683 |
RUN [ "$PWD" = '/test1' ] |
| 661 | 684 |
WORKDIR /test2 |
| 662 | 685 |
RUN [ "$PWD" = '/test2' ] |
| 663 | 686 |
WORKDIR test3 |
| 664 |
- RUN [ "$PWD" = '/test2/test3' ] |
|
| 665 |
- `, |
|
| 666 |
- "testbuildimg", |
|
| 667 |
- "{{json .Config.WorkingDir}}",
|
|
| 668 |
- `"/test2/test3"`) |
|
| 669 |
- |
|
| 670 |
- deleteImages("testbuildimg")
|
|
| 687 |
+ RUN [ "$PWD" = '/test2/test3' ]`, |
|
| 688 |
+ true) |
|
| 689 |
+ if err != nil {
|
|
| 690 |
+ t.Fatal(err) |
|
| 691 |
+ } |
|
| 692 |
+ res, err := inspectField(name, "Config.WorkingDir") |
|
| 693 |
+ if err != nil {
|
|
| 694 |
+ t.Fatal(err) |
|
| 695 |
+ } |
|
| 696 |
+ if res != expected {
|
|
| 697 |
+ t.Fatalf("Workdir %s, expected %s", res, expected)
|
|
| 698 |
+ } |
|
| 671 | 699 |
logDone("build - relative workdir")
|
| 672 | 700 |
} |
| 673 | 701 |
|
| 674 | 702 |
func TestBuildEnv(t *testing.T) {
|
| 675 |
- checkSimpleBuild(t, |
|
| 676 |
- ` |
|
| 677 |
- FROM busybox |
|
| 703 |
+ name := "testbuildenv" |
|
| 704 |
+ expected := "[HOME=/ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PORT=4243]" |
|
| 705 |
+ defer deleteImages(name) |
|
| 706 |
+ _, err := buildImage(name, |
|
| 707 |
+ `FROM busybox |
|
| 678 | 708 |
ENV PORT 4243 |
| 679 |
- RUN [ $(env | grep PORT) = 'PORT=4243' ] |
|
| 680 |
- `, |
|
| 681 |
- "testbuildimg", |
|
| 682 |
- "{{json .Config.Env}}",
|
|
| 683 |
- `["HOME=/","PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","PORT=4243"]`) |
|
| 684 |
- |
|
| 685 |
- deleteImages("testbuildimg")
|
|
| 709 |
+ RUN [ $(env | grep PORT) = 'PORT=4243' ]`, |
|
| 710 |
+ true) |
|
| 711 |
+ if err != nil {
|
|
| 712 |
+ t.Fatal(err) |
|
| 713 |
+ } |
|
| 714 |
+ res, err := inspectField(name, "Config.Env") |
|
| 715 |
+ if err != nil {
|
|
| 716 |
+ t.Fatal(err) |
|
| 717 |
+ } |
|
| 718 |
+ if res != expected {
|
|
| 719 |
+ t.Fatalf("Env %s, expected %s", res, expected)
|
|
| 720 |
+ } |
|
| 686 | 721 |
logDone("build - env")
|
| 687 | 722 |
} |
| 688 | 723 |
|
| 689 | 724 |
func TestBuildCmd(t *testing.T) {
|
| 690 |
- checkSimpleBuild(t, |
|
| 691 |
- ` |
|
| 692 |
- FROM scratch |
|
| 693 |
- CMD ["/bin/echo", "Hello World"] |
|
| 694 |
- `, |
|
| 695 |
- "testbuildimg", |
|
| 696 |
- "{{json .Config.Cmd}}",
|
|
| 697 |
- `["/bin/echo","Hello World"]`) |
|
| 698 |
- |
|
| 699 |
- deleteImages("testbuildimg")
|
|
| 725 |
+ name := "testbuildcmd" |
|
| 726 |
+ expected := "[/bin/echo Hello World]" |
|
| 727 |
+ defer deleteImages(name) |
|
| 728 |
+ _, err := buildImage(name, |
|
| 729 |
+ `FROM scratch |
|
| 730 |
+ CMD ["/bin/echo", "Hello World"]`, |
|
| 731 |
+ true) |
|
| 732 |
+ if err != nil {
|
|
| 733 |
+ t.Fatal(err) |
|
| 734 |
+ } |
|
| 735 |
+ res, err := inspectField(name, "Config.Cmd") |
|
| 736 |
+ if err != nil {
|
|
| 737 |
+ t.Fatal(err) |
|
| 738 |
+ } |
|
| 739 |
+ if res != expected {
|
|
| 740 |
+ t.Fatalf("Cmd %s, expected %s", res, expected)
|
|
| 741 |
+ } |
|
| 700 | 742 |
logDone("build - cmd")
|
| 701 | 743 |
} |
| 702 | 744 |
|
| 703 | 745 |
func TestBuildExpose(t *testing.T) {
|
| 704 |
- checkSimpleBuild(t, |
|
| 705 |
- ` |
|
| 706 |
- FROM scratch |
|
| 707 |
- EXPOSE 4243 |
|
| 708 |
- `, |
|
| 709 |
- |
|
| 710 |
- "testbuildimg", |
|
| 711 |
- "{{json .Config.ExposedPorts}}",
|
|
| 712 |
- `{"4243/tcp":{}}`)
|
|
| 713 |
- |
|
| 714 |
- deleteImages("testbuildimg")
|
|
| 746 |
+ name := "testbuildexpose" |
|
| 747 |
+ expected := "map[4243/tcp:map[]]" |
|
| 748 |
+ defer deleteImages(name) |
|
| 749 |
+ _, err := buildImage(name, |
|
| 750 |
+ `FROM scratch |
|
| 751 |
+ EXPOSE 4243`, |
|
| 752 |
+ true) |
|
| 753 |
+ if err != nil {
|
|
| 754 |
+ t.Fatal(err) |
|
| 755 |
+ } |
|
| 756 |
+ res, err := inspectField(name, "Config.ExposedPorts") |
|
| 757 |
+ if err != nil {
|
|
| 758 |
+ t.Fatal(err) |
|
| 759 |
+ } |
|
| 760 |
+ if res != expected {
|
|
| 761 |
+ t.Fatalf("Exposed ports %s, expected %s", res, expected)
|
|
| 762 |
+ } |
|
| 715 | 763 |
logDone("build - expose")
|
| 716 | 764 |
} |
| 717 | 765 |
|
| 718 | 766 |
func TestBuildEntrypoint(t *testing.T) {
|
| 719 |
- checkSimpleBuild(t, |
|
| 720 |
- ` |
|
| 721 |
- FROM scratch |
|
| 722 |
- ENTRYPOINT ["/bin/echo"] |
|
| 723 |
- `, |
|
| 724 |
- "testbuildimg", |
|
| 725 |
- "{{json .Config.Entrypoint}}",
|
|
| 726 |
- `["/bin/echo"]`) |
|
| 727 |
- |
|
| 728 |
- deleteImages("testbuildimg")
|
|
| 767 |
+ name := "testbuildentrypoint" |
|
| 768 |
+ expected := "[/bin/echo]" |
|
| 769 |
+ defer deleteImages(name) |
|
| 770 |
+ _, err := buildImage(name, |
|
| 771 |
+ `FROM scratch |
|
| 772 |
+ ENTRYPOINT ["/bin/echo"]`, |
|
| 773 |
+ true) |
|
| 774 |
+ if err != nil {
|
|
| 775 |
+ t.Fatal(err) |
|
| 776 |
+ } |
|
| 777 |
+ res, err := inspectField(name, "Config.Entrypoint") |
|
| 778 |
+ if err != nil {
|
|
| 779 |
+ t.Fatal(err) |
|
| 780 |
+ } |
|
| 781 |
+ if res != expected {
|
|
| 782 |
+ t.Fatalf("Entrypoint %s, expected %s", res, expected)
|
|
| 783 |
+ } |
|
| 729 | 784 |
logDone("build - entrypoint")
|
| 730 | 785 |
} |
| 731 | 786 |
|