1package storage
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5
6import (
7	"time"
8
9	chk "gopkg.in/check.v1"
10)
11
12type StorageQueueSuite struct{}
13
14var _ = chk.Suite(&StorageQueueSuite{})
15
16func getQueueClient(c *chk.C) *QueueServiceClient {
17	cli := getBasicClient(c).GetQueueService()
18	return &cli
19}
20
21func (s *StorageQueueSuite) Test_pathForQueue(c *chk.C) {
22	c.Assert(getQueueClient(c).
23		GetQueueReference("q").
24		buildPath(), chk.Equals, "/q")
25}
26
27func (s *StorageQueueSuite) Test_pathForQueueMessages(c *chk.C) {
28	c.Assert(getQueueClient(c).
29		GetQueueReference("q").
30		buildPathMessages(), chk.Equals, "/q/messages")
31}
32
33func (s *StorageQueueSuite) TestCreateQueue_DeleteQueue(c *chk.C) {
34	cli := getQueueClient(c)
35	rec := cli.client.appendRecorder(c)
36	defer rec.Stop()
37
38	q := cli.GetQueueReference(queueName(c))
39	c.Assert(q.Create(nil), chk.IsNil)
40	c.Assert(q.Delete(nil), chk.IsNil)
41}
42
43func (s *StorageQueueSuite) Test_GetMetadata_GetApproximateCount(c *chk.C) {
44	cli := getQueueClient(c)
45	rec := cli.client.appendRecorder(c)
46	defer rec.Stop()
47
48	queue1 := cli.GetQueueReference(queueName(c, "1"))
49	c.Assert(queue1.Create(nil), chk.IsNil)
50	defer queue1.Delete(nil)
51
52	err := queue1.GetMetadata(nil)
53	c.Assert(err, chk.IsNil)
54	c.Assert(queue1.AproxMessageCount, chk.Equals, uint64(0))
55
56	queue2 := cli.GetQueueReference(queueName(c, "2"))
57	c.Assert(queue2.Create(nil), chk.IsNil)
58	defer queue2.Delete(nil)
59	for ix := 0; ix < 3; ix++ {
60		msg := queue2.GetMessageReference("lolrofl")
61		err = msg.Put(nil)
62		c.Assert(err, chk.IsNil)
63	}
64	time.Sleep(1 * time.Second)
65
66	err = queue2.GetMetadata(nil)
67	c.Assert(err, chk.IsNil)
68	c.Assert(queue2.AproxMessageCount, chk.Equals, uint64(3))
69}
70
71func (s *StorageQueueSuite) Test_SetMetadataGetMetadata_Roundtrips(c *chk.C) {
72	cli := getQueueClient(c)
73	rec := cli.client.appendRecorder(c)
74	defer rec.Stop()
75
76	queue1 := cli.GetQueueReference(queueName(c, "1"))
77	c.Assert(queue1.Create(nil), chk.IsNil)
78	defer queue1.Delete(nil)
79
80	metadata := make(map[string]string)
81	metadata["Lol1"] = "rofl1"
82	metadata["lolBaz"] = "rofl"
83	queue1.Metadata = metadata
84	err := queue1.SetMetadata(nil)
85	c.Assert(err, chk.IsNil)
86
87	err = queue1.GetMetadata(nil)
88	c.Assert(err, chk.IsNil)
89	c.Assert(queue1.Metadata["lol1"], chk.Equals, metadata["Lol1"])
90	c.Assert(queue1.Metadata["lolbaz"], chk.Equals, metadata["lolBaz"])
91}
92
93func (s *StorageQueueSuite) TestQueueExists(c *chk.C) {
94	cli := getQueueClient(c)
95	rec := cli.client.appendRecorder(c)
96	defer rec.Stop()
97
98	queue1 := cli.GetQueueReference(queueName(c, "nonexistent"))
99	ok, err := queue1.Exists()
100	c.Assert(err, chk.IsNil)
101	c.Assert(ok, chk.Equals, false)
102
103	queue2 := cli.GetQueueReference(queueName(c, "exisiting"))
104	c.Assert(queue2.Create(nil), chk.IsNil)
105	defer queue2.Delete(nil)
106
107	ok, err = queue2.Exists()
108	c.Assert(err, chk.IsNil)
109	c.Assert(ok, chk.Equals, true)
110}
111
112func (s *StorageQueueSuite) TestGetMessages(c *chk.C) {
113	cli := getQueueClient(c)
114	rec := cli.client.appendRecorder(c)
115	defer rec.Stop()
116
117	queue := cli.GetQueueReference(queueName(c))
118	c.Assert(queue.Create(nil), chk.IsNil)
119	defer queue.Delete(nil)
120
121	msg := queue.GetMessageReference("message")
122	n := 4
123	for i := 0; i < n; i++ {
124		c.Assert(msg.Put(nil), chk.IsNil)
125	}
126
127	list, err := queue.GetMessages(&GetMessagesOptions{NumOfMessages: n})
128	c.Assert(err, chk.IsNil)
129	c.Assert(len(list), chk.Equals, n)
130}
131
132func (s *StorageQueueSuite) TestDeleteMessages(c *chk.C) {
133	cli := getQueueClient(c)
134	rec := cli.client.appendRecorder(c)
135	defer rec.Stop()
136
137	queue := cli.GetQueueReference(queueName(c))
138	c.Assert(queue.Create(nil), chk.IsNil)
139	defer queue.Delete(nil)
140
141	msg := queue.GetMessageReference("message")
142	c.Assert(msg.Put(nil), chk.IsNil)
143	list, err := queue.GetMessages(&GetMessagesOptions{VisibilityTimeout: 1})
144	c.Assert(err, chk.IsNil)
145	c.Assert(len(list), chk.Equals, 1)
146	msg = &(list[0])
147	c.Assert(msg.Delete(nil), chk.IsNil)
148}
149
150func queueName(c *chk.C, extras ...string) string {
151	// 63 is the max len for shares
152	return nameGenerator(63, "queue-", alphanum, c, extras)
153}
154
155func (s *StorageQueueSuite) Test_SetPermissionsAllTrueNoTimeout(c *chk.C) {
156	cli := getQueueClient(c)
157	rec := cli.client.appendRecorder(c)
158	defer rec.Stop()
159
160	queue1 := cli.GetQueueReference(queueName(c, "1"))
161	c.Assert(queue1.Create(nil), chk.IsNil)
162	defer queue1.Delete(nil)
163
164	perms := QueuePermissions{}
165	qapd := QueueAccessPolicy{
166		ID:         "GolangRocksOnAzure",
167		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
168		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
169		CanRead:    true,
170		CanAdd:     true,
171		CanUpdate:  true,
172		CanProcess: true,
173	}
174	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
175	err := queue1.SetPermissions(perms, nil)
176	c.Assert(err, chk.IsNil)
177}
178
179func (s *StorageQueueSuite) Test_SetPermissionsAllTrueWithTimeout(c *chk.C) {
180	cli := getQueueClient(c)
181	rec := cli.client.appendRecorder(c)
182	defer rec.Stop()
183
184	queue1 := cli.GetQueueReference(queueName(c, "1"))
185	c.Assert(queue1.Create(nil), chk.IsNil)
186	defer queue1.Delete(nil)
187
188	perms := QueuePermissions{}
189	qapd := QueueAccessPolicy{
190		ID:         "GolangRocksOnAzure",
191		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
192		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
193		CanRead:    true,
194		CanAdd:     true,
195		CanUpdate:  true,
196		CanProcess: true,
197	}
198	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
199
200	options := SetQueuePermissionOptions{Timeout: 30}
201	err := queue1.SetPermissions(perms, &options)
202	c.Assert(err, chk.IsNil)
203
204}
205
206func (s *StorageQueueSuite) Test_SetPermissionsAlternateTrueNoTimeout(c *chk.C) {
207	cli := getQueueClient(c)
208	rec := cli.client.appendRecorder(c)
209	defer rec.Stop()
210
211	queue1 := cli.GetQueueReference(queueName(c, "1"))
212	c.Assert(queue1.Create(nil), chk.IsNil)
213	defer queue1.Delete(nil)
214
215	perms := QueuePermissions{}
216	qapd := QueueAccessPolicy{
217		ID:         "GolangRocksOnAzure",
218		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
219		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
220		CanRead:    true,
221		CanAdd:     false,
222		CanUpdate:  true,
223		CanProcess: false,
224	}
225	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
226	err := queue1.SetPermissions(perms, nil)
227	c.Assert(err, chk.IsNil)
228}
229
230func (s *StorageQueueSuite) Test_SetPermissionsAlternateTrueWithTimeout(c *chk.C) {
231	cli := getQueueClient(c)
232	rec := cli.client.appendRecorder(c)
233	defer rec.Stop()
234
235	queue1 := cli.GetQueueReference(queueName(c, "1"))
236	c.Assert(queue1.Create(nil), chk.IsNil)
237	defer queue1.Delete(nil)
238
239	perms := QueuePermissions{}
240	qapd := QueueAccessPolicy{
241		ID:         "GolangRocksOnAzure",
242		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
243		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.FixedZone("GMT", -6)),
244		CanRead:    true,
245		CanAdd:     false,
246		CanUpdate:  true,
247		CanProcess: false,
248	}
249	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
250
251	options := SetQueuePermissionOptions{Timeout: 30}
252	err := queue1.SetPermissions(perms, &options)
253	c.Assert(err, chk.IsNil)
254}
255
256func (s *StorageQueueSuite) Test_GetPermissionsAllTrueNoTimeout(c *chk.C) {
257	cli := getQueueClient(c)
258	rec := cli.client.appendRecorder(c)
259	defer rec.Stop()
260
261	queue1 := cli.GetQueueReference(queueName(c, "1"))
262	c.Assert(queue1.Create(nil), chk.IsNil)
263	defer queue1.Delete(nil)
264
265	perms := QueuePermissions{}
266	qapd := QueueAccessPolicy{
267		ID:         "GolangRocksOnAzure",
268		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.UTC),
269		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.UTC),
270		CanRead:    true,
271		CanAdd:     true,
272		CanUpdate:  true,
273		CanProcess: true,
274	}
275	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
276	err := queue1.SetPermissions(perms, nil)
277	c.Assert(err, chk.IsNil)
278
279	returnedPerms, err := queue1.GetPermissions(nil)
280	c.Assert(err, chk.IsNil)
281	c.Assert(returnedPerms.AccessPolicies, chk.HasLen, 1)
282
283	c.Assert(returnedPerms.AccessPolicies[0].CanRead, chk.Equals, true)
284	c.Assert(returnedPerms.AccessPolicies[0].CanAdd, chk.Equals, true)
285	c.Assert(returnedPerms.AccessPolicies[0].CanUpdate, chk.Equals, true)
286	c.Assert(returnedPerms.AccessPolicies[0].CanProcess, chk.Equals, true)
287	c.Assert(returnedPerms.AccessPolicies[0].ID, chk.Equals, "GolangRocksOnAzure")
288	c.Assert(returnedPerms.AccessPolicies[0].StartTime, chk.Equals, qapd.StartTime)
289	c.Assert(returnedPerms.AccessPolicies[0].ExpiryTime, chk.Equals, qapd.ExpiryTime)
290}
291
292func (s *StorageQueueSuite) Test_GetPermissionsAllTrueWithTimeout(c *chk.C) {
293	cli := getQueueClient(c)
294	rec := cli.client.appendRecorder(c)
295	defer rec.Stop()
296
297	queue1 := cli.GetQueueReference(queueName(c, "1"))
298	c.Assert(queue1.Create(nil), chk.IsNil)
299	defer queue1.Delete(nil)
300
301	perms := QueuePermissions{}
302	qapd := QueueAccessPolicy{
303		ID:         "GolangRocksOnAzure",
304		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.UTC),
305		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.UTC),
306		CanRead:    true,
307		CanAdd:     true,
308		CanUpdate:  true,
309		CanProcess: true,
310	}
311	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
312	err := queue1.SetPermissions(perms, nil)
313	c.Assert(err, chk.IsNil)
314
315	options := GetQueuePermissionOptions{Timeout: 30}
316	returnedPerms, err := queue1.GetPermissions(&options)
317	c.Assert(err, chk.IsNil)
318	c.Assert(returnedPerms.AccessPolicies, chk.HasLen, 1)
319
320	c.Assert(returnedPerms.AccessPolicies[0].CanRead, chk.Equals, true)
321	c.Assert(returnedPerms.AccessPolicies[0].CanAdd, chk.Equals, true)
322	c.Assert(returnedPerms.AccessPolicies[0].CanUpdate, chk.Equals, true)
323	c.Assert(returnedPerms.AccessPolicies[0].CanProcess, chk.Equals, true)
324	c.Assert(returnedPerms.AccessPolicies[0].ID, chk.Equals, "GolangRocksOnAzure")
325	c.Assert(returnedPerms.AccessPolicies[0].StartTime, chk.Equals, qapd.StartTime)
326	c.Assert(returnedPerms.AccessPolicies[0].ExpiryTime, chk.Equals, qapd.ExpiryTime)
327
328}
329
330func (s *StorageQueueSuite) Test_GetPermissionsAlternateTrueNoTimeout(c *chk.C) {
331	cli := getQueueClient(c)
332	rec := cli.client.appendRecorder(c)
333	defer rec.Stop()
334
335	queue1 := cli.GetQueueReference(queueName(c, "1"))
336	c.Assert(queue1.Create(nil), chk.IsNil)
337	defer queue1.Delete(nil)
338
339	perms := QueuePermissions{}
340	qapd := QueueAccessPolicy{
341		ID:         "GolangRocksOnAzure",
342		StartTime:  time.Date(2050, time.December, 20, 21, 55, 0, 0, time.UTC),
343		ExpiryTime: time.Date(2051, time.December, 20, 21, 55, 0, 0, time.UTC),
344		CanRead:    true,
345		CanAdd:     false,
346		CanUpdate:  true,
347		CanProcess: false,
348	}
349	perms.AccessPolicies = append(perms.AccessPolicies, qapd)
350	err := queue1.SetPermissions(perms, nil)
351	c.Assert(err, chk.IsNil)
352
353	returnedPerms, err := queue1.GetPermissions(nil)
354	c.Assert(err, chk.IsNil)
355	c.Assert(returnedPerms.AccessPolicies, chk.HasLen, 1)
356
357	c.Assert(returnedPerms.AccessPolicies[0].CanRead, chk.Equals, true)
358	c.Assert(returnedPerms.AccessPolicies[0].CanAdd, chk.Equals, false)
359	c.Assert(returnedPerms.AccessPolicies[0].CanUpdate, chk.Equals, true)
360	c.Assert(returnedPerms.AccessPolicies[0].CanProcess, chk.Equals, false)
361	c.Assert(returnedPerms.AccessPolicies[0].ID, chk.Equals, "GolangRocksOnAzure")
362	c.Assert(returnedPerms.AccessPolicies[0].StartTime, chk.Equals, qapd.StartTime)
363	c.Assert(returnedPerms.AccessPolicies[0].ExpiryTime, chk.Equals, qapd.ExpiryTime)
364}
365