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