github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_instance_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 TestAccOPCInstance_basic(t *testing.T) { 14 resName := "opc_compute_instance.test" 15 rInt := acctest.RandInt() 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccOPCCheckInstanceDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccInstanceBasic(rInt), 24 Check: resource.ComposeTestCheckFunc( 25 testAccOPCCheckInstanceExists, 26 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("acc-test-instance-%d", rInt)), 27 resource.TestCheckResourceAttr(resName, "label", "TestAccOPCInstance_basic"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccOPCInstance_sharedNetworking(t *testing.T) { 35 rInt := acctest.RandInt() 36 resName := "opc_compute_instance.test" 37 dataName := "data.opc_compute_network_interface.test" 38 39 resource.Test(t, resource.TestCase{ 40 PreCheck: func() { testAccPreCheck(t) }, 41 Providers: testAccProviders, 42 CheckDestroy: testAccOPCCheckInstanceDestroy, 43 Steps: []resource.TestStep{ 44 { 45 Config: testAccInstanceSharedNetworking(rInt), 46 Check: resource.ComposeTestCheckFunc( 47 testAccOPCCheckInstanceExists, 48 resource.TestCheckResourceAttrSet(resName, "id"), 49 resource.TestCheckResourceAttrSet(resName, "availability_domain"), 50 resource.TestCheckResourceAttrSet(resName, "domain"), 51 resource.TestCheckResourceAttrSet(resName, "hostname"), 52 resource.TestCheckResourceAttrSet(resName, "ip_address"), 53 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("acc-test-instance-%d", rInt)), 54 resource.TestCheckResourceAttr(resName, "networking_info.#", "1"), 55 // Default Placement Reqs 56 resource.TestCheckResourceAttr(resName, "placement_requirements.#", "2"), 57 resource.TestCheckResourceAttr(resName, "placement_requirements.0", "/system/compute/allow_instances"), 58 resource.TestCheckResourceAttr(resName, "placement_requirements.1", "/system/compute/placement/default"), 59 resource.TestCheckResourceAttr(resName, "platform", "linux"), 60 resource.TestCheckResourceAttr(resName, "priority", "/oracle/public/default"), 61 resource.TestCheckResourceAttr(resName, "reverse_dns", "true"), 62 resource.TestCheckResourceAttr(resName, "state", "running"), 63 resource.TestCheckResourceAttr(resName, "tags.#", "2"), 64 resource.TestCheckResourceAttrSet(resName, "vcable"), 65 resource.TestCheckResourceAttr(resName, "virtio", "false"), 66 67 // Check Data Source to validate networking attributes 68 resource.TestCheckResourceAttr(dataName, "shared_network", "true"), 69 resource.TestCheckResourceAttr(dataName, "nat.#", "1"), 70 resource.TestCheckResourceAttr(dataName, "sec_lists.#", "1"), 71 resource.TestCheckResourceAttr(dataName, "name_servers.#", "0"), 72 resource.TestCheckResourceAttr(dataName, "vnic_sets.#", "0"), 73 ), 74 }, 75 }, 76 }) 77 } 78 79 func TestAccOPCInstance_ipNetwork(t *testing.T) { 80 rInt := acctest.RandInt() 81 resName := "opc_compute_instance.test" 82 dataName := "data.opc_compute_network_interface.test" 83 84 resource.Test(t, resource.TestCase{ 85 PreCheck: func() { testAccPreCheck(t) }, 86 Providers: testAccProviders, 87 CheckDestroy: testAccOPCCheckInstanceDestroy, 88 Steps: []resource.TestStep{ 89 { 90 Config: testAccInstanceIPNetworking(rInt), 91 Check: resource.ComposeTestCheckFunc( 92 testAccOPCCheckInstanceExists, 93 resource.TestCheckResourceAttrSet(resName, "id"), 94 resource.TestCheckResourceAttrSet(resName, "availability_domain"), 95 resource.TestCheckResourceAttrSet(resName, "domain"), 96 resource.TestCheckResourceAttrSet(resName, "ip_address"), 97 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("acc-test-instance-%d", rInt)), 98 resource.TestCheckResourceAttr(resName, "networking_info.#", "1"), 99 // Default Placement Reqs 100 resource.TestCheckResourceAttr(resName, "placement_requirements.#", "2"), 101 resource.TestCheckResourceAttr(resName, "placement_requirements.0", "/system/compute/allow_instances"), 102 resource.TestCheckResourceAttr(resName, "placement_requirements.1", "/system/compute/placement/default"), 103 resource.TestCheckResourceAttr(resName, "platform", "linux"), 104 resource.TestCheckResourceAttr(resName, "priority", "/oracle/public/default"), 105 resource.TestCheckResourceAttr(resName, "reverse_dns", "true"), 106 resource.TestCheckResourceAttr(resName, "state", "running"), 107 resource.TestCheckResourceAttr(resName, "virtio", "false"), 108 109 // Check Data Source to validate networking attributes 110 resource.TestCheckResourceAttr(dataName, "ip_network", fmt.Sprintf("testing-ip-network-%d", rInt)), 111 resource.TestCheckResourceAttr(dataName, "vnic", fmt.Sprintf("ip-network-test-%d", rInt)), 112 resource.TestCheckResourceAttr(dataName, "shared_network", "false"), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func TestAccOPCInstance_storage(t *testing.T) { 120 resName := "opc_compute_instance.test" 121 rInt := acctest.RandInt() 122 123 resource.Test(t, resource.TestCase{ 124 PreCheck: func() { testAccPreCheck(t) }, 125 Providers: testAccProviders, 126 CheckDestroy: testAccOPCCheckInstanceDestroy, 127 Steps: []resource.TestStep{ 128 { 129 Config: testAccInstanceStorage(rInt), 130 Check: resource.ComposeTestCheckFunc( 131 testAccOPCCheckInstanceExists, 132 resource.TestCheckResourceAttr(resName, "storage.#", "2"), 133 ), 134 }, 135 }, 136 }) 137 } 138 139 func TestAccOPCInstance_emptyLabel(t *testing.T) { 140 resName := "opc_compute_instance.test" 141 rInt := acctest.RandInt() 142 143 resource.Test(t, resource.TestCase{ 144 PreCheck: func() { testAccPreCheck(t) }, 145 Providers: testAccProviders, 146 CheckDestroy: testAccOPCCheckInstanceDestroy, 147 Steps: []resource.TestStep{ 148 { 149 Config: testAccInstanceEmptyLabel(rInt), 150 Check: resource.ComposeTestCheckFunc( 151 testAccOPCCheckInstanceExists, 152 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("acc-test-instance-%d", rInt)), 153 resource.TestCheckResourceAttrSet(resName, "label"), 154 ), 155 }, 156 }, 157 }) 158 } 159 160 func testAccOPCCheckInstanceExists(s *terraform.State) error { 161 client := testAccProvider.Meta().(*compute.Client).Instances() 162 163 for _, rs := range s.RootModule().Resources { 164 if rs.Type != "opc_compute_instance" { 165 continue 166 } 167 168 input := &compute.GetInstanceInput{ 169 ID: rs.Primary.ID, 170 Name: rs.Primary.Attributes["name"], 171 } 172 _, err := client.GetInstance(input) 173 if err != nil { 174 return fmt.Errorf("Error retrieving state of Instance %s: %s", input.Name, err) 175 } 176 } 177 178 return nil 179 } 180 181 func testAccOPCCheckInstanceDestroy(s *terraform.State) error { 182 client := testAccProvider.Meta().(*compute.Client).Instances() 183 184 for _, rs := range s.RootModule().Resources { 185 if rs.Type != "opc_compute_instance" { 186 continue 187 } 188 189 input := &compute.GetInstanceInput{ 190 ID: rs.Primary.ID, 191 Name: rs.Primary.Attributes["name"], 192 } 193 if info, err := client.GetInstance(input); err == nil { 194 return fmt.Errorf("Instance %s still exists: %#v", input.Name, info) 195 } 196 } 197 198 return nil 199 } 200 201 func testAccInstanceBasic(rInt int) string { 202 return fmt.Sprintf(` 203 resource "opc_compute_instance" "test" { 204 name = "acc-test-instance-%d" 205 label = "TestAccOPCInstance_basic" 206 shape = "oc3" 207 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 208 instance_attributes = <<JSON 209 { 210 "foo": "bar" 211 } 212 JSON 213 }`, rInt) 214 } 215 216 func testAccInstanceSharedNetworking(rInt int) string { 217 return fmt.Sprintf(` 218 resource "opc_compute_instance" "test" { 219 name = "acc-test-instance-%d" 220 label = "TestAccOPCInstance_sharedNetworking" 221 shape = "oc3" 222 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 223 tags = ["tag1", "tag2"] 224 networking_info { 225 index = 0 226 nat = ["ippool:/oracle/public/ippool"] 227 shared_network = true 228 } 229 } 230 231 data "opc_compute_network_interface" "test" { 232 instance_name = "${opc_compute_instance.test.name}" 233 instance_id = "${opc_compute_instance.test.id}" 234 interface = "eth0" 235 } 236 `, rInt) 237 } 238 239 func testAccInstanceIPNetworking(rInt int) string { 240 return fmt.Sprintf(` 241 resource "opc_compute_ip_network" "foo" { 242 name = "testing-ip-network-%d" 243 description = "testing-ip-network-instance" 244 ip_address_prefix = "10.1.12.0/24" 245 } 246 247 resource "opc_compute_instance" "test" { 248 name = "acc-test-instance-%d" 249 label = "TestAccOPCInstance_ipNetwork" 250 shape = "oc3" 251 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 252 networking_info { 253 index = 0 254 ip_network = "${opc_compute_ip_network.foo.id}" 255 vnic = "ip-network-test-%d" 256 shared_network = false 257 } 258 } 259 260 data "opc_compute_network_interface" "test" { 261 instance_id = "${opc_compute_instance.test.id}" 262 instance_name = "${opc_compute_instance.test.name}" 263 interface = "eth0" 264 } 265 `, rInt, rInt, rInt) 266 } 267 268 func testAccInstanceStorage(rInt int) string { 269 return fmt.Sprintf(` 270 resource "opc_compute_storage_volume" "foo" { 271 name = "acc-test-instance-%d" 272 size = 1 273 } 274 275 resource "opc_compute_storage_volume" "bar" { 276 name = "acc-test-instance-2-%d" 277 size = 1 278 } 279 280 resource "opc_compute_instance" "test" { 281 name = "acc-test-instance-%d" 282 label = "TestAccOPCInstance_basic" 283 shape = "oc3" 284 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 285 storage { 286 volume = "${opc_compute_storage_volume.foo.name}" 287 index = 1 288 } 289 storage { 290 volume = "${opc_compute_storage_volume.bar.name}" 291 index = 2 292 } 293 }`, rInt, rInt, rInt) 294 } 295 296 func testAccInstanceEmptyLabel(rInt int) string { 297 return fmt.Sprintf(` 298 resource "opc_compute_instance" "test" { 299 name = "acc-test-instance-%d" 300 shape = "oc3" 301 image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" 302 instance_attributes = <<JSON 303 { 304 "foo": "bar" 305 } 306 JSON 307 }`, rInt) 308 }