github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/opc/resource_ip_address_reservation_test.go (about)

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/go-oracle-terraform/compute"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccOPCIPAddressReservation_Basic(t *testing.T) {
    14  	rInt := acctest.RandInt()
    15  	resName := "opc_compute_ip_address_reservation.test"
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccOPCCheckIPAddressReservationDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccOPCIPAddressReservationConfig_Basic(rInt),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccOPCCheckIPAddressReservationExists,
    26  					resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-ip-address-reservation-%d", rInt)),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccOPCCheckIPAddressReservationExists(s *terraform.State) error {
    34  	client := testAccProvider.Meta().(*compute.Client).IPAddressReservations()
    35  
    36  	for _, rs := range s.RootModule().Resources {
    37  		if rs.Type != "opc_compute_ip_address_reservation" {
    38  			continue
    39  		}
    40  
    41  		input := compute.GetIPAddressReservationInput{
    42  			Name: rs.Primary.Attributes["name"],
    43  		}
    44  		if _, err := client.GetIPAddressReservation(&input); err != nil {
    45  			return fmt.Errorf("Error retrieving state of IP Address Reservation %s: %s", input.Name, err)
    46  		}
    47  	}
    48  
    49  	return nil
    50  }
    51  func testAccOPCCheckIPAddressReservationDestroy(s *terraform.State) error {
    52  	client := testAccProvider.Meta().(*compute.Client).IPAddressReservations()
    53  
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "opc_compute_ip_address_reservation" {
    56  			continue
    57  		}
    58  
    59  		input := compute.GetIPAddressReservationInput{
    60  			Name: rs.Primary.Attributes["name"],
    61  		}
    62  		if info, err := client.GetIPAddressReservation(&input); err == nil {
    63  			return fmt.Errorf("IP Address Reservation %s still exists: %#v", input.Name, info)
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func testAccOPCIPAddressReservationConfig_Basic(rInt int) string {
    71  	return fmt.Sprintf(`
    72  resource "opc_compute_ip_address_reservation" "test" {
    73    name = "testing-ip-address-reservation-%d"
    74    description = "testing-desc-%d"
    75    ip_address_pool = "public-ippool"
    76  }`, rInt, rInt)
    77  }