2016-08-08 02:56:44 +02:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-12-24 20:52:17 +01:00
|
|
|
"regexp"
|
2016-08-08 02:56:44 +02:00
|
|
|
"testing"
|
2016-08-18 17:36:27 +02:00
|
|
|
|
|
|
|
r "github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2016-08-08 02:56:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccArchiveFile_Basic(t *testing.T) {
|
|
|
|
var fileSize string
|
|
|
|
r.Test(t, r.TestCase{
|
|
|
|
Providers: testProviders,
|
|
|
|
Steps: []r.TestStep{
|
|
|
|
r.TestStep{
|
|
|
|
Config: testAccArchiveFileContentConfig,
|
|
|
|
Check: r.ComposeTestCheckFunc(
|
|
|
|
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
|
2016-10-25 16:59:06 +02:00
|
|
|
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
|
2016-12-24 20:52:17 +01:00
|
|
|
|
|
|
|
// We just check the hashes for syntax rather than exact
|
|
|
|
// content since we don't want to break if the archive
|
|
|
|
// library starts generating different bytes that are
|
|
|
|
// functionally equivalent.
|
|
|
|
r.TestMatchResourceAttr(
|
|
|
|
"data.archive_file.foo", "output_base64sha256",
|
|
|
|
regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`),
|
|
|
|
),
|
|
|
|
r.TestMatchResourceAttr(
|
|
|
|
"data.archive_file.foo", "output_md5", regexp.MustCompile(`^[0-9a-f]{32}$`),
|
|
|
|
),
|
|
|
|
r.TestMatchResourceAttr(
|
|
|
|
"data.archive_file.foo", "output_sha", regexp.MustCompile(`^[0-9a-f]{40}$`),
|
|
|
|
),
|
2016-08-08 02:56:44 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
r.TestStep{
|
|
|
|
Config: testAccArchiveFileFileConfig,
|
|
|
|
Check: r.ComposeTestCheckFunc(
|
|
|
|
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
|
2016-10-25 16:59:06 +02:00
|
|
|
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
|
2016-08-08 02:56:44 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
r.TestStep{
|
|
|
|
Config: testAccArchiveFileDirConfig,
|
|
|
|
Check: r.ComposeTestCheckFunc(
|
|
|
|
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
|
2016-10-25 16:59:06 +02:00
|
|
|
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
|
2016-08-08 02:56:44 +02:00
|
|
|
),
|
|
|
|
},
|
2016-08-18 17:36:27 +02:00
|
|
|
r.TestStep{
|
|
|
|
Config: testAccArchiveFileOutputPath,
|
|
|
|
Check: r.ComposeTestCheckFunc(
|
2016-08-18 19:11:19 +02:00
|
|
|
testAccArchiveFileExists(fmt.Sprintf("%s/test.zip", tmpDir), &fileSize),
|
2016-08-18 17:36:27 +02:00
|
|
|
),
|
|
|
|
},
|
2016-08-08 02:56:44 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
*fileSize = ""
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*fileSize = fmt.Sprintf("%d", fi.Size())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccArchiveFileContentConfig = `
|
2016-10-25 16:59:06 +02:00
|
|
|
data "archive_file" "foo" {
|
2016-08-08 02:56:44 +02:00
|
|
|
type = "zip"
|
|
|
|
source_content = "This is some content"
|
|
|
|
source_content_filename = "content.txt"
|
|
|
|
output_path = "zip_file_acc_test.zip"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-08-18 19:11:19 +02:00
|
|
|
var tmpDir = os.TempDir() + "/test"
|
|
|
|
var testAccArchiveFileOutputPath = fmt.Sprintf(`
|
2016-10-25 16:59:06 +02:00
|
|
|
data "archive_file" "foo" {
|
2016-08-18 17:36:27 +02:00
|
|
|
type = "zip"
|
|
|
|
source_content = "This is some content"
|
|
|
|
source_content_filename = "content.txt"
|
2016-08-18 19:11:19 +02:00
|
|
|
output_path = "%s/test.zip"
|
2016-08-18 17:36:27 +02:00
|
|
|
}
|
2016-08-18 19:11:19 +02:00
|
|
|
`, tmpDir)
|
2016-08-18 17:36:27 +02:00
|
|
|
|
2016-08-08 02:56:44 +02:00
|
|
|
var testAccArchiveFileFileConfig = `
|
2016-10-25 16:59:06 +02:00
|
|
|
data "archive_file" "foo" {
|
2016-08-08 02:56:44 +02:00
|
|
|
type = "zip"
|
|
|
|
source_file = "test-fixtures/test-file.txt"
|
|
|
|
output_path = "zip_file_acc_test.zip"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccArchiveFileDirConfig = `
|
2016-10-25 16:59:06 +02:00
|
|
|
data "archive_file" "foo" {
|
2016-08-08 02:56:44 +02:00
|
|
|
type = "zip"
|
|
|
|
source_dir = "test-fixtures/test-dir"
|
|
|
|
output_path = "zip_file_acc_test.zip"
|
|
|
|
}
|
|
|
|
`
|