Skip to content

Commit f492e3f

Browse files
committed
Adding test to verify that resource archive_file replaces the resource (destroy-create) on every read (#112)
1 parent 92bdcdd commit f492e3f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

internal/provider/resource_archive_file_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,78 @@ func TestResource_UpgradeFromVersion2_2_0_SourceConfig(t *testing.T) {
338338
})
339339
}
340340

341+
// TestResource_FileConfig_ModifiedContents tests that archive_file resource replaces the resource on every read.
342+
// The contents of the source file are altered, but no aspect of the Terraform configuration is changed.
343+
// The change in the output hashes demonstrates that the resource Read function is replacing the resource.
344+
func TestResource_FileConfig_ModifiedContents(t *testing.T) {
345+
td := testTempDir(t)
346+
defer os.RemoveAll(td)
347+
348+
sourceFilePath := filepath.Join(td, "sourceFile")
349+
outputFilePath := filepath.Join(td, "zip_file_acc_test_upgrade_file_config.zip")
350+
351+
var fileSize string
352+
353+
r.ParallelTest(t, r.TestCase{
354+
Steps: []r.TestStep{
355+
{
356+
PreConfig: func() {
357+
alterFileContents("content", sourceFilePath)
358+
},
359+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
360+
Config: testAccArchiveFileResourceFileSourceFileConfig(sourceFilePath, outputFilePath),
361+
Check: r.ComposeTestCheckFunc(
362+
testAccArchiveFileSize(outputFilePath, &fileSize),
363+
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
364+
r.TestCheckResourceAttr(
365+
"archive_file.foo", "output_base64sha256", "8inOmQJB12dXqCyRTdaRO63yP22Rmuube/A1DLDii10=",
366+
),
367+
r.TestCheckResourceAttr(
368+
"archive_file.foo", "output_md5", "20d9c8096f99174d128e5042279fe576",
369+
),
370+
r.TestCheckResourceAttr(
371+
"archive_file.foo", "output_sha", "119d3169ec43fe95bbd38a3824f4e477f4e8d4e7",
372+
),
373+
),
374+
},
375+
{
376+
PreConfig: func() {
377+
alterFileContents("modified content", sourceFilePath)
378+
},
379+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
380+
Config: testAccArchiveFileResourceFileSourceFileConfig(sourceFilePath, outputFilePath),
381+
Check: r.ComposeTestCheckFunc(
382+
testAccArchiveFileSize(outputFilePath, &fileSize),
383+
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
384+
r.TestCheckResourceAttr(
385+
"archive_file.foo", "output_base64sha256", "OnzXDJ3jda5RPuINpxKHQsZ+jSNOupxShSmW3iUWw7Q=",
386+
),
387+
r.TestCheckResourceAttr(
388+
"archive_file.foo", "output_md5", "ce31b13da062764f2975d1ef08ee56fe",
389+
),
390+
r.TestCheckResourceAttr(
391+
"archive_file.foo", "output_sha", "12c51ec24fc5dc10570abbc0b56ac5a3b4141b83",
392+
),
393+
),
394+
},
395+
},
396+
})
397+
}
398+
399+
func alterFileContents(content, path string) {
400+
f, err := os.Create(path)
401+
if err != nil {
402+
panic(fmt.Sprintf("error creating file: %s", err))
403+
}
404+
405+
defer f.Close()
406+
407+
_, err = f.Write([]byte(content))
408+
if err != nil {
409+
panic(fmt.Sprintf("error writing file: %s", err))
410+
}
411+
}
412+
341413
func testAccArchiveFileResourceContentConfig(outputPath string) string {
342414
return fmt.Sprintf(`
343415
resource "archive_file" "foo" {
@@ -360,6 +432,19 @@ resource "archive_file" "foo" {
360432
`, filepath.ToSlash(outputPath))
361433
}
362434

435+
func testAccArchiveFileResourceFileSourceFileConfig(sourceFile, outputPath string) string {
436+
return fmt.Sprintf(`
437+
resource "archive_file" "foo" {
438+
type = "zip"
439+
source_file = "%s"
440+
output_path = "%s"
441+
output_file_mode = "0666"
442+
}
443+
`,
444+
filepath.ToSlash(sourceFile),
445+
filepath.ToSlash(outputPath))
446+
}
447+
363448
func testAccArchiveFileResourceDirConfig(outputPath string) string {
364449
return fmt.Sprintf(`
365450
resource "archive_file" "foo" {

0 commit comments

Comments
 (0)