summaryrefslogtreecommitdiff
path: root/heat/tests/openstack/heat/test_swiftsignal.py
blob: 64a15df1bc5fd6bec1fe388dd6b9e6b03d2bb020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import datetime
import json
from unittest import mock
import uuid

from oslo_utils import timeutils
from swiftclient import client as swiftclient_client
from swiftclient import exceptions as swiftclient_exceptions
from testtools import matchers

from heat.common import exception
from heat.common import template_format
from heat.engine.clients.os import swift
from heat.engine import node_data
from heat.engine import resource
from heat.engine import rsrc_defn
from heat.engine import scheduler
from heat.engine import stack
from heat.engine import template as templatem
from heat.tests import common
from heat.tests import utils


swiftsignal_template = '''
heat_template_version: 2013-05-23

resources:
  test_wait_condition:
    type: "OS::Heat::SwiftSignal"
    properties:
      handle: { get_resource: test_wait_condition_handle }
      timeout: 1
      count: 2

  test_wait_condition_handle:
    type: "OS::Heat::SwiftSignalHandle"
'''

swiftsignalhandle_template = '''
heat_template_version: 2013-05-23

resources:
  test_wait_condition_handle:
    type: "OS::Heat::SwiftSignalHandle"
'''

container_header = {
    'content-length': '2',
    'x-container-object-count': '0',
    'accept-ranges': 'bytes',
    'date': 'Fri, 25 Jul 2014 16:02:03 GMT',
    'x-timestamp': '1405019787.66969',
    'x-trans-id': 'tx6651b005324341f685e71-0053d27f7bdfw1',
    'x-container-bytes-used': '0',
    'content-type': 'application/json; charset=utf-8',
    'x-versions-location': 'test'
}

obj_header = {
    'content-length': '5',
    'accept-ranges': 'bytes',
    'last-modified': 'Fri, 25 Jul 2014 16:05:26 GMT',
    'etag': '5a105e8b9d40e1329780d62ea2265d8a',
    'x-timestamp': '1406304325.40094',
    'x-trans-id': 'tx2f40ff2b4daa4015917fc-0053d28045dfw1',
    'date': 'Fri, 25 Jul 2014 16:05:25 GMT',
    'content-type': 'application/octet-stream'
}


def create_stack(template, stack_id=None, cache_data=None):
    tmpl = template_format.parse(template)
    template = templatem.Template(tmpl)
    ctx = utils.dummy_context(tenant_id='test_tenant')
    st = stack.Stack(ctx, 'test_st', template,
                     disable_rollback=True, cache_data=cache_data)

    # Stub out the stack ID so we have a known value
    if stack_id is None:
        stack_id = str(uuid.uuid4())
    with utils.UUIDStub(stack_id):
        st.store()
    st.id = stack_id

    return st


def cont_index(obj_name, num_version_hist):
    objects = [{'bytes': 11,
                'last_modified': '2014-07-03T19:42:03.281640',
                'hash': '9214b4e4460fcdb9f3a369941400e71e',
                'name': "02b" + obj_name + '/1404416326.51383',
                'content_type': 'application/octet-stream'}] * num_version_hist
    objects.append({'bytes': 8,
                    'last_modified': '2014-07-03T19:42:03.849870',
                    'hash': '9ab7c0738852d7dd6a2dc0b261edc300',
                    'name': obj_name,
                    'content_type': 'application/x-www-form-urlencoded'})
    return (container_header, objects)


class SwiftSignalHandleTest(common.HeatTestCase):

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_create(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': "1234"
        }
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)
        mock_swift_object.get_object.return_value = (obj_header, '{"id": "1"}')

        st.create()
        handle = st.resources['test_wait_condition_handle']
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        regexp = ("http://fake-host.com:8080/v1/AUTH_test_tenant/%s/test_st-"
                  "test_wait_condition_handle-abcdefghijkl"
                  r"\?temp_url_sig=[0-9a-f]{40,64}&temp_url_expires=[0-9]{10}"
                  % st.id)
        res_id = st.resources['test_wait_condition_handle'].resource_id
        self.assertEqual(res_id, handle.physical_resource_name())
        self.assertThat(handle.FnGetRefId(), matchers.MatchesRegex(regexp))

        # Since the account key is mocked out above
        self.assertFalse(mock_swift_object.post_account.called)

        header = {'x-versions-location': st.id}
        self.assertEqual({'headers': header},
                         mock_swift_object.put_container.call_args[1])

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_delete_empty_container(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': "1234"
        }
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        st.create()

        exc = swiftclient_exceptions.ClientException("Object DELETE failed",
                                                     http_status=404)
        mock_swift_object.delete_object.side_effect = (None, None, None, exc)
        exc = swiftclient_exceptions.ClientException("Container DELETE failed",
                                                     http_status=404)
        mock_swift_object.delete_container.side_effect = exc
        rsrc = st.resources['test_wait_condition_handle']
        scheduler.TaskRunner(rsrc.delete)()
        self.assertEqual(('DELETE', 'COMPLETE'), rsrc.state)
        self.assertEqual(4, mock_swift_object.delete_object.call_count)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_delete_object_error(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': "1234"
        }
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        st.create()

        exc = swiftclient_exceptions.ClientException("Overlimit",
                                                     http_status=413)
        mock_swift_object.delete_object.side_effect = (None, None, None, exc)
        rsrc = st.resources['test_wait_condition_handle']
        exc = self.assertRaises(exception.ResourceFailure,
                                scheduler.TaskRunner(rsrc.delete))
        self.assertEqual('ClientException: '
                         'resources.test_wait_condition_handle: '
                         'Overlimit: 413', str(exc))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_delete_container_error(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': "1234"
        }
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        st.create()

        exc = swiftclient_exceptions.ClientException("Object DELETE failed",
                                                     http_status=404)
        mock_swift_object.delete_object.side_effect = (None, None, None, exc)

        exc = swiftclient_exceptions.ClientException("Overlimit",
                                                     http_status=413)
        mock_swift_object.delete_container.side_effect = (exc,)

        rsrc = st.resources['test_wait_condition_handle']
        exc = self.assertRaises(exception.ResourceFailure,
                                scheduler.TaskRunner(rsrc.delete))
        self.assertEqual('ClientException: '
                         'resources.test_wait_condition_handle: '
                         'Overlimit: 413', str(exc))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_delete_non_empty_container(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': "1234"
        }
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        st.create()

        exc = swiftclient_exceptions.ClientException("Object DELETE failed",
                                                     http_status=404)
        mock_swift_object.delete_object.side_effect = (None, None, None, exc)
        exc = swiftclient_exceptions.ClientException("Container DELETE failed",
                                                     http_status=409)
        mock_swift_object.delete_container.side_effect = exc
        rsrc = st.resources['test_wait_condition_handle']
        scheduler.TaskRunner(rsrc.delete)()
        self.assertEqual(('DELETE', 'COMPLETE'), rsrc.state)
        self.assertEqual(4, mock_swift_object.delete_object.call_count)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    def test_handle_update(self, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']
        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': "1234"
        }
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        st.create()
        rsrc = st.resources['test_wait_condition_handle']
        old_url = rsrc.FnGetRefId()
        update_snippet = rsrc_defn.ResourceDefinition(handle.name,
                                                      handle.type(),
                                                      handle.properties.data)
        scheduler.TaskRunner(handle.update, update_snippet)()
        self.assertEqual(old_url, rsrc.FnGetRefId())

    def test_swift_handle_refid_convergence_cache_data(self):
        cache_data = {
            'test_wait_condition_handle': node_data.NodeData.from_dict({
                'uuid': mock.ANY,
                'id': mock.ANY,
                'action': 'CREATE',
                'status': 'COMPLETE',
                'reference_id': 'convg_xyz'
            })
        }
        st = create_stack(swiftsignalhandle_template, cache_data=cache_data)
        rsrc = st.defn['test_wait_condition_handle']
        self.assertEqual('convg_xyz', rsrc.FnGetRefId())


class SwiftSignalTest(common.HeatTestCase):

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_create(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)
        mock_swift_object.get_object.return_value = (obj_header, '')

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)

    @mock.patch.object(swift.SwiftClientPlugin, 'get_signal_url')
    def test_validate_handle_url_bad_tempurl(self, mock_handle_url):
        mock_handle_url.return_value = (
            "http://fake-host.com:8080/v1/my-container/"
            "test_st-test_wait_condition_handle?temp_url_sig="
            "12d8f9f2c923fbeb555041d4ed63d83de6768e95&"
            "temp_url_expires=1404762741")
        st = create_stack(swiftsignal_template)

        st.create()
        self.assertIn('not a valid SwiftSignalHandle.  The Swift TempURL path',
                      str(st.status_reason))

    @mock.patch.object(swift.SwiftClientPlugin, 'get_signal_url')
    def test_validate_handle_url_bad_container_name(self, mock_handle_url):
        mock_handle_url.return_value = (
            "http://fake-host.com:8080/v1/AUTH_test_tenant/my-container/"
            "test_st-test_wait_condition_handle?temp_url_sig="
            "12d8f9f2c923fbeb555041d4ed63d83de6768e95&"
            "temp_url_expires=1404762741")
        st = create_stack(swiftsignal_template)

        st.create()
        self.assertIn('not a valid SwiftSignalHandle.  The container name',
                      str(st.status_reason))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_multiple_signals_same_id_complete(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)
        mock_swift_object.get_object.side_effect = (
            (obj_header, json.dumps({'id': 1})),
            (obj_header, json.dumps({'id': 1})),
            (obj_header, json.dumps({'id': 1})),

            (obj_header, json.dumps({'id': 1})),
            (obj_header, json.dumps({'id': 2})),
            (obj_header, json.dumps({'id': 3})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_multiple_signals_same_id_timeout(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)
        mock_swift_object.get_object.return_value = (obj_header,
                                                     json.dumps({'id': 1}))

        time_now = timeutils.utcnow()
        time_series = [datetime.timedelta(0, t) + time_now
                       for t in range(1, 100)]
        timeutils.set_time_override(time_series)
        self.addCleanup(timeutils.clear_time_override)

        st.create()
        self.assertIn("SwiftSignalTimeout: resources.test_wait_condition: "
                      "1 of 2 received - Signal 1 received",
                      st.status_reason)
        wc = st['test_wait_condition']
        self.assertEqual("SwiftSignalTimeout: resources.test_wait_condition: "
                         "1 of 2 received - Signal 1 received",
                         wc.status_reason)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_post_complete_to_handle(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)
        mock_swift_object.get_object.side_effect = (
            (obj_header, json.dumps({'id': 1, 'status': "SUCCESS"})),
            (obj_header, json.dumps({'id': 1, 'status': "SUCCESS"})),
            (obj_header, json.dumps({'id': 2, 'status': "SUCCESS"})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_post_failed_to_handle(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)
        mock_swift_object.get_object.side_effect = (
            # Create
            (obj_header, json.dumps({'id': 1, 'status': "FAILURE",
                                     'reason': "foo"})),
            (obj_header, json.dumps({'id': 2, 'status': "FAILURE",
                                     'reason': "bar"})),

            # SwiftSignalFailure
            (obj_header, json.dumps({'id': 1, 'status': "FAILURE",
                                     'reason': "foo"})),
            (obj_header, json.dumps({'id': 2, 'status': "FAILURE",
                                     'reason': "bar"})),
        )

        st.create()
        self.assertEqual(('CREATE', 'FAILED'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual("SwiftSignalFailure: resources.test_wait_condition: "
                         "foo;bar", wc.status_reason)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_data(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, json.dumps({'id': 1, 'data': "foo"})),
            (obj_header, json.dumps({'id': 2, 'data': "bar"})),
            (obj_header, json.dumps({'id': 3, 'data': "baz"})),

            # FnGetAtt call
            (obj_header, json.dumps({'id': 1, 'data': "foo"})),
            (obj_header, json.dumps({'id': 2, 'data': "bar"})),
            (obj_header, json.dumps({'id': 3, 'data': "baz"})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual(json.dumps({1: 'foo', 2: 'bar', 3: 'baz'}),
                         wc.FnGetAtt('data'))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_data_noid(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, json.dumps({'data': "foo", 'reason': "bar",
                                     'status': "SUCCESS"})),
            (obj_header, json.dumps({'data': "dog", 'reason': "cat",
                                     'status': "SUCCESS"})),

            # FnGetAtt call
            (obj_header, json.dumps({'data': "foo", 'reason': "bar",
                                     'status': "SUCCESS"})),
            (obj_header, json.dumps({'data': "dog", 'reason': "cat",
                                     'status': "SUCCESS"})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual(json.dumps({1: 'foo', 2: 'dog'}), wc.FnGetAtt('data'))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_data_nodata(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, ''),
            (obj_header, ''),

            # FnGetAtt call
            (obj_header, ''),
            (obj_header, ''),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual(json.dumps({1: None, 2: None}), wc.FnGetAtt('data'))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_data_partial_complete(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']
        wc = st['test_wait_condition']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)
        mock_swift_object.get_object.return_value = (
            obj_header, json.dumps({'status': 'SUCCESS'}))

        st.create()
        self.assertEqual(['SUCCESS', 'SUCCESS'], wc.get_status())
        expected = [{'status': 'SUCCESS', 'reason': 'Signal 1 received',
                     'data': None, 'id': 1},
                    {'status': 'SUCCESS', 'reason': 'Signal 2 received',
                     'data': None, 'id': 2}]
        self.assertEqual(expected, wc.get_signals())

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_get_status_none_complete(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']
        wc = st['test_wait_condition']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)
        mock_swift_object.get_object.return_value = (obj_header, '')

        st.create()
        self.assertEqual(['SUCCESS', 'SUCCESS'], wc.get_status())
        expected = [{'status': 'SUCCESS', 'reason': 'Signal 1 received',
                     'data': None, 'id': 1},
                    {'status': 'SUCCESS', 'reason': 'Signal 2 received',
                     'data': None, 'id': 2}]
        self.assertEqual(expected, wc.get_signals())

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_get_status_partial_complete(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']
        wc = st['test_wait_condition']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)
        mock_swift_object.get_object.return_value = (
            obj_header, json.dumps({'id': 1, 'status': "SUCCESS"}))

        st.create()
        self.assertEqual(['SUCCESS'], wc.get_status())
        expected = [{'status': 'SUCCESS', 'reason': 'Signal 1 received',
                     'data': None, 'id': 1}]
        self.assertEqual(expected, wc.get_signals())

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_get_status_failure(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']
        wc = st['test_wait_condition']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)
        mock_swift_object.get_object.return_value = (
            obj_header, json.dumps({'id': 1, 'status': "FAILURE"}))

        st.create()
        self.assertEqual(('CREATE', 'FAILED'), st.state)
        self.assertEqual(['FAILURE'], wc.get_status())
        expected = [{'status': 'FAILURE', 'reason': 'Signal 1 received',
                     'data': None, 'id': 1}]
        self.assertEqual(expected, wc.get_signals())

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_getatt_token(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, ''),
            (obj_header, ''),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        self.assertEqual('', handle.FnGetAtt('token'))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_getatt_endpoint(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, ''),
            (obj_header, ''),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        expected = ('http://fake-host.com:8080/v1/AUTH_test_tenant/%s/'
                    r'test_st-test_wait_condition_handle-abcdefghijkl\?temp_'
                    'url_sig=[0-9a-f]{40,64}&'
                    'temp_url_expires=[0-9]{10}') % st.id
        self.assertThat(handle.FnGetAtt('endpoint'),
                        matchers.MatchesRegex(expected))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_getatt_curl_cli(self, mock_name, mock_swift):
        st = create_stack(swiftsignalhandle_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, ''),
            (obj_header, ''),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        expected = ("curl -i -X PUT 'http://fake-host.com:8080/v1/"
                    "AUTH_test_tenant/%s/test_st-test_wait_condition_"
                    r"handle-abcdefghijkl\?temp_url_sig=[0-9a-f]{40,64}&"
                    "temp_url_expires=[0-9]{10}'") % st.id
        self.assertThat(handle.FnGetAtt('curl_cli'),
                        matchers.MatchesRegex(expected))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_invalid_json_data(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.side_effect = (
            # st create
            (obj_header, '{"status": "SUCCESS"'),
            (obj_header, '{"status": "FAI'),
        )

        st.create()
        self.assertEqual(('CREATE', 'FAILED'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual('Error: resources.test_wait_condition: '
                         'Failed to parse JSON data: {"status": '
                         '"SUCCESS"', wc.status_reason)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_unknown_status(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 1)

        mock_swift_object.get_object.return_value = (
            obj_header, '{"status": "BOO"}')

        st.create()
        self.assertEqual(('CREATE', 'FAILED'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual('Error: resources.test_wait_condition: '
                         'Unknown status: BOO', wc.status_reason)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_swift_objects_deleted(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.side_effect = (
            cont_index(obj_name, 2),  # Objects are there during create
            (container_header, []),   # The user deleted the objects
        )
        mock_swift_object.get_object.side_effect = (
            (obj_header, json.dumps({'id': 1})),  # Objects there during create
            (obj_header, json.dumps({'id': 2})),
            (obj_header, json.dumps({'id': 3})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual("null", wc.FnGetAtt('data'))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_swift_objects_invisible(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name

        mock_swift_object.get_container.side_effect = (
            (container_header, []),   # Just-created objects aren't visible yet
            (container_header, []),
            (container_header, []),
            (container_header, []),
            cont_index(obj_name, 1),
        )
        mock_swift_object.get_object.side_effect = (
            (obj_header, json.dumps({'id': 1})),
            (obj_header, json.dumps({'id': 2})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_swift_container_deleted(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.side_effect = [
            cont_index(obj_name, 2),  # Objects are there during create
            swiftclient_client.ClientException("Container GET failed",
                                               http_status=404)  # User deleted
        ]
        mock_swift_object.get_object.side_effect = (
            (obj_header, json.dumps({'id': 1})),  # Objects there during create
            (obj_header, json.dumps({'id': 2})),
            (obj_header, json.dumps({'id': 3})),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)
        wc = st['test_wait_condition']
        self.assertEqual("null", wc.FnGetAtt('data'))

    @mock.patch.object(swift.SwiftClientPlugin, '_create')
    @mock.patch.object(resource.Resource, 'physical_resource_name')
    def test_swift_get_object_404(self, mock_name, mock_swift):
        st = create_stack(swiftsignal_template)
        handle = st['test_wait_condition_handle']

        mock_swift_object = mock.Mock()
        mock_swift.return_value = mock_swift_object
        mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234"
        mock_swift_object.head_account.return_value = {
            'x-account-meta-temp-url-key': '123456'
        }
        obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name)
        mock_name.return_value = obj_name
        mock_swift_object.get_container.return_value = cont_index(obj_name, 2)
        mock_swift_object.get_object.side_effect = (
            swiftclient_client.ClientException(
                "Object %s not found" % obj_name, http_status=404),
            (obj_header, '{"id": 1}'),
            (obj_header, '{"id": 2}'),
        )

        st.create()
        self.assertEqual(('CREATE', 'COMPLETE'), st.state)