2015-05-08 23:25:24 +02:00
|
|
|
package chef
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-25 15:48:54 +02:00
|
|
|
"path"
|
2015-05-08 18:17:57 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator"
|
2017-05-19 20:42:14 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-05-08 18:17:57 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
func TestResourceProvider_windowsInstallChefClient(t *testing.T) {
|
2015-05-08 18:17:57 +02:00
|
|
|
cases := map[string]struct {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config map[string]interface{}
|
2015-05-08 18:17:57 +02:00
|
|
|
Commands map[string]bool
|
|
|
|
UploadScripts map[string]string
|
|
|
|
}{
|
|
|
|
"Default": {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config: map[string]interface{}{
|
2016-09-15 14:20:18 +02:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"user_name": "bob",
|
|
|
|
"user_key": "USER-KEY",
|
2017-05-19 20:42:14 +02:00
|
|
|
},
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true,
|
|
|
|
},
|
|
|
|
|
|
|
|
UploadScripts: map[string]string{
|
2015-06-25 14:29:48 +02:00
|
|
|
"ChefClient.ps1": defaultWindowsInstallScript,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"Proxy": {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config: map[string]interface{}{
|
2016-09-15 14:20:18 +02:00
|
|
|
"http_proxy": "http://proxy.local",
|
|
|
|
"no_proxy": []interface{}{"http://local.local", "http://local.org"},
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"user_name": "bob",
|
|
|
|
"user_key": "USER-KEY",
|
2017-05-19 20:42:14 +02:00
|
|
|
},
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true,
|
|
|
|
},
|
|
|
|
|
|
|
|
UploadScripts: map[string]string{
|
2015-06-25 14:29:48 +02:00
|
|
|
"ChefClient.ps1": proxyWindowsInstallScript,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"Version": {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config: map[string]interface{}{
|
2016-09-15 14:20:18 +02:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"user_name": "bob",
|
|
|
|
"user_key": "USER-KEY",
|
|
|
|
"version": "11.18.6",
|
2017-05-19 20:42:14 +02:00
|
|
|
},
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"powershell -NoProfile -ExecutionPolicy Bypass -File ChefClient.ps1": true,
|
|
|
|
},
|
|
|
|
|
|
|
|
UploadScripts: map[string]string{
|
2015-06-25 14:29:48 +02:00
|
|
|
"ChefClient.ps1": versionWindowsInstallScript,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
o := new(terraform.MockUIOutput)
|
|
|
|
c := new(communicator.MockCommunicator)
|
|
|
|
|
|
|
|
for k, tc := range cases {
|
|
|
|
c.Commands = tc.Commands
|
|
|
|
c.UploadScripts = tc.UploadScripts
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
p, err := decodeConfig(
|
|
|
|
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config),
|
|
|
|
)
|
2015-05-08 18:17:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = false
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
err = p.windowsInstallChefClient(o, c)
|
2015-05-08 18:17:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %q failed: %v", k, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
func TestResourceProvider_windowsCreateConfigFiles(t *testing.T) {
|
2015-05-08 18:17:57 +02:00
|
|
|
cases := map[string]struct {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config map[string]interface{}
|
2015-05-08 18:17:57 +02:00
|
|
|
Commands map[string]bool
|
|
|
|
Uploads map[string]string
|
|
|
|
}{
|
|
|
|
"Default": {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config: map[string]interface{}{
|
2016-09-15 14:20:18 +02:00
|
|
|
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"secret_key": "SECRET-KEY",
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"user_name": "bob",
|
|
|
|
"user_key": "USER-KEY",
|
2017-05-19 20:42:14 +02:00
|
|
|
},
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-07-10 12:56:27 +02:00
|
|
|
fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
|
|
|
|
fmt.Sprintf("cmd /c if not exist %q mkdir %q",
|
2015-06-25 15:48:54 +02:00
|
|
|
path.Join(windowsConfDir, "ohai/hints"),
|
|
|
|
path.Join(windowsConfDir, "ohai/hints")): true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
windowsConfDir + "/client.rb": defaultWindowsClientConf,
|
2016-09-15 14:20:18 +02:00
|
|
|
windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY",
|
2015-07-09 15:05:12 +02:00
|
|
|
windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
2015-07-09 21:29:27 +02:00
|
|
|
windowsConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE",
|
2016-09-15 14:20:18 +02:00
|
|
|
windowsConfDir + "/bob.pem": "USER-KEY",
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"Proxy": {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config: map[string]interface{}{
|
2016-09-15 14:20:18 +02:00
|
|
|
"http_proxy": "http://proxy.local",
|
|
|
|
"https_proxy": "https://proxy.local",
|
|
|
|
"no_proxy": []interface{}{"http://local.local", "https://local.local"},
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"secret_key": "SECRET-KEY",
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"ssl_verify_mode": "verify_none",
|
|
|
|
"user_name": "bob",
|
|
|
|
"user_key": "USER-KEY",
|
2017-05-19 20:42:14 +02:00
|
|
|
},
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-07-10 12:56:27 +02:00
|
|
|
fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
windowsConfDir + "/client.rb": proxyWindowsClientConf,
|
|
|
|
windowsConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
2016-09-15 14:20:18 +02:00
|
|
|
windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY",
|
|
|
|
windowsConfDir + "/bob.pem": "USER-KEY",
|
2016-01-29 18:41:14 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-02-09 11:11:46 +01:00
|
|
|
"Attributes JSON": {
|
2017-05-19 20:42:14 +02:00
|
|
|
Config: map[string]interface{}{
|
2016-02-09 11:11:46 +01:00
|
|
|
"attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
|
|
|
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
|
2016-09-15 14:20:18 +02:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"secret_key": "SECRET-KEY",
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"user_name": "bob",
|
|
|
|
"user_key": "USER-KEY",
|
2017-05-19 20:42:14 +02:00
|
|
|
},
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-07-10 12:56:27 +02:00
|
|
|
fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir): true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
windowsConfDir + "/client.rb": defaultWindowsClientConf,
|
2016-09-15 14:20:18 +02:00
|
|
|
windowsConfDir + "/encrypted_data_bag_secret": "SECRET-KEY",
|
|
|
|
windowsConfDir + "/bob.pem": "USER-KEY",
|
2015-06-25 15:48:54 +02:00
|
|
|
windowsConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
2015-05-08 18:17:57 +02:00
|
|
|
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
o := new(terraform.MockUIOutput)
|
|
|
|
c := new(communicator.MockCommunicator)
|
|
|
|
|
|
|
|
for k, tc := range cases {
|
|
|
|
c.Commands = tc.Commands
|
|
|
|
c.Uploads = tc.Uploads
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
p, err := decodeConfig(
|
|
|
|
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config),
|
|
|
|
)
|
2015-05-08 18:17:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = false
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
err = p.windowsCreateConfigFiles(o, c)
|
2015-05-08 18:17:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %q failed: %v", k, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
const defaultWindowsInstallScript = `
|
2015-05-08 18:17:57 +02:00
|
|
|
$winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
|
|
|
|
|
|
|
|
switch ($winver)
|
|
|
|
{
|
|
|
|
"6.0" {$machine_os = "2008"}
|
|
|
|
"6.1" {$machine_os = "2008r2"}
|
|
|
|
"6.2" {$machine_os = "2012"}
|
|
|
|
"6.3" {$machine_os = "2012"}
|
|
|
|
default {$machine_os = "2008r2"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
|
|
|
|
|
|
|
|
$url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v="
|
|
|
|
$dest = [System.IO.Path]::GetTempFileName()
|
|
|
|
$dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
|
|
|
|
$downloader = New-Object System.Net.WebClient
|
|
|
|
|
|
|
|
$http_proxy = ''
|
|
|
|
if ($http_proxy -ne '') {
|
|
|
|
$no_proxy = ''
|
|
|
|
if ($no_proxy -eq ''){
|
|
|
|
$no_proxy = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
$proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
|
|
|
|
$downloader.proxy = $proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host 'Downloading Chef Client...'
|
|
|
|
$downloader.DownloadFile($url, $dest)
|
|
|
|
|
|
|
|
Write-Host 'Installing Chef Client...'
|
|
|
|
Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
|
|
|
|
`
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
const proxyWindowsInstallScript = `
|
2015-05-08 18:17:57 +02:00
|
|
|
$winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
|
|
|
|
|
|
|
|
switch ($winver)
|
|
|
|
{
|
|
|
|
"6.0" {$machine_os = "2008"}
|
|
|
|
"6.1" {$machine_os = "2008r2"}
|
|
|
|
"6.2" {$machine_os = "2012"}
|
|
|
|
"6.3" {$machine_os = "2012"}
|
|
|
|
default {$machine_os = "2008r2"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
|
|
|
|
|
|
|
|
$url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v="
|
|
|
|
$dest = [System.IO.Path]::GetTempFileName()
|
|
|
|
$dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
|
|
|
|
$downloader = New-Object System.Net.WebClient
|
|
|
|
|
|
|
|
$http_proxy = 'http://proxy.local'
|
|
|
|
if ($http_proxy -ne '') {
|
|
|
|
$no_proxy = 'http://local.local,http://local.org'
|
|
|
|
if ($no_proxy -eq ''){
|
|
|
|
$no_proxy = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
$proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
|
|
|
|
$downloader.proxy = $proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host 'Downloading Chef Client...'
|
|
|
|
$downloader.DownloadFile($url, $dest)
|
|
|
|
|
|
|
|
Write-Host 'Installing Chef Client...'
|
|
|
|
Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
|
|
|
|
`
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
const versionWindowsInstallScript = `
|
2015-05-08 18:17:57 +02:00
|
|
|
$winver = [System.Environment]::OSVersion.Version | % {"{0}.{1}" -f $_.Major,$_.Minor}
|
|
|
|
|
|
|
|
switch ($winver)
|
|
|
|
{
|
|
|
|
"6.0" {$machine_os = "2008"}
|
|
|
|
"6.1" {$machine_os = "2008r2"}
|
|
|
|
"6.2" {$machine_os = "2012"}
|
|
|
|
"6.3" {$machine_os = "2012"}
|
|
|
|
default {$machine_os = "2008r2"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
|
|
|
|
|
|
|
|
$url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=11.18.6"
|
|
|
|
$dest = [System.IO.Path]::GetTempFileName()
|
|
|
|
$dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
|
|
|
|
$downloader = New-Object System.Net.WebClient
|
|
|
|
|
|
|
|
$http_proxy = ''
|
|
|
|
if ($http_proxy -ne '') {
|
|
|
|
$no_proxy = ''
|
|
|
|
if ($no_proxy -eq ''){
|
|
|
|
$no_proxy = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
$proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
|
|
|
|
$downloader.proxy = $proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host 'Downloading Chef Client...'
|
|
|
|
$downloader.DownloadFile($url, $dest)
|
|
|
|
|
|
|
|
Write-Host 'Installing Chef Client...'
|
|
|
|
Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
|
|
|
|
`
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
const defaultWindowsClientConf = `log_location STDOUT
|
2016-09-15 14:20:18 +02:00
|
|
|
chef_server_url "https://chef.local/"
|
2015-05-08 18:17:57 +02:00
|
|
|
node_name "nodename1"`
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
const proxyWindowsClientConf = `log_location STDOUT
|
2016-09-15 14:20:18 +02:00
|
|
|
chef_server_url "https://chef.local/"
|
2015-05-08 18:17:57 +02:00
|
|
|
node_name "nodename1"
|
|
|
|
|
|
|
|
http_proxy "http://proxy.local"
|
|
|
|
ENV['http_proxy'] = "http://proxy.local"
|
|
|
|
ENV['HTTP_PROXY'] = "http://proxy.local"
|
|
|
|
|
|
|
|
https_proxy "https://proxy.local"
|
|
|
|
ENV['https_proxy'] = "https://proxy.local"
|
|
|
|
ENV['HTTPS_PROXY'] = "https://proxy.local"
|
|
|
|
|
2016-01-14 19:51:23 +01:00
|
|
|
no_proxy "http://local.local,https://local.local"
|
2016-07-22 16:01:48 +02:00
|
|
|
ENV['no_proxy'] = "http://local.local,https://local.local"
|
|
|
|
|
|
|
|
ssl_verify_mode :verify_none`
|