File: test_utils.py

package info (click to toggle)
rally 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,368 kB
  • sloc: python: 42,541; javascript: 487; sh: 198; makefile: 192; xml: 43
file content (659 lines) | stat: -rw-r--r-- 23,795 bytes parent folder | download
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
# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
#    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 collections
import queue as Queue
import string
import sys
import threading
import time
from unittest import mock

import ddt
import pytest

from rally.common import utils
from rally import exceptions
from tests.unit import test


class ImmutableMixinTestCase(test.TestCase):

    def test_without_base_values(self):
        im = utils.ImmutableMixin()
        self.assertRaises(AttributeError,
                          im.__setattr__, "test", "test")

    def test_with_base_values(self):

        class A(utils.ImmutableMixin):
            def __init__(self, test):
                self.test = test
                super(A, self).__init__()

        a = A("test")
        self.assertRaises(AttributeError,
                          a.__setattr__, "abc", "test")
        self.assertEqual("test", a.test)


class EnumMixinTestCase(test.TestCase):

    def test_enum_mix_in(self):

        class Foo(utils.EnumMixin):
            a = 10
            b = 20
            CC = "2000"

        self.assertEqual(set([10, 20, "2000"]), set(list(Foo())))

    def test_with_underscore(self):

        class Foo(utils.EnumMixin):
            a = 10
            b = 20
            _CC = "2000"

        self.assertEqual(set([10, 20]), set(list(Foo())))


class StdIOCaptureTestCase(test.TestCase):

    def test_stdout_capture(self):
        stdout = sys.stdout
        messages = ["abcdef", "defgaga"]
        with utils.StdOutCapture() as out:
            for msg in messages:
                print(msg)

        self.assertEqual(messages, out.getvalue().rstrip("\n").split("\n"))
        self.assertEqual(sys.stdout, stdout)

    def test_stderr_capture(self):
        stderr = sys.stderr
        messages = ["abcdef", "defgaga"]
        with utils.StdErrCapture() as err:
            for msg in messages:
                print(msg, file=sys.stderr)

        self.assertEqual(messages, err.getvalue().rstrip("\n").split("\n"))
        self.assertEqual(sys.stderr, stderr)


class TimerTestCase(test.TestCase):

    def test_timer_duration(self):
        start_time = time.time()
        end_time = time.time()

        with mock.patch("rally.common.utils.time") as mock_time:
            mock_time.time = mock.MagicMock(return_value=start_time)
            with utils.Timer() as timer:
                mock_time.time = mock.MagicMock(return_value=end_time)

        self.assertIsNone(timer.error)
        self.assertEqual(start_time, timer.timestamp())
        self.assertEqual(end_time, timer.finish_timestamp())
        self.assertEqual(end_time - start_time, timer.duration())

    def test_timer_exception(self):
        try:
            with utils.Timer() as timer:
                raise Exception()
        except Exception:
            pass
        self.assertEqual(3, len(timer.error))
        self.assertEqual(timer.error[0], type(Exception()))


class TenantIteratorTestCase(test.TestCase):

    def test_iterate_per_tenant(self):
        users = []
        tenants_count = 2
        users_per_tenant = 5
        for tenant_id in range(tenants_count):
            for user_id in range(users_per_tenant):
                users.append({"id": str(user_id),
                              "tenant_id": str(tenant_id)})

        expected_result = [
            ({"id": "0", "tenant_id": str(i)}, str(i)) for i in range(
                tenants_count)]
        real_result = [i for i in utils.iterate_per_tenants(users)]

        self.assertEqual(expected_result, real_result)


class RAMIntTestCase(test.TestCase):

    def test__int__(self):
        self.assertEqual(0, int(utils.RAMInt()))
        self.assertEqual(10, int(utils.RAMInt(10)))

    def test__str__(self):
        self.assertEqual("0", str(utils.RAMInt()))
        self.assertEqual("20", str(utils.RAMInt(20)))

    def test__next__(self):
        ri = utils.RAMInt()
        for i in range(0, 3):
            self.assertEqual(i, next(ri))

    def test_next(self):
        ri = utils.RAMInt()
        for i in range(0, 3):
            self.assertEqual(i, ri.next())

    def test_reset(self):
        ri = utils.RAMInt()
        ri.next()
        ri.reset()
        self.assertEqual(0, int(ri))


@ddt.ddt
class RandomNameTestCase(test.TestCase):

    @ddt.data(
        {},
        {"task_id": "fake-task"},
        {"task_id": "2short", "expected": "s_rally_blargles_dweebled"},
        {"task_id": "fake!task",
         "expected": "s_rally_blargles_dweebled"},
        {"fmt": "XXXX-test-XXX-test",
         "expected": "fake-test-bla-test"})
    @ddt.unpack
    @mock.patch("random.choice")
    def test_generate_random_name(self, mock_choice, task_id="faketask",
                                  expected="s_rally_faketask_blargles",
                                  fmt="s_rally_XXXXXXXX_XXXXXXXX"):
        class FakeNameGenerator(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = fmt
            task = {"uuid": task_id}

        generator = FakeNameGenerator()

        mock_choice.side_effect = iter("blarglesdweebled")
        self.assertEqual(expected, generator.generate_random_name())

        class FakeNameGenerator(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = fmt
            verification = {"uuid": task_id}

        generator = FakeNameGenerator()

        mock_choice.side_effect = iter("blarglesdweebled")
        self.assertEqual(expected, generator.generate_random_name())

    def test_generate_random_name_bogus_name_format(self):
        class FakeNameGenerator(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = "invalid_XXX_format"
            task = {"uuid": "fake-task-id"}

        generator = FakeNameGenerator()
        self.assertRaises(ValueError,
                          generator.generate_random_name)

    @ddt.data(
        {"good": ("rally_abcdefgh_abcdefgh", "rally_12345678_abcdefgh",
                  "rally_ABCdef12_ABCdef12"),
         "bad": ("rally_abcd_efgh", "rally_abcd!efg_12345678",
                 "rally_", "rally__", "rally_abcdefgh_",
                 "rally_abcdefghi_12345678", "foo", "foo_abcdefgh_abcdefgh")},
        {"task_id": "abcd1234",
         "good": ("rally_abcd1234_abcdefgh", "rally_abcd1234_abcd1234",
                  "rally_abcd1234_AbCdEf12"),
         "bad": ("rally_12345678_abcdefgh", "rally_12345678_abcd1234",
                 "rally_abcd1234_", "rally_abcd1234_!!!!!!!!",
                 "rally_ABCD1234_abcdefgh")},
        {"task_id": "abcd1234",
         "exact": False,
         "good": ("rally_abcd1234_abcdefghfoo", "rally_abcd1234_abcdefgh",
                  "rally_abcd1234_abcdefgh-bar",
                  "rally_abcd1234_abcdefgh+!@$"),
         "bad": ("rally_abcd1234_", "rally_abcd1234_!!!!!!!!",
                 "rally_abcd1234_abcdefg")},
        {"fmt": "][*_XXX_XXX",
         "chars": "abc(.*)",
         "good": ("][*_abc_abc", "][*_abc_((("),
         "bad": ("rally_ab_cd", "rally_ab!_abc", "rally_", "rally__",
                 "rally_abc_", "rally_abcd_abc", "foo", "foo_abc_abc")},
        {"fmt": "XXXX-test-XXX-test",
         "good": ("abcd-test-abc-test",),
         "bad": ("rally-abcdefgh-abcdefgh", "abc-test-abc-test",
                 "abcd_test_abc_test", "abc-test-abcd-test")})
    @ddt.unpack
    def test_cls_name_matches_object(
            self, good=(), bad=(), fmt="rally_XXXXXXXX_XXXXXXXX",
            chars=string.ascii_letters + string.digits, task_id=None,
            exact=True):
        class FakeNameGenerator(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = fmt
            RESOURCE_NAME_ALLOWED_CHARACTERS = chars
            task = {"uuid": task_id or "fakeuuid"}

        for name in good:
            self.assertTrue(
                FakeNameGenerator.name_matches_object(name, task_id, exact),
                "%(name)s unexpectedly didn't match RESOURCE_NAME_FORMAT "
                "%(fmt)s with exact=%(exact)s" %
                {"name": name, "fmt": fmt, "exact": exact})

        for name in bad:
            self.assertFalse(
                FakeNameGenerator.name_matches_object(name, task_id, exact),
                "%(name)s unexpectedly matched RESOURCE_NAME_FORMAT %(fmt)s "
                "with exact=%(exact)s" %
                {"name": name, "fmt": fmt, "exact": exact})

    def test_name_matches_object(self):
        name = "foo"
        obj = mock.Mock()
        self.assertTrue(utils.name_matches_object(name, obj))
        obj.name_matches_object.assert_called_once_with(name)

    def test_name_matches_object_kwargs(self):
        name = "foo"
        obj = mock.Mock()
        self.assertTrue(utils.name_matches_object(name, obj, task_id="taskid",
                                                  exact=False))
        obj.name_matches_object.assert_called_once_with(name, task_id="taskid",
                                                        exact=False)

    def test_name_matches_object_identical_list(self):
        class One(utils.RandomNameGeneratorMixin):
            name_matches_object = mock.Mock(return_value=False)

        class Two(utils.RandomNameGeneratorMixin):
            name_matches_object = mock.Mock(return_value=False)

        name = "foo"
        self.assertFalse(utils.name_matches_object(name, One, Two))
        # ensure that exactly one of the two objects is checked
        self.assertCountEqual(
            One.name_matches_object.call_args_list
            + Two.name_matches_object.call_args_list,
            [mock.call(name)])

    def test_name_matches_object_differing_list(self):
        class One(utils.RandomNameGeneratorMixin):
            name_matches_object = mock.Mock(return_value=False)

        class Two(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = "foo_XXX_XXX"
            name_matches_object = mock.Mock(return_value=False)

        class Three(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_ALLOWED_CHARACTERS = "12345"
            name_matches_object = mock.Mock(return_value=False)

        class Four(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = "bar_XXX_XXX"
            RESOURCE_NAME_ALLOWED_CHARACTERS = "abcdef"
            name_matches_object = mock.Mock(return_value=False)

        classes = (One, Two, Three, Four)
        name = "foo"
        self.assertFalse(utils.name_matches_object(name, *classes))
        for cls in classes:
            cls.name_matches_object.assert_called_once_with(name)

    def test_cls_name_matches_object_identity(self):
        generator = utils.RandomNameGeneratorMixin()
        generator.task = {"uuid": "faketask"}

        self.assertTrue(generator.name_matches_object(
            generator.generate_random_name()))
        self.assertTrue(utils.RandomNameGeneratorMixin.name_matches_object(
            generator.generate_random_name()))

    def test_name_matches_object_identity(self):
        generator = utils.RandomNameGeneratorMixin()
        generator.task = {"uuid": "faketask"}

        self.assertTrue(utils.name_matches_object(
            generator.generate_random_name(), generator))
        self.assertTrue(utils.name_matches_object(
            generator.generate_random_name(), utils.RandomNameGeneratorMixin))

    def test_consistent_task_id_part(self):
        class FakeNameGenerator(utils.RandomNameGeneratorMixin):
            RESOURCE_NAME_FORMAT = "XXXXXXXX_XXXXXXXX"

        generator = FakeNameGenerator()
        generator.task = {"uuid": "good-task-id"}

        names = [generator.generate_random_name() for i in range(100)]
        task_id_parts = set([n.split("_")[0] for n in names])
        self.assertEqual(1, len(task_id_parts))

        generator.task = {"uuid": "bogus! task! id!"}

        names = [generator.generate_random_name() for i in range(100)]
        task_id_parts = set([n.split("_")[0] for n in names])
        self.assertEqual(1, len(task_id_parts))

    def test_make_name_matcher(self):
        matcher = utils.make_name_matcher("foo", "bar")
        self.assertTrue(matcher.name_matches_object("foo", task_id="task"))
        self.assertTrue(matcher.name_matches_object("bar", task_id="task"))
        self.assertFalse(matcher.name_matches_object("foo1", task_id="task"))


class TimeoutThreadTestCase(test.TestCase):
    @pytest.mark.filterwarnings("ignore")
    def test_timeout_thread(self):
        """Create and kill thread by timeout.

        This single test covers 3 methods: terminate_thread, timeout_thread,
        and interruptable_sleep.

        This test is more like integrated then unit, but it is much better
        then unreadable 500 lines of mocking and checking.
        """
        queue = Queue.Queue()
        killer_thread = threading.Thread(
            target=utils.timeout_thread,
            args=(queue,),
        )
        test_thread = threading.Thread(
            target=utils.interruptable_sleep,
            args=(30, 0.01),
        )
        test_thread.start()
        start_time = time.time()
        queue.put((test_thread, start_time + 1))
        killer_thread.start()
        test_thread.join()
        end_time = time.time()
        queue.put((None, None))
        killer_thread.join()
        time_elapsed = end_time - start_time
        # NOTE(sskripnick): Killing thread with PyThreadState_SetAsyncExc
        # works with significant delay. Make sure this delay is less
        # than 10 seconds.
        self.assertLess(time_elapsed, 11,
                        "Thread killed too late (%s seconds)" % time_elapsed)


class LockedDictTestCase(test.TestCase):

    def test_init_unlock_and_update(self):
        def setitem(obj, key, value):
            obj[key] = value

        def delitem(obj, key):
            del obj[key]

        d = utils.LockedDict()
        self.assertIsInstance(d, dict)
        self.assertEqual({}, d)

        d = utils.LockedDict(foo="bar", spam={"a": ["b", {"c": "d"}]})
        self.assertEqual({"foo": "bar", "spam": {"a": ("b", {"c": "d"})}}, d)
        self.assertIsInstance(d["spam"], utils.LockedDict)
        self.assertIsInstance(d["spam"]["a"][1], utils.LockedDict)
        self.assertRaises(RuntimeError, setitem, d, 123, 456)
        self.assertRaises(RuntimeError, delitem, d, "foo")
        self.assertRaises(RuntimeError, setitem, d["spam"]["a"][1], 123, 456)
        self.assertRaises(RuntimeError, delitem, d["spam"]["a"][1], "c")
        self.assertRaises(RuntimeError, d.update, {123: 456})
        self.assertRaises(RuntimeError, d.setdefault, 123, 456)
        self.assertRaises(RuntimeError, d.pop, "foo")
        self.assertRaises(RuntimeError, d.popitem)
        self.assertRaises(RuntimeError, d.clear)
        self.assertEqual({"foo": "bar", "spam": {"a": ("b", {"c": "d"})}}, d)

        with d.unlocked():
            d["spam"] = 42
            self.assertEqual({"foo": "bar", "spam": 42}, d)
            d.clear()
            self.assertEqual({}, d)
            d.setdefault("foo", 42)
            d.update({"bar": 24})
            self.assertEqual({"foo": 42, "bar": 24}, d)
            self.assertEqual(24, d.pop("bar"))
            self.assertEqual(("foo", 42), d.popitem())
            d[123] = 456

        self.assertEqual({123: 456}, d)

        self.assertRaises(RuntimeError, setitem, d, 123, 456)
        self.assertRaises(RuntimeError, delitem, d, "foo")

    @mock.patch("rally.common.utils.copy.deepcopy")
    def test___deepcopy__(self, mock_deepcopy):
        mock_deepcopy.side_effect = lambda *args, **kw: (args, kw)
        d = utils.LockedDict(foo="bar", spam={"a": ["b", {"c": "d"}]})
        args, kw = d.__deepcopy__()
        self.assertEqual({"memo": None}, kw)
        self.assertEqual(({"foo": "bar", "spam": {"a": ("b", {"c": "d"})}},),
                         args)
        self.assertEqual(dict, type(args[0]))
        self.assertEqual(dict, type(args[0]["spam"]))
        self.assertEqual(dict, type(args[0]["spam"]["a"][1]))

        mock_deepcopy.reset_mock()
        args, kw = d.__deepcopy__("foo_memo")
        self.assertEqual(({"foo": "bar", "spam": {"a": ("b", {"c": "d"})}},),
                         args)
        self.assertEqual({"memo": "foo_memo"}, kw)


class DequeAsQueueTestCase(test.TestCase):

    def setUp(self):
        super(DequeAsQueueTestCase, self).setUp()
        self.deque = collections.deque()
        self.deque_as_queue = utils.DequeAsQueue(self.deque)

    def test_qsize(self):
        self.assertEqual(0, self.deque_as_queue.qsize())
        self.deque.append(10)
        self.assertEqual(1, self.deque_as_queue.qsize())

    def test_put(self):
        self.deque_as_queue.put(10)
        self.assertEqual(10, self.deque.popleft())

    def test_get(self):
        self.deque.append(33)
        self.assertEqual(33, self.deque_as_queue.get())

    def test_empty(self):
        self.assertFalse(self.deque_as_queue.empty())
        self.deque.append(10)
        self.assertTrue(self.deque_as_queue.empty())


class StopwatchTestCase(test.TestCase):

    @mock.patch("rally.common.utils.interruptable_sleep")
    @mock.patch("rally.common.utils.time")
    def test_stopwatch(self, mock_time, mock_interruptable_sleep):
        mock_time.time.side_effect = [0, 0, 1, 2, 3]

        sw = utils.Stopwatch()
        sw.start()
        sw.sleep(1)
        sw.sleep(2)
        sw.sleep(3)

        mock_interruptable_sleep.assert_has_calls([
            mock.call(1),
            mock.call(1),
            mock.call(1),
        ])

    @mock.patch("rally.common.utils.interruptable_sleep")
    @mock.patch("rally.common.utils.time")
    def test_no_sleep(self, mock_time, mock_interruptable_sleep):
        mock_time.time.side_effect = [0, 1]

        sw = utils.Stopwatch()
        sw.start()
        sw.sleep(1)

        self.assertFalse(mock_interruptable_sleep.called)

    @mock.patch("rally.common.utils.time")
    def test_stopwatch_with_event(self, mock_time):
        mock_time.time.side_effect = [0, 0, 1, 2, 3]
        event = mock.Mock(spec=threading.Event)()

        sw = utils.Stopwatch(stop_event=event)
        sw.start()
        sw.sleep(1)
        sw.sleep(2)
        sw.sleep(3)

        event.wait.assert_has_calls([
            mock.call(1),
            mock.call(1),
            mock.call(1),
        ])


class BackupTestCase(test.TestCase):
    def setUp(self):
        super(BackupTestCase, self).setUp()
        p = mock.patch("rally.common.utils.os.mkdir")
        self.mock_mkdir = p.start()
        self.addCleanup(p.stop)

    @mock.patch("rally.common.utils.os.path.exists")
    @mock.patch("rally.common.utils.uuid")
    def test_generate_random_path(self, mock_uuid, mock_exists):
        mock_exists.side_effect = lambda a: "exist" in a
        mock_uuid.uuid4.side_effect = ("exist", "foo")

        self.assertEqual("/some/foo", utils.generate_random_path("/some"))

        mock_exists.assert_has_calls((
            mock.call("/some/exist"),
            mock.call("/some/foo"),
        ))

    @mock.patch("rally.common.utils.generate_random_path")
    def test___init__(self, mock_generate_random_path):
        utils.BackupHelper()

        mock_generate_random_path.assert_called_once_with()
        self.mock_mkdir.assert_called_once_with(
            mock_generate_random_path.return_value)

    @mock.patch("rally.common.utils.generate_random_path")
    @mock.patch("rally.common.utils.shutil.copytree")
    def test_backup(self, mock_copytree, mock_generate_random_path):
        backup_dir = "another_dir"
        mock_generate_random_path.side_effect = ("base_tmp_dir", backup_dir)

        bh = utils.BackupHelper()

        path = "some_path"
        bh.backup(path)
        mock_copytree.assert_called_once_with(path, backup_dir, symlinks=True)
        self.assertEqual(backup_dir, bh._stored_data[path])
        mock_copytree.reset_mock()

        self.assertRaises(exceptions.RallyException, bh.backup, path)
        self.assertFalse(mock_copytree.called)

    @mock.patch("rally.common.utils.BackupHelper.rollback")
    @mock.patch("rally.common.utils.generate_random_path")
    @mock.patch("rally.common.utils.shutil.copytree")
    def test_backup_failed_while_copy(self, mock_copytree,
                                      mock_generate_random_path,
                                      mock_backup_helper_rollback):
        backup_dir = "another_dir"
        mock_generate_random_path.side_effect = ("base_tmp_dir", backup_dir)
        mock_copytree.side_effect = RuntimeError

        bh = utils.BackupHelper()

        path = "some_path"
        self.assertRaises(RuntimeError, bh.backup, path)
        mock_copytree.assert_called_once_with(path, backup_dir, symlinks=True)
        self.assertTrue(not bh._stored_data)
        mock_backup_helper_rollback.assert_called_once_with()

    @mock.patch("rally.common.utils.BackupHelper.backup")
    def test_call(self, mock_backup_helper_backup):
        path = "/some/path"

        bh = utils.BackupHelper()

        self.assertEqual(bh, bh(path))
        mock_backup_helper_backup.assert_called_once_with(path)

    @mock.patch("rally.common.utils."
                "os.path.exists", side_effect=(False, True, True))
    @mock.patch("rally.common.utils.shutil.rmtree")
    def test___del__(self, mock_rmtree, mock_exists):
        paths = {"original_path": "/tmp/backup_of_something",
                 "another_path": "/tmp/backup_of_another_thing"}
        bh = utils.BackupHelper()
        bh._stored_data = paths

        del bh

        self.assertEqual([mock.call(p) for p in paths.values()],
                         mock_rmtree.call_args_list)

    @mock.patch("rally.common.utils.os.path.exists", side_effect=(False, True))
    @mock.patch("rally.common.utils.shutil.rmtree")
    @mock.patch("rally.common.utils.shutil.copytree")
    def test_rollback(self, mock_copytree, mock_rmtree, mock_exists):

        bh = utils.BackupHelper()

        original_path = "/some/original/path"
        tmp_path = "/temporary/location/for/backup"
        path = {original_path: tmp_path}
        bh._stored_data = path

        rollback_method = mock.MagicMock()
        args = (1, 2, 3)
        kwargs = {"arg1": "value"}
        bh.add_rollback_action(rollback_method, *args, **kwargs)

        bh.rollback()

        mock_rmtree.assert_called_once_with(original_path)
        mock_copytree.assert_called_once_with(tmp_path, original_path,
                                              symlinks=True)
        self.assertTrue(not bh._stored_data)
        rollback_method.assert_called_once_with(*args, **kwargs)

    @mock.patch("rally.common.utils.BackupHelper.rollback")
    def test_context_manager(self, mock_backup_helper_rollback):
        bh = utils.BackupHelper()
        with bh:
            pass
        self.assertFalse(mock_backup_helper_rollback.called)

        bh = utils.BackupHelper()
        try:
            with bh:
                raise RuntimeError()
        except RuntimeError:
            # it is expected behaviour
            pass
        else:
            self.fail("BackupHelper context manager should not hide "
                      "an exception")

        self.assertTrue(mock_backup_helper_rollback.called)