github.com/hashicorp/packer@v1.14.3/command/inspect_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package command
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  )
    13  
    14  func Test_commands(t *testing.T) {
    15  
    16  	tc := []struct {
    17  		command  []string
    18  		env      []string
    19  		expected string
    20  	}{
    21  		{[]string{"inspect", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode
    22  
    23  > input-variables:
    24  
    25  var.fruit: "<unknown>"
    26  
    27  > local-variables:
    28  
    29  local.fruit: "<unknown>"
    30  
    31  > builds:
    32  
    33    > <unnamed build 0>:
    34  
    35      sources:
    36  
    37        null.builder
    38  
    39      provisioners:
    40  
    41        shell-local
    42  
    43      post-processors:
    44  
    45        <no post-processor>
    46  
    47  `},
    48  		{[]string{"inspect", "-var=fruit=banana", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode
    49  
    50  > input-variables:
    51  
    52  var.fruit: "banana"
    53  
    54  > local-variables:
    55  
    56  local.fruit: "banana"
    57  
    58  > builds:
    59  
    60    > <unnamed build 0>:
    61  
    62      sources:
    63  
    64        null.builder
    65  
    66      provisioners:
    67  
    68        shell-local
    69  
    70      post-processors:
    71  
    72        <no post-processor>
    73  
    74  `},
    75  		{[]string{"inspect", "-var=fruit=peach",
    76  			"-var=unknown_string=also_peach",
    77  			`-var=unknown_unknown=["peach_too"]`,
    78  			`-var=unknown_list_of_string=["first_peach", "second_peach"]`,
    79  			filepath.Join(testFixture("hcl"), "inspect", "fruit_string.pkr.hcl")}, nil,
    80  			`Packer Inspect: HCL2 mode
    81  
    82  > input-variables:
    83  
    84  var.default_from_env: ""
    85  var.fruit: "peach"
    86  var.other_default_from_env: ""
    87  var.unknown_list_of_string: "[\n  \"first_peach\",\n  \"second_peach\",\n]"
    88  var.unknown_string: "also_peach"
    89  var.unknown_unknown: "[\"peach_too\"]"
    90  
    91  > local-variables:
    92  
    93  
    94  > builds:
    95  
    96  `},
    97  		{[]string{"inspect", "-var=fruit=peach", "-var=other_default_from_env=apple", filepath.Join(testFixture("hcl"), "inspect")}, []string{"DEFAULT_FROM_ENV=cherry"}, `Packer Inspect: HCL2 mode
    98  
    99  > input-variables:
   100  
   101  var.default_from_env: "cherry"
   102  var.fruit: "peach"
   103  var.other_default_from_env: "apple"
   104  var.unknown_list_of_string: "<unknown>"
   105  var.unknown_string: "<unknown>"
   106  var.unknown_unknown: "<unknown>"
   107  
   108  > local-variables:
   109  
   110  
   111  > builds:
   112  
   113    > aws_example_builder:
   114  
   115    > Description: The builder of clouds !!
   116  
   117  Use it at will.
   118  
   119  
   120      sources:
   121  
   122        amazon-ebs.example-1
   123  
   124        amazon-ebs.example-2
   125  
   126      provisioners:
   127  
   128        shell
   129  
   130      post-processors:
   131  
   132        0:
   133          manifest
   134  
   135        1:
   136          shell-local
   137  
   138        2:
   139          manifest
   140          shell-local
   141  
   142  `},
   143  		{[]string{"inspect", filepath.Join(testFixture("inspect"), "unset_var.json")}, nil, `Packer Inspect: JSON mode
   144  Required variables:
   145  
   146    something
   147  
   148  Optional variables and their defaults:
   149  
   150  
   151  Builders:
   152  
   153    <No builders>
   154  
   155  Provisioners:
   156  
   157    <No provisioners>
   158  
   159  Note: If your build names contain user variables or template
   160  functions such as 'timestamp', these are processed at build time,
   161  and therefore only show in their raw form here.
   162  `},
   163  		{
   164  			[]string{
   165  				"inspect", filepath.Join(testFixture("hcl-inspect-with-sensitive-vars")),
   166  			},
   167  			nil,
   168  			testFixtureContent("hcl-inspect-with-sensitive-vars", "expected-output.txt"),
   169  		},
   170  	}
   171  
   172  	for _, tc := range tc {
   173  		t.Run(fmt.Sprintf("packer %s", tc.command), func(t *testing.T) {
   174  			p := helperCommand(t, tc.command...)
   175  			p.Env = append(p.Env, tc.env...)
   176  			bs, err := p.Output()
   177  			if err != nil {
   178  				t.Fatalf("%v: %s", err, bs)
   179  			}
   180  			actual := string(bs)
   181  			if diff := cmp.Diff(tc.expected, actual); diff != "" {
   182  				t.Fatalf("unexpected ouput %s", diff)
   183  			}
   184  		})
   185  	}
   186  }