github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/archive/data_source_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  		Steps: []r.TestStep{
    17  			r.TestStep{
    18  				Config: testAccArchiveFileContentConfig,
    19  				Check: r.ComposeTestCheckFunc(
    20  					testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
    21  					r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
    22  				),
    23  			},
    24  			r.TestStep{
    25  				Config: testAccArchiveFileFileConfig,
    26  				Check: r.ComposeTestCheckFunc(
    27  					testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
    28  					r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
    29  				),
    30  			},
    31  			r.TestStep{
    32  				Config: testAccArchiveFileDirConfig,
    33  				Check: r.ComposeTestCheckFunc(
    34  					testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
    35  					r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
    36  				),
    37  			},
    38  			r.TestStep{
    39  				Config: testAccArchiveFileOutputPath,
    40  				Check: r.ComposeTestCheckFunc(
    41  					testAccArchiveFileExists(fmt.Sprintf("%s/test.zip", tmpDir), &fileSize),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		*fileSize = ""
    51  		fi, err := os.Stat(filename)
    52  		if err != nil {
    53  			return err
    54  		}
    55  		*fileSize = fmt.Sprintf("%d", fi.Size())
    56  		return nil
    57  	}
    58  }
    59  
    60  var testAccArchiveFileContentConfig = `
    61  data "archive_file" "foo" {
    62    type                    = "zip"
    63    source_content          = "This is some content"
    64    source_content_filename = "content.txt"
    65    output_path             = "zip_file_acc_test.zip"
    66  }
    67  `
    68  
    69  var tmpDir = os.TempDir() + "/test"
    70  var testAccArchiveFileOutputPath = fmt.Sprintf(`
    71  data "archive_file" "foo" {
    72    type                    = "zip"
    73    source_content          = "This is some content"
    74    source_content_filename = "content.txt"
    75    output_path             = "%s/test.zip"
    76  }
    77  `, tmpDir)
    78  
    79  var testAccArchiveFileFileConfig = `
    80  data "archive_file" "foo" {
    81    type        = "zip"
    82    source_file = "test-fixtures/test-file.txt"
    83    output_path = "zip_file_acc_test.zip"
    84  }
    85  `
    86  
    87  var testAccArchiveFileDirConfig = `
    88  data "archive_file" "foo" {
    89    type        = "zip"
    90    source_dir  = "test-fixtures/test-dir"
    91    output_path = "zip_file_acc_test.zip"
    92  }
    93  `