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