blob: 04624c993786f5942b4e2f53b72d5acb351ad799 [file] [log] [blame]
jrperrittc5c590a2016-11-04 14:41:15 -05001package testing
2
3import (
4 "testing"
5 "time"
6
7 "github.com/gophercloud/gophercloud/openstack/imageservice/v2/members"
8 "github.com/gophercloud/gophercloud/pagination"
9 th "github.com/gophercloud/gophercloud/testhelper"
10 fakeclient "github.com/gophercloud/gophercloud/testhelper/client"
11)
12
13const createdAtString = "2013-09-20T19:22:19Z"
14const updatedAtString = "2013-09-20T19:25:31Z"
15
16func TestCreateMemberSuccessfully(t *testing.T) {
17 th.SetupHTTP()
18 defer th.TeardownHTTP()
19
20 HandleCreateImageMemberSuccessfully(t)
21 im, err := members.Create(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
22 "8989447062e04a818baf9e073fd04fa7").Extract()
23 th.AssertNoErr(t, err)
24
25 createdAt, err := time.Parse(time.RFC3339, createdAtString)
26 th.AssertNoErr(t, err)
27
28 updatedAt, err := time.Parse(time.RFC3339, updatedAtString)
29 th.AssertNoErr(t, err)
30
31 th.AssertDeepEquals(t, members.Member{
32 CreatedAt: createdAt,
33 ImageID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
34 MemberID: "8989447062e04a818baf9e073fd04fa7",
35 Schema: "/v2/schemas/member",
36 Status: "pending",
37 UpdatedAt: updatedAt,
38 }, *im)
39
40}
41
42func TestMemberListSuccessfully(t *testing.T) {
43 th.SetupHTTP()
44 defer th.TeardownHTTP()
45
46 HandleImageMemberList(t)
47
48 pager := members.List(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea")
49 t.Logf("Pager state %v", pager)
50 count, pages := 0, 0
51 err := pager.EachPage(func(page pagination.Page) (bool, error) {
52 pages++
53 t.Logf("Page %v", page)
54 members, err := members.ExtractMembers(page)
55 if err != nil {
56 return false, err
57 }
58
59 for _, i := range members {
60 t.Logf("%s\t%s\t%s\t%s\t\n", i.ImageID, i.MemberID, i.Status, i.Schema)
61 count++
62 }
63
64 return true, nil
65 })
66
67 th.AssertNoErr(t, err)
68 th.AssertEquals(t, 1, pages)
69 th.AssertEquals(t, 2, count)
70}
71
72func TestMemberListEmpty(t *testing.T) {
73 th.SetupHTTP()
74 defer th.TeardownHTTP()
75
76 HandleImageMemberEmptyList(t)
77
78 pager := members.List(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea")
79 t.Logf("Pager state %v", pager)
80 count, pages := 0, 0
81 err := pager.EachPage(func(page pagination.Page) (bool, error) {
82 pages++
83 t.Logf("Page %v", page)
84 members, err := members.ExtractMembers(page)
85 if err != nil {
86 return false, err
87 }
88
89 for _, i := range members {
90 t.Logf("%s\t%s\t%s\t%s\t\n", i.ImageID, i.MemberID, i.Status, i.Schema)
91 count++
92 }
93
94 return true, nil
95 })
96
97 th.AssertNoErr(t, err)
98 th.AssertEquals(t, 0, pages)
99 th.AssertEquals(t, 0, count)
100}
101
102func TestShowMemberDetails(t *testing.T) {
103 th.SetupHTTP()
104 defer th.TeardownHTTP()
105
106 HandleImageMemberDetails(t)
107 md, err := members.Get(fakeclient.ServiceClient(),
108 "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
109 "8989447062e04a818baf9e073fd04fa7").Extract()
110
111 th.AssertNoErr(t, err)
112 if md == nil {
113 t.Errorf("Expected non-nil value for md")
114 }
115
116 createdAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
117 th.AssertNoErr(t, err)
118
119 updatedAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
120 th.AssertNoErr(t, err)
121
122 th.AssertDeepEquals(t, members.Member{
123 CreatedAt: createdAt,
124 ImageID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
125 MemberID: "8989447062e04a818baf9e073fd04fa7",
126 Schema: "/v2/schemas/member",
127 Status: "pending",
128 UpdatedAt: updatedAt,
129 }, *md)
130}
131
132func TestDeleteMember(t *testing.T) {
133 th.SetupHTTP()
134 defer th.TeardownHTTP()
135
136 counter := HandleImageMemberDeleteSuccessfully(t)
137
138 result := members.Delete(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
139 "8989447062e04a818baf9e073fd04fa7")
140 th.AssertEquals(t, 1, counter.Counter)
141 th.AssertNoErr(t, result.Err)
142}
143
144func TestMemberUpdateSuccessfully(t *testing.T) {
145 th.SetupHTTP()
146 defer th.TeardownHTTP()
147
148 counter := HandleImageMemberUpdate(t)
149 im, err := members.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
150 "8989447062e04a818baf9e073fd04fa7",
151 members.UpdateOpts{
152 Status: "accepted",
153 }).Extract()
154 th.AssertEquals(t, 1, counter.Counter)
155 th.AssertNoErr(t, err)
156
157 createdAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
158 th.AssertNoErr(t, err)
159
160 updatedAt, err := time.Parse(time.RFC3339, "2013-11-26T07:21:21Z")
161 th.AssertNoErr(t, err)
162
163 th.AssertDeepEquals(t, members.Member{
164 CreatedAt: createdAt,
165 ImageID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
166 MemberID: "8989447062e04a818baf9e073fd04fa7",
167 Schema: "/v2/schemas/member",
168 Status: "accepted",
169 UpdatedAt: updatedAt,
170 }, *im)
171
172}