Skip to content

Commit

Permalink
Added new test
Browse files Browse the repository at this point in the history
Added data source instances size test
Fix documentation for template import cmd

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jul 4, 2020
1 parent 8b75b83 commit 834a4e3
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
136 changes: 136 additions & 0 deletions civo/datasource_intances_size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package civo

import (
"fmt"
"strconv"
"testing"

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

func TestAccDataSourceCivoInstanceSize_basic(t *testing.T) {
datasourceName := "data.civo_instances_size.foobar"

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

func TestAccDataSourceCivoInstanceSize_WithFilterAndSort(t *testing.T) {
datasourceName := "data.civo_instances_size.foobar"

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

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

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

return nil
}
}

func testAccCheckDataSourceCivoInstanceSizeFilteredAndSorted(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["sizes.#"]
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 prevCPUCore float64
for i := 0; i < total; i++ {
name := rs.Primary.Attributes[fmt.Sprintf("sizes.%d.name", i)]
if !stringInSlice(name, []string{"g2.large", "g2.xlarge", "g2.2xlarge"}) {
return fmt.Errorf("Name is not in expected test filter values")
}

CPUCore, _ := strconv.ParseFloat(rs.Primary.Attributes[fmt.Sprintf("sizes.%d.cpu_cores", i)], 64)
if prevCPUCore > 0 && prevCPUCore < prevCPUCore {
return fmt.Errorf("Sizes is not sorted by CPU Core in descending order")
}
prevCPUCore = CPUCore

}

return nil
}
}

func testAccDataSourceCivoInstanceSizeConfig() string {
return fmt.Sprintf(`
data "civo_instances_size" "foobar" {
}
`)
}

func testAccDataSourceCivoInstanceSizeConfigWhitFilterAndSort() string {
return fmt.Sprintf(`
data "civo_instances_size" "foobar" {
filter {
key = "name"
values = ["large"]
}
sort {
key = "cpu_cores"
direction = "desc"
}
}
`)
}
4 changes: 2 additions & 2 deletions website/docs/r/template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ the instance is first booted.

## Import

Template can be imported using the template `id`, e.g.
Template can be imported using the template `code`, e.g.

```
terraform import civo_template.my-custom-template aa589909-6bcc-42cc-8be9-b9c3648f74c1
terraform import civo_template.my-custom-template my-template-code
```

0 comments on commit 834a4e3

Please sign in to comment.