blob: 2dfa88b45ecd518750c521b8df7c20305c60a819 [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,
40 "minRam": 0,
41 "metadata": {}
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,
52 "minRam": 0,
53 "metadata": {}
54 }
55 ]
56 }
57 `)
58 case "2":
59 fmt.Fprintf(w, `{ "images": [] }`)
60 default:
61 t.Fatalf("Unexpected marker: [%s]", marker)
62 }
63 })
64
Ash Wilsonfaf006d2014-09-24 17:10:58 -040065 pages := 0
Jon Perrittef168e62014-10-08 11:14:05 -050066 options := &ListOpts{Limit: 2}
67 err := ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040068 pages++
69
70 actual, err := ExtractImages(page)
71 if err != nil {
72 return false, err
73 }
74
75 expected := []Image{
76 Image{
77 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
78 Name: "F17-x86_64-cfntools",
79 Created: "2014-09-23T12:54:52Z",
80 Updated: "2014-09-23T12:54:56Z",
81 MinDisk: 0,
82 MinRAM: 0,
83 Progress: 100,
84 Status: "ACTIVE",
Ash Wilson7ddf0362014-09-17 10:59:09 -040085 },
Ash Wilsonfaf006d2014-09-24 17:10:58 -040086 Image{
87 ID: "f90f6034-2570-4974-8351-6b49732ef2eb",
88 Name: "cirros-0.3.2-x86_64-disk",
89 Created: "2014-09-23T12:51:42Z",
90 Updated: "2014-09-23T12:51:43Z",
91 MinDisk: 0,
92 MinRAM: 0,
93 Progress: 100,
94 Status: "ACTIVE",
95 },
96 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070097
Ash Wilsonfaf006d2014-09-24 17:10:58 -040098 if !reflect.DeepEqual(expected, actual) {
99 t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual)
100 }
101
102 return false, nil
103 })
104
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700105 if err != nil {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400106 t.Fatalf("EachPage error: %v", err)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700107 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400108 if pages != 1 {
109 t.Errorf("Expected one page, got %d", pages)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700110 }
111}
Ash Wilson4b548842014-09-25 08:58:02 -0400112
113func TestGetImage(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -0500114 th.SetupHTTP()
115 defer th.TeardownHTTP()
Ash Wilson4b548842014-09-25 08:58:02 -0400116
Jon Perrittef168e62014-10-08 11:14:05 -0500117 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
118 th.TestMethod(t, r, "GET")
119 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson4b548842014-09-25 08:58:02 -0400120
121 w.Header().Add("Content-Type", "application/json")
122 fmt.Fprintf(w, `
123 {
124 "image": {
125 "status": "ACTIVE",
126 "updated": "2014-09-23T12:54:56Z",
127 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
128 "OS-EXT-IMG-SIZE:size": 476704768,
129 "name": "F17-x86_64-cfntools",
130 "created": "2014-09-23T12:54:52Z",
131 "minDisk": 0,
132 "progress": 100,
133 "minRam": 0,
134 "metadata": {}
135 }
136 }
137 `)
138 })
139
Jon Perrittef168e62014-10-08 11:14:05 -0500140 actual, err := Get(fake.ServiceClient(), "12345678").Extract()
Ash Wilson4b548842014-09-25 08:58:02 -0400141 if err != nil {
142 t.Fatalf("Unexpected error from Get: %v", err)
143 }
Ash Wilson4b548842014-09-25 08:58:02 -0400144
Ash Wilsond2f87032014-09-25 11:34:41 -0400145 expected := &Image{
Ash Wilson4b548842014-09-25 08:58:02 -0400146 Status: "ACTIVE",
147 Updated: "2014-09-23T12:54:56Z",
148 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
149 Name: "F17-x86_64-cfntools",
150 Created: "2014-09-23T12:54:52Z",
151 MinDisk: 0,
152 Progress: 100,
153 MinRAM: 0,
154 }
155
156 if !reflect.DeepEqual(expected, actual) {
157 t.Errorf("Expected %#v, but got %#v", expected, actual)
158 }
159}
Jon Perrittef168e62014-10-08 11:14:05 -0500160
161func TestNextPageURL(t *testing.T) {
162 var page ImagePage
163 var body map[string]interface{}
164 bodyString := []byte(`{"images":{"links":[{"href":"http://192.154.23.87/12345/images/image3","rel":"next"},{"href":"http://192.154.23.87/12345/images/image1","rel":"previous"}]}}`)
165 err := json.Unmarshal(bodyString, &body)
166 if err != nil {
167 t.Fatalf("Error unmarshaling data into page body: %v", err)
168 }
169 page.Body = body
170
171 expected := "http://192.154.23.87/12345/images/image3"
172 actual, err := page.NextPageURL()
173 th.AssertNoErr(t, err)
174 th.CheckEquals(t, expected, actual)
175}