github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/packer/packer_test.go (about) 1 package packer 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestExtractAmiIdFromOneLine(t *testing.T) { 12 t.Parallel() 13 14 expectedAMIID := "ami-b481b3de" 15 text := fmt.Sprintf("1456332887,amazon-ebs,artifact,0,id,us-east-1:%s", expectedAMIID) 16 actualAMIID, err := extractArtifactID(text) 17 18 if err != nil { 19 t.Errorf("Did not expect to get an error when extracting a valid AMI ID: %s", err) 20 } 21 22 if actualAMIID != expectedAMIID { 23 t.Errorf("Did not get expected AMI ID. Expected: %s. Actual: %s.", expectedAMIID, actualAMIID) 24 } 25 } 26 27 func TestExtractImageIdFromOneLine(t *testing.T) { 28 t.Parallel() 29 30 expectedImageID := "terratest-packer-example-2018-08-09t12-02-58z" 31 text := fmt.Sprintf("1533816302,googlecompute,artifact,0,id,%s", expectedImageID) 32 actualImageID, err := extractArtifactID(text) 33 34 if err != nil { 35 t.Errorf("Did not expect to get an error when extracting a valid Image ID: %s", err) 36 } 37 38 if actualImageID != expectedImageID { 39 t.Errorf("Did not get expected Image ID. Expected: %s. Actual: %s.", expectedImageID, actualImageID) 40 } 41 } 42 43 func TestExtractAmiIdFromMultipleLines(t *testing.T) { 44 t.Parallel() 45 46 expectedAMIID := "ami-b481b3de" 47 text := fmt.Sprintf(` 48 foo 49 bar 50 1456332887,amazon-ebs,artifact,0,id,us-east-1:%s 51 baz 52 blah 53 `, expectedAMIID) 54 55 actualAMIID, err := extractArtifactID(text) 56 57 if err != nil { 58 t.Errorf("Did not expect to get an error when extracting a valid AMI ID: %s", err) 59 } 60 61 if actualAMIID != expectedAMIID { 62 t.Errorf("Did not get expected AMI ID. Expected: %s. Actual: %s.", expectedAMIID, actualAMIID) 63 } 64 } 65 66 func TestExtractImageIdFromMultipleLines(t *testing.T) { 67 t.Parallel() 68 69 expectedImageID := "terratest-packer-example-2018-08-09t12-02-58z" 70 text := fmt.Sprintf(` 71 foo 72 bar 73 1533816302,googlecompute,artifact,0,id,%s 74 baz 75 blah 76 `, expectedImageID) 77 78 actualImageID, err := extractArtifactID(text) 79 80 if err != nil { 81 t.Errorf("Did not expect to get an error when extracting a valid Image ID: %s", err) 82 } 83 84 if actualImageID != expectedImageID { 85 t.Errorf("Did not get the expected Image ID. Expected: %s. Actual: %s.", expectedImageID, actualImageID) 86 } 87 } 88 89 func TestExtractAmiIdNoIdPresent(t *testing.T) { 90 t.Parallel() 91 92 text := ` 93 foo 94 bar 95 baz 96 blah 97 ` 98 99 _, err := extractArtifactID(text) 100 101 if err == nil { 102 t.Error("Expected to get an error when extracting an AMI ID from text with no AMI in it, but got nil") 103 } 104 105 } 106 107 func TestExtractArtifactINoIdPresent(t *testing.T) { 108 t.Parallel() 109 110 text := ` 111 foo 112 bar 113 baz 114 blah 115 ` 116 117 _, err := extractArtifactID(text) 118 119 if err == nil { 120 t.Error("Expected to get an error when extracting an Artifact ID from text with no Artifact ID in it, but got nil") 121 } 122 } 123 124 func TestFormatPackerArgs(t *testing.T) { 125 t.Parallel() 126 127 tests := []struct { 128 option *Options 129 expected string 130 }{ 131 { 132 option: &Options{ 133 Template: "packer.json", 134 }, 135 expected: "build -machine-readable packer.json", 136 }, 137 { 138 option: &Options{ 139 Template: "packer.json", 140 Vars: map[string]string{ 141 "foo": "bar", 142 }, 143 Only: "onlythis", 144 }, 145 expected: "build -machine-readable -var foo=bar -only=onlythis packer.json", 146 }, 147 { 148 option: &Options{ 149 Template: "packer.json", 150 Vars: map[string]string{ 151 "foo": "bar", 152 }, 153 Only: "onlythis", 154 Except: "long-run-pp,artifact", 155 }, 156 expected: "build -machine-readable -var foo=bar -only=onlythis -except=long-run-pp,artifact packer.json", 157 }, 158 { 159 option: &Options{ 160 Template: "packer.json", 161 Vars: map[string]string{ 162 "foo": "bar", 163 }, 164 VarFiles: []string{ 165 "foofile.json", 166 }, 167 }, 168 expected: "build -machine-readable -var foo=bar -var-file foofile.json packer.json", 169 }, 170 } 171 172 for _, test := range tests { 173 args := formatPackerArgs(test.option) 174 assert.Equal(t, strings.Join(args, " "), test.expected) 175 } 176 }