Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute committed Dec 5, 2019
1 parent cac1a01 commit 7540680
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 6 deletions.
12 changes: 6 additions & 6 deletions scaleway/data_source_baremetal_offer_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,19 @@ func dataSourceScalewayBaremetalOfferBetaRead(d *schema.ResourceData, m interfac
if err != nil {
return err
}
excludedOffers := []int(nil)
for i, offer := range res.Offers {

for i := 0; i < len(res.Offers); i++ {
offer := res.Offers[i]
switch {
case offer.Name == d.Get("name"), offer.ID == offerID:
if !offer.Enable && !d.Get("allow_disabled").(bool) {
return fmt.Errorf("offer %s (%s) found in zone %s but is disabled. Add allow_disabled=true in your terraform config to use it.", offer.Name, offer.Name, zone)
}
default:
excludedOffers = append(excludedOffers, i)
res.Offers = append(res.Offers[:i], res.Offers[i+1:]...)
i--
}
}
for _, excludedOffer := range excludedOffers {
res.Offers = append(res.Offers[:excludedOffer], res.Offers[:excludedOffer+1]...)
}

if len(res.Offers) == 0 {
return fmt.Errorf("no offer found with the name %s in zone %s", d.Get("name"), zone)
Expand All @@ -190,6 +189,7 @@ func dataSourceScalewayBaremetalOfferBetaRead(d *schema.ResourceData, m interfac
return err
}

d.Set("name", offer.Name)
d.Set("allow_disabled", !offer.Enable)
d.Set("bandwidth", offer.Bandwidth)
d.Set("commercial_range", offer.CommercialRange)
Expand Down
102 changes: 102 additions & 0 deletions scaleway/data_source_baremetal_offer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package scaleway

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func TestAccScalewayDataSourceBaremetalOffer_Basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
data "scaleway_baremetal_offer" "test1" {
zone = "fr-par-2"
name = "HC-BM1-L"
}
data "scaleway_baremetal_offer" "test2" {
zone = "fr-par-2"
offer_id = "3ab0dc29-2fd4-486e-88bf-d08fbf49214b"
}
data "scaleway_baremetal_offer" "test3" {
offer_id = "fr-par-2/3ab0dc29-2fd4-486e-88bf-d08fbf49214b"
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayBaremetalOfferExists("data.scaleway_baremetal_offer.test1"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test1", "name", "HC-BM1-L"),
testAccCheckScalewayBaremetalOfferExists("data.scaleway_baremetal_offer.test2"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "offer_id", "fr-par-2/3ab0dc29-2fd4-486e-88bf-d08fbf49214b"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "name", "HC-BM1-L"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "allow_disabled", "false"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "bandwidth", "1000"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "commercial_range", "high_cpu"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "price_per_sixty_minutes", "EUR 1.50"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "price_per_month", "EUR 749.99"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "quota_name", "bmaas_high"),
//resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "stock", "available"), // skipping this as stocks vary too much
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.0.name", "Intel Xeon Gold 5120"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.0.core_count", "28"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.0.frequency", "2200"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.0.thread_count", "56"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.1.name", "Intel Xeon Gold 5120"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.1.core_count", "28"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.1.frequency", "2200"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "cpu.1.thread_count", "56"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "disk.0.type", "NVMe"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "disk.0.capacity", "1024"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "disk.1.type", "NVMe"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "disk.1.capacity", "1024"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "disk.2.type", "NVMe"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "disk.3.capacity", "1024"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "memory.0.type", "DDR4"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "memory.0.capacity", "384"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "memory.0.frequency", "2133"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test2", "memory.0.ecc", "true"),
testAccCheckScalewayBaremetalOfferExists("data.scaleway_baremetal_offer.test3"),
resource.TestCheckResourceAttr("data.scaleway_baremetal_offer.test3", "name", "HC-BM1-L"),
),
},
},
})
}

func testAccCheckScalewayBaremetalOfferExists(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)
}
fmt.Println(rs)

zone, id, err := parseZonedID(rs.Primary.ID)
if err != nil {
return err
}

meta := testAccProvider.Meta().(*Meta)
baremetalApi := baremetal.NewAPI(meta.scwClient)
resp, err := baremetalApi.ListOffers(&baremetal.ListOffersRequest{
Zone: zone,
}, scw.WithAllPages())

if err != nil {
return err
}
for _, offer := range resp.Offers {
if offer.ID == id {
return nil
}
}
return fmt.Errorf("offer %s not found in zone %s", id, zone)
}
}

0 comments on commit 7540680

Please sign in to comment.