Jon Perritt | 9da13a1 | 2015-01-20 19:25:15 -0700 | [diff] [blame^] | 1 | package services |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
| 12 | func TestList(t *testing.T) { |
| 13 | th.SetupHTTP() |
| 14 | defer th.TeardownHTTP() |
| 15 | |
| 16 | HandleListCDNServiceSuccessfully(t) |
| 17 | |
| 18 | count := 0 |
| 19 | |
| 20 | err := List(fake.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 21 | count++ |
| 22 | actual, err := ExtractServices(page) |
| 23 | if err != nil { |
| 24 | t.Errorf("Failed to extract services: %v", err) |
| 25 | return false, err |
| 26 | } |
| 27 | |
| 28 | expected := []Service{ |
| 29 | Service{ |
| 30 | ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", |
| 31 | Name: "mywebsite.com", |
| 32 | Domains: []Domain{ |
| 33 | Domain{ |
| 34 | Domain: "www.mywebsite.com", |
| 35 | }, |
| 36 | }, |
| 37 | Origins: []Origin{ |
| 38 | Origin{ |
| 39 | Origin: "mywebsite.com", |
| 40 | Port: 80, |
| 41 | SSL: false, |
| 42 | }, |
| 43 | }, |
| 44 | Caching: []CacheRule{ |
| 45 | CacheRule{ |
| 46 | Name: "default", |
| 47 | TTL: 3600, |
| 48 | }, |
| 49 | CacheRule{ |
| 50 | Name: "home", |
| 51 | TTL: 17200, |
| 52 | Rules: []TTLRule{ |
| 53 | TTLRule{ |
| 54 | Name: "index", |
| 55 | RequestURL: "/index.htm", |
| 56 | }, |
| 57 | }, |
| 58 | }, |
| 59 | CacheRule{ |
| 60 | Name: "images", |
| 61 | TTL: 12800, |
| 62 | Rules: []TTLRule{ |
| 63 | TTLRule{ |
| 64 | Name: "images", |
| 65 | RequestURL: "*.png", |
| 66 | }, |
| 67 | }, |
| 68 | }, |
| 69 | }, |
| 70 | Restrictions: []Restriction{ |
| 71 | Restriction{ |
| 72 | Name: "website only", |
| 73 | Rules: []RestrictionRule{ |
| 74 | RestrictionRule{ |
| 75 | Name: "mywebsite.com", |
| 76 | Referrer: "www.mywebsite.com", |
| 77 | }, |
| 78 | }, |
| 79 | }, |
| 80 | }, |
| 81 | FlavorID: "asia", |
| 82 | Status: "deployed", |
| 83 | Errors: []Error{}, |
| 84 | Links: []gophercloud.Link{ |
| 85 | gophercloud.Link{ |
| 86 | Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", |
| 87 | Rel: "self", |
| 88 | }, |
| 89 | gophercloud.Link{ |
| 90 | Href: "mywebsite.com.cdn123.poppycdn.net", |
| 91 | Rel: "access_url", |
| 92 | }, |
| 93 | gophercloud.Link{ |
| 94 | Href: "https://www.poppycdn.io/v1.0/flavors/asia", |
| 95 | Rel: "flavor", |
| 96 | }, |
| 97 | }, |
| 98 | }, |
| 99 | Service{ |
| 100 | ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1", |
| 101 | Name: "myothersite.com", |
| 102 | Domains: []Domain{ |
| 103 | Domain{ |
| 104 | Domain: "www.myothersite.com", |
| 105 | }, |
| 106 | }, |
| 107 | Origins: []Origin{ |
| 108 | Origin{ |
| 109 | Origin: "44.33.22.11", |
| 110 | Port: 80, |
| 111 | SSL: false, |
| 112 | }, |
| 113 | Origin{ |
| 114 | Origin: "77.66.55.44", |
| 115 | Port: 80, |
| 116 | SSL: false, |
| 117 | Rules: []OriginRule{ |
| 118 | OriginRule{ |
| 119 | Name: "videos", |
| 120 | RequestURL: "^/videos/*.m3u", |
| 121 | }, |
| 122 | }, |
| 123 | }, |
| 124 | }, |
| 125 | Caching: []CacheRule{ |
| 126 | CacheRule{ |
| 127 | Name: "default", |
| 128 | TTL: 3600, |
| 129 | }, |
| 130 | }, |
| 131 | Restrictions: []Restriction{}, |
| 132 | FlavorID: "europe", |
| 133 | Status: "deployed", |
| 134 | Links: []gophercloud.Link{ |
| 135 | gophercloud.Link{ |
| 136 | Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1", |
| 137 | Rel: "self", |
| 138 | }, |
| 139 | gophercloud.Link{ |
| 140 | Href: "myothersite.com.poppycdn.net", |
| 141 | Rel: "access_url", |
| 142 | }, |
| 143 | gophercloud.Link{ |
| 144 | Href: "https://www.poppycdn.io/v1.0/flavors/europe", |
| 145 | Rel: "flavor", |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | } |
| 150 | |
| 151 | th.CheckDeepEquals(t, expected, actual) |
| 152 | |
| 153 | return true, nil |
| 154 | }) |
| 155 | th.AssertNoErr(t, err) |
| 156 | |
| 157 | if count != 1 { |
| 158 | t.Errorf("Expected 1 page, got %d", count) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func TestCreate(t *testing.T) { |
| 163 | th.SetupHTTP() |
| 164 | defer th.TeardownHTTP() |
| 165 | |
| 166 | HandleCreateCDNServiceSuccessfully(t) |
| 167 | |
| 168 | createOpts := CreateOpts{ |
| 169 | Name: "mywebsite.com", |
| 170 | Domains: []Domain{ |
| 171 | Domain{ |
| 172 | Domain: "www.mywebsite.com", |
| 173 | }, |
| 174 | Domain{ |
| 175 | Domain: "blog.mywebsite.com", |
| 176 | }, |
| 177 | }, |
| 178 | Origins: []Origin{ |
| 179 | Origin{ |
| 180 | Origin: "mywebsite.com", |
| 181 | Port: 80, |
| 182 | SSL: false, |
| 183 | }, |
| 184 | }, |
| 185 | Restrictions: []Restriction{ |
| 186 | Restriction{ |
| 187 | Name: "website only", |
| 188 | Rules: []RestrictionRule{ |
| 189 | RestrictionRule{ |
| 190 | Name: "mywebsite.com", |
| 191 | Referrer: "www.mywebsite.com", |
| 192 | }, |
| 193 | }, |
| 194 | }, |
| 195 | }, |
| 196 | Caching: []CacheRule{ |
| 197 | CacheRule{ |
| 198 | Name: "default", |
| 199 | TTL: 3600, |
| 200 | }, |
| 201 | }, |
| 202 | FlavorID: "cdn", |
| 203 | } |
| 204 | |
| 205 | expected := "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" |
| 206 | actual, err := Create(fake.ServiceClient(), createOpts).Extract() |
| 207 | th.AssertNoErr(t, err) |
| 208 | th.AssertEquals(t, expected, actual) |
| 209 | } |
| 210 | |
| 211 | func TestGet(t *testing.T) { |
| 212 | th.SetupHTTP() |
| 213 | defer th.TeardownHTTP() |
| 214 | |
| 215 | HandleGetCDNServiceSuccessfully(t) |
| 216 | |
| 217 | expected := &Service{ |
| 218 | ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", |
| 219 | Name: "mywebsite.com", |
| 220 | Domains: []Domain{ |
| 221 | Domain{ |
| 222 | Domain: "www.mywebsite.com", |
| 223 | Protocol: "http", |
| 224 | }, |
| 225 | }, |
| 226 | Origins: []Origin{ |
| 227 | Origin{ |
| 228 | Origin: "mywebsite.com", |
| 229 | Port: 80, |
| 230 | SSL: false, |
| 231 | }, |
| 232 | }, |
| 233 | Caching: []CacheRule{ |
| 234 | CacheRule{ |
| 235 | Name: "default", |
| 236 | TTL: 3600, |
| 237 | }, |
| 238 | CacheRule{ |
| 239 | Name: "home", |
| 240 | TTL: 17200, |
| 241 | Rules: []TTLRule{ |
| 242 | TTLRule{ |
| 243 | Name: "index", |
| 244 | RequestURL: "/index.htm", |
| 245 | }, |
| 246 | }, |
| 247 | }, |
| 248 | CacheRule{ |
| 249 | Name: "images", |
| 250 | TTL: 12800, |
| 251 | Rules: []TTLRule{ |
| 252 | TTLRule{ |
| 253 | Name: "images", |
| 254 | RequestURL: "*.png", |
| 255 | }, |
| 256 | }, |
| 257 | }, |
| 258 | }, |
| 259 | Restrictions: []Restriction{ |
| 260 | Restriction{ |
| 261 | Name: "website only", |
| 262 | Rules: []RestrictionRule{ |
| 263 | RestrictionRule{ |
| 264 | Name: "mywebsite.com", |
| 265 | Referrer: "www.mywebsite.com", |
| 266 | }, |
| 267 | }, |
| 268 | }, |
| 269 | }, |
| 270 | FlavorID: "cdn", |
| 271 | Status: "deployed", |
| 272 | Errors: []Error{}, |
| 273 | Links: []gophercloud.Link{ |
| 274 | gophercloud.Link{ |
| 275 | Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", |
| 276 | Rel: "self", |
| 277 | }, |
| 278 | gophercloud.Link{ |
| 279 | Href: "blog.mywebsite.com.cdn1.raxcdn.com", |
| 280 | Rel: "access_url", |
| 281 | }, |
| 282 | gophercloud.Link{ |
| 283 | Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/flavors/cdn", |
| 284 | Rel: "flavor", |
| 285 | }, |
| 286 | }, |
| 287 | } |
| 288 | |
| 289 | |
| 290 | actual, err := Get(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").Extract() |
| 291 | th.AssertNoErr(t, err) |
| 292 | th.AssertDeepEquals(t, expected, actual) |
| 293 | } |
| 294 | |
| 295 | func TestUpdate(t *testing.T) { |
| 296 | th.SetupHTTP() |
| 297 | defer th.TeardownHTTP() |
| 298 | |
| 299 | HandleUpdateCDNServiceSuccessfully(t) |
| 300 | |
| 301 | expected := "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" |
| 302 | updateOpts := UpdateOpts{ |
| 303 | UpdateOpt{ |
| 304 | Op: Replace, |
| 305 | Path: "/origins/0", |
| 306 | Value: map[string]interface{}{ |
| 307 | "origin": "44.33.22.11", |
| 308 | "port": 80, |
| 309 | "ssl": false, |
| 310 | }, |
| 311 | }, |
| 312 | UpdateOpt{ |
| 313 | Op: Add, |
| 314 | Path: "/domains/0", |
| 315 | Value: map[string]interface{}{ |
| 316 | "domain": "added.mocksite4.com", |
| 317 | }, |
| 318 | }, |
| 319 | } |
| 320 | actual, err := Update(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", updateOpts).Extract() |
| 321 | th.AssertNoErr(t, err) |
| 322 | th.AssertEquals(t, expected, actual) |
| 323 | } |
| 324 | |
| 325 | func TestDelete(t *testing.T) { |
| 326 | th.SetupHTTP() |
| 327 | defer th.TeardownHTTP() |
| 328 | |
| 329 | HandleDeleteCDNServiceSuccessfully(t) |
| 330 | |
| 331 | err := Delete(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").ExtractErr() |
| 332 | th.AssertNoErr(t, err) |
| 333 | } |