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