github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_route_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 TestAccOPCRoute_Basic(t *testing.T) { 14 rInt := acctest.RandInt() 15 resName := "opc_compute_route.test" 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccOPCCheckRouteDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccOPCRouteConfig_Basic(rInt), 24 Check: resource.ComposeTestCheckFunc( 25 testAccOPCCheckRouteExists, 26 resource.TestCheckResourceAttr(resName, "admin_distance", "1"), 27 resource.TestCheckResourceAttr(resName, "ip_address_prefix", "10.0.12.0/24"), 28 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-route-%d", rInt)), 29 ), 30 }, 31 { 32 Config: testAccOPCRouteConfig_BasicUpdate(rInt), 33 Check: resource.ComposeTestCheckFunc( 34 testAccOPCCheckRouteExists, 35 resource.TestCheckResourceAttr(resName, "admin_distance", "2"), 36 resource.TestCheckResourceAttr(resName, "ip_address_prefix", "10.0.14.0/24"), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func testAccOPCRouteConfig_Basic(rInt int) string { 44 return fmt.Sprintf(` 45 resource "opc_compute_ip_network" "foo" { 46 name = "testing-route-%d" 47 description = "testing-route" 48 ip_address_prefix = "10.1.14.0/24" 49 } 50 51 resource "opc_compute_instance" "foo" { 52 name = "test-route-%d" 53 label = "testing" 54 shape = "oc3" 55 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 56 networking_info { 57 index = 0 58 ip_network = "${opc_compute_ip_network.foo.id}" 59 vnic = "test-vnic-set-%d" 60 shared_network = false 61 } 62 } 63 64 data "opc_compute_network_interface" "foo" { 65 instance_name = "${opc_compute_instance.foo.name}" 66 instance_id = "${opc_compute_instance.foo.id}" 67 interface = "eth0" 68 } 69 70 resource "opc_compute_vnic_set" "test" { 71 name = "route-test-%d" 72 description = "route-testing-%d" 73 virtual_nics = ["${data.opc_compute_network_interface.foo.vnic}"] 74 } 75 76 resource "opc_compute_route" "test" { 77 name = "testing-route-%d" 78 description = "testing-desc-%d" 79 admin_distance = 1 80 ip_address_prefix = "10.0.12.0/24" 81 next_hop_vnic_set = "${opc_compute_vnic_set.test.name}" 82 }`, rInt, rInt, rInt, rInt, rInt, rInt, rInt) 83 } 84 85 func testAccOPCRouteConfig_BasicUpdate(rInt int) string { 86 return fmt.Sprintf(` 87 resource "opc_compute_ip_network" "foo" { 88 name = "testing-route-%d" 89 description = "testing-route" 90 ip_address_prefix = "10.1.14.0/24" 91 } 92 93 resource "opc_compute_instance" "foo" { 94 name = "test-route-%d" 95 label = "testing" 96 shape = "oc3" 97 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 98 networking_info { 99 index = 0 100 ip_network = "${opc_compute_ip_network.foo.id}" 101 vnic = "test-vnic-set-%d" 102 shared_network = false 103 } 104 } 105 106 data "opc_compute_network_interface" "foo" { 107 instance_name = "${opc_compute_instance.foo.name}" 108 instance_id = "${opc_compute_instance.foo.id}" 109 interface = "eth0" 110 } 111 112 resource "opc_compute_vnic_set" "test" { 113 name = "route-test-%d" 114 description = "route-testing-%d" 115 virtual_nics = ["${data.opc_compute_network_interface.foo.vnic}"] 116 } 117 118 resource "opc_compute_route" "test" { 119 name = "testing-route-%d" 120 description = "testing-desc-%d" 121 admin_distance = 2 122 ip_address_prefix = "10.0.14.0/24" 123 next_hop_vnic_set = "${opc_compute_vnic_set.test.name}" 124 }`, rInt, rInt, rInt, rInt, rInt, rInt, rInt) 125 } 126 127 func testAccOPCCheckRouteExists(s *terraform.State) error { 128 client := testAccProvider.Meta().(*compute.Client).Routes() 129 130 for _, rs := range s.RootModule().Resources { 131 if rs.Type != "opc_compute_route" { 132 continue 133 } 134 135 input := compute.GetRouteInput{ 136 Name: rs.Primary.Attributes["name"], 137 } 138 if _, err := client.GetRoute(&input); err != nil { 139 return fmt.Errorf("Error retrieving state of Rule %s: %s", input.Name, err) 140 } 141 } 142 143 return nil 144 } 145 146 func testAccOPCCheckRouteDestroy(s *terraform.State) error { 147 client := testAccProvider.Meta().(*compute.Client).Routes() 148 149 for _, rs := range s.RootModule().Resources { 150 if rs.Type != "opc_compute_route" { 151 continue 152 } 153 154 input := compute.GetRouteInput{ 155 Name: rs.Primary.Attributes["name"], 156 } 157 if info, err := client.GetRoute(&input); err == nil { 158 return fmt.Errorf("Rule %s still exists: %#v", input.Name, info) 159 } 160 } 161 162 return nil 163 }