blob: a13b08614cae87dad58a6a72dfc0e0dccefb8781 [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,
41 "minRam": 0,
42 "metadata": {}
43 },
44 {
45 "status": "ACTIVE",
46 "updated": "2014-09-23T12:51:43Z",
47 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
48 "OS-EXT-IMG-SIZE:size": 13167616,
49 "name": "cirros-0.3.2-x86_64-disk",
50 "created": "2014-09-23T12:51:42Z",
51 "minDisk": 0,
52 "progress": 100,
53 "minRam": 0,
54 "metadata": {}
55 }
56 ]
57 }
58 `)
59 case "2":
60 fmt.Fprintf(w, `{ "images": [] }`)
61 default:
62 t.Fatalf("Unexpected marker: [%s]", marker)
63 }
64 })
65
Ash Wilsonfaf006d2014-09-24 17:10:58 -040066 pages := 0
jrperritt3d966162016-06-06 14:08:54 -050067 options := &images.ListOpts{Limit: 2}
68 err := images.ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040069 pages++
70
jrperritt3d966162016-06-06 14:08:54 -050071 actual, err := images.ExtractImages(page)
Ash Wilsonfaf006d2014-09-24 17:10:58 -040072 if err != nil {
73 return false, err
74 }
75
jrperritt3d966162016-06-06 14:08:54 -050076 expected := []images.Image{
77 {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040078 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
79 Name: "F17-x86_64-cfntools",
80 Created: "2014-09-23T12:54:52Z",
81 Updated: "2014-09-23T12:54:56Z",
82 MinDisk: 0,
83 MinRAM: 0,
84 Progress: 100,
85 Status: "ACTIVE",
Ash Wilson7ddf0362014-09-17 10:59:09 -040086 },
jrperritt3d966162016-06-06 14:08:54 -050087 {
Ash Wilsonfaf006d2014-09-24 17:10:58 -040088 ID: "f90f6034-2570-4974-8351-6b49732ef2eb",
89 Name: "cirros-0.3.2-x86_64-disk",
90 Created: "2014-09-23T12:51:42Z",
91 Updated: "2014-09-23T12:51:43Z",
92 MinDisk: 0,
93 MinRAM: 0,
94 Progress: 100,
95 Status: "ACTIVE",
96 },
97 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070098
Ash Wilsonfaf006d2014-09-24 17:10:58 -040099 if !reflect.DeepEqual(expected, actual) {
100 t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual)
101 }
102
103 return false, nil
104 })
105
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700106 if err != nil {
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400107 t.Fatalf("EachPage error: %v", err)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700108 }
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400109 if pages != 1 {
110 t.Errorf("Expected one page, got %d", pages)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700111 }
112}
Ash Wilson4b548842014-09-25 08:58:02 -0400113
114func TestGetImage(t *testing.T) {
Jon Perrittef168e62014-10-08 11:14:05 -0500115 th.SetupHTTP()
116 defer th.TeardownHTTP()
Ash Wilson4b548842014-09-25 08:58:02 -0400117
Jon Perrittef168e62014-10-08 11:14:05 -0500118 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
119 th.TestMethod(t, r, "GET")
120 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson4b548842014-09-25 08:58:02 -0400121
122 w.Header().Add("Content-Type", "application/json")
123 fmt.Fprintf(w, `
124 {
125 "image": {
126 "status": "ACTIVE",
127 "updated": "2014-09-23T12:54:56Z",
128 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
129 "OS-EXT-IMG-SIZE:size": 476704768,
130 "name": "F17-x86_64-cfntools",
131 "created": "2014-09-23T12:54:52Z",
132 "minDisk": 0,
133 "progress": 100,
134 "minRam": 0,
135 "metadata": {}
136 }
137 }
138 `)
139 })
140
jrperritt3d966162016-06-06 14:08:54 -0500141 actual, err := images.Get(fake.ServiceClient(), "12345678").Extract()
Ash Wilson4b548842014-09-25 08:58:02 -0400142 if err != nil {
143 t.Fatalf("Unexpected error from Get: %v", err)
144 }
Ash Wilson4b548842014-09-25 08:58:02 -0400145
jrperritt3d966162016-06-06 14:08:54 -0500146 expected := &images.Image{
Ash Wilson4b548842014-09-25 08:58:02 -0400147 Status: "ACTIVE",
148 Updated: "2014-09-23T12:54:56Z",
149 ID: "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
150 Name: "F17-x86_64-cfntools",
151 Created: "2014-09-23T12:54:52Z",
152 MinDisk: 0,
153 Progress: 100,
154 MinRAM: 0,
155 }
156
157 if !reflect.DeepEqual(expected, actual) {
158 t.Errorf("Expected %#v, but got %#v", expected, actual)
159 }
160}
Jon Perrittef168e62014-10-08 11:14:05 -0500161
162func TestNextPageURL(t *testing.T) {
jrperritt3d966162016-06-06 14:08:54 -0500163 var page images.ImagePage
Jon Perrittef168e62014-10-08 11:14:05 -0500164 var body map[string]interface{}
Jon Perrittb8764b62014-10-09 14:55:59 -0500165 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 -0500166 err := json.Unmarshal(bodyString, &body)
167 if err != nil {
168 t.Fatalf("Error unmarshaling data into page body: %v", err)
169 }
170 page.Body = body
171
Jon Perrittb8764b62014-10-09 14:55:59 -0500172 expected := "http://192.154.23.87/12345/images/image4"
Jon Perrittef168e62014-10-08 11:14:05 -0500173 actual, err := page.NextPageURL()
174 th.AssertNoErr(t, err)
175 th.CheckEquals(t, expected, actual)
176}
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700177
178// Test Image delete
179func TestDeleteImage(t *testing.T) {
180 th.SetupHTTP()
181 defer th.TeardownHTTP()
182
183 th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
184 th.TestMethod(t, r, "DELETE")
185 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
186
187 w.WriteHeader(http.StatusNoContent)
188 })
189
jrperritt3d966162016-06-06 14:08:54 -0500190 res := images.Delete(fake.ServiceClient(), "12345678")
Jesse Nelson8c1e0372015-04-16 10:54:40 -0700191 th.AssertNoErr(t, res.Err)
192}