blob: d40af9c462562d3f4e5f1611242623378b8f792d [file] [log] [blame]
Jon Perrittf95e3e42014-10-21 21:11:25 -05001package virtualinterfaces
Jon Perritt44b1ea22014-10-22 00:13:23 -05002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 th.Mux.HandleFunc("/servers/12345/os-virtual-interfacesv2", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
20
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusOK)
23
24 fmt.Fprintf(w, `
25{
26 "virtual_interfaces": [
27 {
28 "id": "de7c6d53-b895-4b4a-963c-517ccb0f0775",
29 "ip_addresses": [
30 {
31 "address": "192.168.0.2",
32 "network_id": "f212726e-6321-4210-9bae-a13f5a33f83f",
33 "network_label": "superprivate_xml"
34 }
35 ],
36 "mac_address": "BC:76:4E:04:85:20"
37 },
38 {
39 "id": "e14e789d-3b98-44a6-9c2d-c23eb1d1465c",
40 "ip_addresses": [
41 {
42 "address": "10.181.1.30",
43 "network_id": "3b324a1b-31b8-4db5-9fe5-4a2067f60297",
44 "network_label": "private"
45 }
46 ],
47 "mac_address": "BC:76:4E:04:81:55"
48 }
49 ]
50}
51 `)
52 })
53
54 client := fake.ServiceClient()
55 count := 0
56
Jon Perritt004d9c32014-10-23 13:08:43 -050057 err := List(client, "12345").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050058 count++
59 actual, err := ExtractVirtualInterfaces(page)
60 if err != nil {
61 t.Errorf("Failed to extract networks: %v", err)
62 return false, err
63 }
64
65 expected := []VirtualInterface{
66 VirtualInterface{
67 MACAddress: "BC:76:4E:04:85:20",
68 IPAddresses: []IPAddress{
69 IPAddress{
70 Address: "192.168.0.2",
71 NetworkID: "f212726e-6321-4210-9bae-a13f5a33f83f",
72 NetworkLabel: "superprivate_xml",
73 },
74 },
75 ID: "de7c6d53-b895-4b4a-963c-517ccb0f0775",
76 },
77 VirtualInterface{
78 MACAddress: "BC:76:4E:04:81:55",
79 IPAddresses: []IPAddress{
80 IPAddress{
81 Address: "10.181.1.30",
82 NetworkID: "3b324a1b-31b8-4db5-9fe5-4a2067f60297",
83 NetworkLabel: "private",
84 },
85 },
86 ID: "e14e789d-3b98-44a6-9c2d-c23eb1d1465c",
87 },
88 }
89
90 th.CheckDeepEquals(t, expected, actual)
91
92 return true, nil
93 })
Jon Perritt004d9c32014-10-23 13:08:43 -050094 th.AssertNoErr(t, err)
95 th.CheckEquals(t, 1, count)
Jon Perritt44b1ea22014-10-22 00:13:23 -050096}
97
98func TestCreate(t *testing.T) {
99 th.SetupHTTP()
100 defer th.TeardownHTTP()
101
102 th.Mux.HandleFunc("/servers/12345/os-virtual-interfacesv2", func(w http.ResponseWriter, r *http.Request) {
103 th.TestMethod(t, r, "POST")
104 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
105 th.TestHeader(t, r, "Content-Type", "application/json")
106 th.TestHeader(t, r, "Accept", "application/json")
107 th.TestJSONRequest(t, r, `
108{
109 "virtual_interface": {
110 "network_id": "6789"
111 }
112}
113 `)
114
115 w.Header().Add("Content-Type", "application/json")
116 w.WriteHeader(http.StatusCreated)
117
118 fmt.Fprintf(w, `{
119 "virtual_interfaces": [
120 {
121 "id": "de7c6d53-b895-4b4a-963c-517ccb0f0775",
122 "ip_addresses": [
123 {
124 "address": "192.168.0.2",
125 "network_id": "f212726e-6321-4210-9bae-a13f5a33f83f",
126 "network_label": "superprivate_xml"
127 }
128 ],
129 "mac_address": "BC:76:4E:04:85:20"
130 }
131 ]
132 }`)
133 })
134
135 expected := &VirtualInterface{
136 MACAddress: "BC:76:4E:04:85:20",
137 IPAddresses: []IPAddress{
138 IPAddress{
139 Address: "192.168.0.2",
140 NetworkID: "f212726e-6321-4210-9bae-a13f5a33f83f",
141 NetworkLabel: "superprivate_xml",
142 },
143 },
144 ID: "de7c6d53-b895-4b4a-963c-517ccb0f0775",
145 }
146
147 actual, err := Create(fake.ServiceClient(), "12345", "6789").Extract()
148 th.AssertNoErr(t, err)
149
150 th.CheckDeepEquals(t, expected, actual)
151}
152
153func TestDelete(t *testing.T) {
154 th.SetupHTTP()
155 defer th.TeardownHTTP()
156
157 th.Mux.HandleFunc("/servers/12345/os-virtual-interfacesv2/6789", func(w http.ResponseWriter, r *http.Request) {
158 th.TestMethod(t, r, "DELETE")
159 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
160 w.WriteHeader(http.StatusNoContent)
161 })
162
163 res := Delete(fake.ServiceClient(), "12345", "6789")
164 th.AssertNoErr(t, res.Err)
165}