~larryprice/libertine/deb-filepicker

« back to all changes in this revision

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

  • Committer: Larry Price
  • Date: 2017-03-09 17:26:36 UTC
  • mfrom: (388.2.46 devel)
  • Revision ID: larry.price@canonical.com-20170309172636-jketrvc7sj0wnzp0
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2016 Canonical Ltd.
 
1
# Copyright 2016-2017 Canonical Ltd.
2
2
#
3
3
# This program is free software: you can redistribute it and/or modify it
4
4
# under the terms of the GNU General Public License version 3, as published
15
15
 
16
16
import unittest.mock
17
17
from unittest import TestCase
18
 
from libertine.service import tasks
 
18
from libertine.service import tasks, operations_monitor
19
19
from libertine.ContainersConfig import ContainersConfig
20
20
 
21
21
 
22
22
class TestUpdateTask(TestCase):
23
23
    def setUp(self):
24
 
        self.config      = unittest.mock.create_autospec(ContainersConfig)
25
 
        self.connection  = unittest.mock.Mock()
26
 
        self.lock      = unittest.mock.MagicMock()
 
24
        self.config  = unittest.mock.create_autospec(ContainersConfig)
 
25
        self.lock    = unittest.mock.MagicMock()
 
26
        self.monitor = unittest.mock.create_autospec(operations_monitor.OperationsMonitor)
 
27
 
 
28
        self.monitor.new_operation.return_value = "/com/canonical/libertine/Service/Download/123456"
27
29
        self.called_with = None
28
30
 
29
31
    def callback(self, task):
31
33
 
32
34
    def test_sends_error_on_non_existent_container(self):
33
35
        self.config.container_exists.return_value = False
34
 
        with unittest.mock.patch('libertine.service.tasks.base_task.libertine.service.progress.Progress') as MockProgress:
35
 
            progress = MockProgress.return_value
36
 
            task = tasks.UpdateTask('palpatine', self.config, self.lock, self.connection, self.callback)
37
 
            task._instant_callback = True
38
 
 
39
 
            with unittest.mock.patch('libertine.service.tasks.update_task.LibertineContainer') as MockContainer:
40
 
                task.start().join()
41
 
 
42
 
            progress.error.assert_called_once_with('Container \'palpatine\' does not exist, skipping update')
43
 
            self.assertEqual(task, self.called_with)
 
36
        task = tasks.UpdateTask('palpatine', self.config, self.lock, self.monitor, self.callback)
 
37
        task._instant_callback = True
 
38
 
 
39
        with unittest.mock.patch('libertine.service.tasks.update_task.LibertineContainer') as MockContainer:
 
40
            task.start().join()
 
41
 
 
42
        self.monitor.error.assert_called_once_with(self.monitor.new_operation.return_value, 'Container \'palpatine\' does not exist, skipping update')
 
43
 
 
44
        self.assertEqual(task, self.called_with)
44
45
 
45
46
    def test_sends_error_on_failed_update(self):
46
47
        self.config.container_exists.return_value = True
47
 
        with unittest.mock.patch('libertine.service.tasks.base_task.libertine.service.progress.Progress') as MockProgress:
48
 
            progress = MockProgress.return_value
49
 
            task = tasks.UpdateTask('palpatine', self.config, self.lock, self.connection, self.callback)
50
 
            task._instant_callback = True
51
 
 
52
 
            with unittest.mock.patch('libertine.service.tasks.update_task.LibertineContainer') as MockContainer:
53
 
                MockContainer.return_value.update_libertine_container.return_value = False
54
 
                task.start().join()
55
 
 
56
 
            progress.error.assert_called_once_with('Failed to update container \'palpatine\'')
57
 
            self.config.update_container_install_status.assert_has_calls([
58
 
                unittest.mock.call('palpatine', 'updating'),
59
 
                unittest.mock.call('palpatine', 'ready')
60
 
            ], any_order=True)
61
 
            self.assertEqual(task, self.called_with)
 
48
        task = tasks.UpdateTask('palpatine', self.config, self.lock, self.monitor, self.callback)
 
49
        task._instant_callback = True
 
50
 
 
51
        with unittest.mock.patch('libertine.service.tasks.update_task.LibertineContainer') as MockContainer:
 
52
            MockContainer.return_value.update_libertine_container.return_value = False
 
53
            task.start().join()
 
54
 
 
55
        self.monitor.error.assert_called_once_with(self.monitor.new_operation.return_value, 'Failed to update container \'palpatine\'')
 
56
        self.config.update_container_install_status.assert_has_calls([
 
57
            unittest.mock.call('palpatine', 'updating'),
 
58
            unittest.mock.call('palpatine', 'ready')
 
59
        ], any_order=True)
 
60
 
 
61
        self.assertEqual(task, self.called_with)
62
62
 
63
63
    def test_successfully_updates(self):
64
64
        self.config.container_exists.return_value = True
65
 
        with unittest.mock.patch('libertine.service.tasks.base_task.libertine.service.progress.Progress') as MockProgress:
66
 
            progress = MockProgress.return_value
67
 
            progress.done = False
68
 
            task = tasks.UpdateTask('palpatine', self.config, self.lock, self.connection, self.callback)
69
 
            task._instant_callback = True
70
 
 
71
 
            with unittest.mock.patch('libertine.service.tasks.update_task.LibertineContainer') as MockContainer:
72
 
                MockContainer.return_value.update_libertine_container.return_value = True
73
 
                task.start().join()
74
 
 
75
 
            progress.finished.assert_called_once_with('palpatine')
76
 
            self.config.update_container_install_status.assert_has_calls([
77
 
                unittest.mock.call('palpatine', 'updating'),
78
 
                unittest.mock.call('palpatine', 'ready')
79
 
            ], any_order=True)
80
 
            self.assertEqual(task, self.called_with)
 
65
        self.monitor.done.return_value = False
 
66
        task = tasks.UpdateTask('palpatine', self.config, self.lock, self.monitor, self.callback)
 
67
        task._instant_callback = True
 
68
 
 
69
        with unittest.mock.patch('libertine.service.tasks.update_task.LibertineContainer') as MockContainer:
 
70
            MockContainer.return_value.update_libertine_container.return_value = True
 
71
            task.start().join()
 
72
 
 
73
        self.monitor.finished.assert_called_once_with(self.monitor.new_operation.return_value)
 
74
        self.config.update_container_install_status.assert_has_calls([
 
75
            unittest.mock.call('palpatine', 'updating'),
 
76
            unittest.mock.call('palpatine', 'ready')
 
77
        ], any_order=True)
 
78
 
 
79
        self.assertEqual(task, self.called_with)