From 9ceef2b32e5d881045c9a1daa7ef37675c636105 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Fri, 9 Jun 2017 11:00:46 +0200 Subject: [PATCH] test: move discover tests --- command/agent/command_test.go | 95 ------------------------------ command/agent/config_azure_test.go | 43 ++++++++++++++ command/agent/config_ec2_test.go | 38 ++++++++++++ command/agent/config_gce_test.go | 35 +++++++++++ 4 files changed, 116 insertions(+), 95 deletions(-) create mode 100644 command/agent/config_azure_test.go create mode 100644 command/agent/config_ec2_test.go create mode 100644 command/agent/config_gce_test.go diff --git a/command/agent/command_test.go b/command/agent/command_test.go index d6e5dcaa40..29ea690245 100644 --- a/command/agent/command_test.go +++ b/command/agent/command_test.go @@ -352,101 +352,6 @@ func TestRetryJoinWanFail(t *testing.T) { } } -func TestDiscoverEC2Hosts(t *testing.T) { - t.Parallel() - if os.Getenv("AWS_REGION") == "" { - t.Skip("AWS_REGION not set, skipping") - } - - if os.Getenv("AWS_ACCESS_KEY_ID") == "" { - t.Skip("AWS_ACCESS_KEY_ID not set, skipping") - } - - if os.Getenv("AWS_SECRET_ACCESS_KEY") == "" { - t.Skip("AWS_SECRET_ACCESS_KEY not set, skipping") - } - - c := &Config{ - RetryJoinEC2: RetryJoinEC2{ - Region: os.Getenv("AWS_REGION"), - TagKey: "ConsulRole", - TagValue: "Server", - }, - } - - servers, err := c.discoverEc2Hosts(&log.Logger{}) - if err != nil { - t.Fatal(err) - } - if len(servers) != 3 { - t.Fatalf("bad: %v", servers) - } -} - -func TestDiscoverGCEHosts(t *testing.T) { - t.Parallel() - if os.Getenv("GCE_PROJECT") == "" { - t.Skip("GCE_PROJECT not set, skipping") - } - - if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" && os.Getenv("GCE_CONFIG_CREDENTIALS") == "" { - t.Skip("GOOGLE_APPLICATION_CREDENTIALS or GCE_CONFIG_CREDENTIALS not set, skipping") - } - - c := &Config{ - RetryJoinGCE: RetryJoinGCE{ - ProjectName: os.Getenv("GCE_PROJECT"), - ZonePattern: os.Getenv("GCE_ZONE"), - TagValue: "consulrole-server", - CredentialsFile: os.Getenv("GCE_CONFIG_CREDENTIALS"), - }, - } - - servers, err := c.discoverGCEHosts(log.New(os.Stderr, "", log.LstdFlags)) - if err != nil { - t.Fatal(err) - } - if len(servers) != 3 { - t.Fatalf("bad: %v", servers) - } -} - -func TestDiscoverAzureHosts(t *testing.T) { - subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") - tenantID := os.Getenv("ARM_TENANT_ID") - clientID := os.Getenv("ARM_CLIENT_ID") - clientSecret := os.Getenv("ARM_CLIENT_SECRET") - environment := os.Getenv("ARM_ENVIRONMENT") - - if subscriptionID == "" || clientID == "" || clientSecret == "" || tenantID == "" { - t.Skip("ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET and ARM_TENANT_ID " + - "must be set to test Discover Azure Hosts") - } - - if environment == "" { - t.Log("Environments other than Public not supported at the moment") - } - - c := &Config{ - RetryJoinAzure: RetryJoinAzure{ - SubscriptionID: subscriptionID, - ClientID: clientID, - SecretAccessKey: clientSecret, - TenantID: tenantID, - TagName: "type", - TagValue: "Foundation", - }, - } - - servers, err := c.discoverAzureHosts(log.New(os.Stderr, "", log.LstdFlags)) - if err != nil { - t.Fatal(err) - } - if len(servers) != 3 { - t.Fatalf("bad: %v", servers) - } -} - func TestProtectDataDir(t *testing.T) { t.Parallel() dir := testutil.TempDir(t, "consul") diff --git a/command/agent/config_azure_test.go b/command/agent/config_azure_test.go new file mode 100644 index 0000000000..f451703348 --- /dev/null +++ b/command/agent/config_azure_test.go @@ -0,0 +1,43 @@ +package agent + +import ( + "log" + "os" + "testing" +) + +func TestDiscoverAzureHosts(t *testing.T) { + subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") + tenantID := os.Getenv("ARM_TENANT_ID") + clientID := os.Getenv("ARM_CLIENT_ID") + clientSecret := os.Getenv("ARM_CLIENT_SECRET") + environment := os.Getenv("ARM_ENVIRONMENT") + + if subscriptionID == "" || clientID == "" || clientSecret == "" || tenantID == "" { + t.Skip("ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET and ARM_TENANT_ID " + + "must be set to test Discover Azure Hosts") + } + + if environment == "" { + t.Log("Environments other than Public not supported at the moment") + } + + c := &Config{ + RetryJoinAzure: RetryJoinAzure{ + SubscriptionID: subscriptionID, + ClientID: clientID, + SecretAccessKey: clientSecret, + TenantID: tenantID, + TagName: "type", + TagValue: "Foundation", + }, + } + + servers, err := c.discoverAzureHosts(log.New(os.Stderr, "", log.LstdFlags)) + if err != nil { + t.Fatal(err) + } + if len(servers) != 3 { + t.Fatalf("bad: %v", servers) + } +} diff --git a/command/agent/config_ec2_test.go b/command/agent/config_ec2_test.go new file mode 100644 index 0000000000..2a529b752f --- /dev/null +++ b/command/agent/config_ec2_test.go @@ -0,0 +1,38 @@ +package agent + +import ( + "log" + "os" + "testing" +) + +func TestDiscoverEC2Hosts(t *testing.T) { + t.Parallel() + if os.Getenv("AWS_REGION") == "" { + t.Skip("AWS_REGION not set, skipping") + } + + if os.Getenv("AWS_ACCESS_KEY_ID") == "" { + t.Skip("AWS_ACCESS_KEY_ID not set, skipping") + } + + if os.Getenv("AWS_SECRET_ACCESS_KEY") == "" { + t.Skip("AWS_SECRET_ACCESS_KEY not set, skipping") + } + + c := &Config{ + RetryJoinEC2: RetryJoinEC2{ + Region: os.Getenv("AWS_REGION"), + TagKey: "ConsulRole", + TagValue: "Server", + }, + } + + servers, err := c.discoverEc2Hosts(&log.Logger{}) + if err != nil { + t.Fatal(err) + } + if len(servers) != 3 { + t.Fatalf("bad: %v", servers) + } +} diff --git a/command/agent/config_gce_test.go b/command/agent/config_gce_test.go new file mode 100644 index 0000000000..aeae75f241 --- /dev/null +++ b/command/agent/config_gce_test.go @@ -0,0 +1,35 @@ +package agent + +import ( + "log" + "os" + "testing" +) + +func TestDiscoverGCEHosts(t *testing.T) { + t.Parallel() + if os.Getenv("GCE_PROJECT") == "" { + t.Skip("GCE_PROJECT not set, skipping") + } + + if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" && os.Getenv("GCE_CONFIG_CREDENTIALS") == "" { + t.Skip("GOOGLE_APPLICATION_CREDENTIALS or GCE_CONFIG_CREDENTIALS not set, skipping") + } + + c := &Config{ + RetryJoinGCE: RetryJoinGCE{ + ProjectName: os.Getenv("GCE_PROJECT"), + ZonePattern: os.Getenv("GCE_ZONE"), + TagValue: "consulrole-server", + CredentialsFile: os.Getenv("GCE_CONFIG_CREDENTIALS"), + }, + } + + servers, err := c.discoverGCEHosts(log.New(os.Stderr, "", log.LstdFlags)) + if err != nil { + t.Fatal(err) + } + if len(servers) != 3 { + t.Fatalf("bad: %v", servers) + } +}