mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-20 06:51:39 -04:00
The file datasource is meant to be used to pre-create a file that can then be used elsewhere in the build process by its path. This is useful for example when building a configuration file from a template, so then the resulting file can be referenced by components which only accept file paths.
181 lines
3.0 KiB
Go
181 lines
3.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/packer-plugin-sdk/acctest"
|
|
)
|
|
|
|
func TestFileDataSource(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
template string
|
|
createOutput bool
|
|
expectError bool
|
|
expectOutput string
|
|
}{
|
|
{
|
|
"Success - write empty file",
|
|
basicEmptyFileWrite,
|
|
false,
|
|
false,
|
|
"",
|
|
},
|
|
{
|
|
"Fail - write empty file, pre-existing output",
|
|
basicEmptyFileWrite,
|
|
true,
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"Success - write empty file, pre-existing output",
|
|
basicEmptyFileWriteForce,
|
|
true,
|
|
false,
|
|
"",
|
|
},
|
|
{
|
|
"Success - write template to output",
|
|
basicFileWithTemplateContents,
|
|
false,
|
|
false,
|
|
"contents are 12345\n",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
testCase := &acctest.PluginTestCase{
|
|
Name: tt.name,
|
|
Setup: func() error {
|
|
return nil
|
|
},
|
|
Teardown: func() error {
|
|
return nil
|
|
},
|
|
Template: tt.template,
|
|
Type: "http",
|
|
Check: func(buildCommand *exec.Cmd, logfile string) error {
|
|
if buildCommand.ProcessState != nil {
|
|
if buildCommand.ProcessState.ExitCode() != 0 && !tt.expectError {
|
|
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
|
|
}
|
|
if tt.expectError && buildCommand.ProcessState.ExitCode() == 0 {
|
|
return fmt.Errorf("Expected an error but succeeded.")
|
|
}
|
|
}
|
|
|
|
if tt.expectError {
|
|
return nil
|
|
}
|
|
|
|
outFile, err := os.ReadFile("output")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read output file: %s", err)
|
|
}
|
|
|
|
diff := cmp.Diff(string(outFile), tt.expectOutput)
|
|
if diff != "" {
|
|
return fmt.Errorf("diff found in output: %s", diff)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
os.RemoveAll("output")
|
|
if tt.createOutput {
|
|
err := os.WriteFile("output", []byte{}, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to pre-create output file: %s", err)
|
|
}
|
|
}
|
|
|
|
acctest.TestPlugin(t, testCase)
|
|
|
|
os.RemoveAll("output")
|
|
})
|
|
}
|
|
}
|
|
|
|
var basicEmptyFileWrite string = `
|
|
source "null" "test" {
|
|
communicator = "none"
|
|
}
|
|
|
|
data "file" "empty" {
|
|
destination = "output"
|
|
}
|
|
|
|
build {
|
|
sources = [
|
|
"source.null.test"
|
|
]
|
|
|
|
provisioner "shell-local" {
|
|
inline = [
|
|
"set -ex",
|
|
"test -f ${data.file.empty.path}",
|
|
]
|
|
}
|
|
}
|
|
`
|
|
|
|
var basicEmptyFileWriteForce string = `
|
|
source "null" "test" {
|
|
communicator = "none"
|
|
}
|
|
|
|
data "file" "empty" {
|
|
destination = "output"
|
|
force = true
|
|
}
|
|
|
|
build {
|
|
sources = [
|
|
"source.null.test"
|
|
]
|
|
|
|
provisioner "shell-local" {
|
|
inline = [
|
|
"set -ex",
|
|
"test -f ${data.file.empty.path}",
|
|
]
|
|
}
|
|
}
|
|
`
|
|
|
|
var basicFileWithTemplateContents string = `
|
|
source "null" "test" {
|
|
communicator = "none"
|
|
}
|
|
|
|
data "file" "empty" {
|
|
contents = templatefile("test-fixtures/template.pkrtpl.hcl", {
|
|
"value" = "12345",
|
|
})
|
|
destination = "output"
|
|
}
|
|
|
|
build {
|
|
sources = [
|
|
"source.null.test"
|
|
]
|
|
|
|
provisioner "shell-local" {
|
|
inline = [
|
|
"set -ex",
|
|
"test -f ${data.file.empty.path}",
|
|
]
|
|
}
|
|
}
|
|
`
|