github.com/vijayrajah/packer@v1.3.2/post-processor/shell-local/post-processor_test.go (about) 1 package shell_local 2 3 import ( 4 "io/ioutil" 5 "os" 6 "runtime" 7 "testing" 8 9 "github.com/hashicorp/packer/packer" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { 14 var _ packer.PostProcessor = new(PostProcessor) 15 } 16 17 func testConfig() map[string]interface{} { 18 return map[string]interface{}{ 19 "inline": []interface{}{"foo", "bar"}, 20 } 21 } 22 23 func TestPostProcessor_Impl(t *testing.T) { 24 var raw interface{} 25 raw = &PostProcessor{} 26 if _, ok := raw.(packer.PostProcessor); !ok { 27 t.Fatalf("must be a post processor") 28 } 29 } 30 31 func TestPostProcessorPrepare_Defaults(t *testing.T) { 32 var p PostProcessor 33 raws := testConfig() 34 35 err := p.Configure(raws) 36 if err != nil { 37 t.Fatalf("err: %s", err) 38 } 39 } 40 41 func TestPostProcessorPrepare_InlineShebang(t *testing.T) { 42 raws := testConfig() 43 44 delete(raws, "inline_shebang") 45 p := new(PostProcessor) 46 err := p.Configure(raws) 47 if err != nil { 48 t.Fatalf("should not have error: %s", err) 49 } 50 expected := "" 51 if runtime.GOOS != "windows" { 52 expected = "/bin/sh -e" 53 } 54 if p.config.InlineShebang != expected { 55 t.Fatalf("bad value: %s", p.config.InlineShebang) 56 } 57 58 // Test with a good one 59 raws["inline_shebang"] = "foo" 60 p = new(PostProcessor) 61 err = p.Configure(raws) 62 if err != nil { 63 t.Fatalf("should not have error: %s", err) 64 } 65 66 if p.config.InlineShebang != "foo" { 67 t.Fatalf("bad value: %s", p.config.InlineShebang) 68 } 69 } 70 71 func TestPostProcessorPrepare_InvalidKey(t *testing.T) { 72 var p PostProcessor 73 raws := testConfig() 74 75 // Add a random key 76 raws["i_should_not_be_valid"] = true 77 err := p.Configure(raws) 78 if err == nil { 79 t.Fatal("should have error") 80 } 81 } 82 83 func TestPostProcessorPrepare_Script(t *testing.T) { 84 raws := testConfig() 85 delete(raws, "inline") 86 87 raws["script"] = "/this/should/not/exist" 88 p := new(PostProcessor) 89 err := p.Configure(raws) 90 if err == nil { 91 t.Fatal("should have error") 92 } 93 94 // Test with a good one 95 tf, err := ioutil.TempFile("", "packer") 96 if err != nil { 97 t.Fatalf("error tempfile: %s", err) 98 } 99 defer os.Remove(tf.Name()) 100 101 raws["script"] = tf.Name() 102 p = new(PostProcessor) 103 err = p.Configure(raws) 104 if err != nil { 105 t.Fatalf("should not have error: %s", err) 106 } 107 } 108 109 func TestPostProcessorPrepare_ExecuteCommand(t *testing.T) { 110 // Check that passing a string will work (Backwards Compatibility) 111 p := new(PostProcessor) 112 raws := testConfig() 113 raws["execute_command"] = "foo bar" 114 err := p.Configure(raws) 115 expected := []string{"sh", "-c", "foo bar"} 116 if err != nil { 117 t.Fatalf("should handle backwards compatibility: %s", err) 118 } 119 assert.Equal(t, p.config.ExecuteCommand, expected, 120 "Did not get expected execute_command: expected: %#v; received %#v", expected, p.config.ExecuteCommand) 121 122 // Check that passing a list will work 123 p = new(PostProcessor) 124 raws = testConfig() 125 raws["execute_command"] = []string{"foo", "bar"} 126 err = p.Configure(raws) 127 if err != nil { 128 t.Fatalf("should handle backwards compatibility: %s", err) 129 } 130 expected = []string{"foo", "bar"} 131 assert.Equal(t, p.config.ExecuteCommand, expected, 132 "Did not get expected execute_command: expected: %#v; received %#v", expected, p.config.ExecuteCommand) 133 134 // Check that default is as expected 135 raws = testConfig() 136 delete(raws, "execute_command") 137 p = new(PostProcessor) 138 p.Configure(raws) 139 if runtime.GOOS != "windows" { 140 expected = []string{"/bin/sh", "-c", "{{.Vars}} {{.Script}}"} 141 } else { 142 expected = []string{"cmd", "/V", "/C", "{{.Vars}}", "call", "{{.Script}}"} 143 } 144 assert.Equal(t, p.config.ExecuteCommand, expected, 145 "Did not get expected default: expected: %#v; received %#v", expected, p.config.ExecuteCommand) 146 } 147 148 func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) { 149 var p PostProcessor 150 raws := testConfig() 151 152 // Error if no scripts/inline commands provided 153 delete(raws, "inline") 154 delete(raws, "script") 155 delete(raws, "command") 156 delete(raws, "scripts") 157 err := p.Configure(raws) 158 if err == nil { 159 t.Fatalf("should error when no scripts/inline commands are provided") 160 } 161 162 // Test with both 163 tf, err := ioutil.TempFile("", "packer") 164 if err != nil { 165 t.Fatalf("error tempfile: %s", err) 166 } 167 defer os.Remove(tf.Name()) 168 169 raws["inline"] = []interface{}{"foo"} 170 raws["script"] = tf.Name() 171 err = p.Configure(raws) 172 if err == nil { 173 t.Fatal("should have error") 174 } 175 } 176 177 func TestPostProcessorPrepare_ScriptAndScripts(t *testing.T) { 178 var p PostProcessor 179 raws := testConfig() 180 181 // Test with both 182 tf, err := ioutil.TempFile("", "packer") 183 if err != nil { 184 t.Fatalf("error tempfile: %s", err) 185 } 186 defer os.Remove(tf.Name()) 187 188 raws["inline"] = []interface{}{"foo"} 189 raws["scripts"] = []string{tf.Name()} 190 err = p.Configure(raws) 191 if err == nil { 192 t.Fatal("should have error") 193 } 194 } 195 196 func TestPostProcessorPrepare_Scripts(t *testing.T) { 197 raws := testConfig() 198 delete(raws, "inline") 199 200 raws["scripts"] = []string{} 201 p := new(PostProcessor) 202 err := p.Configure(raws) 203 if err == nil { 204 t.Fatal("should have error") 205 } 206 207 // Test with a good one 208 tf, err := ioutil.TempFile("", "packer") 209 if err != nil { 210 t.Fatalf("error tempfile: %s", err) 211 } 212 defer os.Remove(tf.Name()) 213 214 raws["scripts"] = []string{tf.Name()} 215 p = new(PostProcessor) 216 err = p.Configure(raws) 217 if err != nil { 218 t.Fatalf("should not have error: %s", err) 219 } 220 } 221 222 func TestPostProcessorPrepare_EnvironmentVars(t *testing.T) { 223 raws := testConfig() 224 225 // Test with a bad case 226 raws["environment_vars"] = []string{"badvar", "good=var"} 227 p := new(PostProcessor) 228 err := p.Configure(raws) 229 if err == nil { 230 t.Fatal("should have error") 231 } 232 233 // Test with a trickier case 234 raws["environment_vars"] = []string{"=bad"} 235 p = new(PostProcessor) 236 err = p.Configure(raws) 237 if err == nil { 238 t.Fatal("should have error") 239 } 240 241 // Test with a good case 242 // Note: baz= is a real env variable, just empty 243 raws["environment_vars"] = []string{"FOO=bar", "baz="} 244 p = new(PostProcessor) 245 err = p.Configure(raws) 246 if err != nil { 247 t.Fatalf("should not have error: %s", err) 248 } 249 250 // Test when the env variable value contains an equals sign 251 raws["environment_vars"] = []string{"good=withequals=true"} 252 p = new(PostProcessor) 253 err = p.Configure(raws) 254 if err != nil { 255 t.Fatalf("should not have error: %s", err) 256 } 257 258 // Test when the env variable value starts with an equals sign 259 raws["environment_vars"] = []string{"good==true"} 260 p = new(PostProcessor) 261 err = p.Configure(raws) 262 if err != nil { 263 t.Fatalf("should not have error: %s", err) 264 } 265 }