github.com/handlerbot/terraform@v0.10.0-beta1.0.20180726153736-26b68d98f9cb/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  		"Channel": {
    77  			Config: map[string]interface{}{
    78  				"channel":    "current",
    79  				"node_name":  "nodename1",
    80  				"run_list":   []interface{}{"cookbook::recipe"},
    81  				"server_url": "https://chef.local",
    82  				"user_name":  "bob",
    83  				"user_key":   "USER-KEY",
    84  				"version":    "11.18.6",
    85  			},
    86  
    87  			Commands: map[string]bool{
    88  				"powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true,
    89  			},
    90  
    91  			UploadScripts: map[string]string{
    92  				"ChefClient.ps1": channelWindowsInstallScript,
    93  			},
    94  		},
    95  	}
    96  
    97  	o := new(terraform.MockUIOutput)
    98  	c := new(communicator.MockCommunicator)
    99  
   100  	for k, tc := range cases {
   101  		c.Commands = tc.Commands
   102  		c.UploadScripts = tc.UploadScripts
   103  
   104  		p, err := decodeConfig(
   105  			schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config),
   106  		)
   107  		if err != nil {
   108  			t.Fatalf("Error: %v", err)
   109  		}
   110  
   111  		p.useSudo = false
   112  
   113  		err = p.windowsInstallChefClient(o, c)
   114  		if err != nil {
   115  			t.Fatalf("Test %q failed: %v", k, err)
   116  		}
   117  	}
   118  }
   119  
   120  func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
   121  	cases := map[string]struct {
   122  		Config   map[string]interface{}
   123  		Commands map[string]bool
   124  		Uploads  map[string]string
   125  	}{
   126  		"Default": {
   127  			Config: map[string]interface{}{
   128  				"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
   129  				"node_name":  "nodename1",
   130  				"run_list":   []interface{}{"cookbook::recipe"},
   131  				"secret_key": "SECRET-KEY",
   132  				"server_url": "https://chef.local",
   133  				"user_name":  "bob",
   134  				"user_key":   "USER-KEY",
   135  			},
   136  
   137  			Commands: map[string]bool{
   138  				fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
   139  				fmt.Sprintf("cmd /c if not exist %q mkdir %q",
   140  					path.Join(windowsConfDir, "ohai/hints"),
   141  					path.Join(windowsConfDir, "ohai/hints")): true,
   142  			},
   143  
   144  			Uploads: map[string]string{
   145  				windowsConfDir + "/client.rb":                 defaultWindowsClientConf,
   146  				windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY",
   147  				windowsConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   148  				windowsConfDir + "/ohai/hints/ohaihint.json":  "OHAI-HINT-FILE",
   149  				windowsConfDir + "/bob.pem":                   "USER-KEY",
   150  			},
   151  		},
   152  
   153  		"Proxy": {
   154  			Config: map[string]interface{}{
   155  				"http_proxy":      "http://proxy.local",
   156  				"https_proxy":     "https://proxy.local",
   157  				"no_proxy":        []interface{}{"http://local.local", "https://local.local"},
   158  				"node_name":       "nodename1",
   159  				"run_list":        []interface{}{"cookbook::recipe"},
   160  				"secret_key":      "SECRET-KEY",
   161  				"server_url":      "https://chef.local",
   162  				"ssl_verify_mode": "verify_none",
   163  				"user_name":       "bob",
   164  				"user_key":        "USER-KEY",
   165  			},
   166  
   167  			Commands: map[string]bool{
   168  				fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
   169  			},
   170  
   171  			Uploads: map[string]string{
   172  				windowsConfDir + "/client.rb":                 proxyWindowsClientConf,
   173  				windowsConfDir + "/first-boot.json":           `{"run_list":["cookbook::recipe"]}`,
   174  				windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY",
   175  				windowsConfDir + "/bob.pem":                   "USER-KEY",
   176  			},
   177  		},
   178  
   179  		"Attributes JSON": {
   180  			Config: map[string]interface{}{
   181  				"attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   182  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
   183  				"node_name":  "nodename1",
   184  				"run_list":   []interface{}{"cookbook::recipe"},
   185  				"secret_key": "SECRET-KEY",
   186  				"server_url": "https://chef.local",
   187  				"user_name":  "bob",
   188  				"user_key":   "USER-KEY",
   189  			},
   190  
   191  			Commands: map[string]bool{
   192  				fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
   193  			},
   194  
   195  			Uploads: map[string]string{
   196  				windowsConfDir + "/client.rb":                 defaultWindowsClientConf,
   197  				windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY",
   198  				windowsConfDir + "/bob.pem":                   "USER-KEY",
   199  				windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
   200  					`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
   201  			},
   202  		},
   203  	}
   204  
   205  	o := new(terraform.MockUIOutput)
   206  	c := new(communicator.MockCommunicator)
   207  
   208  	for k, tc := range cases {
   209  		c.Commands = tc.Commands
   210  		c.Uploads = tc.Uploads
   211  
   212  		p, err := decodeConfig(
   213  			schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config),
   214  		)
   215  		if err != nil {
   216  			t.Fatalf("Error: %v", err)
   217  		}
   218  
   219  		p.useSudo = false
   220  
   221  		err = p.windowsCreateConfigFiles(o, c)
   222  		if err != nil {
   223  			t.Fatalf("Test %q failed: %v", k, err)
   224  		}
   225  	}
   226  }
   227  
   228  const defaultWindowsInstallScript = `
   229  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   230  
   231  switch ($winver)
   232  {
   233    "6.0" {$machine_os = "2008"}
   234    "6.1" {$machine_os = "2008r2"}
   235    "6.2" {$machine_os = "2012"}
   236    "6.3" {$machine_os = "2012"}
   237    default {$machine_os = "2008r2"}
   238  }
   239  
   240  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   241  
   242  $url = "http://omnitruck.chef.io/stable/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v="
   243  $dest = [System.IO.Path]::GetTempFileName()
   244  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   245  $downloader = New-Object System.Net.WebClient
   246  
   247  $http_proxy = ''
   248  if ($http_proxy -ne '') {
   249  	$no_proxy = ''
   250    if ($no_proxy -eq ''){
   251      $no_proxy = "127.0.0.1"
   252    }
   253  
   254    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   255    $downloader.proxy = $proxy
   256  }
   257  
   258  Write-Host 'Downloading Chef Client...'
   259  $downloader.DownloadFile($url, $dest)
   260  
   261  Write-Host 'Installing Chef Client...'
   262  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   263  `
   264  
   265  const proxyWindowsInstallScript = `
   266  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   267  
   268  switch ($winver)
   269  {
   270    "6.0" {$machine_os = "2008"}
   271    "6.1" {$machine_os = "2008r2"}
   272    "6.2" {$machine_os = "2012"}
   273    "6.3" {$machine_os = "2012"}
   274    default {$machine_os = "2008r2"}
   275  }
   276  
   277  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   278  
   279  $url = "http://omnitruck.chef.io/stable/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v="
   280  $dest = [System.IO.Path]::GetTempFileName()
   281  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   282  $downloader = New-Object System.Net.WebClient
   283  
   284  $http_proxy = 'http://proxy.local'
   285  if ($http_proxy -ne '') {
   286  	$no_proxy = 'http://local.local,http://local.org'
   287    if ($no_proxy -eq ''){
   288      $no_proxy = "127.0.0.1"
   289    }
   290  
   291    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   292    $downloader.proxy = $proxy
   293  }
   294  
   295  Write-Host 'Downloading Chef Client...'
   296  $downloader.DownloadFile($url, $dest)
   297  
   298  Write-Host 'Installing Chef Client...'
   299  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   300  `
   301  
   302  const versionWindowsInstallScript = `
   303  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   304  
   305  switch ($winver)
   306  {
   307    "6.0" {$machine_os = "2008"}
   308    "6.1" {$machine_os = "2008r2"}
   309    "6.2" {$machine_os = "2012"}
   310    "6.3" {$machine_os = "2012"}
   311    default {$machine_os = "2008r2"}
   312  }
   313  
   314  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   315  
   316  $url = "http://omnitruck.chef.io/stable/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=11.18.6"
   317  $dest = [System.IO.Path]::GetTempFileName()
   318  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   319  $downloader = New-Object System.Net.WebClient
   320  
   321  $http_proxy = ''
   322  if ($http_proxy -ne '') {
   323  	$no_proxy = ''
   324    if ($no_proxy -eq ''){
   325      $no_proxy = "127.0.0.1"
   326    }
   327  
   328    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   329    $downloader.proxy = $proxy
   330  }
   331  
   332  Write-Host 'Downloading Chef Client...'
   333  $downloader.DownloadFile($url, $dest)
   334  
   335  Write-Host 'Installing Chef Client...'
   336  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   337  `
   338  const channelWindowsInstallScript = `
   339  $winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
   340  
   341  switch ($winver)
   342  {
   343    "6.0" {$machine_os = "2008"}
   344    "6.1" {$machine_os = "2008r2"}
   345    "6.2" {$machine_os = "2012"}
   346    "6.3" {$machine_os = "2012"}
   347    default {$machine_os = "2008r2"}
   348  }
   349  
   350  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
   351  
   352  $url = "http://omnitruck.chef.io/current/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=11.18.6"
   353  $dest = [System.IO.Path]::GetTempFileName()
   354  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
   355  $downloader = New-Object System.Net.WebClient
   356  
   357  $http_proxy = ''
   358  if ($http_proxy -ne '') {
   359  	$no_proxy = ''
   360    if ($no_proxy -eq ''){
   361      $no_proxy = "127.0.0.1"
   362    }
   363  
   364    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
   365    $downloader.proxy = $proxy
   366  }
   367  
   368  Write-Host 'Downloading Chef Client...'
   369  $downloader.DownloadFile($url, $dest)
   370  
   371  Write-Host 'Installing Chef Client...'
   372  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
   373  `
   374  
   375  const defaultWindowsClientConf = `log_location            STDOUT
   376  chef_server_url         "https://chef.local/"
   377  node_name               "nodename1"`
   378  
   379  const proxyWindowsClientConf = `log_location            STDOUT
   380  chef_server_url         "https://chef.local/"
   381  node_name               "nodename1"
   382  
   383  http_proxy          "http://proxy.local"
   384  ENV['http_proxy'] = "http://proxy.local"
   385  ENV['HTTP_PROXY'] = "http://proxy.local"
   386  
   387  https_proxy          "https://proxy.local"
   388  ENV['https_proxy'] = "https://proxy.local"
   389  ENV['HTTPS_PROXY'] = "https://proxy.local"
   390  
   391  no_proxy          "http://local.local,https://local.local"
   392  ENV['no_proxy'] = "http://local.local,https://local.local"
   393  
   394  ssl_verify_mode  :verify_none`