github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/provisioners/chef/windows_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_windowsInstallChefClient(t *testing.T) { 13 cases := map[string]struct { 14 Config *terraform.ResourceConfig 15 Commands map[string]bool 16 UploadScripts map[string]string 17 }{ 18 "Default": { 19 Config: testConfig(t, map[string]interface{}{ 20 "node_name": "nodename1", 21 "run_list": []interface{}{"cookbook::recipe"}, 22 "server_url": "https://chef.local", 23 "validation_client_name": "validator", 24 "validation_key_path": "validator.pem", 25 }), 26 27 Commands: map[string]bool{ 28 "powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true, 29 }, 30 31 UploadScripts: map[string]string{ 32 "ChefClient.ps1": defaultWindowsInstallScript, 33 }, 34 }, 35 36 "Proxy": { 37 Config: testConfig(t, map[string]interface{}{ 38 "http_proxy": "http://proxy.local", 39 "no_proxy": []interface{}{"http://local.local", "http://local.org"}, 40 "node_name": "nodename1", 41 "run_list": []interface{}{"cookbook::recipe"}, 42 "server_url": "https://chef.local", 43 "validation_client_name": "validator", 44 "validation_key_path": "validator.pem", 45 }), 46 47 Commands: map[string]bool{ 48 "powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true, 49 }, 50 51 UploadScripts: map[string]string{ 52 "ChefClient.ps1": proxyWindowsInstallScript, 53 }, 54 }, 55 56 "Version": { 57 Config: testConfig(t, map[string]interface{}{ 58 "node_name": "nodename1", 59 "run_list": []interface{}{"cookbook::recipe"}, 60 "server_url": "https://chef.local", 61 "validation_client_name": "validator", 62 "validation_key_path": "validator.pem", 63 "version": "11.18.6", 64 }), 65 66 Commands: map[string]bool{ 67 "powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true, 68 }, 69 70 UploadScripts: map[string]string{ 71 "ChefClient.ps1": versionWindowsInstallScript, 72 }, 73 }, 74 } 75 76 r := new(ResourceProvisioner) 77 o := new(terraform.MockUIOutput) 78 c := new(communicator.MockCommunicator) 79 80 for k, tc := range cases { 81 c.Commands = tc.Commands 82 c.UploadScripts = tc.UploadScripts 83 84 p, err := r.decodeConfig(tc.Config) 85 if err != nil { 86 t.Fatalf("Error: %v", err) 87 } 88 89 p.useSudo = false 90 91 err = p.windowsInstallChefClient(o, c) 92 if err != nil { 93 t.Fatalf("Test %q failed: %v", k, err) 94 } 95 } 96 } 97 98 func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) { 99 cases := map[string]struct { 100 Config *terraform.ResourceConfig 101 Commands map[string]bool 102 Uploads map[string]string 103 }{ 104 "Default": { 105 Config: testConfig(t, map[string]interface{}{ 106 "ohai_hints": []interface{}{"test-fixtures/ohaihint.json"}, 107 "node_name": "nodename1", 108 "run_list": []interface{}{"cookbook::recipe"}, 109 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 110 "server_url": "https://chef.local", 111 "validation_client_name": "validator", 112 "validation_key_path": "test-fixtures/validator.pem", 113 }), 114 115 Commands: map[string]bool{ 116 fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true, 117 fmt.Sprintf("cmd /c if not exist %q mkdir %q", 118 path.Join(windowsConfDir, "ohai/hints"), 119 path.Join(windowsConfDir, "ohai/hints")): true, 120 }, 121 122 Uploads: map[string]string{ 123 windowsConfDir + "/client.rb": defaultWindowsClientConf, 124 windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 125 windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`, 126 windowsConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE", 127 windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 128 }, 129 }, 130 131 "Proxy": { 132 Config: testConfig(t, map[string]interface{}{ 133 "http_proxy": "http://proxy.local", 134 "https_proxy": "https://proxy.local", 135 "no_proxy": []interface{}{"http://local.local", "https://local.local"}, 136 "node_name": "nodename1", 137 "run_list": []interface{}{"cookbook::recipe"}, 138 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 139 "server_url": "https://chef.local", 140 "ssl_verify_mode": "verify_none", 141 "validation_client_name": "validator", 142 "validation_key_path": "test-fixtures/validator.pem", 143 }), 144 145 Commands: map[string]bool{ 146 fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true, 147 }, 148 149 Uploads: map[string]string{ 150 windowsConfDir + "/client.rb": proxyWindowsClientConf, 151 windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`, 152 windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 153 windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 154 }, 155 }, 156 157 "Attributes": { 158 Config: testConfig(t, map[string]interface{}{ 159 "attributes": []map[string]interface{}{ 160 map[string]interface{}{ 161 "key1": []map[string]interface{}{ 162 map[string]interface{}{ 163 "subkey1": []map[string]interface{}{ 164 map[string]interface{}{ 165 "subkey2a": []interface{}{ 166 "val1", "val2", "val3", 167 }, 168 "subkey2b": []map[string]interface{}{ 169 map[string]interface{}{ 170 "subkey3": "value3", 171 }, 172 }, 173 }, 174 }, 175 }, 176 }, 177 "key2": "value2", 178 }, 179 }, 180 "node_name": "nodename1", 181 "run_list": []interface{}{"cookbook::recipe"}, 182 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 183 "server_url": "https://chef.local", 184 "validation_client_name": "validator", 185 "validation_key_path": "test-fixtures/validator.pem", 186 }), 187 188 Commands: map[string]bool{ 189 fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true, 190 }, 191 192 Uploads: map[string]string{ 193 windowsConfDir + "/client.rb": defaultWindowsClientConf, 194 windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 195 windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 196 windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + 197 `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, 198 }, 199 }, 200 201 "Attributes JSON": { 202 Config: testConfig(t, map[string]interface{}{ 203 "attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + 204 `"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`, 205 "node_name": "nodename1", 206 "run_list": []interface{}{"cookbook::recipe"}, 207 "secret_key_path": "test-fixtures/encrypted_data_bag_secret", 208 "server_url": "https://chef.local", 209 "validation_client_name": "validator", 210 "validation_key_path": "test-fixtures/validator.pem", 211 }), 212 213 Commands: map[string]bool{ 214 fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true, 215 }, 216 217 Uploads: map[string]string{ 218 windowsConfDir + "/client.rb": defaultWindowsClientConf, 219 windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE", 220 windowsConfDir + "/validation.pem": "VALIDATOR-PEM-FILE", 221 windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` + 222 `"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`, 223 }, 224 }, 225 } 226 227 r := new(ResourceProvisioner) 228 o := new(terraform.MockUIOutput) 229 c := new(communicator.MockCommunicator) 230 231 for k, tc := range cases { 232 c.Commands = tc.Commands 233 c.Uploads = tc.Uploads 234 235 p, err := r.decodeConfig(tc.Config) 236 if err != nil { 237 t.Fatalf("Error: %v", err) 238 } 239 240 p.useSudo = false 241 242 err = p.windowsCreateConfigFiles(o, c) 243 if err != nil { 244 t.Fatalf("Test %q failed: %v", k, err) 245 } 246 } 247 } 248 249 const defaultWindowsInstallScript = ` 250 $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor} 251 252 switch ($winver) 253 { 254 "6.0" {$machine_os = "2008"} 255 "6.1" {$machine_os = "2008r2"} 256 "6.2" {$machine_os = "2012"} 257 "6.3" {$machine_os = "2012"} 258 default {$machine_os = "2008r2"} 259 } 260 261 if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"} 262 263 $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=" 264 $dest = [System.IO.Path]::GetTempFileName() 265 $dest = [System.IO.Path]::ChangeExtension($dest, ".msi") 266 $downloader = New-Object System.Net.WebClient 267 268 $http_proxy = '' 269 if ($http_proxy -ne '') { 270 $no_proxy = '' 271 if ($no_proxy -eq ''){ 272 $no_proxy = "127.0.0.1" 273 } 274 275 $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(',')) 276 $downloader.proxy = $proxy 277 } 278 279 Write-Host 'Downloading Chef Client...' 280 $downloader.DownloadFile($url, $dest) 281 282 Write-Host 'Installing Chef Client...' 283 Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait 284 ` 285 286 const proxyWindowsInstallScript = ` 287 $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor} 288 289 switch ($winver) 290 { 291 "6.0" {$machine_os = "2008"} 292 "6.1" {$machine_os = "2008r2"} 293 "6.2" {$machine_os = "2012"} 294 "6.3" {$machine_os = "2012"} 295 default {$machine_os = "2008r2"} 296 } 297 298 if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"} 299 300 $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=" 301 $dest = [System.IO.Path]::GetTempFileName() 302 $dest = [System.IO.Path]::ChangeExtension($dest, ".msi") 303 $downloader = New-Object System.Net.WebClient 304 305 $http_proxy = 'http://proxy.local' 306 if ($http_proxy -ne '') { 307 $no_proxy = 'http://local.local,http://local.org' 308 if ($no_proxy -eq ''){ 309 $no_proxy = "127.0.0.1" 310 } 311 312 $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(',')) 313 $downloader.proxy = $proxy 314 } 315 316 Write-Host 'Downloading Chef Client...' 317 $downloader.DownloadFile($url, $dest) 318 319 Write-Host 'Installing Chef Client...' 320 Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait 321 ` 322 323 const versionWindowsInstallScript = ` 324 $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor} 325 326 switch ($winver) 327 { 328 "6.0" {$machine_os = "2008"} 329 "6.1" {$machine_os = "2008r2"} 330 "6.2" {$machine_os = "2012"} 331 "6.3" {$machine_os = "2012"} 332 default {$machine_os = "2008r2"} 333 } 334 335 if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"} 336 337 $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=11.18.6" 338 $dest = [System.IO.Path]::GetTempFileName() 339 $dest = [System.IO.Path]::ChangeExtension($dest, ".msi") 340 $downloader = New-Object System.Net.WebClient 341 342 $http_proxy = '' 343 if ($http_proxy -ne '') { 344 $no_proxy = '' 345 if ($no_proxy -eq ''){ 346 $no_proxy = "127.0.0.1" 347 } 348 349 $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(',')) 350 $downloader.proxy = $proxy 351 } 352 353 Write-Host 'Downloading Chef Client...' 354 $downloader.DownloadFile($url, $dest) 355 356 Write-Host 'Installing Chef Client...' 357 Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait 358 ` 359 360 const defaultWindowsClientConf = `log_location STDOUT 361 chef_server_url "https://chef.local" 362 validation_client_name "validator" 363 node_name "nodename1"` 364 365 const proxyWindowsClientConf = `log_location STDOUT 366 chef_server_url "https://chef.local" 367 validation_client_name "validator" 368 node_name "nodename1" 369 370 http_proxy "http://proxy.local" 371 ENV['http_proxy'] = "http://proxy.local" 372 ENV['HTTP_PROXY'] = "http://proxy.local" 373 374 https_proxy "https://proxy.local" 375 ENV['https_proxy'] = "https://proxy.local" 376 ENV['HTTPS_PROXY'] = "https://proxy.local" 377 378 no_proxy "http://local.local,https://local.local" 379 ENV['no_proxy'] = "http://local.local,https://local.local" 380 381 ssl_verify_mode :verify_none`