github.com/rjeczalik/terraform@v0.6.7-0.20160812060014-e251d5c7bd39/builtin/providers/archive/resource_archive_file_test.go (about) 1 package archive 2 3 import ( 4 "fmt" 5 r "github.com/hashicorp/terraform/helper/resource" 6 "github.com/hashicorp/terraform/terraform" 7 "os" 8 "testing" 9 ) 10 11 func TestAccArchiveFile_Basic(t *testing.T) { 12 var fileSize string 13 r.Test(t, r.TestCase{ 14 Providers: testProviders, 15 CheckDestroy: r.ComposeTestCheckFunc( 16 testAccArchiveFileMissing("zip_file_acc_test.zip"), 17 ), 18 Steps: []r.TestStep{ 19 r.TestStep{ 20 Config: testAccArchiveFileContentConfig, 21 Check: r.ComposeTestCheckFunc( 22 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 23 r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize), 24 ), 25 }, 26 r.TestStep{ 27 Config: testAccArchiveFileFileConfig, 28 Check: r.ComposeTestCheckFunc( 29 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 30 r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize), 31 ), 32 }, 33 r.TestStep{ 34 Config: testAccArchiveFileDirConfig, 35 Check: r.ComposeTestCheckFunc( 36 testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize), 37 r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc { 45 return func(s *terraform.State) error { 46 *fileSize = "" 47 fi, err := os.Stat(filename) 48 if err != nil { 49 return err 50 } 51 *fileSize = fmt.Sprintf("%d", fi.Size()) 52 return nil 53 } 54 } 55 56 func testAccArchiveFileMissing(filename string) r.TestCheckFunc { 57 return func(s *terraform.State) error { 58 _, err := os.Stat(filename) 59 if err != nil { 60 if os.IsNotExist(err) { 61 return nil 62 } 63 return err 64 } 65 return fmt.Errorf("found file expected to be deleted: %s", filename) 66 } 67 } 68 69 var testAccArchiveFileContentConfig = ` 70 resource "archive_file" "foo" { 71 type = "zip" 72 source_content = "This is some content" 73 source_content_filename = "content.txt" 74 output_path = "zip_file_acc_test.zip" 75 } 76 ` 77 78 var testAccArchiveFileFileConfig = ` 79 resource "archive_file" "foo" { 80 type = "zip" 81 source_file = "test-fixtures/test-file.txt" 82 output_path = "zip_file_acc_test.zip" 83 } 84 ` 85 86 var testAccArchiveFileDirConfig = ` 87 resource "archive_file" "foo" { 88 type = "zip" 89 source_dir = "test-fixtures/test-dir" 90 output_path = "zip_file_acc_test.zip" 91 } 92 `