2015-05-08 23:25:24 +02:00
|
|
|
package chef
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
import (
|
2016-01-09 15:56:49 +01:00
|
|
|
"fmt"
|
2015-06-25 15:48:54 +02:00
|
|
|
"path"
|
2015-05-08 18:17:57 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
|
2015-05-08 18:17:57 +02:00
|
|
|
cases := map[string]struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
Commands map[string]bool
|
|
|
|
}{
|
|
|
|
"Sudo": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-05-08 23:25:24 +02:00
|
|
|
"sudo curl -LO https://www.chef.io/chef/install.sh": true,
|
2015-06-30 14:13:36 +02:00
|
|
|
"sudo bash ./install.sh -v \"\"": true,
|
2015-05-08 23:25:24 +02:00
|
|
|
"sudo rm -f install.sh": true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"NoSudo": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
2015-07-09 15:05:12 +02:00
|
|
|
"secret_key_path": "encrypted_data_bag_secret",
|
2015-05-08 18:17:57 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-05-08 23:25:24 +02:00
|
|
|
"curl -LO https://www.chef.io/chef/install.sh": true,
|
2015-06-30 14:13:36 +02:00
|
|
|
"bash ./install.sh -v \"\"": true,
|
2015-05-08 23:25:24 +02:00
|
|
|
"rm -f install.sh": true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"HTTPProxy": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"http_proxy": "http://proxy.local",
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-07-03 15:04:58 +02:00
|
|
|
"http_proxy='http://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
|
|
|
|
"http_proxy='http://proxy.local' bash ./install.sh -v \"\"": true,
|
|
|
|
"http_proxy='http://proxy.local' rm -f install.sh": true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-07-03 16:27:52 +02:00
|
|
|
"HTTPSProxy": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"https_proxy": "https://proxy.local",
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"https_proxy='https://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
|
|
|
|
"https_proxy='https://proxy.local' bash ./install.sh -v \"\"": true,
|
|
|
|
"https_proxy='https://proxy.local' rm -f install.sh": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-07-09 21:29:27 +02:00
|
|
|
"NoProxy": {
|
2015-05-08 18:17:57 +02:00
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"http_proxy": "http://proxy.local",
|
|
|
|
"no_proxy": []interface{}{"http://local.local", "http://local.org"},
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-07-03 15:04:58 +02:00
|
|
|
"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
|
2015-05-08 23:25:24 +02:00
|
|
|
"curl -LO https://www.chef.io/chef/install.sh": true,
|
2015-07-03 15:04:58 +02:00
|
|
|
"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
|
2015-06-30 14:13:36 +02:00
|
|
|
"bash ./install.sh -v \"\"": true,
|
2015-07-03 15:04:58 +02:00
|
|
|
"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
|
2015-05-08 18:17:57 +02:00
|
|
|
"rm -f install.sh": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"Version": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
|
|
|
"version": "11.18.6",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2015-05-08 23:25:24 +02:00
|
|
|
"curl -LO https://www.chef.io/chef/install.sh": true,
|
2015-06-30 14:13:36 +02:00
|
|
|
"bash ./install.sh -v \"11.18.6\"": true,
|
2015-05-08 23:25:24 +02:00
|
|
|
"rm -f install.sh": true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(ResourceProvisioner)
|
|
|
|
o := new(terraform.MockUIOutput)
|
|
|
|
c := new(communicator.MockCommunicator)
|
|
|
|
|
|
|
|
for k, tc := range cases {
|
|
|
|
c.Commands = tc.Commands
|
|
|
|
|
|
|
|
p, err := r.decodeConfig(tc.Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = !p.PreventSudo
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
err = p.linuxInstallChefClient(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_linuxCreateConfigFiles(t *testing.T) {
|
2015-05-08 18:17:57 +02:00
|
|
|
cases := map[string]struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
Commands map[string]bool
|
|
|
|
Uploads map[string]string
|
|
|
|
}{
|
|
|
|
"Sudo": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
2015-06-25 15:48:54 +02:00
|
|
|
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
|
2015-05-08 18:17:57 +02:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
2015-07-09 21:29:27 +02:00
|
|
|
"secret_key_path": "test-fixtures/encrypted_data_bag_secret",
|
2015-05-08 18:17:57 +02:00
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
2016-01-09 15:56:49 +01:00
|
|
|
"sudo mkdir -p " + linuxConfDir: true,
|
|
|
|
"sudo chmod 777 " + linuxConfDir: true,
|
|
|
|
"sudo " + fmt.Sprintf(chmod, linuxConfDir, 666): true,
|
|
|
|
"sudo mkdir -p " + path.Join(linuxConfDir, "ohai/hints"): true,
|
|
|
|
"sudo chmod 777 " + path.Join(linuxConfDir, "ohai/hints"): true,
|
|
|
|
"sudo " + fmt.Sprintf(chmod, path.Join(linuxConfDir, "ohai/hints"), 666): true,
|
|
|
|
"sudo chmod 755 " + path.Join(linuxConfDir, "ohai/hints"): true,
|
|
|
|
"sudo " + fmt.Sprintf(chmod, path.Join(linuxConfDir, "ohai/hints"), 600): true,
|
|
|
|
"sudo chown -R root.root " + path.Join(linuxConfDir, "ohai/hints"): true,
|
|
|
|
"sudo chmod 755 " + linuxConfDir: true,
|
|
|
|
"sudo " + fmt.Sprintf(chmod, linuxConfDir, 600): true,
|
|
|
|
"sudo chown -R root.root " + linuxConfDir: true,
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/ohai/hints/ohaihint.json": "OHAI-HINT-FILE",
|
|
|
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"NoSudo": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
2015-07-09 21:29:27 +02:00
|
|
|
"secret_key_path": "test-fixtures/encrypted_data_bag_secret",
|
2015-05-08 18:17:57 +02:00
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"mkdir -p " + linuxConfDir: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"Proxy": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"http_proxy": "http://proxy.local",
|
|
|
|
"https_proxy": "https://proxy.local",
|
|
|
|
"no_proxy": []interface{}{"http://local.local", "https://local.local"},
|
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
2015-07-09 21:29:27 +02:00
|
|
|
"secret_key_path": "test-fixtures/encrypted_data_bag_secret",
|
2015-05-08 18:17:57 +02:00
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"mkdir -p " + linuxConfDir: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/client.rb": proxyLinuxClientConf,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/first-boot.json": `{"run_list":["cookbook::recipe"]}`,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
2015-05-08 18:17:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-02-09 11:11:46 +01:00
|
|
|
"Attributes": {
|
2016-01-29 18:41:14 +01:00
|
|
|
Config: testConfig(t, map[string]interface{}{
|
2016-02-09 11:11:46 +01:00
|
|
|
"attributes": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"key1": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"subkey1": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"subkey2a": []interface{}{
|
|
|
|
"val1", "val2", "val3",
|
|
|
|
},
|
|
|
|
"subkey2b": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"subkey3": "value3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"key2": "value2",
|
|
|
|
},
|
|
|
|
},
|
2016-01-29 18:41:14 +01:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"secret_key_path": "test-fixtures/encrypted_data_bag_secret",
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"mkdir -p " + linuxConfDir: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
|
|
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
|
|
|
linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
|
|
|
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
|
|
|
linuxConfDir + "/first-boot.json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
|
|
|
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2","run_list":["cookbook::recipe"]}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-02-09 11:11:46 +01:00
|
|
|
"Attributes JSON": {
|
2015-05-08 18:17:57 +02:00
|
|
|
Config: testConfig(t, map[string]interface{}{
|
2016-02-09 11:11:46 +01:00
|
|
|
"attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
|
|
|
|
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
|
2015-05-08 18:17:57 +02:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"prevent_sudo": true,
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
2015-07-09 21:29:27 +02:00
|
|
|
"secret_key_path": "test-fixtures/encrypted_data_bag_secret",
|
2015-05-08 18:17:57 +02:00
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
"mkdir -p " + linuxConfDir: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
Uploads: map[string]string{
|
2015-07-09 15:05:12 +02:00
|
|
|
linuxConfDir + "/client.rb": defaultLinuxClientConf,
|
2015-07-09 21:29:27 +02:00
|
|
|
linuxConfDir + "/encrypted_data_bag_secret": "SECRET-KEY-FILE",
|
|
|
|
linuxConfDir + "/validation.pem": "VALIDATOR-PEM-FILE",
|
2015-06-25 15:48:54 +02:00
|
|
|
linuxConfDir + "/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"]}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(ResourceProvisioner)
|
|
|
|
o := new(terraform.MockUIOutput)
|
|
|
|
c := new(communicator.MockCommunicator)
|
|
|
|
|
|
|
|
for k, tc := range cases {
|
|
|
|
c.Commands = tc.Commands
|
|
|
|
c.Uploads = tc.Uploads
|
|
|
|
|
|
|
|
p, err := r.decodeConfig(tc.Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = !p.PreventSudo
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
err = p.linuxCreateConfigFiles(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 defaultLinuxClientConf = `log_location STDOUT
|
2015-05-08 18:17:57 +02:00
|
|
|
chef_server_url "https://chef.local"
|
|
|
|
validation_client_name "validator"
|
|
|
|
node_name "nodename1"`
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
const proxyLinuxClientConf = `log_location STDOUT
|
2015-05-08 18:17:57 +02:00
|
|
|
chef_server_url "https://chef.local"
|
|
|
|
validation_client_name "validator"
|
|
|
|
node_name "nodename1"
|
|
|
|
|
|
|
|
|
2015-09-26 01:56:24 +02:00
|
|
|
|
|
|
|
|
2015-05-08 18:17:57 +02:00
|
|
|
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"
|
|
|
|
ENV['no_proxy'] = "http://local.local,https://local.local"`
|