github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/provisioners/chef/linux_provisioner_test.go (about) 1 package chef 2 3 import ( 4 "fmt" 5 "path" 6 "testing" 7 8 "github.com/hashicorp/terraform/communicator" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestResourceProvider_linuxInstallChefClient(t *testing.T) { 13 cases := map[string]struct { 14 Config *terraform.ResourceConfig 15 Commands map[string]bool 16 }{ 17 "Sudo": { 18 Config: testConfig(t, map[string]interface{}{ 19 "node_name": "nodename1", 20 "run_list": []interface{}{"cookbook::recipe"}, 21 "server_url": "https://chef.local", 22 "validation_client_name": "validator", 23 "validation_key_path": "validator.pem", 24 }), 25 26 Commands: map[string]bool{ 27 "sudo curl -LO https://www.chef.io/chef/install.sh": true, 28 "sudo bash ./install.sh -v \"\"": true, 29 "sudo rm -f install.sh": true, 30 }, 31 }, 32 33 "NoSudo": { 34 Config: testConfig(t, map[string]interface{}{ 35 "node_name": "nodename1", 36 "prevent_sudo": true, 37 "run_list": []interface{}{"cookbook::recipe"}, 38 "server_url": "https://chef.local", 39 "validation_client_name": "validator", 40 "validation_key_path": "validator.pem", 41 "secret_key_path": "encrypted_data_bag_secret", 42 }), 43 44 Commands: map[string]bool{ 45 "curl -LO https://www.chef.io/chef/install.sh": true, 46 "bash ./install.sh -v \"\"": true, 47 "rm -f install.sh": true, 48 }, 49 }, 50 51 "HTTPProxy": { 52 Config: testConfig(t, map[string]interface{}{ 53 "http_proxy": "http://proxy.local", 54 "node_name": "nodename1", 55 "prevent_sudo": true, 56 "run_list": []interface{}{"cookbook::recipe"}, 57 "server_url": "https://chef.local", 58 "validation_client_name": "validator", 59 "validation_key_path": "validator.pem", 60 }), 61 62 Commands: map[string]bool{ 63 "http_proxy='http://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true, 64 "http_proxy='http://proxy.local' bash ./install.sh -v \"\"": true, 65 "http_proxy='http://proxy.local' rm -f install.sh": true, 66 }, 67 }, 68 69 "HTTPSProxy": { 70 Config: testConfig(t, map[string]interface{}{ 71 "https_proxy": "https://proxy.local", 72 "node_name": "nodename1", 73 "prevent_sudo": true, 74 "run_list": []interface{}{"cookbook::recipe"}, 75 "server_url": "https://chef.local", 76 "validation_client_name": "validator", 77 "validation_key_path": "validator.pem", 78 }), 79 80 Commands: map[string]bool{ 81 "https_proxy='https://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true, 82 "https_proxy='https://proxy.local' bash ./install.sh -v \"\"": true, 83 "https_proxy='https://proxy.local' rm -f install.sh": true, 84 }, 85 }, 86 87 "NoProxy": { 88 Config: testConfig(t, map[string]interface{}{ 89 "http_proxy": "http://proxy.local", 90 "no_proxy": []interface{}{"http://local.local", "http://local.org"}, 91 "node_name": "nodename1", 92 "prevent_sudo": true, 93 "run_list": []interface{}{"cookbook::recipe"}, 94 "server_url": "https://chef.local", 95 "validation_client_name": "validator", 96 "validation_key_path": "validator.pem", 97 }), 98 99 Commands: map[string]bool{ 100 "http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " + 101 "curl -LO https://www.chef.io/chef/install.sh": true, 102 "http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " + 103 "bash ./install.sh -v \"\"": true, 104 "http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " + 105 "rm -f install.sh": true, 106 }, 107 }, 108 109 "Version": { 110 Config: testConfig(t, map[string]interface{}{ 111 "node_name": "nodename1", 112 "prevent_sudo": true, 113 "run_list": []interface{}{"cookbook::recipe"}, 114 "server_url": "https://chef.local", 115 "validation_client_name": "validator", 116 "validation_key_path": "validator.pem", 117 "version": "11.18.6", 118 }), 119 120 Commands: map[string]bool{ 121 "curl -LO https://www.chef.io/chef/install.sh": true, 122 "bash ./install.sh -v \"11.18.6\"": true, 123 "rm -f install.sh": true, 124 }, 125 }, 126 } 127 128 r := new(ResourceProvisioner) 129 o := new(terraform.MockUIOutput) 130 c := new(communicator.MockCommunicator) 131 132 for k, tc := range cases { 133 c.Commands = tc.Commands 134 135 p, err := r.decodeConfig(tc.Config) 136 if err != nil { 137 t.Fatalf("Error: %v", err) 138 } 139 140 p.useSudo = !p.PreventSudo 141 142 err = p.linuxInstallChefClient(o, c) 143 if err != nil { 144 t.Fatalf("Test %q failed: %v", k, err) 145 } 146 } 147 } 148 149 func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) { 150 cases := map[string]struct { 151 Config *terraform.ResourceConfig 152 Commands map[string]bool 153 Uploads map[string]string 154 }{ 155 "Sudo": { 156 Config: testConfig(t, map[string]interface{}{ 157 "ohai_hints": []interface{}{"test-fixtures/ohaihint.json"}, 158 "node_name": "nodename1", 159 "run_list": []interface{}{"cookbook::recipe"}, 160 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 161 "server_url": "https://chef.local", 162 "validation_client_name": "validator", 163 "validation_key_path": "test-fixtures/validator.pem", 164 }), 165 166 Commands: map[string]bool{ 167 "sudo mkdir -p " + linuxConfDir: true, 168 "sudo chmod 777 " + linuxConfDir: true, 169 "sudo " + fmt.Sprintf(chmod, linuxConfDir, 666): true, 170 "sudo mkdir -p " + path.Join(linuxConfDir, "ohai/hints"): true, 171 "sudo chmod 777 " + path.Join(linuxConfDir, "ohai/hints"): true, 172 "sudo " + fmt.Sprintf(chmod, path.Join(linuxConfDir, "ohai/hints"), 666): true, 173 "sudo chmod 755 " + path.Join(linuxConfDir, "ohai/hints"): true, 174 "sudo " + fmt.Sprintf(chmod, path.Join(linuxConfDir, "ohai/hints"), 600): true, 175 "sudo chown -R root.root " + path.Join(linuxConfDir, "ohai/hints"): true, 176 "sudo chmod 755 " + linuxConfDir: true, 177 "sudo " + fmt.Sprintf(chmod, linuxConfDir, 600): true, 178 "sudo chown -R root.root " + linuxConfDir: true, 179 }, 180 181 Uploads: map[string]string{ 182 linuxConfDir + "/client.rb": defaultLinuxClientConf, 183 linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 184 linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`, 185 linuxConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE", 186 linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 187 }, 188 }, 189 190 "NoSudo": { 191 Config: testConfig(t, map[string]interface{}{ 192 "node_name": "nodename1", 193 "prevent_sudo": true, 194 "run_list": []interface{}{"cookbook::recipe"}, 195 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 196 "server_url": "https://chef.local", 197 "validation_client_name": "validator", 198 "validation_key_path": "test-fixtures/validator.pem", 199 }), 200 201 Commands: map[string]bool{ 202 "mkdir -p " + linuxConfDir: true, 203 }, 204 205 Uploads: map[string]string{ 206 linuxConfDir + "/client.rb": defaultLinuxClientConf, 207 linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 208 linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`, 209 linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 210 }, 211 }, 212 213 "Proxy": { 214 Config: testConfig(t, map[string]interface{}{ 215 "http_proxy": "http://proxy.local", 216 "https_proxy": "https://proxy.local", 217 "no_proxy": []interface{}{"http://local.local", "https://local.local"}, 218 "node_name": "nodename1", 219 "prevent_sudo": true, 220 "run_list": []interface{}{"cookbook::recipe"}, 221 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 222 "server_url": "https://chef.local", 223 "validation_client_name": "validator", 224 "validation_key_path": "test-fixtures/validator.pem", 225 }), 226 227 Commands: map[string]bool{ 228 "mkdir -p " + linuxConfDir: true, 229 }, 230 231 Uploads: map[string]string{ 232 linuxConfDir + "/client.rb": proxyLinuxClientConf, 233 linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 234 linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`, 235 linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 236 }, 237 }, 238 239 "Attributes": { 240 Config: testConfig(t, map[string]interface{}{ 241 "attributes": []map[string]interface{}{ 242 map[string]interface{}{ 243 "key1": []map[string]interface{}{ 244 map[string]interface{}{ 245 "subkey1": []map[string]interface{}{ 246 map[string]interface{}{ 247 "subkey2a": []interface{}{ 248 "val1", "val2", "val3", 249 }, 250 "subkey2b": []map[string]interface{}{ 251 map[string]interface{}{ 252 "subkey3": "value3", 253 }, 254 }, 255 }, 256 }, 257 }, 258 }, 259 "key2": "value2", 260 }, 261 }, 262 "node_name": "nodename1", 263 "prevent_sudo": true, 264 "run_list": []interface{}{"cookbook::recipe"}, 265 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 266 "server_url": "https://chef.local", 267 "validation_client_name": "validator", 268 "validation_key_path": "test-fixtures/validator.pem", 269 }), 270 271 Commands: map[string]bool{ 272 "mkdir -p " + linuxConfDir: true, 273 }, 274 275 Uploads: map[string]string{ 276 linuxConfDir + "/client.rb": defaultLinuxClientConf, 277 linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 278 linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 279 linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + 280 `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, 281 }, 282 }, 283 284 "Attributes JSON": { 285 Config: testConfig(t, map[string]interface{}{ 286 "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + 287 `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, 288 "node_name": "nodename1", 289 "prevent_sudo": true, 290 "run_list": []interface{}{"cookbook::recipe"}, 291 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 292 "server_url": "https://chef.local", 293 "validation_client_name": "validator", 294 "validation_key_path": "test-fixtures/validator.pem", 295 }), 296 297 Commands: map[string]bool{ 298 "mkdir -p " + linuxConfDir: true, 299 }, 300 301 Uploads: map[string]string{ 302 linuxConfDir + "/client.rb": defaultLinuxClientConf, 303 linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 304 linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 305 linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + 306 `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, 307 }, 308 }, 309 } 310 311 r := new(ResourceProvisioner) 312 o := new(terraform.MockUIOutput) 313 c := new(communicator.MockCommunicator) 314 315 for k, tc := range cases { 316 c.Commands = tc.Commands 317 c.Uploads = tc.Uploads 318 319 p, err := r.decodeConfig(tc.Config) 320 if err != nil { 321 t.Fatalf("Error: %v", err) 322 } 323 324 p.useSudo = !p.PreventSudo 325 326 err = p.linuxCreateConfigFiles(o, c) 327 if err != nil { 328 t.Fatalf("Test %q failed: %v", k, err) 329 } 330 } 331 } 332 333 const defaultLinuxClientConf = `log_location STDOUT 334 chef_server_url "https://chef.local" 335 validation_client_name "validator" 336 node_name "nodename1"` 337 338 const proxyLinuxClientConf = `log_location STDOUT 339 chef_server_url "https://chef.local" 340 validation_client_name "validator" 341 node_name "nodename1" 342 343 344 345 346 http_proxy "http://proxy.local" 347 ENV['http_proxy'] = "http://proxy.local" 348 ENV['HTTP_PROXY'] = "http://proxy.local" 349 350 351 352 https_proxy "https://proxy.local" 353 ENV['https_proxy'] = "https://proxy.local" 354 ENV['HTTPS_PROXY'] = "https://proxy.local" 355 356 357 358 no_proxy "http://local.local,https://local.local" 359 ENV['no_proxy'] = "http://local.local,https://local.local"`