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