github.com/daveadams/terraform@v0.6.4-0.20160830094355-13ce74975936/builtin/providers/archive/resource_archive_file_test.go (about)

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