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