2020-06-23 05:58:57 -04:00
|
|
|
package command
|
2020-06-23 09:13:53 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_commands(t *testing.T) {
|
|
|
|
|
|
|
|
tc := []struct {
|
|
|
|
command []string
|
|
|
|
env []string
|
|
|
|
expected string
|
|
|
|
}{
|
2020-08-27 05:55:57 -04:00
|
|
|
{[]string{"inspect", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode
|
|
|
|
|
|
|
|
> input-variables:
|
|
|
|
|
|
|
|
var.fruit: "<unknown>"
|
|
|
|
|
|
|
|
> local-variables:
|
|
|
|
|
|
|
|
local.fruit: "<unknown>"
|
|
|
|
|
|
|
|
> builds:
|
|
|
|
|
|
|
|
> <unnamed build 0>:
|
|
|
|
|
|
|
|
sources:
|
|
|
|
|
|
|
|
null.builder
|
|
|
|
|
|
|
|
provisioners:
|
|
|
|
|
|
|
|
shell-local
|
|
|
|
|
|
|
|
post-processors:
|
|
|
|
|
|
|
|
<no post-processor>
|
|
|
|
|
|
|
|
`},
|
2020-06-23 09:13:53 -04:00
|
|
|
{[]string{"inspect", "-var=fruit=banana", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode
|
|
|
|
|
|
|
|
> input-variables:
|
|
|
|
|
2020-08-26 09:21:32 -04:00
|
|
|
var.fruit: "banana"
|
2020-06-23 09:13:53 -04:00
|
|
|
|
|
|
|
> local-variables:
|
|
|
|
|
|
|
|
local.fruit: "banana"
|
|
|
|
|
|
|
|
> builds:
|
|
|
|
|
|
|
|
> <unnamed build 0>:
|
|
|
|
|
2020-06-23 09:40:54 -04:00
|
|
|
sources:
|
|
|
|
|
|
|
|
null.builder
|
|
|
|
|
2020-06-23 09:13:53 -04:00
|
|
|
provisioners:
|
|
|
|
|
|
|
|
shell-local
|
|
|
|
|
|
|
|
post-processors:
|
|
|
|
|
|
|
|
<no post-processor>
|
|
|
|
|
2020-06-23 09:17:49 -04:00
|
|
|
`},
|
2020-09-02 10:26:50 -04:00
|
|
|
{[]string{"inspect", "-var=fruit=peach",
|
|
|
|
"-var=unknown_string=also_peach",
|
|
|
|
`-var=unknown_unknown=["peach_too"]`,
|
|
|
|
`-var=unknown_list_of_string=["first_peach", "second_peach"]`,
|
|
|
|
filepath.Join(testFixture("hcl"), "inspect", "fruit_string.pkr.hcl")}, nil,
|
|
|
|
`Packer Inspect: HCL2 mode
|
2020-06-23 09:40:54 -04:00
|
|
|
|
|
|
|
> input-variables:
|
|
|
|
|
2020-08-26 09:21:32 -04:00
|
|
|
var.fruit: "peach"
|
2020-09-02 10:26:50 -04:00
|
|
|
var.unknown_list_of_string: "[\n \"first_peach\",\n \"second_peach\",\n]"
|
2020-09-01 07:07:45 -04:00
|
|
|
var.unknown_string: "also_peach"
|
2020-09-02 10:26:50 -04:00
|
|
|
var.unknown_unknown: "[\"peach_too\"]"
|
2020-06-23 09:40:54 -04:00
|
|
|
|
|
|
|
> local-variables:
|
|
|
|
|
|
|
|
|
|
|
|
> builds:
|
|
|
|
|
|
|
|
`},
|
|
|
|
{[]string{"inspect", "-var=fruit=peach", filepath.Join(testFixture("hcl"), "inspect")}, nil, `Packer Inspect: HCL2 mode
|
2020-06-23 09:17:49 -04:00
|
|
|
|
|
|
|
> input-variables:
|
|
|
|
|
2020-08-26 09:21:32 -04:00
|
|
|
var.fruit: "peach"
|
2020-09-02 10:26:50 -04:00
|
|
|
var.unknown_list_of_string: "<unknown>"
|
2020-09-01 07:07:45 -04:00
|
|
|
var.unknown_string: "<unknown>"
|
|
|
|
var.unknown_unknown: "<unknown>"
|
2020-06-23 09:17:49 -04:00
|
|
|
|
|
|
|
> local-variables:
|
|
|
|
|
|
|
|
|
|
|
|
> builds:
|
|
|
|
|
2020-06-23 09:40:54 -04:00
|
|
|
> aws_example_builder:
|
|
|
|
|
|
|
|
> Description: The builder of clouds !!
|
|
|
|
|
|
|
|
Use it at will.
|
|
|
|
|
|
|
|
|
|
|
|
sources:
|
|
|
|
|
|
|
|
amazon-ebs.example-1
|
|
|
|
|
|
|
|
amazon-ebs.example-2
|
|
|
|
|
|
|
|
provisioners:
|
|
|
|
|
|
|
|
shell
|
|
|
|
|
|
|
|
post-processors:
|
|
|
|
|
2020-07-28 04:02:37 -04:00
|
|
|
0:
|
|
|
|
manifest
|
|
|
|
|
|
|
|
1:
|
|
|
|
shell-local
|
|
|
|
|
|
|
|
2:
|
|
|
|
manifest
|
|
|
|
shell-local
|
2020-06-23 09:40:54 -04:00
|
|
|
|
2020-09-01 07:07:45 -04:00
|
|
|
`},
|
|
|
|
{[]string{"inspect", filepath.Join(testFixture("inspect"), "unset_var.json")}, nil, `Packer Inspect: JSON mode
|
|
|
|
Required variables:
|
|
|
|
|
|
|
|
something
|
|
|
|
|
|
|
|
Optional variables and their defaults:
|
|
|
|
|
|
|
|
|
|
|
|
Builders:
|
|
|
|
|
|
|
|
<No builders>
|
|
|
|
|
|
|
|
Provisioners:
|
|
|
|
|
|
|
|
<No provisioners>
|
|
|
|
|
|
|
|
Note: If your build names contain user variables or template
|
|
|
|
functions such as 'timestamp', these are processed at build time,
|
|
|
|
and therefore only show in their raw form here.
|
2020-06-23 09:13:53 -04:00
|
|
|
`},
|
2020-10-02 04:49:21 -04:00
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"inspect", filepath.Join(testFixture("hcl-inspect-with-sensitive-vars")),
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
testFixtureContent("hcl-inspect-with-sensitive-vars", "expected-output.txt"),
|
|
|
|
},
|
2020-06-23 09:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tc {
|
|
|
|
t.Run(fmt.Sprintf("packer %s", tc.command), func(t *testing.T) {
|
|
|
|
p := helperCommand(t, tc.command...)
|
|
|
|
p.Env = append(p.Env, tc.env...)
|
|
|
|
bs, err := p.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v: %s", err, bs)
|
|
|
|
}
|
|
|
|
actual := string(bs)
|
|
|
|
if diff := cmp.Diff(tc.expected, actual); diff != "" {
|
|
|
|
t.Fatalf("unexpected ouput %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|