Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 4 | "encoding/json" |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 5 | "fmt" |
| 6 | "net/http" |
| 7 | "reflect" |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 8 | "testing" |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 9 | |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 11 | th "github.com/rackspace/gophercloud/testhelper" |
| 12 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 15 | func TestListImages(t *testing.T) { |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 16 | th.SetupHTTP() |
| 17 | defer th.TeardownHTTP() |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 18 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 19 | th.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) { |
| 20 | th.TestMethod(t, r, "GET") |
| 21 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 22 | |
| 23 | w.Header().Add("Content-Type", "application/json") |
| 24 | r.ParseForm() |
| 25 | marker := r.Form.Get("marker") |
| 26 | switch marker { |
| 27 | case "": |
| 28 | fmt.Fprintf(w, ` |
| 29 | { |
| 30 | "images": [ |
| 31 | { |
| 32 | "status": "ACTIVE", |
| 33 | "updated": "2014-09-23T12:54:56Z", |
| 34 | "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7", |
| 35 | "OS-EXT-IMG-SIZE:size": 476704768, |
| 36 | "name": "F17-x86_64-cfntools", |
| 37 | "created": "2014-09-23T12:54:52Z", |
| 38 | "minDisk": 0, |
| 39 | "progress": 100, |
| 40 | "minRam": 0, |
| 41 | "metadata": {} |
| 42 | }, |
| 43 | { |
| 44 | "status": "ACTIVE", |
| 45 | "updated": "2014-09-23T12:51:43Z", |
| 46 | "id": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 47 | "OS-EXT-IMG-SIZE:size": 13167616, |
| 48 | "name": "cirros-0.3.2-x86_64-disk", |
| 49 | "created": "2014-09-23T12:51:42Z", |
| 50 | "minDisk": 0, |
| 51 | "progress": 100, |
| 52 | "minRam": 0, |
| 53 | "metadata": {} |
| 54 | } |
| 55 | ] |
| 56 | } |
| 57 | `) |
| 58 | case "2": |
| 59 | fmt.Fprintf(w, `{ "images": [] }`) |
| 60 | default: |
| 61 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 62 | } |
| 63 | }) |
| 64 | |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 65 | pages := 0 |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 66 | options := &ListOpts{Limit: 2} |
| 67 | err := ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 68 | pages++ |
| 69 | |
| 70 | actual, err := ExtractImages(page) |
| 71 | if err != nil { |
| 72 | return false, err |
| 73 | } |
| 74 | |
| 75 | expected := []Image{ |
| 76 | Image{ |
| 77 | ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7", |
| 78 | Name: "F17-x86_64-cfntools", |
| 79 | Created: "2014-09-23T12:54:52Z", |
| 80 | Updated: "2014-09-23T12:54:56Z", |
| 81 | MinDisk: 0, |
| 82 | MinRAM: 0, |
| 83 | Progress: 100, |
| 84 | Status: "ACTIVE", |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 85 | }, |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 86 | Image{ |
| 87 | ID: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 88 | Name: "cirros-0.3.2-x86_64-disk", |
| 89 | Created: "2014-09-23T12:51:42Z", |
| 90 | Updated: "2014-09-23T12:51:43Z", |
| 91 | MinDisk: 0, |
| 92 | MinRAM: 0, |
| 93 | Progress: 100, |
| 94 | Status: "ACTIVE", |
| 95 | }, |
| 96 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 97 | |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 98 | if !reflect.DeepEqual(expected, actual) { |
| 99 | t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual) |
| 100 | } |
| 101 | |
| 102 | return false, nil |
| 103 | }) |
| 104 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 105 | if err != nil { |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 106 | t.Fatalf("EachPage error: %v", err) |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 107 | } |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 108 | if pages != 1 { |
| 109 | t.Errorf("Expected one page, got %d", pages) |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
Ash Wilson | 4b54884 | 2014-09-25 08:58:02 -0400 | [diff] [blame] | 112 | |
| 113 | func TestGetImage(t *testing.T) { |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 114 | th.SetupHTTP() |
| 115 | defer th.TeardownHTTP() |
Ash Wilson | 4b54884 | 2014-09-25 08:58:02 -0400 | [diff] [blame] | 116 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 117 | th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) { |
| 118 | th.TestMethod(t, r, "GET") |
| 119 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 4b54884 | 2014-09-25 08:58:02 -0400 | [diff] [blame] | 120 | |
| 121 | w.Header().Add("Content-Type", "application/json") |
| 122 | fmt.Fprintf(w, ` |
| 123 | { |
| 124 | "image": { |
| 125 | "status": "ACTIVE", |
| 126 | "updated": "2014-09-23T12:54:56Z", |
| 127 | "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7", |
| 128 | "OS-EXT-IMG-SIZE:size": 476704768, |
| 129 | "name": "F17-x86_64-cfntools", |
| 130 | "created": "2014-09-23T12:54:52Z", |
| 131 | "minDisk": 0, |
| 132 | "progress": 100, |
| 133 | "minRam": 0, |
| 134 | "metadata": {} |
| 135 | } |
| 136 | } |
| 137 | `) |
| 138 | }) |
| 139 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 140 | actual, err := Get(fake.ServiceClient(), "12345678").Extract() |
Ash Wilson | 4b54884 | 2014-09-25 08:58:02 -0400 | [diff] [blame] | 141 | if err != nil { |
| 142 | t.Fatalf("Unexpected error from Get: %v", err) |
| 143 | } |
Ash Wilson | 4b54884 | 2014-09-25 08:58:02 -0400 | [diff] [blame] | 144 | |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 145 | expected := &Image{ |
Ash Wilson | 4b54884 | 2014-09-25 08:58:02 -0400 | [diff] [blame] | 146 | Status: "ACTIVE", |
| 147 | Updated: "2014-09-23T12:54:56Z", |
| 148 | ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7", |
| 149 | Name: "F17-x86_64-cfntools", |
| 150 | Created: "2014-09-23T12:54:52Z", |
| 151 | MinDisk: 0, |
| 152 | Progress: 100, |
| 153 | MinRAM: 0, |
| 154 | } |
| 155 | |
| 156 | if !reflect.DeepEqual(expected, actual) { |
| 157 | t.Errorf("Expected %#v, but got %#v", expected, actual) |
| 158 | } |
| 159 | } |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame^] | 160 | |
| 161 | func TestNextPageURL(t *testing.T) { |
| 162 | var page ImagePage |
| 163 | var body map[string]interface{} |
| 164 | bodyString := []byte(`{"images":{"links":[{"href":"http://192.154.23.87/12345/images/image3","rel":"next"},{"href":"http://192.154.23.87/12345/images/image1","rel":"previous"}]}}`) |
| 165 | err := json.Unmarshal(bodyString, &body) |
| 166 | if err != nil { |
| 167 | t.Fatalf("Error unmarshaling data into page body: %v", err) |
| 168 | } |
| 169 | page.Body = body |
| 170 | |
| 171 | expected := "http://192.154.23.87/12345/images/image3" |
| 172 | actual, err := page.NextPageURL() |
| 173 | th.AssertNoErr(t, err) |
| 174 | th.CheckEquals(t, expected, actual) |
| 175 | } |