blob: e2d64d34b7c67dbb279753e898cf224898cf8c4b [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,
jrperritt9b7b9e62016-07-11 22:30:50 -050041 "minRam": 0
Ash Wilsonfaf006d2014-09-24 17:10:58 -040042 },
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,
jrperritt9b7b9e62016-07-11 22:30:50 -050052 "minRam": 0
Ash Wilsonfaf006d2014-09-24 17:10:58 -040053 }
54 ]
55 }
56 `)
57 case "2":
58 fmt.Fprintf(w, `{ "images": [] }`)
59 default:
60 t.Fatalf("Unexpected marker: [%s]", marker)
61 }
62 })
63
Ash Wilsonfaf006d2014-09-24 17:10:58 -040064 pages := 0
jrperritt3d966162016-06-06 14:08:54 -050065 options := &images.ListOpts{Limit: 2}
66 err := images.ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040067 pages++
68
jrperritt3d966162016-06-06 14:08:54 -050069 actual, err := images.ExtractImages(page)
Ash Wilsonfaf006d2014-09-24 17:10:58 -040070 if err != nil {
71 return false, err
72 }
73
jrperritt3d966162016-06-06 14:08:54 -050074 expected := []images.Image{
75 {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040076 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 Wilson7ddf0362014-09-17 10:59:09 -040084 },
jrperritt3d966162016-06-06 14:08:54 -050085 {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040086 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 II808bb632014-03-12 00:07:50 -070096
Ash Wilsonfaf006d2014-09-24 17:10:58 -040097 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 II808bb632014-03-12 00:07:50 -0700104 if err != nil {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400105 t.Fatalf("EachPage error: %v", err)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700106 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400107 if pages != 1 {
108 t.Errorf("Expected one page, got %d", pages)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700109 }
110}
Ash Wilson4b548842014-09-25 08:58:02 -0400111
112func TestGetImage(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -0500113 th.SetupHTTP()
114 defer th.TeardownHTTP()
Ash Wilson4b548842014-09-25 08:58:02 -0400115
Jon Perrittef168e62014-10-08 11:14:05 -0500116 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 Wilson4b548842014-09-25 08:58:02 -0400119
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,
jrperritt9b7b9e62016-07-11 22:30:50 -0500132 "minRam": 0
Ash Wilson4b548842014-09-25 08:58:02 -0400133 }
134 }
135 `)
136 })
137
jrperritt3d966162016-06-06 14:08:54 -0500138 actual, err := images.Get(fake.ServiceClient(), "12345678").Extract()
Ash Wilson4b548842014-09-25 08:58:02 -0400139 if err != nil {
140 t.Fatalf("Unexpected error from Get: %v", err)
141 }
Ash Wilson4b548842014-09-25 08:58:02 -0400142
jrperritt3d966162016-06-06 14:08:54 -0500143 expected := &images.Image{
Ash Wilson4b548842014-09-25 08:58:02 -0400144 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 Perrittef168e62014-10-08 11:14:05 -0500158
159func TestNextPageURL(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500160 var page images.ImagePage
Jon Perrittef168e62014-10-08 11:14:05 -0500161 var body map[string]interface{}
Jon Perrittb8764b62014-10-09 14:55:59 -0500162 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 -0500163 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 Perrittb8764b62014-10-09 14:55:59 -0500169 expected := "http://192.154.23.87/12345/images/image4"
Jon Perrittef168e62014-10-08 11:14:05 -0500170 actual, err := page.NextPageURL()
171 th.AssertNoErr(t, err)
172 th.CheckEquals(t, expected, actual)
173}
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700174
175// Test Image delete
176func 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
jrperritt3d966162016-06-06 14:08:54 -0500187 res := images.Delete(fake.ServiceClient(), "12345678")
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700188 th.AssertNoErr(t, res.Err)
189}