github.com/dacamp/packer@v0.10.2/provisioner/chef-solo/provisioner_test.go (about) 1 package chefsolo 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func testConfig() map[string]interface{} { 11 return map[string]interface{}{} 12 } 13 14 func TestProvisioner_Impl(t *testing.T) { 15 var raw interface{} 16 raw = &Provisioner{} 17 if _, ok := raw.(packer.Provisioner); !ok { 18 t.Fatalf("must be a Provisioner") 19 } 20 } 21 22 func TestProvisionerPrepare_chefEnvironment(t *testing.T) { 23 var p Provisioner 24 25 config := testConfig() 26 config["chef_environment"] = "some-env" 27 28 err := p.Prepare(config) 29 if err != nil { 30 t.Fatalf("err: %s", err) 31 } 32 33 if p.config.ChefEnvironment != "some-env" { 34 t.Fatalf("unexpected: %#v", p.config.ChefEnvironment) 35 } 36 } 37 38 func TestProvisionerPrepare_configTemplate(t *testing.T) { 39 var err error 40 var p Provisioner 41 42 // Test no config template 43 config := testConfig() 44 delete(config, "config_template") 45 err = p.Prepare(config) 46 if err != nil { 47 t.Fatalf("err: %s", err) 48 } 49 50 // Test with a file 51 tf, err := ioutil.TempFile("", "packer") 52 if err != nil { 53 t.Fatalf("err: %s", err) 54 } 55 defer os.Remove(tf.Name()) 56 57 config = testConfig() 58 config["config_template"] = tf.Name() 59 p = Provisioner{} 60 err = p.Prepare(config) 61 if err != nil { 62 t.Fatalf("err: %s", err) 63 } 64 65 // Test with a directory 66 td, err := ioutil.TempDir("", "packer") 67 if err != nil { 68 t.Fatalf("err: %s", err) 69 } 70 defer os.RemoveAll(td) 71 72 config = testConfig() 73 config["config_template"] = td 74 p = Provisioner{} 75 err = p.Prepare(config) 76 if err == nil { 77 t.Fatal("should have err") 78 } 79 } 80 81 func TestProvisionerPrepare_cookbookPaths(t *testing.T) { 82 var p Provisioner 83 84 path1, err := ioutil.TempDir("", "cookbooks_one") 85 if err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 89 path2, err := ioutil.TempDir("", "cookbooks_two") 90 if err != nil { 91 t.Fatalf("err: %s", err) 92 } 93 94 rolesPath, err := ioutil.TempDir("", "roles") 95 if err != nil { 96 t.Fatalf("err: %s", err) 97 } 98 99 dataBagsPath, err := ioutil.TempDir("", "data_bags") 100 if err != nil { 101 t.Fatalf("err: %s", err) 102 } 103 104 defer os.Remove(path1) 105 defer os.Remove(path2) 106 defer os.Remove(rolesPath) 107 defer os.Remove(dataBagsPath) 108 109 config := testConfig() 110 config["cookbook_paths"] = []string{path1, path2} 111 config["roles_path"] = rolesPath 112 config["data_bags_path"] = dataBagsPath 113 114 err = p.Prepare(config) 115 if err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 if len(p.config.CookbookPaths) != 2 { 120 t.Fatalf("unexpected: %#v", p.config.CookbookPaths) 121 } 122 123 if p.config.CookbookPaths[0] != path1 || p.config.CookbookPaths[1] != path2 { 124 t.Fatalf("unexpected: %#v", p.config.CookbookPaths) 125 } 126 127 if p.config.RolesPath != rolesPath { 128 t.Fatalf("unexpected: %#v", p.config.RolesPath) 129 } 130 131 if p.config.DataBagsPath != dataBagsPath { 132 t.Fatalf("unexpected: %#v", p.config.DataBagsPath) 133 } 134 } 135 136 func TestProvisionerPrepare_dataBagsPath(t *testing.T) { 137 var p Provisioner 138 139 dataBagsPath, err := ioutil.TempDir("", "data_bags") 140 if err != nil { 141 t.Fatalf("err: %s", err) 142 } 143 defer os.Remove(dataBagsPath) 144 145 config := testConfig() 146 config["data_bags_path"] = dataBagsPath 147 148 err = p.Prepare(config) 149 if err != nil { 150 t.Fatalf("err: %s", err) 151 } 152 153 if p.config.DataBagsPath != dataBagsPath { 154 t.Fatalf("unexpected: %#v", p.config.DataBagsPath) 155 } 156 } 157 158 func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { 159 var err error 160 var p Provisioner 161 162 // Test no config template 163 config := testConfig() 164 delete(config, "encrypted_data_bag_secret_path") 165 err = p.Prepare(config) 166 if err != nil { 167 t.Fatalf("err: %s", err) 168 } 169 170 // Test with a file 171 tf, err := ioutil.TempFile("", "packer") 172 if err != nil { 173 t.Fatalf("err: %s", err) 174 } 175 defer os.Remove(tf.Name()) 176 177 config = testConfig() 178 config["encrypted_data_bag_secret_path"] = tf.Name() 179 p = Provisioner{} 180 err = p.Prepare(config) 181 if err != nil { 182 t.Fatalf("err: %s", err) 183 } 184 185 // Test with a directory 186 td, err := ioutil.TempDir("", "packer") 187 if err != nil { 188 t.Fatalf("err: %s", err) 189 } 190 defer os.RemoveAll(td) 191 192 config = testConfig() 193 config["encrypted_data_bag_secret_path"] = td 194 p = Provisioner{} 195 err = p.Prepare(config) 196 if err == nil { 197 t.Fatal("should have err") 198 } 199 } 200 201 func TestProvisionerPrepare_environmentsPath(t *testing.T) { 202 var p Provisioner 203 204 environmentsPath, err := ioutil.TempDir("", "environments") 205 if err != nil { 206 t.Fatalf("err: %s", err) 207 } 208 defer os.Remove(environmentsPath) 209 210 config := testConfig() 211 config["environments_path"] = environmentsPath 212 213 err = p.Prepare(config) 214 if err != nil { 215 t.Fatalf("err: %s", err) 216 } 217 218 if p.config.EnvironmentsPath != environmentsPath { 219 t.Fatalf("unexpected: %#v", p.config.EnvironmentsPath) 220 } 221 } 222 223 func TestProvisionerPrepare_rolesPath(t *testing.T) { 224 var p Provisioner 225 226 rolesPath, err := ioutil.TempDir("", "roles") 227 if err != nil { 228 t.Fatalf("err: %s", err) 229 } 230 defer os.Remove(rolesPath) 231 232 config := testConfig() 233 config["roles_path"] = rolesPath 234 235 err = p.Prepare(config) 236 if err != nil { 237 t.Fatalf("err: %s", err) 238 } 239 240 if p.config.RolesPath != rolesPath { 241 t.Fatalf("unexpected: %#v", p.config.RolesPath) 242 } 243 } 244 245 func TestProvisionerPrepare_json(t *testing.T) { 246 config := testConfig() 247 config["json"] = map[string]interface{}{ 248 "foo": "{{ user `foo` }}", 249 } 250 251 config[packer.UserVariablesConfigKey] = map[string]string{ 252 "foo": `"bar\baz"`, 253 } 254 255 var p Provisioner 256 err := p.Prepare(config) 257 if err != nil { 258 t.Fatalf("err: %s", err) 259 } 260 261 if p.config.Json["foo"] != `"bar\baz"` { 262 t.Fatalf("bad: %#v", p.config.Json) 263 } 264 } 265 266 func TestProvisionerPrepare_jsonNested(t *testing.T) { 267 config := testConfig() 268 config["json"] = map[string]interface{}{ 269 "foo": map[interface{}]interface{}{ 270 "bar": []uint8("baz"), 271 }, 272 273 "bar": []interface{}{ 274 "foo", 275 276 map[interface{}]interface{}{ 277 "bar": "baz", 278 }, 279 }, 280 281 "bFalse": false, 282 "bTrue": true, 283 "bNil": nil, 284 "bStr": []uint8("bar"), 285 286 "bInt": 1, 287 "bFloat": 4.5, 288 } 289 290 var p Provisioner 291 err := p.Prepare(config) 292 if err != nil { 293 t.Fatalf("err: %s", err) 294 } 295 296 fooMap := p.config.Json["foo"].(map[string]interface{}) 297 if fooMap["bar"] != "baz" { 298 t.Fatalf("nope: %#v", fooMap["bar"]) 299 } 300 if p.config.Json["bStr"] != "bar" { 301 t.Fatalf("nope: %#v", fooMap["bar"]) 302 } 303 }