blob: c3487d2e856514e912e197974050ad35f55babf4 [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
57 List(client, "12345").EachPage(func(page pagination.Page) (bool, error) {
58 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 })
94
95 if count != 1 {
96 t.Errorf("Expected 1 page, got %d", count)
97 }
98}
99
100func TestCreate(t *testing.T) {
101 th.SetupHTTP()
102 defer th.TeardownHTTP()
103
104 th.Mux.HandleFunc("/servers/12345/os-virtual-interfacesv2", func(w http.ResponseWriter, r *http.Request) {
105 th.TestMethod(t, r, "POST")
106 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
107 th.TestHeader(t, r, "Content-Type", "application/json")
108 th.TestHeader(t, r, "Accept", "application/json")
109 th.TestJSONRequest(t, r, `
110{
111 "virtual_interface": {
112 "network_id": "6789"
113 }
114}
115 `)
116
117 w.Header().Add("Content-Type", "application/json")
118 w.WriteHeader(http.StatusCreated)
119
120 fmt.Fprintf(w, `{
121 "virtual_interfaces": [
122 {
123 "id": "de7c6d53-b895-4b4a-963c-517ccb0f0775",
124 "ip_addresses": [
125 {
126 "address": "192.168.0.2",
127 "network_id": "f212726e-6321-4210-9bae-a13f5a33f83f",
128 "network_label": "superprivate_xml"
129 }
130 ],
131 "mac_address": "BC:76:4E:04:85:20"
132 }
133 ]
134 }`)
135 })
136
137 expected := &VirtualInterface{
138 MACAddress: "BC:76:4E:04:85:20",
139 IPAddresses: []IPAddress{
140 IPAddress{
141 Address: "192.168.0.2",
142 NetworkID: "f212726e-6321-4210-9bae-a13f5a33f83f",
143 NetworkLabel: "superprivate_xml",
144 },
145 },
146 ID: "de7c6d53-b895-4b4a-963c-517ccb0f0775",
147 }
148
149 actual, err := Create(fake.ServiceClient(), "12345", "6789").Extract()
150 th.AssertNoErr(t, err)
151
152 th.CheckDeepEquals(t, expected, actual)
153}
154
155func TestDelete(t *testing.T) {
156 th.SetupHTTP()
157 defer th.TeardownHTTP()
158
159 th.Mux.HandleFunc("/servers/12345/os-virtual-interfacesv2/6789", func(w http.ResponseWriter, r *http.Request) {
160 th.TestMethod(t, r, "DELETE")
161 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
162 w.WriteHeader(http.StatusNoContent)
163 })
164
165 res := Delete(fake.ServiceClient(), "12345", "6789")
166 th.AssertNoErr(t, res.Err)
167}