Skip to content

Commit

Permalink
Added data source Template 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 3, 2020
1 parent 439c0d4 commit efbfce8
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions civo/datasource_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package civo

import (
"fmt"
"strconv"
"testing"

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

func TestAccDataSourceCivoTemplate_basic(t *testing.T) {
datasourceName := "data.civo_template.foobar"

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

func TestAccDataSourceCivoTemplate_WithFilter(t *testing.T) {
datasourceName := "data.civo_template.foobar"

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

func testAccCheckDataSourceCivoTemplateExist(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["templates.#"]
total, err := strconv.Atoi(rawTotal)
if err != nil {
return err
}

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

return nil
}
}

func testAccCheckDataSourceCivoTemplatesFiltered(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["templates.#"]
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
}

var prevCode string
for i := 0; i < total; i++ {
code := rs.Primary.Attributes[fmt.Sprintf("templates.%d.code", i)]
if !stringInSlice(code, []string{"debian-stretch", "debian-buster"}) {
return fmt.Errorf("Code is not in expected test filter values")
}
if prevCode != "" && prevCode < code {
return fmt.Errorf("Template is not sorted by code")
}
prevCode = code
}

return nil
}
}

func testAccDataSourceCivoTemplatesConfig() string {
return fmt.Sprintf(`
data "civo_template" "foobar" {
}
`)
}

func testAccDataSourceCivoTemplatesConfigWhitFilter() string {
return fmt.Sprintf(`
data "civo_template" "foobar" {
filter {
key = "code"
values = ["debian"]
}
}
`)
}

0 comments on commit efbfce8

Please sign in to comment.