github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/monitors/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 // HealthmonitorsListBody contains the canned body of a healthmonitor list response. 14 const HealthmonitorsListBody = ` 15 { 16 "healthmonitors":[ 17 { 18 "admin_state_up":true, 19 "project_id":"83657cfcdfe44cd5920adaf26c48ceea", 20 "delay":10, 21 "name":"web", 22 "max_retries":1, 23 "max_retries_down":7, 24 "timeout":1, 25 "type":"PING", 26 "pools": [{"id": "84f1b61f-58c4-45bf-a8a9-2dafb9e5214d"}], 27 "id":"466c8345-28d8-4f84-a246-e04380b0461d" 28 }, 29 { 30 "admin_state_up":true, 31 "project_id":"83657cfcdfe44cd5920adaf26c48ceea", 32 "delay":5, 33 "name":"db", 34 "expected_codes":"200", 35 "max_retries":2, 36 "max_retries_down":4, 37 "http_method":"GET", 38 "timeout":2, 39 "url_path":"/", 40 "type":"HTTP", 41 "pools": [{"id": "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}], 42 "id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7" 43 } 44 ] 45 } 46 ` 47 48 // SingleHealthmonitorBody is the canned body of a Get request on an existing healthmonitor. 49 const SingleHealthmonitorBody = ` 50 { 51 "healthmonitor": { 52 "admin_state_up":true, 53 "project_id":"83657cfcdfe44cd5920adaf26c48ceea", 54 "delay":5, 55 "name":"db", 56 "expected_codes":"200", 57 "max_retries":2, 58 "max_retries_down":4, 59 "http_method":"GET", 60 "timeout":2, 61 "url_path":"/", 62 "type":"HTTP", 63 "pools": [{"id": "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}], 64 "id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7" 65 } 66 } 67 ` 68 69 // PostUpdateHealthmonitorBody is the canned response body of a Update request on an existing healthmonitor. 70 const PostUpdateHealthmonitorBody = ` 71 { 72 "healthmonitor": { 73 "admin_state_up":true, 74 "project_id":"83657cfcdfe44cd5920adaf26c48ceea", 75 "delay":3, 76 "name":"NewHealthmonitorName", 77 "expected_codes":"301", 78 "max_retries":10, 79 "max_retries_down":8, 80 "http_method":"GET", 81 "timeout":20, 82 "url_path":"/another_check", 83 "type":"HTTP", 84 "pools": [{"id": "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}], 85 "id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7" 86 } 87 } 88 ` 89 90 var ( 91 HealthmonitorWeb = monitors.Monitor{ 92 AdminStateUp: true, 93 Name: "web", 94 ProjectID: "83657cfcdfe44cd5920adaf26c48ceea", 95 Delay: 10, 96 MaxRetries: 1, 97 MaxRetriesDown: 7, 98 Timeout: 1, 99 Type: "PING", 100 ID: "466c8345-28d8-4f84-a246-e04380b0461d", 101 Pools: []monitors.PoolID{{ID: "84f1b61f-58c4-45bf-a8a9-2dafb9e5214d"}}, 102 } 103 HealthmonitorDb = monitors.Monitor{ 104 AdminStateUp: true, 105 Name: "db", 106 ProjectID: "83657cfcdfe44cd5920adaf26c48ceea", 107 Delay: 5, 108 ExpectedCodes: "200", 109 MaxRetries: 2, 110 MaxRetriesDown: 4, 111 Timeout: 2, 112 URLPath: "/", 113 Type: "HTTP", 114 HTTPMethod: "GET", 115 ID: "5d4b5228-33b0-4e60-b225-9b727c1a20e7", 116 Pools: []monitors.PoolID{{ID: "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}}, 117 } 118 HealthmonitorUpdated = monitors.Monitor{ 119 AdminStateUp: true, 120 Name: "NewHealthmonitorName", 121 ProjectID: "83657cfcdfe44cd5920adaf26c48ceea", 122 Delay: 3, 123 ExpectedCodes: "301", 124 MaxRetries: 10, 125 MaxRetriesDown: 8, 126 Timeout: 20, 127 URLPath: "/another_check", 128 Type: "HTTP", 129 HTTPMethod: "GET", 130 ID: "5d4b5228-33b0-4e60-b225-9b727c1a20e7", 131 Pools: []monitors.PoolID{{ID: "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}}, 132 } 133 ) 134 135 // HandleHealthmonitorListSuccessfully sets up the test server to respond to a healthmonitor List request. 136 func HandleHealthmonitorListSuccessfully(t *testing.T) { 137 th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors", func(w http.ResponseWriter, r *http.Request) { 138 th.TestMethod(t, r, "GET") 139 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 140 141 w.Header().Add("Content-Type", "application/json") 142 r.ParseForm() 143 marker := r.Form.Get("marker") 144 switch marker { 145 case "": 146 fmt.Fprintf(w, HealthmonitorsListBody) 147 case "556c8345-28d8-4f84-a246-e04380b0461d": 148 fmt.Fprintf(w, `{ "healthmonitors": [] }`) 149 default: 150 t.Fatalf("/v2.0/lbaas/healthmonitors invoked with unexpected marker=[%s]", marker) 151 } 152 }) 153 } 154 155 // HandleHealthmonitorCreationSuccessfully sets up the test server to respond to a healthmonitor creation request 156 // with a given response. 157 func HandleHealthmonitorCreationSuccessfully(t *testing.T, response string) { 158 th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors", func(w http.ResponseWriter, r *http.Request) { 159 th.TestMethod(t, r, "POST") 160 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 161 th.TestJSONRequest(t, r, `{ 162 "healthmonitor": { 163 "type":"HTTP", 164 "pool_id":"84f1b61f-58c4-45bf-a8a9-2dafb9e5214d", 165 "project_id":"453105b9-1754-413f-aab1-55f1af620750", 166 "delay":20, 167 "name":"db", 168 "timeout":10, 169 "max_retries":5, 170 "max_retries_down":4, 171 "url_path":"/check", 172 "expected_codes":"200-299" 173 } 174 }`) 175 176 w.WriteHeader(http.StatusAccepted) 177 w.Header().Add("Content-Type", "application/json") 178 fmt.Fprintf(w, response) 179 }) 180 } 181 182 // HandleHealthmonitorGetSuccessfully sets up the test server to respond to a healthmonitor Get request. 183 func HandleHealthmonitorGetSuccessfully(t *testing.T) { 184 th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors/5d4b5228-33b0-4e60-b225-9b727c1a20e7", func(w http.ResponseWriter, r *http.Request) { 185 th.TestMethod(t, r, "GET") 186 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 187 th.TestHeader(t, r, "Accept", "application/json") 188 189 fmt.Fprintf(w, SingleHealthmonitorBody) 190 }) 191 } 192 193 // HandleHealthmonitorDeletionSuccessfully sets up the test server to respond to a healthmonitor deletion request. 194 func HandleHealthmonitorDeletionSuccessfully(t *testing.T) { 195 th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors/5d4b5228-33b0-4e60-b225-9b727c1a20e7", func(w http.ResponseWriter, r *http.Request) { 196 th.TestMethod(t, r, "DELETE") 197 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 198 199 w.WriteHeader(http.StatusNoContent) 200 }) 201 } 202 203 // HandleHealthmonitorUpdateSuccessfully sets up the test server to respond to a healthmonitor Update request. 204 func HandleHealthmonitorUpdateSuccessfully(t *testing.T) { 205 th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors/5d4b5228-33b0-4e60-b225-9b727c1a20e7", func(w http.ResponseWriter, r *http.Request) { 206 th.TestMethod(t, r, "PUT") 207 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 208 th.TestHeader(t, r, "Accept", "application/json") 209 th.TestHeader(t, r, "Content-Type", "application/json") 210 th.TestJSONRequest(t, r, `{ 211 "healthmonitor": { 212 "name": "NewHealthmonitorName", 213 "delay": 3, 214 "timeout": 20, 215 "max_retries": 10, 216 "max_retries_down": 8, 217 "url_path": "/another_check", 218 "expected_codes": "301" 219 } 220 }`) 221 222 fmt.Fprintf(w, PostUpdateHealthmonitorBody) 223 }) 224 }