github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/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  		"Attributes JSON": {
   201  			Config: testConfig(t, map[string]interface{}{
   202  				"attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   203  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
   204  				"node_name":              "nodename1",
   205  				"run_list":               []interface{}{"cookbook::recipe"},
   206  				"secret_key_path":        "test-fixtures/encrypted_data_bag_secret",
   207  				"server_url":             "https://chef.local",
   208  				"validation_client_name": "validator",
   209  				"validation_key_path":    "test-fixtures/validator.pem",
   210  			}),
   211  
   212  			Commands: map[string]bool{
   213  				fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
   214  			},
   215  
   216  			Uploads: map[string]string{
   217  				windowsConfDir + "/client.rb":                 defaultWindowsClientConf,
   218  				windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
   219  				windowsConfDir + "/validation.pem":            "VALIDATOR-PEM-FILE",
   220  				windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   221  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   222  			},
   223  		},
   224  	}
   225  
   226  	r := new(ResourceProvisioner)
   227  	o := new(terraform.MockUIOutput)
   228  	c := new(communicator.MockCommunicator)
   229  
   230  	for k, tc := range cases {
   231  		c.Commands = tc.Commands
   232  		c.Uploads = tc.Uploads
   233  
   234  		p, err := r.decodeConfig(tc.Config)
   235  		if err != nil {
   236  			t.Fatalf("Error: %v", err)
   237  		}
   238  
   239  		p.useSudo = false
   240  
   241  		err = p.windowsCreateConfigFiles(o, c)
   242  		if err != nil {
   243  			t.Fatalf("Test %q failed: %v", k, err)
   244  		}
   245  	}
   246  }
   247  
   248  const defaultWindowsInstallScript = `
   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 = ''
   268  if ($http_proxy -ne '') {
   269  	$no_proxy = ''
   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 proxyWindowsInstallScript = `
   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="
   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 = 'http://proxy.local'
   305  if ($http_proxy -ne '') {
   306  	$no_proxy = 'http://local.local,http://local.org'
   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 versionWindowsInstallScript = `
   323  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   324  
   325  switch ($winver)
   326  {
   327    "6.0" {$machine_os = "2008"}
   328    "6.1" {$machine_os = "2008r2"}
   329    "6.2" {$machine_os = "2012"}
   330    "6.3" {$machine_os = "2012"}
   331    default {$machine_os = "2008r2"}
   332  }
   333  
   334  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   335  
   336  $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=11.18.6"
   337  $dest = [System.IO.Path]::GetTempFileName()
   338  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   339  $downloader = New-Object System.Net.WebClient
   340  
   341  $http_proxy = ''
   342  if ($http_proxy -ne '') {
   343  	$no_proxy = ''
   344    if ($no_proxy -eq ''){
   345      $no_proxy = "127.0.0.1"
   346    }
   347  
   348    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   349    $downloader.proxy = $proxy
   350  }
   351  
   352  Write-Host 'Downloading Chef Client...'
   353  $downloader.DownloadFile($url, $dest)
   354  
   355  Write-Host 'Installing Chef Client...'
   356  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   357  `
   358  
   359  const defaultWindowsClientConf = `log_location            STDOUT
   360  chef_server_url         "https://chef.local"
   361  validation_client_name  "validator"
   362  node_name               "nodename1"`
   363  
   364  const proxyWindowsClientConf = `log_location            STDOUT
   365  chef_server_url         "https://chef.local"
   366  validation_client_name  "validator"
   367  node_name               "nodename1"
   368  
   369  
   370  
   371  
   372  http_proxy          "http://proxy.local"
   373  ENV['http_proxy'] = "http://proxy.local"
   374  ENV['HTTP_PROXY'] = "http://proxy.local"
   375  
   376  
   377  
   378  https_proxy          "https://proxy.local"
   379  ENV['https_proxy'] = "https://proxy.local"
   380  ENV['HTTPS_PROXY'] = "https://proxy.local"
   381  
   382  
   383  
   384  no_proxy          "http://local.local,https://local.local"
   385  ENV['no_proxy'] = "http://local.local,https://local.local"`