github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/baremetal/v1/ports/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/baremetal/v1/ports" 10 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 11 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 12 ) 13 14 // PortListBody contains the canned body of a ports.List response, without detail. 15 const PortListBody = ` 16 { 17 "ports": [ 18 { 19 "uuid": "3abe3f36-9708-4e9f-b07e-0f898061d3a7", 20 "links": [ 21 { 22 "href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", 23 "rel": "self" 24 }, 25 { 26 "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", 27 "rel": "bookmark" 28 } 29 ], 30 "address": "52:54:00:0a:af:d1" 31 }, 32 { 33 "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a", 34 "links": [ 35 { 36 "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", 37 "rel": "self" 38 }, 39 { 40 "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", 41 "rel": "bookmark" 42 } 43 ], 44 "address": "52:54:00:4d:87:e6" 45 } 46 ] 47 } 48 ` 49 50 // PortListDetailBody contains the canned body of a port.ListDetail response. 51 const PortListDetailBody = ` 52 { 53 "ports": [ 54 { 55 "local_link_connection": {}, 56 "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 57 "uuid": "3abe3f36-9708-4e9f-b07e-0f898061d3a7", 58 "links": [ 59 { 60 "href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", 61 "rel": "self" 62 }, 63 { 64 "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", 65 "rel": "bookmark" 66 } 67 ], 68 "extra": {}, 69 "pxe_enabled": true, 70 "portgroup_uuid": null, 71 "updated_at": "2019-02-15T09:55:19+00:00", 72 "physical_network": null, 73 "address": "52:54:00:0a:af:d1", 74 "internal_info": { 75 76 }, 77 "created_at": "2019-02-15T09:52:23+00:00" 78 }, 79 { 80 "local_link_connection": {}, 81 "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 82 "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a", 83 "links": [ 84 { 85 "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", 86 "rel": "self" 87 }, 88 { 89 "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", 90 "rel": "bookmark" 91 } 92 ], 93 "extra": {}, 94 "pxe_enabled": true, 95 "portgroup_uuid": null, 96 "updated_at": "2019-02-15T09:55:19+00:00", 97 "physical_network": null, 98 "address": "52:54:00:4d:87:e6", 99 "internal_info": {}, 100 "created_at": "2019-02-15T09:52:24+00:00" 101 } 102 ] 103 } 104 ` 105 106 // SinglePortBody is the canned body of a Get request on an existing port. 107 const SinglePortBody = ` 108 { 109 "local_link_connection": { 110 111 }, 112 "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 113 "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a", 114 "links": [ 115 { 116 "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", 117 "rel": "self" 118 }, 119 { 120 "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", 121 "rel": "bookmark" 122 } 123 ], 124 "extra": { 125 126 }, 127 "pxe_enabled": true, 128 "portgroup_uuid": null, 129 "updated_at": "2019-02-15T09:55:19+00:00", 130 "physical_network": null, 131 "address": "52:54:00:4d:87:e6", 132 "internal_info": { 133 134 }, 135 "created_at": "2019-02-15T09:52:24+00:00" 136 } 137 ` 138 139 var ( 140 fooCreated, _ = time.Parse(time.RFC3339, "2019-02-15T09:52:24+00:00") 141 fooUpdated, _ = time.Parse(time.RFC3339, "2019-02-15T09:55:19+00:00") 142 BarCreated, _ = time.Parse(time.RFC3339, "2019-02-15T09:52:23+00:00") 143 BarUpdated, _ = time.Parse(time.RFC3339, "2019-02-15T09:55:19+00:00") 144 PortFoo = ports.Port{ 145 UUID: "f2845e11-dbd4-4728-a8c0-30d19f48924a", 146 NodeUUID: "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 147 Address: "52:54:00:4d:87:e6", 148 PXEEnabled: true, 149 LocalLinkConnection: map[string]any{}, 150 InternalInfo: map[string]any{}, 151 Extra: map[string]any{}, 152 CreatedAt: fooCreated, 153 UpdatedAt: fooUpdated, 154 Links: []any{map[string]any{"href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", "rel": "self"}, map[string]any{"href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", "rel": "bookmark"}}, 155 } 156 157 PortBar = ports.Port{ 158 UUID: "3abe3f36-9708-4e9f-b07e-0f898061d3a7", 159 NodeUUID: "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 160 Address: "52:54:00:0a:af:d1", 161 PXEEnabled: true, 162 LocalLinkConnection: map[string]any{}, 163 InternalInfo: map[string]any{}, 164 Extra: map[string]any{}, 165 CreatedAt: BarCreated, 166 UpdatedAt: BarUpdated, 167 Links: []any{map[string]any{"href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", "rel": "self"}, map[string]any{"rel": "bookmark", "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7"}}, 168 } 169 ) 170 171 // HandlePortListSuccessfully sets up the test server to respond to a port List request. 172 func HandlePortListSuccessfully(t *testing.T) { 173 th.Mux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) { 174 th.TestMethod(t, r, "GET") 175 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 176 w.Header().Add("Content-Type", "application/json") 177 if err := r.ParseForm(); err != nil { 178 t.Errorf("Failed to parse request form %v", err) 179 } 180 181 marker := r.Form.Get("marker") 182 switch marker { 183 case "": 184 fmt.Fprint(w, PortListBody) 185 186 case "f2845e11-dbd4-4728-a8c0-30d19f48924a": 187 fmt.Fprint(w, `{ "ports": [] }`) 188 default: 189 t.Fatalf("/ports invoked with unexpected marker=[%s]", marker) 190 } 191 }) 192 } 193 194 // HandlePortListSuccessfully sets up the test server to respond to a port List request. 195 func HandlePortListDetailSuccessfully(t *testing.T) { 196 th.Mux.HandleFunc("/ports/detail", func(w http.ResponseWriter, r *http.Request) { 197 th.TestMethod(t, r, "GET") 198 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 199 w.Header().Add("Content-Type", "application/json") 200 if err := r.ParseForm(); err != nil { 201 t.Errorf("Failed to parse request form %v", err) 202 } 203 204 fmt.Fprint(w, PortListDetailBody) 205 }) 206 } 207 208 // HandleSPortCreationSuccessfully sets up the test server to respond to a port creation request 209 // with a given response. 210 func HandlePortCreationSuccessfully(t *testing.T, response string) { 211 th.Mux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) { 212 th.TestMethod(t, r, "POST") 213 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 214 th.TestJSONRequest(t, r, `{ 215 "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 216 "address": "52:54:00:4d:87:e6", 217 "pxe_enabled": true 218 }`) 219 220 w.WriteHeader(http.StatusAccepted) 221 w.Header().Add("Content-Type", "application/json") 222 fmt.Fprint(w, response) 223 }) 224 } 225 226 // HandlePortDeletionSuccessfully sets up the test server to respond to a port deletion request. 227 func HandlePortDeletionSuccessfully(t *testing.T) { 228 th.Mux.HandleFunc("/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", func(w http.ResponseWriter, r *http.Request) { 229 th.TestMethod(t, r, "DELETE") 230 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 231 232 w.WriteHeader(http.StatusNoContent) 233 }) 234 } 235 236 func HandlePortGetSuccessfully(t *testing.T) { 237 th.Mux.HandleFunc("/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", func(w http.ResponseWriter, r *http.Request) { 238 th.TestMethod(t, r, "GET") 239 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 240 th.TestHeader(t, r, "Accept", "application/json") 241 242 fmt.Fprint(w, SinglePortBody) 243 }) 244 } 245 246 func HandlePortUpdateSuccessfully(t *testing.T, response string) { 247 th.Mux.HandleFunc("/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", func(w http.ResponseWriter, r *http.Request) { 248 th.TestMethod(t, r, "PATCH") 249 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 250 th.TestHeader(t, r, "Accept", "application/json") 251 th.TestHeader(t, r, "Content-Type", "application/json") 252 th.TestJSONRequest(t, r, `[{"op": "replace", "path": "/address", "value": "22:22:22:22:22:22"}]`) 253 254 fmt.Fprint(w, response) 255 }) 256 }