~larryprice/+junk/libertine-with-platform

« back to all changes in this revision

Viewing changes to tests/unit/service/tasks/test_destroy_task.py

  • Committer: Larry Price
  • Date: 2016-11-21 17:04:35 UTC
  • mfrom: (302.2.32 devel)
  • Revision ID: larry.price@canonical.com-20161121170435-aba347gd2irs69l2
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2016 Canonical Ltd.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify it
 
4
# under the terms of the GNU General Public License version 3, as published
 
5
# by the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful, but
 
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
10
# PURPOSE.  See the GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License along
 
13
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 
 
15
 
 
16
import unittest.mock
 
17
from unittest import TestCase
 
18
from libertine.service import tasks
 
19
from libertine.ContainersConfig import ContainersConfig
 
20
 
 
21
 
 
22
class TestDestroyTask(TestCase):
 
23
    def setUp(self):
 
24
        self.config      = unittest.mock.create_autospec(ContainersConfig)
 
25
        self.connection  = unittest.mock.Mock()
 
26
        self.lock      = unittest.mock.MagicMock()
 
27
        self.called_with = None
 
28
 
 
29
    def callback(self, task):
 
30
        self.called_with = task
 
31
 
 
32
    def test_sends_error_on_non_ready_container(self):
 
33
        self.config._get_value_by_key.return_value = ''
 
34
        with unittest.mock.patch('libertine.service.tasks.base_task.libertine.service.progress.Progress') as MockProgress:
 
35
            progress = MockProgress.return_value
 
36
            progress.done = False
 
37
            task = tasks.DestroyTask('palpatine', self.config, self.lock, self.connection, self.callback)
 
38
            task._instant_callback = True
 
39
 
 
40
            with unittest.mock.patch('libertine.service.tasks.destroy_task.LibertineContainer') as MockContainer:
 
41
                MockContainer.return_value.destroy_libertine_container.return_value = True
 
42
                task.start().join()
 
43
 
 
44
            progress.error.assert_called_once_with('Container \'palpatine\' does not exist')
 
45
            self.assertEqual(task, self.called_with)
 
46
 
 
47
    def test_sends_error_on_failed_destroy(self):
 
48
        self.config._get_value_by_key.return_value = 'ready'
 
49
        with unittest.mock.patch('libertine.service.tasks.base_task.libertine.service.progress.Progress') as MockProgress:
 
50
            progress = MockProgress.return_value
 
51
            task = tasks.DestroyTask('palpatine', self.config, self.lock, self.connection, self.callback)
 
52
            task._instant_callback = True
 
53
 
 
54
            with unittest.mock.patch('libertine.service.tasks.destroy_task.LibertineContainer') as MockContainer:
 
55
                MockContainer.return_value.destroy_libertine_container.return_value = False
 
56
                task.start().join()
 
57
 
 
58
            progress.error.assert_called_once_with('Destroying container \'palpatine\' failed')
 
59
            self.config.update_container_install_status.assert_has_calls([
 
60
                unittest.mock.call('palpatine', 'removing'),
 
61
                unittest.mock.call('palpatine', 'ready')
 
62
            ], any_order=True)
 
63
            self.assertEqual(task, self.called_with)
 
64
 
 
65
    def test_successfully_destroys(self):
 
66
        self.config._get_value_by_key.return_value = 'ready'
 
67
        with unittest.mock.patch('libertine.service.tasks.base_task.libertine.service.progress.Progress') as MockProgress:
 
68
            progress = MockProgress.return_value
 
69
            progress.done = False
 
70
            task = tasks.DestroyTask('palpatine', self.config, self.lock, self.connection, self.callback)
 
71
            task._instant_callback = True
 
72
 
 
73
            with unittest.mock.patch('libertine.service.tasks.destroy_task.LibertineContainer') as MockContainer:
 
74
                MockContainer.return_value.destroy_libertine_container.return_value = True
 
75
                task.start().join()
 
76
 
 
77
            progress.finished.assert_called_once_with('palpatine')
 
78
            self.config.update_container_install_status.assert_called_once_with('palpatine', 'removing')
 
79
            self.config.delete_container.assert_called_once_with('palpatine')
 
80
            self.assertEqual(task, self.called_with)