Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 1 | package lb |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame^] | 6 | "strconv" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 7 | "testing" |
| 8 | |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
| 13 | func mockListLBResponse(t *testing.T) { |
| 14 | th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) { |
| 15 | th.TestMethod(t, r, "GET") |
| 16 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 17 | |
| 18 | w.Header().Add("Content-Type", "application/json") |
| 19 | w.WriteHeader(http.StatusOK) |
| 20 | |
| 21 | fmt.Fprintf(w, ` |
| 22 | { |
| 23 | "loadBalancers": [ |
| 24 | { |
| 25 | "name": "lb-site1", |
| 26 | "id": 71, |
| 27 | "protocol": "HTTP", |
| 28 | "port": 80, |
| 29 | "algorithm": "RANDOM", |
| 30 | "status": "ACTIVE", |
| 31 | "nodeCount": 3, |
| 32 | "virtualIps": [ |
| 33 | { |
| 34 | "id": 403, |
| 35 | "address": "206.55.130.1", |
| 36 | "type": "PUBLIC", |
| 37 | "ipVersion": "IPV4" |
| 38 | } |
| 39 | ], |
| 40 | "created": { |
| 41 | "time": "2010-11-30T03:23:42Z" |
| 42 | }, |
| 43 | "updated": { |
| 44 | "time": "2010-11-30T03:23:44Z" |
| 45 | } |
| 46 | } |
| 47 | ] |
| 48 | } |
| 49 | `) |
| 50 | }) |
| 51 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 52 | |
| 53 | func mockCreateLBResponse(t *testing.T) { |
| 54 | th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) { |
| 55 | th.TestMethod(t, r, "POST") |
| 56 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 57 | |
| 58 | th.TestJSONRequest(t, r, ` |
| 59 | { |
| 60 | "loadBalancer": { |
| 61 | "name": "a-new-loadbalancer", |
| 62 | "port": 80, |
| 63 | "protocol": "HTTP", |
| 64 | "virtualIps": [ |
| 65 | { |
| 66 | "id": 2341 |
| 67 | }, |
| 68 | { |
| 69 | "id": 900001 |
| 70 | } |
| 71 | ], |
| 72 | "nodes": [ |
| 73 | { |
| 74 | "address": "10.1.1.1", |
| 75 | "port": 80, |
| 76 | "condition": "ENABLED" |
| 77 | } |
| 78 | ] |
| 79 | } |
| 80 | } |
| 81 | `) |
| 82 | |
| 83 | w.Header().Add("Content-Type", "application/json") |
| 84 | w.WriteHeader(http.StatusOK) |
| 85 | |
| 86 | fmt.Fprintf(w, ` |
| 87 | { |
| 88 | "loadBalancer": { |
| 89 | "name": "a-new-loadbalancer", |
| 90 | "id": 144, |
| 91 | "protocol": "HTTP", |
| 92 | "halfClosed": false, |
| 93 | "port": 83, |
| 94 | "algorithm": "RANDOM", |
| 95 | "status": "BUILD", |
| 96 | "timeout": 30, |
| 97 | "cluster": { |
| 98 | "name": "ztm-n01.staging1.lbaas.rackspace.net" |
| 99 | }, |
| 100 | "nodes": [ |
| 101 | { |
| 102 | "address": "10.1.1.1", |
| 103 | "id": 653, |
| 104 | "port": 80, |
| 105 | "status": "ONLINE", |
| 106 | "condition": "ENABLED", |
| 107 | "weight": 1 |
| 108 | } |
| 109 | ], |
| 110 | "virtualIps": [ |
| 111 | { |
| 112 | "address": "206.10.10.210", |
| 113 | "id": 39, |
| 114 | "type": "PUBLIC", |
| 115 | "ipVersion": "IPV4" |
| 116 | }, |
| 117 | { |
| 118 | "address": "2001:4801:79f1:0002:711b:be4c:0000:0021", |
| 119 | "id": 900001, |
| 120 | "type": "PUBLIC", |
| 121 | "ipVersion": "IPV6" |
| 122 | } |
| 123 | ], |
| 124 | "created": { |
| 125 | "time": "2011-04-13T14:18:07Z" |
| 126 | }, |
| 127 | "updated": { |
| 128 | "time": "2011-04-13T14:18:07Z" |
| 129 | }, |
| 130 | "connectionLogging": { |
| 131 | "enabled": false |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | `) |
| 136 | }) |
| 137 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 138 | |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame^] | 139 | func mockBatchDeleteLBResponse(t *testing.T, ids []int) { |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 140 | th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) { |
| 141 | th.TestMethod(t, r, "DELETE") |
| 142 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame^] | 143 | |
| 144 | r.ParseForm() |
| 145 | |
| 146 | for k, v := range ids { |
| 147 | fids := r.Form["id"] |
| 148 | th.AssertEquals(t, strconv.Itoa(v), fids[k]) |
| 149 | } |
| 150 | |
| 151 | w.WriteHeader(http.StatusAccepted) |
| 152 | }) |
| 153 | } |
| 154 | |
| 155 | func mockDeleteLBResponse(t *testing.T, id int) { |
| 156 | th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) { |
| 157 | th.TestMethod(t, r, "DELETE") |
| 158 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 159 | w.WriteHeader(http.StatusAccepted) |
| 160 | }) |
| 161 | } |