|
4 | 4 | "archive/tar"
|
5 | 5 | "bytes"
|
6 | 6 | "fmt"
|
| 7 | + "io/ioutil" |
7 | 8 | "os"
|
8 | 9 | "reflect"
|
9 | 10 | "regexp"
|
@@ -658,6 +659,112 @@ func TestAccDockerContainer_upload(t *testing.T) {
|
658 | 659 | })
|
659 | 660 | }
|
660 | 661 |
|
| 662 | +func TestAccDockerContainer_uploadSource(t *testing.T) { |
| 663 | + var c types.ContainerJSON |
| 664 | + |
| 665 | + wd, _ := os.Getwd() |
| 666 | + testFile := wd + "/../scripts/testing/testingFile" |
| 667 | + testFileContent, _ := ioutil.ReadFile(testFile) |
| 668 | + |
| 669 | + testCheck := func(*terraform.State) error { |
| 670 | + client := testAccProvider.Meta().(*ProviderConfig).DockerClient |
| 671 | + |
| 672 | + srcPath := "/terraform/test.txt" |
| 673 | + r, _, err := client.CopyFromContainer(context.Background(), c.ID, srcPath) |
| 674 | + if err != nil { |
| 675 | + return fmt.Errorf("Unable to download a file from container: %s", err) |
| 676 | + } |
| 677 | + |
| 678 | + tr := tar.NewReader(r) |
| 679 | + if header, err := tr.Next(); err != nil { |
| 680 | + return fmt.Errorf("Unable to read content of tar archive: %s", err) |
| 681 | + } else { |
| 682 | + mode := strconv.FormatInt(header.Mode, 8) |
| 683 | + if !strings.HasSuffix(mode, "744") { |
| 684 | + return fmt.Errorf("File permissions are incorrect: %s", mode) |
| 685 | + } |
| 686 | + } |
| 687 | + |
| 688 | + fbuf := new(bytes.Buffer) |
| 689 | + fbuf.ReadFrom(tr) |
| 690 | + content := fbuf.String() |
| 691 | + if content != string(testFileContent) { |
| 692 | + return fmt.Errorf("file content is invalid") |
| 693 | + } |
| 694 | + |
| 695 | + return nil |
| 696 | + } |
| 697 | + |
| 698 | + resource.Test(t, resource.TestCase{ |
| 699 | + PreCheck: func() { testAccPreCheck(t) }, |
| 700 | + Providers: testAccProviders, |
| 701 | + Steps: []resource.TestStep{ |
| 702 | + { |
| 703 | + Config: fmt.Sprintf(testAccDockerContainerUploadSourceConfig, testFile), |
| 704 | + Check: resource.ComposeTestCheckFunc( |
| 705 | + testAccContainerRunning("docker_container.foo", &c), |
| 706 | + testCheck, |
| 707 | + resource.TestCheckResourceAttr("docker_container.foo", "name", "tf-test"), |
| 708 | + resource.TestCheckResourceAttr("docker_container.foo", "upload.#", "1"), |
| 709 | + // NOTE mavogel: current the terraform-plugin-sdk it's likely that |
| 710 | + // the acceptance testing framework shims (still using the older flatmap-style addressing) |
| 711 | + // are missing a conversion with the hashes. |
| 712 | + // See https://github.com/hashicorp/terraform-plugin-sdk/issues/196 |
| 713 | + // resource.TestCheckResourceAttr("docker_container.foo", "upload.0.content", "foo"), |
| 714 | + // resource.TestCheckResourceAttr("docker_container.foo", "upload.0.content_base64", ""), |
| 715 | + // resource.TestCheckResourceAttr("docker_container.foo", "upload.0.executable", "true"), |
| 716 | + // resource.TestCheckResourceAttr("docker_container.foo", "upload.0.file", "/terraform/test.txt"), |
| 717 | + ), |
| 718 | + }, |
| 719 | + }, |
| 720 | + }) |
| 721 | +} |
| 722 | + |
| 723 | +// |
| 724 | +func TestAccDockerContainer_uploadSourceHash(t *testing.T) { |
| 725 | + var c types.ContainerJSON |
| 726 | + var firstRunId string |
| 727 | + |
| 728 | + wd, _ := os.Getwd() |
| 729 | + testFile := wd + "/../scripts/testing/testingFile" |
| 730 | + hash, _ := ioutil.ReadFile(testFile + ".base64") |
| 731 | + grabFirstCheck := func(*terraform.State) error { |
| 732 | + firstRunId = c.ID |
| 733 | + return nil |
| 734 | + } |
| 735 | + testCheck := func(*terraform.State) error { |
| 736 | + if c.ID == firstRunId { |
| 737 | + return fmt.Errorf("Container should have been recreated due to changed hash") |
| 738 | + } |
| 739 | + return nil |
| 740 | + } |
| 741 | + |
| 742 | + resource.Test(t, resource.TestCase{ |
| 743 | + PreCheck: func() { testAccPreCheck(t) }, |
| 744 | + Providers: testAccProviders, |
| 745 | + Steps: []resource.TestStep{ |
| 746 | + { |
| 747 | + Config: fmt.Sprintf(testAccDockerContainerUploadSourceHashConfig, testFile, string(hash)), |
| 748 | + Check: resource.ComposeTestCheckFunc( |
| 749 | + testAccContainerRunning("docker_container.foo", &c), |
| 750 | + grabFirstCheck, |
| 751 | + resource.TestCheckResourceAttr("docker_container.foo", "name", "tf-test"), |
| 752 | + resource.TestCheckResourceAttr("docker_container.foo", "upload.#", "1"), |
| 753 | + ), |
| 754 | + }, |
| 755 | + { |
| 756 | + Config: fmt.Sprintf(testAccDockerContainerUploadSourceHashConfig, testFile, string(hash)+"arbitrary"), |
| 757 | + Check: resource.ComposeTestCheckFunc( |
| 758 | + testAccContainerRunning("docker_container.foo", &c), |
| 759 | + testCheck, |
| 760 | + resource.TestCheckResourceAttr("docker_container.foo", "name", "tf-test"), |
| 761 | + resource.TestCheckResourceAttr("docker_container.foo", "upload.#", "1"), |
| 762 | + ), |
| 763 | + }, |
| 764 | + }, |
| 765 | + }) |
| 766 | +} |
| 767 | + |
661 | 768 | func TestAccDockerContainer_uploadAsBase64(t *testing.T) {
|
662 | 769 | var c types.ContainerJSON
|
663 | 770 |
|
@@ -765,7 +872,7 @@ func TestAccDockerContainer_multipleUploadContentsConfig(t *testing.T) {
|
765 | 872 | }
|
766 | 873 | }
|
767 | 874 | `,
|
768 |
| - ExpectError: regexp.MustCompile(`.*only one of 'content' or 'content_base64' can be specified.*`), |
| 875 | + ExpectError: regexp.MustCompile(`.*only one of 'content', 'content_base64', or 'source' can be set.*`), |
769 | 876 | },
|
770 | 877 | },
|
771 | 878 | })
|
@@ -794,7 +901,7 @@ func TestAccDockerContainer_noUploadContentsConfig(t *testing.T) {
|
794 | 901 | }
|
795 | 902 | }
|
796 | 903 | `,
|
797 |
| - ExpectError: regexp.MustCompile(`.* neither 'content', nor 'content_base64' was set.*`), |
| 904 | + ExpectError: regexp.MustCompile(`.* one of 'content', 'content_base64', or 'source' must be set.*`), |
798 | 905 | },
|
799 | 906 | },
|
800 | 907 | })
|
@@ -1791,6 +1898,43 @@ resource "docker_container" "foo" {
|
1791 | 1898 | }
|
1792 | 1899 | `
|
1793 | 1900 |
|
| 1901 | +const testAccDockerContainerUploadSourceConfig = ` |
| 1902 | +resource "docker_image" "foo" { |
| 1903 | + name = "nginx:latest" |
| 1904 | + keep_locally = true |
| 1905 | +} |
| 1906 | +
|
| 1907 | +resource "docker_container" "foo" { |
| 1908 | + name = "tf-test" |
| 1909 | + image = "${docker_image.foo.latest}" |
| 1910 | +
|
| 1911 | + upload { |
| 1912 | + source = "%s" |
| 1913 | + file = "/terraform/test.txt" |
| 1914 | + executable = true |
| 1915 | + } |
| 1916 | +} |
| 1917 | +` |
| 1918 | + |
| 1919 | +const testAccDockerContainerUploadSourceHashConfig = ` |
| 1920 | +resource "docker_image" "foo" { |
| 1921 | + name = "nginx:latest" |
| 1922 | + keep_locally = true |
| 1923 | +} |
| 1924 | +
|
| 1925 | +resource "docker_container" "foo" { |
| 1926 | + name = "tf-test" |
| 1927 | + image = "${docker_image.foo.latest}" |
| 1928 | +
|
| 1929 | + upload { |
| 1930 | + source = "%s" |
| 1931 | + source_hash = "%s" |
| 1932 | + file = "/terraform/test.txt" |
| 1933 | + executable = true |
| 1934 | + } |
| 1935 | +} |
| 1936 | +` |
| 1937 | + |
1794 | 1938 | const testAccDockerContainerUploadBase64Config = `
|
1795 | 1939 | resource "docker_image" "foo" {
|
1796 | 1940 | name = "nginx:latest"
|
|
0 commit comments