-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Civo Kubernetes node pool configuration and organize code in…
… their own folders per service (#196) * Refactor Civo Kubernetes node pool configuration, also organize the code in they own folder per service * Add diagnosticsToString function to convert diag.Diagnostics to string * Refactor resource and data source names to add the right comment * Refactor acceptance tests and fix typos * Remove unused firewall rule resource for deprecation and update dependencies * Remove toolchain version from go.mod * Update import statement in resource_firewall.go * Fix volume typo and add volume-related tests * Update dependencies and fix test function names * Refactor acceptance test setup variables
- Loading branch information
1 parent
b80476b
commit 2a1d762
Showing
90 changed files
with
1,324 additions
and
2,471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package acceptance | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"testing" | ||
|
||
"github.com/civo/terraform-provider-civo/civo" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
var ( | ||
// TestAccProvider is a global instance of the provider under test. | ||
// It is used in acceptance tests to configure resources. | ||
TestAccProvider *schema.Provider | ||
|
||
// TestAccProviders is a map of provider instances keyed by their name. | ||
// It is used in acceptance tests where multiple providers are in play. | ||
TestAccProviders map[string]*schema.Provider | ||
|
||
// TestAccProviderFactories is a map of functions that return a provider instance and an error. | ||
// It is used in acceptance tests where the provider needs to be configured in a certain way. | ||
TestAccProviderFactories map[string]func() (*schema.Provider, error) | ||
) | ||
|
||
func init() { | ||
TestAccProvider = civo.Provider() | ||
TestAccProviders = map[string]*schema.Provider{ | ||
"civo": TestAccProvider, | ||
} | ||
TestAccProviderFactories = map[string]func() (*schema.Provider, error){ | ||
"civo": func() (*schema.Provider, error) { | ||
return TestAccProvider, nil | ||
}, | ||
} | ||
} | ||
|
||
// TestProvider - Test the provider itself | ||
func TestProvider(t *testing.T) { | ||
if err := civo.Provider().InternalValidate(); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
// TestProviderImpl is a test function to ensure that the Provider function of the civo package | ||
// returns an instance of the *schema.Provider type. It doesn't test any behavior of the provider itself. | ||
func TestProviderImpl(t *testing.T) { | ||
var _ *schema.Provider = civo.Provider() | ||
} | ||
|
||
// TestToken - Test the provider token | ||
func TestToken(t *testing.T) { | ||
rawProvider := civo.Provider() | ||
raw := map[string]interface{}{ | ||
"token": "123456789", | ||
} | ||
|
||
diags := rawProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(raw)) | ||
if diags.HasError() { | ||
t.Fatalf("provider configure failed: %s", DiagnosticsToString(diags)) | ||
} | ||
} | ||
|
||
// DiagnosticsToString - Convert diag.Diagnostics to string | ||
func DiagnosticsToString(diags diag.Diagnostics) string { | ||
diagsAsStrings := make([]string, len(diags)) | ||
for i, diag := range diags { | ||
diagsAsStrings[i] = diag.Summary | ||
} | ||
|
||
return strings.Join(diagsAsStrings, "; ") | ||
} | ||
|
||
// TestAccPreCheck - Check if the environment variables are set | ||
func TestAccPreCheck(t *testing.T) { | ||
if v := os.Getenv("CIVO_TOKEN"); v == "" { | ||
t.Fatal("CIVO_TOKEN must be set for acceptance tests") | ||
} | ||
if v := os.Getenv("CIVO_REGION"); v == "" { | ||
t.Fatal("CIVO_REGION must be set for acceptance tests") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// CivoInstanceDestroy is used to destroy the instance created during the test | ||
func CivoInstanceDestroy(s *terraform.State) error { | ||
client := TestAccProvider.Meta().(*civogo.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "civo_instance" { | ||
continue | ||
} | ||
|
||
_, err := client.GetInstance(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("instance still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CivoInstanceResourceExists queries the API and retrieves the matching Widget. | ||
func CivoInstanceResourceExists(n string, instance *civogo.Instance) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// find the corresponding state object | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", n) | ||
} | ||
|
||
// retrieve the configured client from the test setup | ||
client := TestAccProvider.Meta().(*civogo.Client) | ||
resp, err := client.GetInstance(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("instance not found: (%s) %s", rs.Primary.ID, err) | ||
} | ||
|
||
// If no error, assign the response Widget attribute to the widget pointer | ||
*instance = *resp | ||
|
||
// return fmt.Errorf("Domain (%s) not found", rs.Primary.ID) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// CivoReservedIPResourceExists queries the API and retrieves the matching Widget. | ||
func CivoReservedIPResourceExists(n string, ip *civogo.IP) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// find the corresponding state object | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", n) | ||
} | ||
|
||
// retrieve the configured client from the test setup | ||
client := TestAccProvider.Meta().(*civogo.Client) | ||
resp, err := client.FindIP(rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("ip not found: (%s) %s", rs.Primary.ID, err) | ||
} | ||
|
||
*ip = *resp | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// CivoKubernetesClusterDestroy is used to destroy the kubernetes cluster created during the test | ||
func CivoKubernetesClusterDestroy(s *terraform.State) error { | ||
client := TestAccProvider.Meta().(*civogo.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "civo_kubernetes_cluster" { | ||
continue | ||
} | ||
|
||
_, err := client.GetKubernetesCluster(rs.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("kubernetes Cluster still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.