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