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