/usr/lib/python3/dist-packages/plainbox/impl/session/test_manager.py is in python3-plainbox 0.25-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # This file is part of Checkbox.
#
# Copyright 2014 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
plainbox.impl.test_manager
==========================
Test definitions for plainbox.impl.session.manager module
"""
from unittest import expectedFailure
from plainbox.abc import IJobDefinition
from plainbox.impl.session import SessionManager
from plainbox.impl.session import SessionState
from plainbox.impl.session import SessionStorage
from plainbox.impl.session.state import SessionDeviceContext
from plainbox.impl.session.suspend import SessionSuspendHelper
from plainbox.vendor import mock
from plainbox.vendor.morris import SignalTestCase
class SessionManagerTests(SignalTestCase):
def setUp(self):
self.storage = mock.Mock(name="storage", spec=SessionStorage)
self.state = mock.Mock(name="state", spec=SessionState)
self.context = mock.Mock(name="context", spec=SessionDeviceContext)
self.context2 = mock.Mock(
name='context2', spec_set=SessionDeviceContext)
self.context_list = [self.context] # NOTE: just the first context
self.manager = SessionManager(self.context_list, self.storage)
def test_device_context_list(self):
"""
Verify that accessing SessionManager.device_context_list works okay
"""
self.assertEqual(self.manager.device_context_list, self.context_list)
def test_default_device_context__typical(self):
"""
Verify that accessing SessionManager.default_device_context returns
the first context from the context list
"""
self.assertEqual(self.manager.default_device_context, self.context)
def test_default_device_context__no_contexts(self):
"""
Verify that accessing SessionManager.default_device_context returns
None when the manager doesn't have any device context objects yet
"""
manager = SessionManager([], self.storage)
self.assertIsNone(manager.default_device_context, None)
def test_state(self):
"""
verify that accessing SessionManager.state works okay
"""
self.assertIs(self.manager.state, self.context.state)
def test_storage(self):
"""
verify that accessing SessionManager.storage works okay
"""
self.assertIs(self.manager.storage, self.storage)
def test_checkpoint(self):
"""
verify that SessionManager.checkpoint() creates an image of the
suspended session and writes it using the storage system.
"""
# Mock the suspend helper, we don't want to suspend our mock objects
helper_name = "plainbox.impl.session.manager.SessionSuspendHelper"
with mock.patch(helper_name, spec=SessionSuspendHelper) as helper_cls:
# Call the tested method
self.manager.checkpoint()
# Ensure that a fresh instance of the suspend helper was used to
# call the suspend() method and that the session state parameter
# was passed to it.
helper_cls().suspend.assert_called_with(
self.context.state, self.storage.location)
# Ensure that save_checkpoint() was called on the storage object with
# the return value of what the suspend helper produced.
self.storage.save_checkpoint.assert_called_with(
helper_cls().suspend(self.context.state))
def test_load_session(self):
"""
verify that SessionManager.load_session() correctly delegates the task
to various other objects
"""
job = mock.Mock(name='job', spec_set=IJobDefinition)
unit_list = [job]
flags = None
helper_name = "plainbox.impl.session.manager.SessionResumeHelper"
with mock.patch(helper_name) as helper_cls:
resumed_state = mock.Mock(spec_set=SessionState)
resumed_state.unit_list = unit_list
helper_cls().resume.return_value = resumed_state
# NOTE: mock away _propagate_test_plans() so that we don't get
# unwanted side effects we're not testing here.
with mock.patch.object(SessionManager, '_propagate_test_plans'):
manager = SessionManager.load_session(unit_list, self.storage)
# Ensure that the storage object was used to load the session snapshot
self.storage.load_checkpoint.assert_called_with()
# Ensure that the helper was instantiated with the unit list, flags and
# location
helper_cls.assert_called_with(unit_list, flags, self.storage.location)
# Ensure that the helper instance was asked to recreate session state
helper_cls().resume.assert_called_with(
self.storage.load_checkpoint(), None)
# Ensure that the resulting manager has correct data inside
self.assertEqual(manager.state, helper_cls().resume())
self.assertEqual(manager.storage, self.storage)
@mock.patch.multiple(
"plainbox.impl.session.manager", spec_set=True,
SessionStorageRepository=mock.DEFAULT,
SessionStorage=mock.DEFAULT,
WellKnownDirsHelper=mock.DEFAULT)
def test_create(self, **mocks):
"""
verify that SessionManager.create() correctly sets up
storage repository and creates session directories
"""
mocks['SessionStorage'].create.return_value = mock.MagicMock(
spec_set=SessionStorage)
# Create the new manager
manager = SessionManager.create()
# Ensure that a default repository was created
mocks['SessionStorageRepository'].assert_called_with()
repo = mocks['SessionStorageRepository']()
# Ensure that a storage was created, with repository location and
# without legacy mode turned on
mocks['SessionStorage'].create.assert_called_with(repo.location, False)
storage = mocks['SessionStorage'].create()
# Ensure that a default directories were created
mocks['WellKnownDirsHelper'].assert_called_with(storage)
helper = mocks['WellKnownDirsHelper']()
helper.populate.assert_called_with()
# Ensure that the resulting manager has correct data inside
self.assertEqual(manager.device_context_list, [])
self.assertEqual(manager.storage, storage)
@mock.patch.multiple(
"plainbox.impl.session.manager", spec_set=True,
SessionStorageRepository=mock.DEFAULT,
SessionState=mock.DEFAULT,
SessionStorage=mock.DEFAULT,
WellKnownDirsHelper=mock.DEFAULT)
def test_create_with_unit_list(self, **mocks):
"""
verify that SessionManager.create_with_unit_list() correctly sets up
storage repository and creates session directories
"""
mocks['SessionStorage'].create.return_value = mock.MagicMock(
spec_set=SessionStorage)
# Mock unit list
unit_list = mock.Mock(name='unit_list')
# Create the new manager
manager = SessionManager.create_with_unit_list(unit_list)
# Ensure that a state object was created
mocks['SessionState'].assert_called_with(unit_list)
state = mocks['SessionState']()
# Ensure that a default repository was created
mocks['SessionStorageRepository'].assert_called_with()
repo = mocks['SessionStorageRepository']()
# Ensure that a storage was created, with repository location and
# without legacy mode turned on
mocks['SessionStorage'].create.assert_called_with(repo.location, False)
storage = mocks['SessionStorage'].create()
# Ensure that a default directories were created
mocks['WellKnownDirsHelper'].assert_called_with(storage)
helper = mocks['WellKnownDirsHelper']()
helper.populate.assert_called_with()
# Ensure that the resulting manager has correct data inside
self.assertEqual(manager.state, state)
self.assertEqual(manager.storage, storage)
@mock.patch.multiple(
"plainbox.impl.session.manager", spec_set=True,
SessionStorageRepository=mock.DEFAULT,
SessionState=mock.DEFAULT,
SessionStorage=mock.DEFAULT,
SessionDeviceContext=mock.DEFAULT,
WellKnownDirsHelper=mock.DEFAULT)
def test_create_with_state(self, **mocks):
"""
verify that SessionManager.create_with_state() correctly sets up
storage repository and creates session directories
"""
mocks['SessionStorage'].create.return_value = mock.MagicMock(
spec_set=SessionStorage)
# Mock an empty list of units in teh session state object
self.state.unit_list = []
# Create the new manager
manager = SessionManager.create_with_state(self.state)
# Ensure that a default repository was created
mocks['SessionStorageRepository'].assert_called_with()
repo = mocks['SessionStorageRepository']()
# Ensure that a storage was created, with repository location and
# without legacy mode turned on
mocks['SessionStorage'].create.assert_called_with(repo.location, False)
storage = mocks['SessionStorage'].create()
# Ensure that a default directories were created
mocks['WellKnownDirsHelper'].assert_called_with(storage)
helper = mocks['WellKnownDirsHelper']()
helper.populate.assert_called_with()
# Ensure that the device context was created with the right state
# object
mocks['SessionDeviceContext'].assert_called_with(self.state)
# Ensure that the resulting manager has correct data inside
self.assertEqual(
manager.device_context_list, [mocks['SessionDeviceContext']()])
# self.assertEqual(manager.state, self.state)
self.assertEqual(manager.storage, storage)
def test_add_device_context(self):
"""
Ensure that adding a device context works
"""
manager = SessionManager([], self.storage)
manager.add_device_context(self.context)
self.assertIn(self.context, manager.device_context_list)
@expectedFailure
def test_add_device_context__add_another(self):
"""
Ensure that adding a second context also works
"""
manager = SessionManager([], self.storage)
manager.add_device_context(self.context)
manager.add_device_context(self.context2)
self.assertIn(self.context, manager.device_context_list)
self.assertIn(self.context2, manager.device_context_list)
def test_add_device_context__twice(self):
"""
Ensure that you cannot add the same device context twice
"""
manager = SessionManager([], self.storage)
manager.add_device_context(self.context)
with self.assertRaises(ValueError):
manager.add_device_context(self.context)
def test_remove_context(self):
"""
Ensure that removing a device context works
"""
manager = SessionManager([], self.storage)
manager.add_device_context(self.context)
manager.remove_device_context(self.context)
self.assertNotIn(self.context, manager.device_context_list)
def test_remove_context__missing(self):
"""
Ensure that you cannot remove a device context that is not added first
"""
with self.assertRaises(ValueError):
self.manager.remove_device_context(self.context2)
def test_on_device_context_added(self):
"""
Ensure that adding a device context sends the appropriate signal
"""
manager = SessionManager([], self.storage)
self.watchSignal(manager.on_device_context_added)
manager.add_device_context(self.context)
self.assertSignalFired(manager.on_device_context_added, self.context)
def test_on_device_context_removed(self):
"""
Ensure that removing a device context sends the appropriate signal
"""
manager = SessionManager([self.context], self.storage)
self.watchSignal(manager.on_device_context_removed)
manager.remove_device_context(self.context)
self.assertSignalFired(manager.on_device_context_removed, self.context)
def test_add_local_device_context(self):
"""
Ensure that using add_local_device_context() adds a context with
a special 'local' device and fires the appropriate signal
"""
manager = SessionManager([], self.storage)
self.watchSignal(manager.on_device_context_added)
cls_name = "plainbox.impl.session.manager.SessionDeviceContext"
with mock.patch(cls_name) as sdc:
manager.add_local_device_context()
self.assertSignalFired(manager.on_device_context_added, sdc())
self.assertIn(sdc(), manager.device_context_list)
|