blob: 1de030352e400df71054f5e4b5b783933bd797fb [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Samuel A. Falvo II808bb632014-03-12 00:07:50 -07002
3import (
Jon Perrittef168e62014-10-08 11:14:05 -05004 "encoding/json"
Ash Wilsonfaf006d2014-09-24 17:10:58 -04005 "fmt"
6 "net/http"
7 "reflect"
Jon Perritt30558642014-04-14 17:07:12 -05008 "testing"
Ash Wilsonfaf006d2014-09-24 17:10:58 -04009
jrperritt3d966162016-06-06 14:08:54 -050010 "github.com/gophercloud/gophercloud/openstack/compute/v2/images"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/pagination"
12 th "github.com/gophercloud/gophercloud/testhelper"
13 fake "github.com/gophercloud/gophercloud/testhelper/client"
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070014)
15
Ash Wilsonfaf006d2014-09-24 17:10:58 -040016func TestListImages(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -050017 th.SetupHTTP()
18 defer th.TeardownHTTP()
Ash Wilsonfaf006d2014-09-24 17:10:58 -040019
Jon Perrittef168e62014-10-08 11:14:05 -050020 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 Wilsonfaf006d2014-09-24 17:10:58 -040023
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,
Joe Topjianafdc3502017-03-09 18:51:37 -070041 "minRam": 0,
42 "metadata": {
43 "architecture": "x86_64",
44 "block_device_mapping": {
45 "guest_format": null,
46 "boot_index": 0,
47 "device_name": "/dev/vda",
48 "delete_on_termination": false
49 }
50 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -040051 },
52 {
53 "status": "ACTIVE",
54 "updated": "2014-09-23T12:51:43Z",
55 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
56 "OS-EXT-IMG-SIZE:size": 13167616,
57 "name": "cirros-0.3.2-x86_64-disk",
58 "created": "2014-09-23T12:51:42Z",
59 "minDisk": 0,
60 "progress": 100,
jrperritt9b7b9e62016-07-11 22:30:50 -050061 "minRam": 0
Ash Wilsonfaf006d2014-09-24 17:10:58 -040062 }
63 ]
64 }
65 `)
66 case "2":
67 fmt.Fprintf(w, `{ "images": [] }`)
68 default:
69 t.Fatalf("Unexpected marker: [%s]", marker)
70 }
71 })
72
Ash Wilsonfaf006d2014-09-24 17:10:58 -040073 pages := 0
jrperritt3d966162016-06-06 14:08:54 -050074 options := &images.ListOpts{Limit: 2}
75 err := images.ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040076 pages++
77
jrperritt3d966162016-06-06 14:08:54 -050078 actual, err := images.ExtractImages(page)
Ash Wilsonfaf006d2014-09-24 17:10:58 -040079 if err != nil {
80 return false, err
81 }
82
jrperritt3d966162016-06-06 14:08:54 -050083 expected := []images.Image{
84 {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040085 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",
Joe Topjianafdc3502017-03-09 18:51:37 -070093 Metadata: map[string]interface{}{
94 "architecture": "x86_64",
95 "block_device_mapping": map[string]interface{}{
96 "guest_format": interface{}(nil),
97 "boot_index": float64(0),
98 "device_name": "/dev/vda",
99 "delete_on_termination": false,
100 },
101 },
Ash Wilson7ddf0362014-09-17 10:59:09 -0400102 },
jrperritt3d966162016-06-06 14:08:54 -0500103 {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400104 ID: "f90f6034-2570-4974-8351-6b49732ef2eb",
105 Name: "cirros-0.3.2-x86_64-disk",
106 Created: "2014-09-23T12:51:42Z",
107 Updated: "2014-09-23T12:51:43Z",
108 MinDisk: 0,
109 MinRAM: 0,
110 Progress: 100,
111 Status: "ACTIVE",
112 },
113 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700114
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400115 if !reflect.DeepEqual(expected, actual) {
116 t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual)
117 }
118
119 return false, nil
120 })
121
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700122 if err != nil {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400123 t.Fatalf("EachPage error: %v", err)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700124 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400125 if pages != 1 {
126 t.Errorf("Expected one page, got %d", pages)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700127 }
128}
Ash Wilson4b548842014-09-25 08:58:02 -0400129
130func TestGetImage(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -0500131 th.SetupHTTP()
132 defer th.TeardownHTTP()
Ash Wilson4b548842014-09-25 08:58:02 -0400133
Jon Perrittef168e62014-10-08 11:14:05 -0500134 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
135 th.TestMethod(t, r, "GET")
136 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson4b548842014-09-25 08:58:02 -0400137
138 w.Header().Add("Content-Type", "application/json")
139 fmt.Fprintf(w, `
140 {
141 "image": {
142 "status": "ACTIVE",
143 "updated": "2014-09-23T12:54:56Z",
144 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
145 "OS-EXT-IMG-SIZE:size": 476704768,
146 "name": "F17-x86_64-cfntools",
147 "created": "2014-09-23T12:54:52Z",
148 "minDisk": 0,
149 "progress": 100,
Joe Topjianafdc3502017-03-09 18:51:37 -0700150 "minRam": 0,
151 "metadata": {
152 "architecture": "x86_64",
153 "block_device_mapping": {
154 "guest_format": null,
155 "boot_index": 0,
156 "device_name": "/dev/vda",
157 "delete_on_termination": false
158 }
159 }
Ash Wilson4b548842014-09-25 08:58:02 -0400160 }
161 }
162 `)
163 })
164
jrperritt3d966162016-06-06 14:08:54 -0500165 actual, err := images.Get(fake.ServiceClient(), "12345678").Extract()
Ash Wilson4b548842014-09-25 08:58:02 -0400166 if err != nil {
167 t.Fatalf("Unexpected error from Get: %v", err)
168 }
Ash Wilson4b548842014-09-25 08:58:02 -0400169
jrperritt3d966162016-06-06 14:08:54 -0500170 expected := &images.Image{
Ash Wilson4b548842014-09-25 08:58:02 -0400171 Status: "ACTIVE",
172 Updated: "2014-09-23T12:54:56Z",
173 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
174 Name: "F17-x86_64-cfntools",
175 Created: "2014-09-23T12:54:52Z",
176 MinDisk: 0,
177 Progress: 100,
178 MinRAM: 0,
Joe Topjianafdc3502017-03-09 18:51:37 -0700179 Metadata: map[string]interface{}{
180 "architecture": "x86_64",
181 "block_device_mapping": map[string]interface{}{
182 "guest_format": interface{}(nil),
183 "boot_index": float64(0),
184 "device_name": "/dev/vda",
185 "delete_on_termination": false,
186 },
187 },
Ash Wilson4b548842014-09-25 08:58:02 -0400188 }
189
190 if !reflect.DeepEqual(expected, actual) {
191 t.Errorf("Expected %#v, but got %#v", expected, actual)
192 }
193}
Jon Perrittef168e62014-10-08 11:14:05 -0500194
195func TestNextPageURL(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500196 var page images.ImagePage
Jon Perrittef168e62014-10-08 11:14:05 -0500197 var body map[string]interface{}
Jon Perrittb8764b62014-10-09 14:55:59 -0500198 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 Perrittef168e62014-10-08 11:14:05 -0500199 err := json.Unmarshal(bodyString, &body)
200 if err != nil {
201 t.Fatalf("Error unmarshaling data into page body: %v", err)
202 }
203 page.Body = body
204
Jon Perrittb8764b62014-10-09 14:55:59 -0500205 expected := "http://192.154.23.87/12345/images/image4"
Jon Perrittef168e62014-10-08 11:14:05 -0500206 actual, err := page.NextPageURL()
207 th.AssertNoErr(t, err)
208 th.CheckEquals(t, expected, actual)
209}
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700210
211// Test Image delete
212func TestDeleteImage(t *testing.T) {
213 th.SetupHTTP()
214 defer th.TeardownHTTP()
215
216 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
217 th.TestMethod(t, r, "DELETE")
218 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
219
220 w.WriteHeader(http.StatusNoContent)
221 })
222
jrperritt3d966162016-06-06 14:08:54 -0500223 res := images.Delete(fake.ServiceClient(), "12345678")
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700224 th.AssertNoErr(t, res.Err)
225}