github.com/nbering/terraform@v0.8.5-0.20170113232247-453f670684b5/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: testAccArchiveFileOutputPath,
    56  				Check: r.ComposeTestCheckFunc(
    57  					testAccArchiveFileExists(fmt.Sprintf("%s/test.zip", tmpDir), &fileSize),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		*fileSize = ""
    67  		fi, err := os.Stat(filename)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		*fileSize = fmt.Sprintf("%d", fi.Size())
    72  		return nil
    73  	}
    74  }
    75  
    76  var testAccArchiveFileContentConfig = `
    77  data "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  data "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  data "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  data "archive_file" "foo" {
   105    type        = "zip"
   106    source_dir  = "test-fixtures/test-dir"
   107    output_path = "zip_file_acc_test.zip"
   108  }
   109  `