-
Notifications
You must be signed in to change notification settings - Fork 54
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 instances size test Fix documentation for template import cmd Signed-off-by: Alejandro JNM <[email protected]>
- Loading branch information
1 parent
8b75b83
commit 834a4e3
Showing
2 changed files
with
138 additions
and
2 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,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" | ||
} | ||
} | ||
`) | ||
} |
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