Skip to content

Commit f558a6f

Browse files
committed
add test
1 parent 94140c7 commit f558a6f

File tree

2 files changed

+214
-5
lines changed

2 files changed

+214
-5
lines changed

cloudstack/resource_cloudstack_nic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error {
139139
d.Set("ip_address", n.Ipaddress)
140140
d.Set("network_id", n.Networkid)
141141
d.Set("virtual_machine_id", vm.Id)
142+
d.Set("mac_address", n.Macaddress)
142143
found = true
143144
break
144145
}

cloudstack/resource_cloudstack_nic_test.go

Lines changed: 213 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestAccCloudStackNIC_basic(t *testing.T) {
4848
})
4949
}
5050

51-
func TestAccCloudStackNIC_update(t *testing.T) {
51+
func TestAccCloudStackNIC_ipaddress(t *testing.T) {
5252
var nic cloudstack.Nic
5353

5454
resource.Test(t, resource.TestCase{
@@ -57,22 +57,87 @@ func TestAccCloudStackNIC_update(t *testing.T) {
5757
CheckDestroy: testAccCheckCloudStackNICDestroy,
5858
Steps: []resource.TestStep{
5959
{
60-
Config: testAccCloudStackNIC_basic,
60+
Config: testAccCloudStackNIC_ipaddress,
61+
Check: resource.ComposeTestCheckFunc(
62+
testAccCheckCloudStackNICExists(
63+
"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
64+
testAccCheckCloudStackNICIPAddress(&nic),
65+
resource.TestCheckResourceAttr(
66+
"cloudstack_nic.foo", "ip_address", "10.1.2.123"),
67+
),
68+
},
69+
},
70+
})
71+
}
72+
73+
func TestAccCloudStackNIC_macaddress(t *testing.T) {
74+
var nic cloudstack.Nic
75+
76+
resource.Test(t, resource.TestCase{
77+
PreCheck: func() { testAccPreCheck(t) },
78+
Providers: testAccProviders,
79+
CheckDestroy: testAccCheckCloudStackNICDestroy,
80+
Steps: []resource.TestStep{
81+
{
82+
Config: testAccCloudStackNIC_macaddress,
83+
Check: resource.ComposeTestCheckFunc(
84+
testAccCheckCloudStackNICExists(
85+
"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
86+
testAccCheckCloudStackNICMacAddress(&nic),
87+
resource.TestCheckResourceAttr(
88+
"cloudstack_nic.foo", "mac_address", "02:1A:4B:3C:5D:6E"),
89+
),
90+
},
91+
},
92+
})
93+
}
94+
95+
func TestAccCloudStackNIC_dhcpoptions(t *testing.T) {
96+
var nic cloudstack.Nic
97+
98+
resource.Test(t, resource.TestCase{
99+
PreCheck: func() { testAccPreCheck(t) },
100+
Providers: testAccProviders,
101+
CheckDestroy: testAccCheckCloudStackNICDestroy,
102+
Steps: []resource.TestStep{
103+
{
104+
Config: testAccCloudStackNIC_dhcpoptions,
61105
Check: resource.ComposeTestCheckFunc(
62106
testAccCheckCloudStackNICExists(
63107
"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
64108
testAccCheckCloudStackNICAttributes(&nic),
109+
resource.TestCheckResourceAttr(
110+
"cloudstack_nic.foo", "dhcp_options.dhcp:15", "example.com"),
111+
resource.TestCheckResourceAttr(
112+
"cloudstack_nic.foo", "dhcp_options.dhcp:6", "8.8.8.8,8.8.4.4"),
65113
),
66114
},
115+
},
116+
})
117+
}
67118

119+
func TestAccCloudStackNIC_complete(t *testing.T) {
120+
var nic cloudstack.Nic
121+
122+
resource.Test(t, resource.TestCase{
123+
PreCheck: func() { testAccPreCheck(t) },
124+
Providers: testAccProviders,
125+
CheckDestroy: testAccCheckCloudStackNICDestroy,
126+
Steps: []resource.TestStep{
68127
{
69-
Config: testAccCloudStackNIC_ipaddress,
128+
Config: testAccCloudStackNIC_complete,
70129
Check: resource.ComposeTestCheckFunc(
71130
testAccCheckCloudStackNICExists(
72131
"cloudstack_instance.foobar", "cloudstack_nic.foo", &nic),
73-
testAccCheckCloudStackNICIPAddress(&nic),
132+
testAccCheckCloudStackNICCompleteAttributes(&nic),
74133
resource.TestCheckResourceAttr(
75-
"cloudstack_nic.foo", "ip_address", "10.1.2.123"),
134+
"cloudstack_nic.foo", "ip_address", "10.1.2.150"),
135+
resource.TestCheckResourceAttr(
136+
"cloudstack_nic.foo", "mac_address", "02:1A:4B:3C:5D:6E"),
137+
resource.TestCheckResourceAttr(
138+
"cloudstack_nic.foo", "dhcp_options.dhcp:15", "test.com"),
139+
resource.TestCheckResourceAttr(
140+
"cloudstack_nic.foo", "dhcp_options.dhcp:6", "1.1.1.1,1.0.0.1"),
76141
),
77142
},
78143
},
@@ -146,6 +211,42 @@ func testAccCheckCloudStackNICIPAddress(
146211
}
147212
}
148213

214+
func testAccCheckCloudStackNICMacAddress(
215+
nic *cloudstack.Nic) resource.TestCheckFunc {
216+
return func(s *terraform.State) error {
217+
218+
if nic.Networkname != "terraform-network" {
219+
return fmt.Errorf("Bad network name: %s", nic.Networkname)
220+
}
221+
222+
if nic.Macaddress != "02:1A:4B:3C:5D:6E" {
223+
return fmt.Errorf("Bad MAC address: %s", nic.Macaddress)
224+
}
225+
226+
return nil
227+
}
228+
}
229+
230+
func testAccCheckCloudStackNICCompleteAttributes(
231+
nic *cloudstack.Nic) resource.TestCheckFunc {
232+
return func(s *terraform.State) error {
233+
234+
if nic.Networkname != "terraform-network" {
235+
return fmt.Errorf("Bad network name: %s", nic.Networkname)
236+
}
237+
238+
if nic.Ipaddress != "10.1.2.150" {
239+
return fmt.Errorf("Bad IP address: %s", nic.Ipaddress)
240+
}
241+
242+
if nic.Macaddress != "02:1A:4B:3C:5D:6E" {
243+
return fmt.Errorf("Bad MAC address: %s", nic.Macaddress)
244+
}
245+
246+
return nil
247+
}
248+
}
249+
149250
func testAccCheckCloudStackNICDestroy(s *terraform.State) error {
150251
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
151252

@@ -232,3 +333,110 @@ resource "cloudstack_nic" "foo" {
232333
virtual_machine_id = cloudstack_instance.foobar.id
233334
ip_address = "10.1.2.123"
234335
}`
336+
337+
const testAccCloudStackNIC_macaddress = `
338+
resource "cloudstack_network" "foo" {
339+
name = "terraform-network"
340+
display_text = "terraform-network"
341+
cidr = "10.1.1.0/24"
342+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
343+
zone = "Sandbox-simulator"
344+
}
345+
346+
resource "cloudstack_network" "bar" {
347+
name = "terraform-network"
348+
display_text = "terraform-network"
349+
cidr = "10.1.2.0/24"
350+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
351+
zone = "Sandbox-simulator"
352+
}
353+
354+
resource "cloudstack_instance" "foobar" {
355+
name = "terraform-test"
356+
display_name = "terraform"
357+
service_offering= "Medium Instance"
358+
network_id = cloudstack_network.foo.id
359+
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
360+
zone = "Sandbox-simulator"
361+
expunge = true
362+
}
363+
364+
resource "cloudstack_nic" "foo" {
365+
network_id = cloudstack_network.bar.id
366+
virtual_machine_id = cloudstack_instance.foobar.id
367+
mac_address = "02:1A:4B:3C:5D:6E"
368+
}`
369+
370+
const testAccCloudStackNIC_dhcpoptions = `
371+
resource "cloudstack_network" "foo" {
372+
name = "terraform-network"
373+
display_text = "terraform-network"
374+
cidr = "10.1.1.0/24"
375+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
376+
zone = "Sandbox-simulator"
377+
}
378+
379+
resource "cloudstack_network" "bar" {
380+
name = "terraform-network"
381+
display_text = "terraform-network"
382+
cidr = "10.1.2.0/24"
383+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
384+
zone = "Sandbox-simulator"
385+
}
386+
387+
resource "cloudstack_instance" "foobar" {
388+
name = "terraform-test"
389+
display_name = "terraform"
390+
service_offering= "Medium Instance"
391+
network_id = cloudstack_network.foo.id
392+
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
393+
zone = "Sandbox-simulator"
394+
expunge = true
395+
}
396+
397+
resource "cloudstack_nic" "foo" {
398+
network_id = cloudstack_network.bar.id
399+
virtual_machine_id = cloudstack_instance.foobar.id
400+
dhcp_options = {
401+
"dhcp:15" = "example.com"
402+
"dhcp:6" = "8.8.8.8,8.8.4.4"
403+
}
404+
}`
405+
406+
const testAccCloudStackNIC_complete = `
407+
resource "cloudstack_network" "foo" {
408+
name = "terraform-network"
409+
display_text = "terraform-network"
410+
cidr = "10.1.1.0/24"
411+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
412+
zone = "Sandbox-simulator"
413+
}
414+
415+
resource "cloudstack_network" "bar" {
416+
name = "terraform-network"
417+
display_text = "terraform-network"
418+
cidr = "10.1.2.0/24"
419+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
420+
zone = "Sandbox-simulator"
421+
}
422+
423+
resource "cloudstack_instance" "foobar" {
424+
name = "terraform-test"
425+
display_name = "terraform"
426+
service_offering= "Medium Instance"
427+
network_id = cloudstack_network.foo.id
428+
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
429+
zone = "Sandbox-simulator"
430+
expunge = true
431+
}
432+
433+
resource "cloudstack_nic" "foo" {
434+
network_id = cloudstack_network.bar.id
435+
virtual_machine_id = cloudstack_instance.foobar.id
436+
ip_address = "10.1.2.150"
437+
mac_address = "02:1A:4B:3C:5D:6E"
438+
dhcp_options = {
439+
"dhcp:15" = "test.com"
440+
"dhcp:6" = "1.1.1.1,1.0.0.1"
441+
}
442+
}`

0 commit comments

Comments
 (0)