blob: 396c21fcdb5bac322ae58711ea8984adb594dc72 [file] [log] [blame]
Samuel A. Falvo II808bb632014-03-12 00:07:50 -07001package images
2
3import (
Ash Wilsonfaf006d2014-09-24 17:10:58 -04004 "fmt"
5 "net/http"
6 "reflect"
Jon Perritt30558642014-04-14 17:07:12 -05007 "testing"
Ash Wilsonfaf006d2014-09-24 17:10:58 -04008
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/pagination"
11 "github.com/rackspace/gophercloud/testhelper"
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070012)
13
Ash Wilsonfaf006d2014-09-24 17:10:58 -040014const tokenID = "aaaaaa"
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070015
Ash Wilsonfaf006d2014-09-24 17:10:58 -040016func serviceClient() *gophercloud.ServiceClient {
17 return &gophercloud.ServiceClient{
18 Provider: &gophercloud.ProviderClient{TokenID: tokenID},
19 Endpoint: testhelper.Endpoint(),
20 }
21}
22
23func TestListImages(t *testing.T) {
24 testhelper.SetupHTTP()
25 defer testhelper.TeardownHTTP()
26
27 testhelper.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) {
28 testhelper.TestMethod(t, r, "GET")
29 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
30
31 w.Header().Add("Content-Type", "application/json")
32 r.ParseForm()
33 marker := r.Form.Get("marker")
34 switch marker {
35 case "":
36 fmt.Fprintf(w, `
37 {
38 "images": [
39 {
40 "status": "ACTIVE",
41 "updated": "2014-09-23T12:54:56Z",
42 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
43 "OS-EXT-IMG-SIZE:size": 476704768,
44 "name": "F17-x86_64-cfntools",
45 "created": "2014-09-23T12:54:52Z",
46 "minDisk": 0,
47 "progress": 100,
48 "minRam": 0,
49 "metadata": {}
50 },
51 {
52 "status": "ACTIVE",
53 "updated": "2014-09-23T12:51:43Z",
54 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
55 "OS-EXT-IMG-SIZE:size": 13167616,
56 "name": "cirros-0.3.2-x86_64-disk",
57 "created": "2014-09-23T12:51:42Z",
58 "minDisk": 0,
59 "progress": 100,
60 "minRam": 0,
61 "metadata": {}
62 }
63 ]
64 }
65 `)
66 case "2":
67 fmt.Fprintf(w, `{ "images": [] }`)
68 default:
69 t.Fatalf("Unexpected marker: [%s]", marker)
70 }
71 })
72
73 client := serviceClient()
74 pages := 0
75 err := List(client).EachPage(func(page pagination.Page) (bool, error) {
76 pages++
77
78 actual, err := ExtractImages(page)
79 if err != nil {
80 return false, err
81 }
82
83 expected := []Image{
84 Image{
85 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",
Ash Wilson7ddf0362014-09-17 10:59:09 -040093 },
Ash Wilsonfaf006d2014-09-24 17:10:58 -040094 Image{
95 ID: "f90f6034-2570-4974-8351-6b49732ef2eb",
96 Name: "cirros-0.3.2-x86_64-disk",
97 Created: "2014-09-23T12:51:42Z",
98 Updated: "2014-09-23T12:51:43Z",
99 MinDisk: 0,
100 MinRAM: 0,
101 Progress: 100,
102 Status: "ACTIVE",
103 },
104 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700105
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400106 if !reflect.DeepEqual(expected, actual) {
107 t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual)
108 }
109
110 return false, nil
111 })
112
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700113 if err != nil {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400114 t.Fatalf("EachPage error: %v", err)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700115 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400116 if pages != 1 {
117 t.Errorf("Expected one page, got %d", pages)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700118 }
119}
Ash Wilson4b548842014-09-25 08:58:02 -0400120
121func TestGetImage(t *testing.T) {
122 testhelper.SetupHTTP()
123 defer testhelper.TeardownHTTP()
124
125 testhelper.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
126 testhelper.TestMethod(t, r, "GET")
127 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
128
129 w.Header().Add("Content-Type", "application/json")
130 fmt.Fprintf(w, `
131 {
132 "image": {
133 "status": "ACTIVE",
134 "updated": "2014-09-23T12:54:56Z",
135 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
136 "OS-EXT-IMG-SIZE:size": 476704768,
137 "name": "F17-x86_64-cfntools",
138 "created": "2014-09-23T12:54:52Z",
139 "minDisk": 0,
140 "progress": 100,
141 "minRam": 0,
142 "metadata": {}
143 }
144 }
145 `)
146 })
147
148 client := serviceClient()
Ash Wilsond2f87032014-09-25 11:34:41 -0400149 actual, err := Get(client, "12345678").Extract()
Ash Wilson4b548842014-09-25 08:58:02 -0400150 if err != nil {
151 t.Fatalf("Unexpected error from Get: %v", err)
152 }
Ash Wilson4b548842014-09-25 08:58:02 -0400153
Ash Wilsond2f87032014-09-25 11:34:41 -0400154 expected := &Image{
Ash Wilson4b548842014-09-25 08:58:02 -0400155 Status: "ACTIVE",
156 Updated: "2014-09-23T12:54:56Z",
157 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
158 Name: "F17-x86_64-cfntools",
159 Created: "2014-09-23T12:54:52Z",
160 MinDisk: 0,
161 Progress: 100,
162 MinRAM: 0,
163 }
164
165 if !reflect.DeepEqual(expected, actual) {
166 t.Errorf("Expected %#v, but got %#v", expected, actual)
167 }
168}