Skip to content

Commit

Permalink
Fix nic ressource testacc
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericBerot-Armand committed Feb 21, 2023
1 parent 0aa3218 commit afe4e25
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
2 changes: 0 additions & 2 deletions outscale/resource_outscale_nic_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

func TestAccOutscaleNicLink_Basic(t *testing.T) {
var conf oscgo.Nic
omi := os.Getenv("OUTSCALE_IMAGEID")
region := os.Getenv("OUTSCALE_REGION")
rInt := acctest.RandInt()
Expand All @@ -32,7 +31,6 @@ func TestAccOutscaleNicLink_Basic(t *testing.T) {
{
Config: testAccOutscaleNicLinkConfigBasic(rInt, omi, "tinav4.c2r2p2", region),
Check: resource.ComposeTestCheckFunc(
testAccCheckOutscaleOAPIENIExists("outscale_nic.outscale_nic", &conf),
resource.TestCheckResourceAttr(
"outscale_nic_link.outscale_nic_link", "device_number", "1"),
resource.TestCheckResourceAttrSet(
Expand Down
4 changes: 0 additions & 4 deletions outscale/resource_outscale_nic_private_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"os"
"testing"

oscgo "github.com/outscale/osc-sdk-go/v2"

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

func TestAccOutscaleOAPINetworkInterfacePrivateIPBasic(t *testing.T) {
region := os.Getenv("OUTSCALE_REGION")
var conf oscgo.Nic

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -23,7 +20,6 @@ func TestAccOutscaleOAPINetworkInterfacePrivateIPBasic(t *testing.T) {
{
Config: testAccOutscaleOAPINetworkInterfacePrivateIPConfigBasic(region),
Check: resource.ComposeTestCheckFunc(
testAccCheckOutscaleOAPIENIExists("outscale_nic.outscale_nic", &conf),
resource.TestCheckResourceAttr("outscale_nic_private_ip.outscale_nic_private_ip", "private_ips.#", "1"),
resource.TestCheckResourceAttr("outscale_nic_private_ip.outscale_nic_private_ip", "private_ips.0", "10.0.45.67"),
resource.TestCheckResourceAttrSet("outscale_nic_private_ip.outscale_nic_private_ip", "primary_private_ip")),
Expand Down
80 changes: 44 additions & 36 deletions outscale/resource_outscale_nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"

Expand All @@ -17,7 +17,6 @@ import (

func TestAccOutscaleOAPIENI_basic(t *testing.T) {
t.Parallel()
var conf oscgo.Nic
subregion := os.Getenv("OUTSCALE_REGION")

resource.Test(t, resource.TestCase{
Expand All @@ -29,32 +28,23 @@ func TestAccOutscaleOAPIENI_basic(t *testing.T) {
{
Config: testAccOutscaleOAPIENIConfig(subregion),
Check: resource.ComposeTestCheckFunc(
testAccCheckOutscaleOAPIENIExists("outscale_nic.outscale_nic", &conf),
testAccCheckOutscaleOAPIENIAttributes(&conf, subregion),
resource.TestCheckResourceAttr("outscale_nic.outscale_nic", "private_ips.#", "2"),
),
},
{
Config: testAccOutscaleOAPIENIConfigUpdate(subregion),
Check: resource.ComposeTestCheckFunc(
testAccCheckOutscaleOAPIENIExists("outscale_nic.outscale_nic", &conf),
testAccCheckOutscaleOAPIENIAttributes(&conf, subregion),
resource.TestCheckResourceAttr("outscale_nic.outscale_nic", "private_ips.#", "3"),
),
},
},
})
}

func testAccCheckOutscaleOAPIENIExists(n string, res *oscgo.Nic) 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 ENI ID is set")
func testAccCheckOutscaleOAPINICDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "outscale_nic" {
continue
}

conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI
Expand All @@ -74,35 +64,20 @@ func testAccCheckOutscaleOAPIENIExists(n string, res *oscgo.Nic) resource.TestCh
})

if err != nil {
if strings.Contains(fmt.Sprint(err), "InvalidNetworkInterfaceID.NotFound") {
return nil
}
errString := err.Error()
return fmt.Errorf("Could not find network interface: %s", errString)

}

if len(resp.GetNics()) != 1 ||
resp.GetNics()[0].GetNicId() != rs.Primary.ID {
return fmt.Errorf("ENI not found")
if len(resp.GetNics()) > 0 {
return fmt.Errorf("Nic with id %s is not destroyed yet", rs.Primary.ID)
}

*res = resp.GetNics()[0]

return nil
}
}

func testAccCheckOutscaleOAPIENIAttributes(conf *oscgo.Nic, suregion string) resource.TestCheckFunc {
return func(s *terraform.State) error {

if !reflect.DeepEqual(conf.GetLinkNic(), oscgo.LinkNic{}) {
return fmt.Errorf("expected attachment to be nil")
}

if conf.GetSubregionName() != fmt.Sprintf("%sa", suregion) {
return fmt.Errorf("expected subregion_name to be %sa, but was %s", suregion, conf.GetSubregionName())
}

return nil
}
return nil
}

func testAccOutscaleOAPIENIConfig(subregion string) string {
Expand Down Expand Up @@ -187,3 +162,36 @@ func testAccOutscaleOAPIENIConfigUpdate(subregion string) string {
}
`, subregion)
}

func testAccCheckOutscaleOAPIENIDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "outscale_nic" {
continue
}

var resp oscgo.ReadNicsResponse
conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI
req := oscgo.ReadNicsRequest{
Filters: &oscgo.FiltersNic{NicIds: &[]string{rs.Primary.ID}},
}

var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.NicApi.ReadNics(context.Background()).ReadNicsRequest(req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return err
}

if len(resp.GetNics()) != 0 {
return fmt.Errorf("Nic is not destroyed yet")
}
}
return nil
}

0 comments on commit afe4e25

Please sign in to comment.