Skip to content

Commit

Permalink
Added data source kubernetes versions test
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jul 5, 2020
1 parent 4f3a1a1 commit 9d480c9
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions civo/datasource_kubernetes_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package civo

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccDataSourceCivoKubernetesVersion_basic(t *testing.T) {
datasourceName := "data.civo_kubernetes_version.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoKubernetesVersionConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckDataSourceCivoKubernetesVersionExist(datasourceName),
),
},
},
})
}

func TestAccDataSourceCivoKubernetesVersion_WithFilter(t *testing.T) {
datasourceName := "data.civo_kubernetes_version.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoKubernetesVersionConfigWhitFilter(),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceCivoKubernetesVersionExist(datasourceName),
testAccCheckDataSourceCivoKubernetesVersionFiltered(datasourceName),
),
},
},
})
}

func testAccCheckDataSourceCivoKubernetesVersionExist(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Record ID is set")
}

rawTotal := rs.Primary.Attributes["versions.#"]
total, err := strconv.Atoi(rawTotal)
if err != nil {
return err
}

if total < 1 {
return fmt.Errorf("No Civo kubernetes versions retrieved")
}

return nil
}
}

func testAccCheckDataSourceCivoKubernetesVersionFiltered(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

if !ok {
return fmt.Errorf("Not found: %s", n)
}

rawTotal := rs.Primary.Attributes["versions.#"]
total, err := strconv.Atoi(rawTotal)
if err != nil {
return err
}

stringInSlice := func(value string, slice []string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}

for i := 0; i < total; i++ {
name := rs.Primary.Attributes[fmt.Sprintf("versions.%d.type", i)]
if !stringInSlice(name, []string{"stable"}) {
return fmt.Errorf("Type is not in expected test filter values")
}

}

return nil
}
}

func testAccDataSourceCivoKubernetesVersionConfig() string {
return fmt.Sprintf(`
data "civo_kubernetes_version" "foobar" {
}
`)
}

func testAccDataSourceCivoKubernetesVersionConfigWhitFilter() string {
return fmt.Sprintf(`
data "civo_kubernetes_version" "foobar" {
filter {
key = "type"
values = ["stable"]
}
}
`)
}

0 comments on commit 9d480c9

Please sign in to comment.