github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  				"validation_client_name": "validator",
   141  				"validation_key_path":    "test-fixtures/validator.pem",
   142  			}),
   143  
   144  			Commands: map[string]bool{
   145  				fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
   146  			},
   147  
   148  			Uploads: map[string]string{
   149  				windowsConfDir + "/client.rb":                 proxyWindowsClientConf,
   150  				windowsConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   151  				windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   152  				windowsConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   153  			},
   154  		},
   155  
   156  		"Attributes": {
   157  			Config: testConfig(t, map[string]interface{}{
   158  				"attributes": []map[string]interface{}{
   159  					map[string]interface{}{
   160  						"key1": []map[string]interface{}{
   161  							map[string]interface{}{
   162  								"subkey1": []map[string]interface{}{
   163  									map[string]interface{}{
   164  										"subkey2a": []interface{}{
   165  											"val1", "val2", "val3",
   166  										},
   167  										"subkey2b": []map[string]interface{}{
   168  											map[string]interface{}{
   169  												"subkey3": "value3",
   170  											},
   171  										},
   172  									},
   173  								},
   174  							},
   175  						},
   176  						"key2": "value2",
   177  					},
   178  				},
   179  				"node_name":              "nodename1",
   180  				"run_list":               []interface{}{"cookbook::recipe"},
   181  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   182  				"server_url":             "https://chef.local",
   183  				"validation_client_name": "validator",
   184  				"validation_key_path":    "test-fixtures/validator.pem",
   185  			}),
   186  
   187  			Commands: map[string]bool{
   188  				fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
   189  			},
   190  
   191  			Uploads: map[string]string{
   192  				windowsConfDir + "/client.rb":                 defaultWindowsClientConf,
   193  				windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   194  				windowsConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   195  				windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   196  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   197  			},
   198  		},
   199  	}
   200  
   201  	r := new(ResourceProvisioner)
   202  	o := new(terraform.MockUIOutput)
   203  	c := new(communicator.MockCommunicator)
   204  
   205  	for k, tc := range cases {
   206  		c.Commands = tc.Commands
   207  		c.Uploads = tc.Uploads
   208  
   209  		p, err := r.decodeConfig(tc.Config)
   210  		if err != nil {
   211  			t.Fatalf("Error: %v", err)
   212  		}
   213  
   214  		p.useSudo = false
   215  
   216  		err = p.windowsCreateConfigFiles(o, c)
   217  		if err != nil {
   218  			t.Fatalf("Test %q failed: %v", k, err)
   219  		}
   220  	}
   221  }
   222  
   223  const defaultWindowsInstallScript = `
   224  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   225  
   226  switch ($winver)
   227  {
   228    "6.0" {$machine_os = "2008"}
   229    "6.1" {$machine_os = "2008r2"}
   230    "6.2" {$machine_os = "2012"}
   231    "6.3" {$machine_os = "2012"}
   232    default {$machine_os = "2008r2"}
   233  }
   234  
   235  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   236  
   237  $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v="
   238  $dest = [System.IO.Path]::GetTempFileName()
   239  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   240  $downloader = New-Object System.Net.WebClient
   241  
   242  $http_proxy = ''
   243  if ($http_proxy -ne '') {
   244  	$no_proxy = ''
   245    if ($no_proxy -eq ''){
   246      $no_proxy = "127.0.0.1"
   247    }
   248  
   249    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   250    $downloader.proxy = $proxy
   251  }
   252  
   253  Write-Host 'Downloading Chef Client...'
   254  $downloader.DownloadFile($url, $dest)
   255  
   256  Write-Host 'Installing Chef Client...'
   257  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   258  `
   259  
   260  const proxyWindowsInstallScript = `
   261  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   262  
   263  switch ($winver)
   264  {
   265    "6.0" {$machine_os = "2008"}
   266    "6.1" {$machine_os = "2008r2"}
   267    "6.2" {$machine_os = "2012"}
   268    "6.3" {$machine_os = "2012"}
   269    default {$machine_os = "2008r2"}
   270  }
   271  
   272  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   273  
   274  $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v="
   275  $dest = [System.IO.Path]::GetTempFileName()
   276  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   277  $downloader = New-Object System.Net.WebClient
   278  
   279  $http_proxy = 'http://proxy.local'
   280  if ($http_proxy -ne '') {
   281  	$no_proxy = 'http://local.local,http://local.org'
   282    if ($no_proxy -eq ''){
   283      $no_proxy = "127.0.0.1"
   284    }
   285  
   286    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   287    $downloader.proxy = $proxy
   288  }
   289  
   290  Write-Host 'Downloading Chef Client...'
   291  $downloader.DownloadFile($url, $dest)
   292  
   293  Write-Host 'Installing Chef Client...'
   294  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   295  `
   296  
   297  const versionWindowsInstallScript = `
   298  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   299  
   300  switch ($winver)
   301  {
   302    "6.0" {$machine_os = "2008"}
   303    "6.1" {$machine_os = "2008r2"}
   304    "6.2" {$machine_os = "2012"}
   305    "6.3" {$machine_os = "2012"}
   306    default {$machine_os = "2008r2"}
   307  }
   308  
   309  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   310  
   311  $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=11.18.6"
   312  $dest = [System.IO.Path]::GetTempFileName()
   313  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   314  $downloader = New-Object System.Net.WebClient
   315  
   316  $http_proxy = ''
   317  if ($http_proxy -ne '') {
   318  	$no_proxy = ''
   319    if ($no_proxy -eq ''){
   320      $no_proxy = "127.0.0.1"
   321    }
   322  
   323    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   324    $downloader.proxy = $proxy
   325  }
   326  
   327  Write-Host 'Downloading Chef Client...'
   328  $downloader.DownloadFile($url, $dest)
   329  
   330  Write-Host 'Installing Chef Client...'
   331  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   332  `
   333  
   334  const defaultWindowsClientConf = `log_location            STDOUT
   335  chef_server_url         "https://chef.local"
   336  validation_client_name  "validator"
   337  node_name               "nodename1"`
   338  
   339  const proxyWindowsClientConf = `log_location            STDOUT
   340  chef_server_url         "https://chef.local"
   341  validation_client_name  "validator"
   342  node_name               "nodename1"
   343  
   344  
   345  
   346  
   347  http_proxy          "http://proxy.local"
   348  ENV['http_proxy'] = "http://proxy.local"
   349  ENV['HTTP_PROXY'] = "http://proxy.local"
   350  
   351  
   352  
   353  https_proxy          "https://proxy.local"
   354  ENV['https_proxy'] = "https://proxy.local"
   355  ENV['HTTPS_PROXY'] = "https://proxy.local"
   356  
   357  
   358  no_proxy "http://local.local,https://local.local"`