1
# Copyright 2016 Canonical Ltd.
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.
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.
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/>.
17
from unittest import TestCase
18
from libertine.service import tasks
19
from libertine.ContainersConfig import ContainersConfig
22
class TestDestroyTask(TestCase):
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
29
def callback(self, task):
30
self.called_with = task
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
37
task = tasks.DestroyTask('palpatine', self.config, self.lock, self.connection, self.callback)
38
task._instant_callback = True
40
with unittest.mock.patch('libertine.service.tasks.destroy_task.LibertineContainer') as MockContainer:
41
MockContainer.return_value.destroy_libertine_container.return_value = True
44
progress.error.assert_called_once_with('Container \'palpatine\' does not exist')
45
self.assertEqual(task, self.called_with)
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
54
with unittest.mock.patch('libertine.service.tasks.destroy_task.LibertineContainer') as MockContainer:
55
MockContainer.return_value.destroy_libertine_container.return_value = False
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')
63
self.assertEqual(task, self.called_with)
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
70
task = tasks.DestroyTask('palpatine', self.config, self.lock, self.connection, self.callback)
71
task._instant_callback = True
73
with unittest.mock.patch('libertine.service.tasks.destroy_task.LibertineContainer') as MockContainer:
74
MockContainer.return_value.destroy_libertine_container.return_value = True
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)