2015-05-08 23:25:24 +02:00
|
|
|
package chef
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-05-08 18:17:57 +02:00
|
|
|
"github.com/hashicorp/terraform/communicator"
|
2015-05-08 13:45:31 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceProvisioner_impl(t *testing.T) {
|
|
|
|
var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_good(t *testing.T) {
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
2015-05-08 18:17:57 +02:00
|
|
|
"attributes": []interface{}{"key1 { subkey1 = value1 }"},
|
2015-05-08 13:45:31 +02:00
|
|
|
"environment": "_default",
|
2015-05-08 18:17:57 +02:00
|
|
|
"node_name": "nodename1",
|
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
2015-05-08 13:45:31 +02:00
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "validator.pem",
|
|
|
|
})
|
2015-05-08 18:17:57 +02:00
|
|
|
r := new(ResourceProvisioner)
|
|
|
|
warn, errs := r.Validate(c)
|
2015-05-08 13:45:31 +02:00
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("Warnings: %v", warn)
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("Errors: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_bad(t *testing.T) {
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
2015-05-08 18:17:57 +02:00
|
|
|
"invalid": "nope",
|
2015-05-08 13:45:31 +02:00
|
|
|
})
|
|
|
|
p := new(ResourceProvisioner)
|
|
|
|
warn, errs := p.Validate(c)
|
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("Warnings: %v", warn)
|
|
|
|
}
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Fatalf("Should have errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
|
|
|
|
r, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.NewResourceConfig(r)
|
|
|
|
}
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
func TestResourceProvider_runChefClient(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
ConfDir string
|
|
|
|
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": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
ConfDir: linuxConfDir,
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
`sudo chef-client -j "/etc/chef/first-boot.json" -E "_default"`: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"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": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
ConfDir: linuxConfDir,
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
`chef-client -j "/etc/chef/first-boot.json" -E "_default"`: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"Environment": {
|
|
|
|
Config: testConfig(t, map[string]interface{}{
|
|
|
|
"environment": "production",
|
|
|
|
"node_name": "nodename1",
|
2015-06-25 15:48:54 +02:00
|
|
|
"prevent_sudo": true,
|
2015-05-08 18:17:57 +02:00
|
|
|
"run_list": []interface{}{"cookbook::recipe"},
|
|
|
|
"server_url": "https://chef.local",
|
|
|
|
"validation_client_name": "validator",
|
|
|
|
"validation_key_path": "test-fixtures/validator.pem",
|
|
|
|
}),
|
|
|
|
|
|
|
|
ConfDir: windowsConfDir,
|
|
|
|
|
|
|
|
Commands: map[string]bool{
|
|
|
|
`chef-client -j "C:/chef/first-boot.json" -E "production"`: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.runChefClient = p.runChefClientFunc(tc.ConfDir)
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = !p.PreventSudo
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
err = p.runChefClient(o, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %q failed: %v", k, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|