-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added data source kubernetes versions test
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
4f3a1a1
commit 9d480c9
Showing
1 changed file
with
124 additions
and
0 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,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"] | ||
} | ||
} | ||
`) | ||
} |