Skip to content

Commit

Permalink
Refactor Civo Kubernetes node pool configuration and organize code in…
Browse files Browse the repository at this point in the history
… 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
alejandrojnm committed Feb 5, 2024
1 parent b80476b commit 2a1d762
Show file tree
Hide file tree
Showing 90 changed files with 1,324 additions and 2,471 deletions.
86 changes: 86 additions & 0 deletions civo/acceptance/acceptance.go
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")
}
}
51 changes: 51 additions & 0 deletions civo/acceptance/instances.go
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
}
}
31 changes: 31 additions & 0 deletions civo/acceptance/ip.go
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
}
}
26 changes: 26 additions & 0 deletions civo/acceptance/kubernetes.go
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
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package civo
package database

import (
"context"
Expand All @@ -12,9 +12,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Data source to get from the api a specific Database
// DataSourceDatabase Data source to get from the api a specific Database
// using the id or the name
func dataSourceDatabase() *schema.Resource {
func DataSourceDatabase() *schema.Resource {
return &schema.Resource{
Description: strings.Join([]string{
"Get information of an Database for use in other resources. This data source provides all of the Database's properties as configured on your Civo account.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package civo
package database_test

import (
"fmt"
"testing"

"github.com/civo/terraform-provider-civo/civo/acceptance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// TestAccDataSourceCivoDatabase_basic is used to test the data source
func TestAccDataSourceCivoDatabase_basic(t *testing.T) {
datasourceName := "data.civo_database.foobar"
name := acctest.RandomWithPrefix("database")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoDatabaseConfig(name),
Config: DataSourceCivoDatabaseConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttrSet(datasourceName, "size"),
Expand All @@ -31,7 +33,8 @@ func TestAccDataSourceCivoDatabase_basic(t *testing.T) {
})
}

func testAccDataSourceCivoDatabaseConfig(name string) string {
// DataSourceCivoDatabaseConfig is used to configure the data source
func DataSourceCivoDatabaseConfig(name string) string {
return fmt.Sprintf(`
resource "civo_database" "foobar" {
name = "%s"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package civo
package database

import (
"fmt"
Expand All @@ -7,16 +7,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// DatabaseVersion is a temporal struct to save all versions
type DatabaseVersion struct {
// Version is a temporal struct to save all versions
type Version struct {
Engine string
Version string
Default bool
}

// Data source to get and filter all database version
// DataDatabaseVersion Data source to get and filter all database version
// use to define the engine and version in resourceDatabase
func dataDatabaseVersion() *schema.Resource {
func DataDatabaseVersion() *schema.Resource {
dataListConfig := &datalist.ResourceConfig{
Description: "Retrieves information about the database versions that Civo supports, with the ability to filter the results.",
RecordSchema: versionSchema(),
Expand All @@ -37,10 +37,10 @@ func getVersion(m interface{}, _ map[string]interface{}) ([]interface{}, error)
return nil, fmt.Errorf("[ERR] error retrieving version: %s", err)
}

versionList := []DatabaseVersion{}
versionList := []Version{}
for k, v := range partialVersions {
for _, version := range v {
versionList = append(versionList, DatabaseVersion{
versionList = append(versionList, Version{
Engine: k,
Version: version.SoftwareVersion,
Default: version.Default,
Expand All @@ -56,7 +56,7 @@ func getVersion(m interface{}, _ map[string]interface{}) ([]interface{}, error)
}

func flattenVersion(versions, _ interface{}, _ map[string]interface{}) (map[string]interface{}, error) {
s := versions.(DatabaseVersion)
s := versions.(Version)

flattenedSize := map[string]interface{}{}
flattenedSize["engine"] = s.Engine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
package civo
package database_test

import (
"fmt"
"strconv"
"testing"

"github.com/civo/terraform-provider-civo/civo/acceptance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// TestAccDataSourceCivoDatabaseVersion_basic - Test the data source for database version
func TestAccDataSourceCivoDatabaseVersion_basic(t *testing.T) {
datasourceName := "data.civo_database_version.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoDatabaseVersionConfig(),
Config: DataSourceCivoDatabaseVersionConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckDataSourceDatabaseVersionExist(datasourceName),
DataSourceDatabaseVersionExist(datasourceName),
),
},
},
})
}

// TestAccDataSourceCivoDatabaseVersion_WithFilterAndSort - Test the data source for database version with filter and sort
func TestAccDataSourceCivoDatabaseVersion_WithFilterAndSort(t *testing.T) {
datasourceName := "data.civo_database_version.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoDatabaseVersionWhitFilterAndSort(),
Config: DataSourceCivoDatabaseVersionWhitFilterAndSort(),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceDatabaseVersionExist(datasourceName),
testAccCheckDataSourceCivoDatabaseVersionFilteredAndSorted(datasourceName),
DataSourceDatabaseVersionExist(datasourceName),
DataSourceCivoDatabaseVersionFilteredAndSorted(datasourceName),
),
},
},
})
}

func testAccCheckDataSourceDatabaseVersionExist(n string) resource.TestCheckFunc {
// DataSourceDatabaseVersionExist - Check if the data source exist
func DataSourceDatabaseVersionExist(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

Expand All @@ -70,7 +74,8 @@ func testAccCheckDataSourceDatabaseVersionExist(n string) resource.TestCheckFunc
}
}

func testAccCheckDataSourceCivoDatabaseVersionFilteredAndSorted(n string) resource.TestCheckFunc {
// DataSourceCivoDatabaseVersionFilteredAndSorted - Check if the data source is filtered and sorted
func DataSourceCivoDatabaseVersionFilteredAndSorted(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

Expand Down Expand Up @@ -104,14 +109,16 @@ func testAccCheckDataSourceCivoDatabaseVersionFilteredAndSorted(n string) resour
}
}

func testAccDataSourceCivoDatabaseVersionConfig() string {
// DataSourceCivoDatabaseVersionConfig - Config for the data source
func DataSourceCivoDatabaseVersionConfig() string {
return `
data "civo_database_version" "foobar" {
}
`
}

func testAccDataSourceCivoDatabaseVersionWhitFilterAndSort() string {
// DataSourceCivoDatabaseVersionWhitFilterAndSort - Config for the data source with filter and sort
func DataSourceCivoDatabaseVersionWhitFilterAndSort() string {
return `
data "civo_database_version" "foobar" {
filter {
Expand Down
Loading

0 comments on commit 2a1d762

Please sign in to comment.