Jon Perritt | 9776ef6 | 2015-03-16 17:11:22 -0600 | [diff] [blame] | 1 | package cloudnetworks |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | func TestListCloudNetworks(t *testing.T) { |
| 15 | th.SetupHTTP() |
| 16 | defer th.TeardownHTTP() |
| 17 | th.Mux.HandleFunc("/cloud_networks", func(w http.ResponseWriter, r *http.Request) { |
| 18 | th.TestMethod(t, r, "GET") |
| 19 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 20 | th.TestHeader(t, r, "Accept", "application/json") |
| 21 | |
| 22 | w.Header().Set("Content-Type", "application/json") |
| 23 | fmt.Fprintf(w, `[{ |
| 24 | "cidr": "192.168.100.0/24", |
| 25 | "created": "2014-05-25T01:23:42Z", |
| 26 | "id": "07426958-1ebf-4c38-b032-d456820ca21a", |
| 27 | "name": "RC-CLOUD", |
| 28 | "updated": "2014-05-25T02:28:44Z" |
| 29 | }]`) |
| 30 | }) |
| 31 | |
| 32 | expected := []CloudNetwork{ |
| 33 | CloudNetwork{ |
| 34 | CIDR: "192.168.100.0/24", |
| 35 | CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC), |
| 36 | ID: "07426958-1ebf-4c38-b032-d456820ca21a", |
| 37 | Name: "RC-CLOUD", |
| 38 | UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC), |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | count := 0 |
| 43 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 44 | count++ |
| 45 | actual, err := ExtractCloudNetworks(page) |
| 46 | th.AssertNoErr(t, err) |
| 47 | |
| 48 | th.CheckDeepEquals(t, expected, actual) |
| 49 | |
| 50 | return true, nil |
| 51 | }) |
| 52 | th.AssertNoErr(t, err) |
| 53 | th.CheckEquals(t, count, 1) |
| 54 | } |
| 55 | |
| 56 | func TestGetCloudNetwork(t *testing.T) { |
| 57 | th.SetupHTTP() |
| 58 | defer th.TeardownHTTP() |
| 59 | th.Mux.HandleFunc("/cloud_networks/07426958-1ebf-4c38-b032-d456820ca21a", func(w http.ResponseWriter, r *http.Request) { |
| 60 | th.TestMethod(t, r, "GET") |
| 61 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 62 | th.TestHeader(t, r, "Accept", "application/json") |
| 63 | |
| 64 | w.Header().Set("Content-Type", "application/json") |
| 65 | w.WriteHeader(http.StatusOK) |
| 66 | fmt.Fprintf(w, `{ |
| 67 | "cidr": "192.168.100.0/24", |
| 68 | "created": "2014-05-25T01:23:42Z", |
| 69 | "id": "07426958-1ebf-4c38-b032-d456820ca21a", |
| 70 | "name": "RC-CLOUD", |
| 71 | "updated": "2014-05-25T02:28:44Z" |
| 72 | }`) |
| 73 | }) |
| 74 | |
| 75 | expected := &CloudNetwork{ |
| 76 | CIDR: "192.168.100.0/24", |
| 77 | CreatedAt: time.Date(2014, 5, 25, 1, 23, 42, 0, time.UTC), |
| 78 | ID: "07426958-1ebf-4c38-b032-d456820ca21a", |
| 79 | Name: "RC-CLOUD", |
| 80 | UpdatedAt: time.Date(2014, 5, 25, 2, 28, 44, 0, time.UTC), |
| 81 | } |
| 82 | |
| 83 | actual, err := Get(fake.ServiceClient(), "07426958-1ebf-4c38-b032-d456820ca21a").Extract() |
| 84 | th.AssertNoErr(t, err) |
| 85 | |
| 86 | th.AssertDeepEquals(t, expected, actual) |
| 87 | } |