Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 4 | "encoding/json" |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 5 | "reflect" |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 6 | "testing" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // Taken from: http://docs.openstack.org/api/openstack-compute/2/content/List_Servers-d1e2078.html |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 10 | const goodListServersResult = ` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 11 | { |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 12 | "servers": [ |
| 13 | { |
| 14 | "id": "52415800-8b69-11e0-9b19-734f6af67565", |
| 15 | "tenant_id": "1234", |
| 16 | "user_id": "5678", |
| 17 | "name": "sample-server", |
| 18 | "updated": "2010-10-10T12:00:00Z", |
| 19 | "created": "2010-08-10T12:00:00Z", |
| 20 | "hostId": "e4d909c290d0fb1ca068ffaddf22cbd0", |
| 21 | "status": "BUILD", |
| 22 | "progress": 60, |
| 23 | "accessIPv4": "67.23.10.132", |
| 24 | "accessIPv6": "::babe:67.23.10.132", |
| 25 | "image": { |
| 26 | "id": "52415800-8b69-11e0-9b19-734f6f006e54", |
| 27 | "links": [ |
| 28 | { |
| 29 | "rel": "self", |
| 30 | "href": "http://servers.api.openstack.org/v2/1234/images/52415800-8b69-11e0-9b19-734f6f006e54" |
| 31 | }, |
| 32 | { |
| 33 | "rel": "bookmark", |
| 34 | "href": "http://servers.api.openstack.org/1234/images/52415800-8b69-11e0-9b19-734f6f006e54" |
| 35 | } |
| 36 | ] |
| 37 | }, |
| 38 | "flavor": { |
| 39 | "id": "52415800-8b69-11e0-9b19-734f216543fd", |
| 40 | "links": [ |
| 41 | { |
| 42 | "rel": "self", |
| 43 | "href": "http://servers.api.openstack.org/v2/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd" |
| 44 | }, |
| 45 | { |
| 46 | "rel": "bookmark", |
| 47 | "href": "http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd" |
| 48 | } |
| 49 | ] |
| 50 | }, |
| 51 | "addresses": { |
| 52 | "public": [ |
| 53 | { |
| 54 | "version": 4, |
| 55 | "addr": "67.23.10.132" |
| 56 | }, |
| 57 | { |
| 58 | "version": 6, |
| 59 | "addr": "::babe:67.23.10.132" |
| 60 | }, |
| 61 | { |
| 62 | "version": 4, |
| 63 | "addr": "67.23.10.131" |
| 64 | }, |
| 65 | { |
| 66 | "version": 6, |
| 67 | "addr": "::babe:4317:0A83" |
| 68 | } |
| 69 | ], |
| 70 | "private": [ |
| 71 | { |
| 72 | "version": 4, |
| 73 | "addr": "10.176.42.16" |
| 74 | }, |
| 75 | { |
| 76 | "version": 6, |
| 77 | "addr": "::babe:10.176.42.16" |
| 78 | } |
| 79 | ] |
| 80 | }, |
| 81 | "metadata": { |
| 82 | "Server Label": "Web Head 1", |
| 83 | "Image Version": "2.1" |
| 84 | }, |
| 85 | "links": [ |
| 86 | { |
| 87 | "rel": "self", |
| 88 | "href": "http://servers.api.openstack.org/v2/1234/servers/52415800-8b69-11e0-9b19-734f6af67565" |
| 89 | }, |
| 90 | { |
| 91 | "rel": "bookmark", |
| 92 | "href": "http://servers.api.openstack.org/1234/servers/52415800-8b69-11e0-9b19-734f6af67565" |
| 93 | } |
| 94 | ] |
| 95 | }, |
| 96 | { |
| 97 | "id": "52415800-8b69-11e0-9b19-734f1f1350e5", |
| 98 | "user_id": "5678", |
| 99 | "name": "sample-server2", |
| 100 | "tenant_id": "1234", |
| 101 | "updated": "2010-10-10T12:00:00Z", |
| 102 | "created": "2010-08-10T12:00:00Z", |
| 103 | "hostId": "9e107d9d372bb6826bd81d3542a419d6", |
| 104 | "status": "ACTIVE", |
| 105 | "accessIPv4": "67.23.10.133", |
| 106 | "accessIPv6": "::babe:67.23.10.133", |
| 107 | "image": { |
| 108 | "id": "52415800-8b69-11e0-9b19-734f5736d2a2", |
| 109 | "links": [ |
| 110 | { |
| 111 | "rel": "self", |
| 112 | "href": "http://servers.api.openstack.org/v2/1234/images/52415800-8b69-11e0-9b19-734f5736d2a2" |
| 113 | }, |
| 114 | { |
| 115 | "rel": "bookmark", |
| 116 | "href": "http://servers.api.openstack.org/1234/images/52415800-8b69-11e0-9b19-734f5736d2a2" |
| 117 | } |
| 118 | ] |
| 119 | }, |
| 120 | "flavor": { |
| 121 | "id": "52415800-8b69-11e0-9b19-734f216543fd", |
| 122 | "links": [ |
| 123 | { |
| 124 | "rel": "self", |
| 125 | "href": "http://servers.api.openstack.org/v2/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd" |
| 126 | }, |
| 127 | { |
| 128 | "rel": "bookmark", |
| 129 | "href": "http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd" |
| 130 | } |
| 131 | ] |
| 132 | }, |
| 133 | "addresses": { |
| 134 | "public": [ |
| 135 | { |
| 136 | "version": 4, |
| 137 | "addr": "67.23.10.133" |
| 138 | }, |
| 139 | { |
| 140 | "version": 6, |
| 141 | "addr": "::babe:67.23.10.133" |
| 142 | } |
| 143 | ], |
| 144 | "private": [ |
| 145 | { |
| 146 | "version": 4, |
| 147 | "addr": "10.176.42.17" |
| 148 | }, |
| 149 | { |
| 150 | "version": 6, |
| 151 | "addr": "::babe:10.176.42.17" |
| 152 | } |
| 153 | ] |
| 154 | }, |
| 155 | "metadata": { |
| 156 | "Server Label": "DB 1" |
| 157 | }, |
| 158 | "links": [ |
| 159 | { |
| 160 | "rel": "self", |
| 161 | "href": "http://servers.api.openstack.org/v2/1234/servers/52415800-8b69-11e0-9b19-734f1f1350e5" |
| 162 | }, |
| 163 | { |
| 164 | "rel": "bookmark", |
| 165 | "href": "http://servers.api.openstack.org/1234/servers/52415800-8b69-11e0-9b19-734f1f1350e5" |
| 166 | } |
| 167 | ] |
| 168 | } |
| 169 | ] |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 170 | } |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 171 | ` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 172 | |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 173 | func TestExtractServers(t *testing.T) { |
| 174 | var listPage ListPage |
| 175 | err := json.Unmarshal([]byte(goodListServersResult), &listPage.MarkerPageBase.LastHTTPResponse.Body) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 176 | if err != nil { |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 177 | t.Fatalf("Error decoding JSON fixture: %v", err) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 180 | svrs, err := ExtractServers(listPage) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 181 | if err != nil { |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 182 | t.Fatalf("Error extracting servers: %v", err) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if len(svrs) != 2 { |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 186 | t.Fatalf("Expected 2 servers; got %d", len(svrs)) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 189 | |
| 190 | // This provides more fine-grained failures when Servers differ, because Server structs are too damn big to compare by eye. |
| 191 | // FIXME I should absolutely refactor this into a general-purpose thing in testhelper. |
| 192 | func equalServers(t *testing.T, expected Server, actual Server) { |
| 193 | if expected.ID != actual.ID { |
| 194 | t.Errorf("ID differs. expected=[%s], actual=[%s]", expected.ID, actual.ID) |
| 195 | } |
| 196 | if expected.TenantID != actual.TenantID { |
| 197 | t.Errorf("TenantID differs. expected=[%s], actual=[%s]", expected.TenantID, actual.TenantID) |
| 198 | } |
| 199 | if expected.UserID != actual.UserID { |
| 200 | t.Errorf("UserID differs. expected=[%s], actual=[%s]", expected.UserID, actual.UserID) |
| 201 | } |
| 202 | if expected.Name != actual.Name { |
| 203 | t.Errorf("Name differs. expected=[%s], actual=[%s]", expected.Name, actual.Name) |
| 204 | } |
| 205 | if expected.Updated != actual.Updated { |
| 206 | t.Errorf("Updated differs. expected=[%s], actual=[%s]", expected.Updated, actual.Updated) |
| 207 | } |
| 208 | if expected.Created != actual.Created { |
| 209 | t.Errorf("Created differs. expected=[%s], actual=[%s]", expected.Created, actual.Created) |
| 210 | } |
| 211 | if expected.HostID != actual.HostID { |
| 212 | t.Errorf("HostID differs. expected=[%s], actual=[%s]", expected.HostID, actual.HostID) |
| 213 | } |
| 214 | if expected.Status != actual.Status { |
| 215 | t.Errorf("Status differs. expected=[%s], actual=[%s]", expected.Status, actual.Status) |
| 216 | } |
| 217 | if expected.Progress != actual.Progress { |
| 218 | t.Errorf("Progress differs. expected=[%s], actual=[%s]", expected.Progress, actual.Progress) |
| 219 | } |
| 220 | if expected.AccessIPv4 != actual.AccessIPv4 { |
| 221 | t.Errorf("AccessIPv4 differs. expected=[%s], actual=[%s]", expected.AccessIPv4, actual.AccessIPv4) |
| 222 | } |
| 223 | if expected.AccessIPv6 != actual.AccessIPv6 { |
| 224 | t.Errorf("AccessIPv6 differs. expected=[%s], actual=[%s]", expected.AccessIPv6, actual.AccessIPv6) |
| 225 | } |
| 226 | if !reflect.DeepEqual(expected.Image, actual.Image) { |
| 227 | t.Errorf("Image differs. expected=[%s], actual=[%s]", expected.Image, actual.Image) |
| 228 | } |
| 229 | if !reflect.DeepEqual(expected.Flavor, actual.Flavor) { |
| 230 | t.Errorf("Flavor differs. expected=[%s], actual=[%s]", expected.Flavor, actual.Flavor) |
| 231 | } |
| 232 | if !reflect.DeepEqual(expected.Addresses, actual.Addresses) { |
| 233 | t.Errorf("Addresses differ. expected=[%s], actual=[%s]", expected.Addresses, actual.Addresses) |
| 234 | } |
| 235 | if !reflect.DeepEqual(expected.Metadata, actual.Metadata) { |
| 236 | t.Errorf("Metadata differs. expected=[%s], actual=[%s]", expected.Metadata, actual.Metadata) |
| 237 | } |
| 238 | if !reflect.DeepEqual(expected.Links, actual.Links) { |
| 239 | t.Errorf("Links differs. expected=[%s], actual=[%s]", expected.Links, actual.Links) |
| 240 | } |
| 241 | if expected.KeyName != actual.KeyName { |
| 242 | t.Errorf("KeyName differs. expected=[%s], actual=[%s]", expected.KeyName, actual.KeyName) |
| 243 | } |
| 244 | if expected.AdminPass != actual.AdminPass { |
| 245 | t.Errorf("AdminPass differs. expected=[%s], actual=[%s]", expected.AdminPass, actual.AdminPass) |
| 246 | } |
| 247 | } |