blob: 21e82969066708346e07240be93c586c21cfef89 [file] [log] [blame]
Samuel A. Falvo II808bb632014-03-12 00:07:50 -07001package images
2
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
Ash Wilsonfaf006d2014-09-24 17:10:58 -040010 "github.com/rackspace/gophercloud/pagination"
Jon Perrittef168e62014-10-08 11:14:05 -050011 th "github.com/rackspace/gophercloud/testhelper"
12 fake "github.com/rackspace/gophercloud/testhelper/client"
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070013)
14
Ash Wilsonfaf006d2014-09-24 17:10:58 -040015func TestListImages(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -050016 th.SetupHTTP()
17 defer th.TeardownHTTP()
Ash Wilsonfaf006d2014-09-24 17:10:58 -040018
Jon Perrittef168e62014-10-08 11:14:05 -050019 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 Wilsonfaf006d2014-09-24 17:10:58 -040022
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,
jrperritt65d5ee22016-05-09 16:43:32 -050040 "minRam": 0
Ash Wilsonfaf006d2014-09-24 17:10:58 -040041 },
42 {
43 "status": "ACTIVE",
44 "updated": "2014-09-23T12:51:43Z",
45 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
46 "OS-EXT-IMG-SIZE:size": 13167616,
47 "name": "cirros-0.3.2-x86_64-disk",
48 "created": "2014-09-23T12:51:42Z",
49 "minDisk": 0,
50 "progress": 100,
jrperritt65d5ee22016-05-09 16:43:32 -050051 "minRam": 0
Ash Wilsonfaf006d2014-09-24 17:10:58 -040052 }
53 ]
54 }
55 `)
56 case "2":
57 fmt.Fprintf(w, `{ "images": [] }`)
58 default:
59 t.Fatalf("Unexpected marker: [%s]", marker)
60 }
61 })
62
Ash Wilsonfaf006d2014-09-24 17:10:58 -040063 pages := 0
Jon Perrittef168e62014-10-08 11:14:05 -050064 options := &ListOpts{Limit: 2}
65 err := ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040066 pages++
67
68 actual, err := ExtractImages(page)
69 if err != nil {
70 return false, err
71 }
72
73 expected := []Image{
74 Image{
75 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
76 Name: "F17-x86_64-cfntools",
77 Created: "2014-09-23T12:54:52Z",
78 Updated: "2014-09-23T12:54:56Z",
79 MinDisk: 0,
80 MinRAM: 0,
81 Progress: 100,
82 Status: "ACTIVE",
Ash Wilson7ddf0362014-09-17 10:59:09 -040083 },
Ash Wilsonfaf006d2014-09-24 17:10:58 -040084 Image{
85 ID: "f90f6034-2570-4974-8351-6b49732ef2eb",
86 Name: "cirros-0.3.2-x86_64-disk",
87 Created: "2014-09-23T12:51:42Z",
88 Updated: "2014-09-23T12:51:43Z",
89 MinDisk: 0,
90 MinRAM: 0,
91 Progress: 100,
92 Status: "ACTIVE",
93 },
94 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070095
Ash Wilsonfaf006d2014-09-24 17:10:58 -040096 if !reflect.DeepEqual(expected, actual) {
97 t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual)
98 }
99
100 return false, nil
101 })
102
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700103 if err != nil {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400104 t.Fatalf("EachPage error: %v", err)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700105 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400106 if pages != 1 {
107 t.Errorf("Expected one page, got %d", pages)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700108 }
109}
Ash Wilson4b548842014-09-25 08:58:02 -0400110
111func TestGetImage(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -0500112 th.SetupHTTP()
113 defer th.TeardownHTTP()
Ash Wilson4b548842014-09-25 08:58:02 -0400114
Jon Perrittef168e62014-10-08 11:14:05 -0500115 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
116 th.TestMethod(t, r, "GET")
117 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson4b548842014-09-25 08:58:02 -0400118
119 w.Header().Add("Content-Type", "application/json")
120 fmt.Fprintf(w, `
121 {
122 "image": {
123 "status": "ACTIVE",
124 "updated": "2014-09-23T12:54:56Z",
125 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
126 "OS-EXT-IMG-SIZE:size": 476704768,
127 "name": "F17-x86_64-cfntools",
128 "created": "2014-09-23T12:54:52Z",
129 "minDisk": 0,
130 "progress": 100,
jrperritt65d5ee22016-05-09 16:43:32 -0500131 "minRam": 0
Ash Wilson4b548842014-09-25 08:58:02 -0400132 }
133 }
134 `)
135 })
136
Jon Perrittef168e62014-10-08 11:14:05 -0500137 actual, err := Get(fake.ServiceClient(), "12345678").Extract()
Ash Wilson4b548842014-09-25 08:58:02 -0400138 if err != nil {
139 t.Fatalf("Unexpected error from Get: %v", err)
140 }
Ash Wilson4b548842014-09-25 08:58:02 -0400141
Ash Wilsond2f87032014-09-25 11:34:41 -0400142 expected := &Image{
Ash Wilson4b548842014-09-25 08:58:02 -0400143 Status: "ACTIVE",
144 Updated: "2014-09-23T12:54:56Z",
145 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
146 Name: "F17-x86_64-cfntools",
147 Created: "2014-09-23T12:54:52Z",
148 MinDisk: 0,
149 Progress: 100,
150 MinRAM: 0,
151 }
152
153 if !reflect.DeepEqual(expected, actual) {
154 t.Errorf("Expected %#v, but got %#v", expected, actual)
155 }
156}
Jon Perrittef168e62014-10-08 11:14:05 -0500157
158func TestNextPageURL(t *testing.T) {
159 var page ImagePage
160 var body map[string]interface{}
Jon Perrittb8764b62014-10-09 14:55:59 -0500161 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 -0500162 err := json.Unmarshal(bodyString, &body)
163 if err != nil {
164 t.Fatalf("Error unmarshaling data into page body: %v", err)
165 }
166 page.Body = body
167
Jon Perrittb8764b62014-10-09 14:55:59 -0500168 expected := "http://192.154.23.87/12345/images/image4"
Jon Perrittef168e62014-10-08 11:14:05 -0500169 actual, err := page.NextPageURL()
170 th.AssertNoErr(t, err)
171 th.CheckEquals(t, expected, actual)
172}
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700173
174// Test Image delete
175func TestDeleteImage(t *testing.T) {
176 th.SetupHTTP()
177 defer th.TeardownHTTP()
178
179 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
180 th.TestMethod(t, r, "DELETE")
181 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
182
183 w.WriteHeader(http.StatusNoContent)
184 })
185
186 res := Delete(fake.ServiceClient(), "12345678")
187 th.AssertNoErr(t, res.Err)
188}