blob: 895dc83be312c65175f385da76d3268fae4cfa38 [file] [log] [blame]
Jon Perritt24270c42014-10-21 21:11:04 -05001package networks
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("/os-networksv2", 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 "networks": [
27 {
28 "label": "test-network-1",
29 "cidr": "192.168.100.0/24",
30 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
31 },
32 {
33 "label": "test-network-2",
34 "cidr": "192.30.250.00/18",
35 "id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324"
36 }
37 ]
38}
39 `)
40 })
41
42 client := fake.ServiceClient()
43 count := 0
44
45 List(client).EachPage(func(page pagination.Page) (bool, error) {
46 count++
47 actual, err := ExtractNetworks(page)
48 if err != nil {
49 t.Errorf("Failed to extract networks: %v", err)
50 return false, err
51 }
52
53 expected := []Network{
54 Network{
55 Label: "test-network-1",
56 CIDR: "192.168.100.0/24",
57 ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
58 },
59 Network{
60 Label: "test-network-2",
61 CIDR: "192.30.250.00/18",
62 ID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
63 },
64 }
65
66 th.CheckDeepEquals(t, expected, actual)
67
68 return true, nil
69 })
70
71 if count != 1 {
72 t.Errorf("Expected 1 page, got %d", count)
73 }
74}
75
76func TestGet(t *testing.T) {
77 th.SetupHTTP()
78 defer th.TeardownHTTP()
79
80 th.Mux.HandleFunc("/os-networksv2/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
81 th.TestMethod(t, r, "GET")
82 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
83
84 w.Header().Add("Content-Type", "application/json")
85 w.WriteHeader(http.StatusOK)
86
87 fmt.Fprintf(w, `
88{
89 "network": {
90 "label": "test-network-1",
91 "cidr": "192.168.100.0/24",
92 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
93 }
94}
95 `)
96 })
97
98 n, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
99 th.AssertNoErr(t, err)
100
101 th.AssertEquals(t, n.CIDR, "192.168.100.0/24")
102 th.AssertEquals(t, n.Label, "test-network-1")
103 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
104}
105
106func TestCreate(t *testing.T) {
107 th.SetupHTTP()
108 defer th.TeardownHTTP()
109
110 th.Mux.HandleFunc("/os-networksv2", func(w http.ResponseWriter, r *http.Request) {
111 th.TestMethod(t, r, "POST")
112 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
113 th.TestHeader(t, r, "Content-Type", "application/json")
114 th.TestHeader(t, r, "Accept", "application/json")
115 th.TestJSONRequest(t, r, `
116{
117 "network": {
118 "label": "test-network-1",
119 "cidr": "192.168.100.0/24"
120 }
121}
122 `)
123
124 w.Header().Add("Content-Type", "application/json")
125 w.WriteHeader(http.StatusCreated)
126
127 fmt.Fprintf(w, `
128{
129 "network": {
130 "label": "test-network-1",
131 "cidr": "192.168.100.0/24",
132 "id": "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
133 }
134}
135 `)
136 })
137
138 options := CreateOpts{Label: "test-network-1", CIDR: "192.168.100.0/24"}
139 n, err := Create(fake.ServiceClient(), options).Extract()
140 th.AssertNoErr(t, err)
141
142 th.AssertEquals(t, n.Label, "test-network-1")
143 th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
144}
145
146func TestDelete(t *testing.T) {
147 th.SetupHTTP()
148 defer th.TeardownHTTP()
149
150 th.Mux.HandleFunc("/os-networksv2/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
151 th.TestMethod(t, r, "DELETE")
152 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
153 w.WriteHeader(http.StatusNoContent)
154 })
155
Jon Perritt26ff5b72014-10-23 11:54:39 -0500156 res := Delete(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
Jon Perritt44b1ea22014-10-22 00:13:23 -0500157 th.AssertNoErr(t, res.Err)
158}