github.com/rothwerx/packer@v0.9.0/post-processor/shell-local/post-processor_test.go (about) 1 package shell_local 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { 11 var _ packer.PostProcessor = new(PostProcessor) 12 } 13 14 func testConfig() map[string]interface{} { 15 return map[string]interface{}{ 16 "inline": []interface{}{"foo", "bar"}, 17 } 18 } 19 20 func TestPostProcessor_Impl(t *testing.T) { 21 var raw interface{} 22 raw = &PostProcessor{} 23 if _, ok := raw.(packer.PostProcessor); !ok { 24 t.Fatalf("must be a post processor") 25 } 26 } 27 28 func TestPostProcessorPrepare_Defaults(t *testing.T) { 29 var p PostProcessor 30 config := testConfig() 31 32 err := p.Configure(config) 33 if err != nil { 34 t.Fatalf("err: %s", err) 35 } 36 } 37 38 func TestPostProcessorPrepare_InlineShebang(t *testing.T) { 39 config := testConfig() 40 41 delete(config, "inline_shebang") 42 p := new(PostProcessor) 43 err := p.Configure(config) 44 if err != nil { 45 t.Fatalf("should not have error: %s", err) 46 } 47 48 if p.config.InlineShebang != "/bin/sh -e" { 49 t.Fatalf("bad value: %s", p.config.InlineShebang) 50 } 51 52 // Test with a good one 53 config["inline_shebang"] = "foo" 54 p = new(PostProcessor) 55 err = p.Configure(config) 56 if err != nil { 57 t.Fatalf("should not have error: %s", err) 58 } 59 60 if p.config.InlineShebang != "foo" { 61 t.Fatalf("bad value: %s", p.config.InlineShebang) 62 } 63 } 64 65 func TestPostProcessorPrepare_InvalidKey(t *testing.T) { 66 var p PostProcessor 67 config := testConfig() 68 69 // Add a random key 70 config["i_should_not_be_valid"] = true 71 err := p.Configure(config) 72 if err == nil { 73 t.Fatal("should have error") 74 } 75 } 76 77 func TestPostProcessorPrepare_Script(t *testing.T) { 78 config := testConfig() 79 delete(config, "inline") 80 81 config["script"] = "/this/should/not/exist" 82 p := new(PostProcessor) 83 err := p.Configure(config) 84 if err == nil { 85 t.Fatal("should have error") 86 } 87 88 // Test with a good one 89 tf, err := ioutil.TempFile("", "packer") 90 if err != nil { 91 t.Fatalf("error tempfile: %s", err) 92 } 93 defer os.Remove(tf.Name()) 94 95 config["script"] = tf.Name() 96 p = new(PostProcessor) 97 err = p.Configure(config) 98 if err != nil { 99 t.Fatalf("should not have error: %s", err) 100 } 101 } 102 103 func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) { 104 var p PostProcessor 105 config := testConfig() 106 107 delete(config, "inline") 108 delete(config, "script") 109 err := p.Configure(config) 110 if err == nil { 111 t.Fatal("should have error") 112 } 113 114 // Test with both 115 tf, err := ioutil.TempFile("", "packer") 116 if err != nil { 117 t.Fatalf("error tempfile: %s", err) 118 } 119 defer os.Remove(tf.Name()) 120 121 config["inline"] = []interface{}{"foo"} 122 config["script"] = tf.Name() 123 err = p.Configure(config) 124 if err == nil { 125 t.Fatal("should have error") 126 } 127 } 128 129 func TestPostProcessorPrepare_ScriptAndScripts(t *testing.T) { 130 var p PostProcessor 131 config := testConfig() 132 133 // Test with both 134 tf, err := ioutil.TempFile("", "packer") 135 if err != nil { 136 t.Fatalf("error tempfile: %s", err) 137 } 138 defer os.Remove(tf.Name()) 139 140 config["inline"] = []interface{}{"foo"} 141 config["scripts"] = []string{tf.Name()} 142 err = p.Configure(config) 143 if err == nil { 144 t.Fatal("should have error") 145 } 146 } 147 148 func TestPostProcessorPrepare_Scripts(t *testing.T) { 149 config := testConfig() 150 delete(config, "inline") 151 152 config["scripts"] = []string{} 153 p := new(PostProcessor) 154 err := p.Configure(config) 155 if err == nil { 156 t.Fatal("should have error") 157 } 158 159 // Test with a good one 160 tf, err := ioutil.TempFile("", "packer") 161 if err != nil { 162 t.Fatalf("error tempfile: %s", err) 163 } 164 defer os.Remove(tf.Name()) 165 166 config["scripts"] = []string{tf.Name()} 167 p = new(PostProcessor) 168 err = p.Configure(config) 169 if err != nil { 170 t.Fatalf("should not have error: %s", err) 171 } 172 } 173 174 func TestPostProcessorPrepare_EnvironmentVars(t *testing.T) { 175 config := testConfig() 176 177 // Test with a bad case 178 config["environment_vars"] = []string{"badvar", "good=var"} 179 p := new(PostProcessor) 180 err := p.Configure(config) 181 if err == nil { 182 t.Fatal("should have error") 183 } 184 185 // Test with a trickier case 186 config["environment_vars"] = []string{"=bad"} 187 p = new(PostProcessor) 188 err = p.Configure(config) 189 if err == nil { 190 t.Fatal("should have error") 191 } 192 193 // Test with a good case 194 // Note: baz= is a real env variable, just empty 195 config["environment_vars"] = []string{"FOO=bar", "baz="} 196 p = new(PostProcessor) 197 err = p.Configure(config) 198 if err != nil { 199 t.Fatalf("should not have error: %s", err) 200 } 201 } 202 203 func TestPostProcessorQuote_EnvironmentVars(t *testing.T) { 204 config := testConfig() 205 206 config["environment_vars"] = []string{"keyone=valueone", "keytwo=value\ntwo"} 207 p := new(PostProcessor) 208 p.Configure(config) 209 210 expectedValue := "keyone='valueone'" 211 if p.config.Vars[0] != expectedValue { 212 t.Fatalf("%s should be equal to %s", p.config.Vars[0], expectedValue) 213 } 214 215 expectedValue = "keytwo='value\ntwo'" 216 if p.config.Vars[1] != expectedValue { 217 t.Fatalf("%s should be equal to %s", p.config.Vars[1], expectedValue) 218 } 219 }