/usr/lib/python3/dist-packages/plainbox/impl/test_depmgr.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 | # This file is part of Checkbox.
#
# Copyright 2012, 2013 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_depmgr
=========================
Test definitions for plainbox.impl.depmgr module
"""
from unittest import TestCase
from plainbox.impl.depmgr import DependencyCycleError
from plainbox.impl.depmgr import DependencyDuplicateError
from plainbox.impl.depmgr import DependencyMissingError
from plainbox.impl.depmgr import DependencySolver
from plainbox.impl.testing_utils import make_job
class DependencyCycleErrorTests(TestCase):
def setUp(self):
self.A = make_job("A", depends="B")
self.B = make_job("B", depends="A")
self.exc = DependencyCycleError([self.A, self.B, self.A])
def test_job_list(self):
self.assertEqual(self.exc.job_list, [self.A, self.B, self.A])
def test_affected_job(self):
self.assertIs(self.exc.affected_job, self.A)
def test_affecting_job(self):
# This is the same as affected_job as this is a cycle
self.assertIs(self.exc.affecting_job, self.A)
def test_str(self):
expected = "dependency cycle detected: A -> B -> A"
observed = str(self.exc)
self.assertEqual(expected, observed)
def test_repr(self):
expected = ("<DependencyCycleError job_list:["
"<JobDefinition id:'A' plugin:'dummy'>, "
"<JobDefinition id:'B' plugin:'dummy'>, "
"<JobDefinition id:'A' plugin:'dummy'>]>")
observed = repr(self.exc)
self.assertEqual(expected, observed)
class DependencyMissingErrorTests(TestCase):
def setUp(self):
self.A = make_job("A")
self.exc_direct = DependencyMissingError(
self.A, 'B', DependencyMissingError.DEP_TYPE_DIRECT)
self.exc_resource = DependencyMissingError(
self.A, 'B', DependencyMissingError.DEP_TYPE_RESOURCE)
def test_job(self):
self.assertIs(self.exc_direct.job, self.A)
self.assertIs(self.exc_resource.job, self.A)
def test_affected_job(self):
self.assertIs(self.exc_direct.affected_job, self.A)
self.assertIs(self.exc_resource.affected_job, self.A)
def test_affecting_job(self):
self.assertIs(self.exc_direct.affecting_job, None)
self.assertIs(self.exc_resource.affecting_job, None)
def test_missing_job_id(self):
self.assertEqual(self.exc_direct.missing_job_id, 'B')
self.assertEqual(self.exc_resource.missing_job_id, 'B')
def test_str_direct(self):
expected = "missing dependency: 'B' (direct)"
observed = str(self.exc_direct)
self.assertEqual(expected, observed)
def test_str_resoucee(self):
expected = "missing dependency: 'B' (resource)"
observed = str(self.exc_resource)
self.assertEqual(expected, observed)
def test_repr_direct(self):
expected = ("<DependencyMissingError "
"job:<JobDefinition id:'A' plugin:'dummy'> "
"missing_job_id:'B' "
"dep_type:'direct'>")
observed = repr(self.exc_direct)
self.assertEqual(expected, observed)
def test_repr_resource(self):
expected = ("<DependencyMissingError "
"job:<JobDefinition id:'A' plugin:'dummy'> "
"missing_job_id:'B' "
"dep_type:'resource'>")
observed = repr(self.exc_resource)
self.assertEqual(expected, observed)
class DependencyDuplicateErrorTests(TestCase):
def setUp(self):
self.A = make_job("A")
self.another_A = make_job("A")
self.exc = DependencyDuplicateError(self.A, self.another_A)
def test_job(self):
self.assertIs(self.exc.job, self.A)
def test_duplicate_job(self):
self.assertIs(self.exc.duplicate_job, self.another_A)
def test_affected_job(self):
self.assertIs(self.exc.affected_job, self.A)
def test_affecting_job(self):
self.assertIs(self.exc.affecting_job, self.another_A)
def test_str(self):
expected = "duplicate job id: 'A'"
observed = str(self.exc)
self.assertEqual(expected, observed)
def test_repr(self):
expected = ("<DependencyDuplicateError "
"job:<JobDefinition id:'A' plugin:'dummy'> "
"duplicate_job:<JobDefinition id:'A' plugin:'dummy'>>")
observed = repr(self.exc)
self.assertEqual(expected, observed)
class DependencySolverInternalsTests(TestCase):
def test_get_job_map_produces_map(self):
A = make_job('A')
B = make_job('B')
expected = {'A': A, 'B': B}
observed = DependencySolver._get_job_map([A, B])
self.assertEqual(expected, observed)
def test_get_job_map_find_duplicates(self):
A = make_job('A')
another_A = make_job('A')
with self.assertRaises(DependencyDuplicateError) as call:
DependencySolver._get_job_map([A, another_A])
self.assertIs(call.exception.job, A)
self.assertIs(call.exception.duplicate_job, another_A)
class TestDependencySolver(TestCase):
def test_empty(self):
observed = DependencySolver.resolve_dependencies([])
expected = []
self.assertEqual(expected, observed)
def test_direct_deps(self):
# This tests the following simple job chain
# A -> B -> C
A = make_job(id='A', depends='B')
B = make_job(id='B', depends='C')
C = make_job(id='C')
job_list = [A, B, C]
expected = [C, B, A]
observed = DependencySolver.resolve_dependencies(job_list)
self.assertEqual(expected, observed)
def test_independent_groups_deps(self):
# This tests two independent job chains
# A1 -> B1
# A2 -> B2
A1 = make_job(id='A1', depends='B1')
B1 = make_job(id='B1',)
A2 = make_job(id='A2', depends='B2')
B2 = make_job(id='B2')
job_list = [A1, B1, A2, B2]
expected = [B1, A1, B2, A2]
observed = DependencySolver.resolve_dependencies(job_list)
self.assertEqual(expected, observed)
def test_visiting_blackend_node(self):
# This tests a visit to already visited job
# A
# B -> A
# A will be visited twice
A = make_job(id='A')
B = make_job(id='B', depends='A')
job_list = [A, B]
expected = [A, B]
observed = DependencySolver.resolve_dependencies(job_list)
self.assertEqual(expected, observed)
def test_resource_deps(self):
# This tests resource deps
# A ~> R
A = make_job(id='A', requires='R.foo == "bar"')
R = make_job(id='R', plugin='resource')
job_list = [A, R]
expected = [R, A]
observed = DependencySolver.resolve_dependencies(job_list)
self.assertEqual(expected, observed)
def test_duplicate_error(self):
A = make_job('A')
another_A = make_job('A')
job_list = [A, another_A]
with self.assertRaises(DependencyDuplicateError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertIs(call.exception.job, A)
self.assertIs(call.exception.duplicate_job, another_A)
def test_missing_direct_dependency(self):
# This tests missing dependencies
# A -> (inexisting B)
A = make_job(id='A', depends='B')
job_list = [A]
with self.assertRaises(DependencyMissingError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertIs(call.exception.job, A)
self.assertEqual(call.exception.missing_job_id, 'B')
self.assertEqual(call.exception.dep_type,
call.exception.DEP_TYPE_DIRECT)
def test_missing_resource_dependency(self):
# This tests missing resource dependencies
# A ~> (inexisting R)
A = make_job(id='A', requires='R.attr == "value"')
job_list = [A]
with self.assertRaises(DependencyMissingError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertIs(call.exception.job, A)
self.assertEqual(call.exception.missing_job_id, 'R')
self.assertEqual(call.exception.dep_type,
call.exception.DEP_TYPE_RESOURCE)
def test_dependency_cycle_self(self):
# This tests dependency loops
# A -> A
A = make_job(id='A', depends='A')
job_list = [A]
with self.assertRaises(DependencyCycleError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertEqual(call.exception.job_list, [A, A])
def test_dependency_cycle_simple(self):
# This tests dependency loops
# A -> B -> A
A = make_job(id='A', depends='B')
B = make_job(id='B', depends='A')
job_list = [A, B]
with self.assertRaises(DependencyCycleError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertEqual(call.exception.job_list, [A, B, A])
def test_dependency_cycle_longer(self):
# This tests dependency loops
# A -> B -> C -> D -> B
A = make_job(id='A', depends='B')
B = make_job(id='B', depends='C')
C = make_job(id='C', depends='D')
D = make_job(id='D', depends='B')
job_list = [A, B, C, D]
with self.assertRaises(DependencyCycleError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertEqual(call.exception.job_list, [B, C, D, B])
def test_dependency_cycle_via_resource(self):
# This tests dependency loops
# A -> R -> A
A = make_job(id='A', requires='R.key == "value"')
R = make_job(id='R', depends='A', plugin="resource")
job_list = [A, R]
with self.assertRaises(DependencyCycleError) as call:
DependencySolver.resolve_dependencies(job_list)
self.assertEqual(call.exception.job_list, [A, R, A])
|