github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/archive/data_source_archive_file_test.go (about) 1 package archive 2 3 import ( 4 "fmt" 5 "os" 6 "regexp" 7 "testing" 8 9 r "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccArchiveFile_Basic(t *testing.T) { 14 var fileSize string 15 r.Test(t, r.TestCase{ 16 Providers: testProviders, 17 Steps: []r.TestStep{ 18 r.TestStep{ 19 Config: testAccArchiveFileContentConfig, 20 Check: r.ComposeTestCheckFunc( 21 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 22 r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize), 23 24 // We just check the hashes for syntax rather than exact 25 // content since we don't want to break if the archive 26 // library starts generating different bytes that are 27 // functionally equivalent. 28 r.TestMatchResourceAttr( 29 "data.archive_file.foo", "output_base64sha256", 30 regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`), 31 ), 32 r.TestMatchResourceAttr( 33 "data.archive_file.foo", "output_md5", regexp.MustCompile(`^[0-9a-f]{32}$`), 34 ), 35 r.TestMatchResourceAttr( 36 "data.archive_file.foo", "output_sha", regexp.MustCompile(`^[0-9a-f]{40}$`), 37 ), 38 ), 39 }, 40 r.TestStep{ 41 Config: testAccArchiveFileFileConfig, 42 Check: r.ComposeTestCheckFunc( 43 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 44 r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize), 45 ), 46 }, 47 r.TestStep{ 48 Config: testAccArchiveFileDirConfig, 49 Check: r.ComposeTestCheckFunc( 50 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 51 r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize), 52 ), 53 }, 54 r.TestStep{ 55 Config: testAccArchiveFileMultiConfig, 56 Check: r.ComposeTestCheckFunc( 57 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 58 r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize), 59 ), 60 }, 61 r.TestStep{ 62 Config: testAccArchiveFileOutputPath, 63 Check: r.ComposeTestCheckFunc( 64 testAccArchiveFileExists(fmt.Sprintf("%s/test.zip", tmpDir), &fileSize), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc { 72 return func(s *terraform.State) error { 73 *fileSize = "" 74 fi, err := os.Stat(filename) 75 if err != nil { 76 return err 77 } 78 *fileSize = fmt.Sprintf("%d", fi.Size()) 79 return nil 80 } 81 } 82 83 var testAccArchiveFileContentConfig = ` 84 data "archive_file" "foo" { 85 type = "zip" 86 source_content = "This is some content" 87 source_content_filename = "content.txt" 88 output_path = "zip_file_acc_test.zip" 89 } 90 ` 91 92 var tmpDir = os.TempDir() + "/test" 93 var testAccArchiveFileOutputPath = fmt.Sprintf(` 94 data "archive_file" "foo" { 95 type = "zip" 96 source_content = "This is some content" 97 source_content_filename = "content.txt" 98 output_path = "%s/test.zip" 99 } 100 `, tmpDir) 101 102 var testAccArchiveFileFileConfig = ` 103 data "archive_file" "foo" { 104 type = "zip" 105 source_file = "test-fixtures/test-file.txt" 106 output_path = "zip_file_acc_test.zip" 107 } 108 ` 109 110 var testAccArchiveFileDirConfig = ` 111 data "archive_file" "foo" { 112 type = "zip" 113 source_dir = "test-fixtures/test-dir" 114 output_path = "zip_file_acc_test.zip" 115 } 116 ` 117 118 var testAccArchiveFileMultiConfig = ` 119 data "archive_file" "foo" { 120 type = "zip" 121 source { 122 filename = "content.txt" 123 content = "This is some content" 124 } 125 output_path = "zip_file_acc_test.zip" 126 } 127 `