2019-07-31 01:54:48 +02:00
|
|
|
package cliconfig
|
2014-05-26 02:39:44 +02:00
|
|
|
|
|
|
|
import (
|
2017-02-14 02:52:51 +01:00
|
|
|
"os"
|
2014-05-26 02:39:44 +02:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-10-20 02:40:20 +02:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-04-22 00:48:07 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2014-05-26 02:39:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is the directory where our test fixtures are.
|
2019-06-30 09:38:36 +02:00
|
|
|
const fixtureDir = "./testdata"
|
2014-05-26 02:39:44 +02:00
|
|
|
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
2017-10-20 02:01:02 +02:00
|
|
|
c, err := loadConfigFile(filepath.Join(fixtureDir, "config"))
|
2014-05-26 02:39:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Providers: map[string]string{
|
|
|
|
"aws": "foo",
|
|
|
|
"do": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, expected) {
|
|
|
|
t.Fatalf("bad: %#v", c)
|
|
|
|
}
|
|
|
|
}
|
2014-06-10 06:57:37 +02:00
|
|
|
|
2017-02-14 02:52:51 +01:00
|
|
|
func TestLoadConfig_env(t *testing.T) {
|
|
|
|
defer os.Unsetenv("TFTEST")
|
|
|
|
os.Setenv("TFTEST", "hello")
|
|
|
|
|
2017-10-20 02:01:02 +02:00
|
|
|
c, err := loadConfigFile(filepath.Join(fixtureDir, "config-env"))
|
2017-02-14 02:52:51 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Providers: map[string]string{
|
|
|
|
"aws": "hello",
|
|
|
|
"google": "bar",
|
|
|
|
},
|
|
|
|
Provisioners: map[string]string{
|
|
|
|
"local": "hello",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, expected) {
|
|
|
|
t.Fatalf("bad: %#v", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:00:08 +02:00
|
|
|
func TestLoadConfig_hosts(t *testing.T) {
|
|
|
|
got, diags := loadConfigFile(filepath.Join(fixtureDir, "hosts"))
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Fatalf("%s", diags.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := &Config{
|
|
|
|
Hosts: map[string]*ConfigHost{
|
|
|
|
"example.com": {
|
|
|
|
Services: map[string]interface{}{
|
|
|
|
"modules.v1": "https://example.com/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 02:40:20 +02:00
|
|
|
func TestLoadConfig_credentials(t *testing.T) {
|
2017-10-20 02:01:02 +02:00
|
|
|
got, err := loadConfigFile(filepath.Join(fixtureDir, "credentials"))
|
2017-10-20 02:40:20 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := &Config{
|
|
|
|
Credentials: map[string]map[string]interface{}{
|
|
|
|
"example.com": map[string]interface{}{
|
|
|
|
"token": "foo the bar baz",
|
|
|
|
},
|
|
|
|
"example.net": map[string]interface{}{
|
|
|
|
"username": "foo",
|
|
|
|
"password": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CredentialsHelpers: map[string]*ConfigCredentialsHelper{
|
|
|
|
"foo": &ConfigCredentialsHelper{
|
|
|
|
Args: []string{"bar", "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:43:18 +02:00
|
|
|
func TestConfigValidate(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
Config *Config
|
|
|
|
DiagCount int
|
|
|
|
}{
|
|
|
|
"nil": {
|
|
|
|
nil,
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
"empty": {
|
|
|
|
&Config{},
|
|
|
|
0,
|
|
|
|
},
|
2017-10-26 01:00:08 +02:00
|
|
|
"host good": {
|
|
|
|
&Config{
|
|
|
|
Hosts: map[string]*ConfigHost{
|
|
|
|
"example.com": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
"host with bad hostname": {
|
|
|
|
&Config{
|
|
|
|
Hosts: map[string]*ConfigHost{
|
|
|
|
"example..com": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
1, // host block has invalid hostname
|
|
|
|
},
|
2017-10-20 01:43:18 +02:00
|
|
|
"credentials good": {
|
|
|
|
&Config{
|
|
|
|
Credentials: map[string]map[string]interface{}{
|
|
|
|
"example.com": map[string]interface{}{
|
|
|
|
"token": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
"credentials with bad hostname": {
|
|
|
|
&Config{
|
|
|
|
Credentials: map[string]map[string]interface{}{
|
|
|
|
"example..com": map[string]interface{}{
|
|
|
|
"token": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
1, // credentials block has invalid hostname
|
|
|
|
},
|
|
|
|
"credentials helper good": {
|
|
|
|
&Config{
|
|
|
|
CredentialsHelpers: map[string]*ConfigCredentialsHelper{
|
|
|
|
"foo": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
"credentials helper too many": {
|
|
|
|
&Config{
|
|
|
|
CredentialsHelpers: map[string]*ConfigCredentialsHelper{
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
1, // no more than one credentials_helper block allowed
|
|
|
|
},
|
2020-04-22 00:48:07 +02:00
|
|
|
"provider_installation good none": {
|
|
|
|
&Config{
|
|
|
|
ProviderInstallation: nil,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
"provider_installation good one": {
|
|
|
|
&Config{
|
|
|
|
ProviderInstallation: []*ProviderInstallation{
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
"provider_installation too many": {
|
|
|
|
&Config{
|
|
|
|
ProviderInstallation: []*ProviderInstallation{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
1, // no more than one provider_installation block allowed
|
|
|
|
},
|
2017-10-20 01:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
diags := test.Config.Validate()
|
|
|
|
if len(diags) != test.DiagCount {
|
|
|
|
t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.DiagCount)
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %#v", diag.Description())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 06:57:37 +02:00
|
|
|
func TestConfig_Merge(t *testing.T) {
|
|
|
|
c1 := &Config{
|
|
|
|
Providers: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "blah",
|
|
|
|
},
|
2014-07-10 00:01:47 +02:00
|
|
|
Provisioners: map[string]string{
|
|
|
|
"local": "local",
|
|
|
|
"remote": "bad",
|
|
|
|
},
|
2017-10-26 01:00:08 +02:00
|
|
|
Hosts: map[string]*ConfigHost{
|
|
|
|
"example.com": {
|
|
|
|
Services: map[string]interface{}{
|
|
|
|
"modules.v1": "http://example.com/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-20 01:41:03 +02:00
|
|
|
Credentials: map[string]map[string]interface{}{
|
|
|
|
"foo": {
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CredentialsHelpers: map[string]*ConfigCredentialsHelper{
|
|
|
|
"buz": {},
|
|
|
|
},
|
2020-04-22 00:48:07 +02:00
|
|
|
ProviderInstallation: []*ProviderInstallation{
|
|
|
|
{
|
2020-04-23 01:28:06 +02:00
|
|
|
Methods: []*ProviderInstallationMethod{
|
2020-04-22 00:48:07 +02:00
|
|
|
{Location: ProviderInstallationFilesystemMirror("a")},
|
|
|
|
{Location: ProviderInstallationFilesystemMirror("b")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-23 01:28:06 +02:00
|
|
|
Methods: []*ProviderInstallationMethod{
|
2020-04-22 00:48:07 +02:00
|
|
|
{Location: ProviderInstallationFilesystemMirror("c")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-10 06:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c2 := &Config{
|
|
|
|
Providers: map[string]string{
|
|
|
|
"bar": "baz",
|
|
|
|
"baz": "what",
|
|
|
|
},
|
2014-07-10 00:01:47 +02:00
|
|
|
Provisioners: map[string]string{
|
|
|
|
"remote": "remote",
|
|
|
|
},
|
2017-10-26 01:00:08 +02:00
|
|
|
Hosts: map[string]*ConfigHost{
|
|
|
|
"example.net": {
|
|
|
|
Services: map[string]interface{}{
|
|
|
|
"modules.v1": "https://example.net/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-20 01:41:03 +02:00
|
|
|
Credentials: map[string]map[string]interface{}{
|
|
|
|
"fee": {
|
|
|
|
"bur": "bez",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CredentialsHelpers: map[string]*ConfigCredentialsHelper{
|
|
|
|
"biz": {},
|
|
|
|
},
|
2020-04-22 00:48:07 +02:00
|
|
|
ProviderInstallation: []*ProviderInstallation{
|
|
|
|
{
|
2020-04-23 01:28:06 +02:00
|
|
|
Methods: []*ProviderInstallationMethod{
|
2020-04-22 00:48:07 +02:00
|
|
|
{Location: ProviderInstallationFilesystemMirror("d")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-10 06:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Providers: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
"baz": "what",
|
|
|
|
},
|
2014-07-10 00:01:47 +02:00
|
|
|
Provisioners: map[string]string{
|
|
|
|
"local": "local",
|
|
|
|
"remote": "remote",
|
|
|
|
},
|
2017-10-26 01:00:08 +02:00
|
|
|
Hosts: map[string]*ConfigHost{
|
|
|
|
"example.com": {
|
|
|
|
Services: map[string]interface{}{
|
|
|
|
"modules.v1": "http://example.com/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"example.net": {
|
|
|
|
Services: map[string]interface{}{
|
|
|
|
"modules.v1": "https://example.net/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-20 01:41:03 +02:00
|
|
|
Credentials: map[string]map[string]interface{}{
|
|
|
|
"foo": {
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
"fee": {
|
|
|
|
"bur": "bez",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CredentialsHelpers: map[string]*ConfigCredentialsHelper{
|
|
|
|
"buz": {},
|
|
|
|
"biz": {},
|
|
|
|
},
|
2020-04-22 00:48:07 +02:00
|
|
|
ProviderInstallation: []*ProviderInstallation{
|
|
|
|
{
|
2020-04-23 01:28:06 +02:00
|
|
|
Methods: []*ProviderInstallationMethod{
|
2020-04-22 00:48:07 +02:00
|
|
|
{Location: ProviderInstallationFilesystemMirror("a")},
|
|
|
|
{Location: ProviderInstallationFilesystemMirror("b")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-23 01:28:06 +02:00
|
|
|
Methods: []*ProviderInstallationMethod{
|
2020-04-22 00:48:07 +02:00
|
|
|
{Location: ProviderInstallationFilesystemMirror("c")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-23 01:28:06 +02:00
|
|
|
Methods: []*ProviderInstallationMethod{
|
2020-04-22 00:48:07 +02:00
|
|
|
{Location: ProviderInstallationFilesystemMirror("d")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-10 06:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := c1.Merge(c2)
|
2020-04-22 00:48:07 +02:00
|
|
|
if diff := cmp.Diff(expected, actual); diff != "" {
|
|
|
|
t.Fatalf("wrong result\n%s", diff)
|
2014-06-10 06:57:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-23 18:34:05 +01:00
|
|
|
|
|
|
|
func TestConfig_Merge_disableCheckpoint(t *testing.T) {
|
|
|
|
c1 := &Config{
|
|
|
|
DisableCheckpoint: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 := &Config{}
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Providers: map[string]string{},
|
|
|
|
Provisioners: map[string]string{},
|
|
|
|
DisableCheckpoint: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := c1.Merge(c2)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Merge_disableCheckpointSignature(t *testing.T) {
|
|
|
|
c1 := &Config{
|
|
|
|
DisableCheckpointSignature: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 := &Config{}
|
|
|
|
|
|
|
|
expected := &Config{
|
|
|
|
Providers: map[string]string{},
|
|
|
|
Provisioners: map[string]string{},
|
|
|
|
DisableCheckpointSignature: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := c1.Merge(c2)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|