/usr/lib/python3/dist-packages/plainbox/impl/unit/test_testplan.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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | # 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.unit.test_testplan
================================
Test definitions for plainbox.impl.unit.testplan module
"""
from unittest import TestCase
import doctest
import operator
from plainbox.abc import IProvider1
from plainbox.abc import ITextSource
from plainbox.impl.secure.origin import Origin
from plainbox.impl.secure.qualifiers import OperatorMatcher
from plainbox.impl.secure.qualifiers import PatternMatcher
from plainbox.impl.unit.testplan import TestPlanUnit
from plainbox.vendor import mock
def load_tests(loader, tests, ignore):
tests.addTests(
doctest.DocTestSuite('plainbox.impl.unit.testplan',
optionflags=doctest.REPORT_NDIFF))
return tests
class TestTestPlan(TestCase):
def setUp(self):
self.provider = mock.Mock(name='provider', spec_set=IProvider1)
self.provider.namespace = 'ns'
def test_name__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.name, None)
def test_name__normal(self):
unit = TestPlanUnit({
'name': 'name'
}, provider=self.provider)
self.assertEqual(unit.name, "name")
def test_description__default(self):
name = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(name.description, None)
def test_description__normal(self):
name = TestPlanUnit({
'description': 'description'
}, provider=self.provider)
self.assertEqual(name.description, "description")
def test_icon__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.icon, None)
def test_icon__normal(self):
unit = TestPlanUnit({
'icon': 'icon'
}, provider=self.provider)
self.assertEqual(unit.icon, "icon")
def test_include__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.include, None)
def test_include__normal(self):
unit = TestPlanUnit({
'include': 'include'
}, provider=self.provider)
self.assertEqual(unit.include, "include")
def test_mandatory_include__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.mandatory_include, None)
def test_mandatory_include__normal(self):
unit = TestPlanUnit({
'mandatory_include': 'mandatory_include'
}, provider=self.provider)
self.assertEqual(unit.mandatory_include, "mandatory_include")
def test_bootstrap_include__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.bootstrap_include, None)
def test_bootstrap_include__normal(self):
unit = TestPlanUnit({
'bootstrap_include': 'bootstrap_include'
}, provider=self.provider)
self.assertEqual(unit.bootstrap_include, 'bootstrap_include')
def test_exclude__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.exclude, None)
def test_exclude__normal(self):
unit = TestPlanUnit({
'exclude': 'exclude'
}, provider=self.provider)
self.assertEqual(unit.exclude, "exclude")
def test_category_override__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.category_overrides, None)
def test_category_override__normal(self):
unit = TestPlanUnit({
'category-overrides': 'value',
}, provider=self.provider)
self.assertEqual(unit.category_overrides, 'value')
def test_str(self):
unit = TestPlanUnit({
'name': 'name'
}, provider=self.provider)
self.assertEqual(str(unit), "name")
def test_repr(self):
unit = TestPlanUnit({
'name': 'name',
'id': 'id',
}, provider=self.provider)
self.assertEqual(repr(unit), "<TestPlanUnit id:'ns::id' name:'name'>")
def test_tr_unit(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.tr_unit(), 'test plan')
def test_estimated_duration__default(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.estimated_duration, None)
def test_estimated_duration__normal(self):
self.assertEqual(TestPlanUnit(
{}, provider=self.provider).estimated_duration, None)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '5'}, provider=self.provider
).estimated_duration, 5)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '123.5'}, provider=self.provider
).estimated_duration, 123.5)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '5s'}, provider=self.provider
).estimated_duration, 5)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1m 5s'}, provider=self.provider
).estimated_duration, 65)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1h 1m 5s'}, provider=self.provider
).estimated_duration, 3665)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1h'}, provider=self.provider
).estimated_duration, 3600)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '2m'}, provider=self.provider
).estimated_duration, 120)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1h 1s'}, provider=self.provider
).estimated_duration, 3601)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1m:5s'}, provider=self.provider
).estimated_duration, 65)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1h:1m:5s'}, provider=self.provider
).estimated_duration, 3665)
self.assertEqual(TestPlanUnit(
{'estimated_duration': '1h:1s'}, provider=self.provider
).estimated_duration, 3601)
def test_estimated_duration__broken(self):
unit = TestPlanUnit({
'estimated_duration': 'foo'
}, provider=self.provider)
with self.assertRaises(ValueError):
unit.estimated_duration
def test_tr_name(self):
unit = TestPlanUnit({
}, provider=self.provider)
with mock.patch.object(unit, "get_translated_record_value") as mgtrv:
retval = unit.tr_name()
# Ensure that get_translated_record_value() was called
mgtrv.assert_called_once_with('name')
# Ensure tr_summary() returned its return value
self.assertEqual(retval, mgtrv())
def test_tr_description(self):
unit = TestPlanUnit({
}, provider=self.provider)
with mock.patch.object(unit, "get_translated_record_value") as mgtrv:
retval = unit.tr_description()
# Ensure that get_translated_record_value() was called
mgtrv.assert_called_once_with('description')
# Ensure tr_summary() returned its return value
self.assertEqual(retval, mgtrv())
def test_parse_matchers__with_provider(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(
list(unit.parse_matchers("foo")),
[(0, 'id', OperatorMatcher(operator.eq, 'ns::foo'), None)])
self.assertEqual(
list(unit.parse_matchers("other::bar")),
[(0, 'id', OperatorMatcher(operator.eq, "other::bar"), None)])
self.assertEqual(
list(unit.parse_matchers("sd[a-z]")),
[(0, 'id', PatternMatcher("^ns::sd[a-z]$"), None)])
self.assertEqual(
list(unit.parse_matchers("sd[a-z]$")),
[(0, 'id', PatternMatcher("^ns::sd[a-z]$"), None)])
self.assertEqual(
list(unit.parse_matchers("^sd[a-z]")),
[(0, 'id', PatternMatcher("^ns::sd[a-z]$"), None)])
self.assertEqual(
list(unit.parse_matchers("^sd[a-z]$")),
[(0, 'id', PatternMatcher("^ns::sd[a-z]$"), None)])
def test_parse_matchers__without_provider(self):
unit = TestPlanUnit({
}, provider=None)
self.assertEqual(
list(unit.parse_matchers("foo")),
[(0, 'id', OperatorMatcher(operator.eq, 'foo'), None)])
self.assertEqual(
list(unit.parse_matchers("other::bar")),
[(0, 'id', OperatorMatcher(operator.eq, "other::bar"), None)])
self.assertEqual(
list(unit.parse_matchers("sd[a-z]")),
[(0, 'id', PatternMatcher("^sd[a-z]$"), None)])
self.assertEqual(
list(unit.parse_matchers("sd[a-z]$")),
[(0, 'id', PatternMatcher("^sd[a-z]$"), None)])
self.assertEqual(
list(unit.parse_matchers("^sd[a-z]")),
[(0, 'id', PatternMatcher("^sd[a-z]$"), None)])
self.assertEqual(
list(unit.parse_matchers("^sd[a-z]$")),
[(0, 'id', PatternMatcher("^sd[a-z]$"), None)])
def test_get_qualifier__full(self):
# Let's pretend the unit looks like this:
# +0 unit: test-plan
# +1 name: An example test plan
# +2 include:
# +3 foo
# +4 # nothing
# +5 b.*
# +6 exclude: bar
# Let's also assume that it is at a +10 offset in the file it comes
# from so that the first line +0 is actually the 10th Line
src = mock.Mock(name='source', spec_set=ITextSource)
origin = Origin(src, 10, 16)
field_offset_map = {
'unit': 0,
'name': 1,
'include': 3,
'exclude': 6
}
unit = TestPlanUnit({
'unit': 'test-plan',
'name': 'An example test plan',
'include': (
'foo\n'
'# nothing\n'
'b.*\n'
),
'exclude': 'bar\n'
}, provider=self.provider, origin=origin,
field_offset_map=field_offset_map)
qual_list = unit.get_qualifier().get_primitive_qualifiers()
self.assertEqual(qual_list[0].field, 'id')
self.assertIsInstance(qual_list[0].matcher, OperatorMatcher)
self.assertEqual(qual_list[0].matcher.value, 'ns::foo')
self.assertEqual(qual_list[0].origin, Origin(src, 13, 13))
self.assertEqual(qual_list[0].inclusive, True)
self.assertEqual(qual_list[1].field, 'id')
self.assertIsInstance(qual_list[1].matcher, PatternMatcher)
self.assertEqual(qual_list[1].matcher.pattern_text, '^ns::b.*$')
self.assertEqual(qual_list[1].origin, Origin(src, 15, 15))
self.assertEqual(qual_list[1].inclusive, True)
self.assertEqual(qual_list[2].field, 'id')
self.assertIsInstance(qual_list[2].matcher, OperatorMatcher)
self.assertEqual(qual_list[2].matcher.value, 'ns::bar')
self.assertEqual(qual_list[2].origin, Origin(src, 16, 16))
self.assertEqual(qual_list[2].inclusive, False)
def test_get_qualifier__only_comments(self):
unit = TestPlanUnit({
'include': '# nothing\n'
}, provider=self.provider)
self.assertEqual(unit.get_qualifier().get_primitive_qualifiers(), [])
def test_get_qualifier__empty(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(unit.get_qualifier().get_primitive_qualifiers(), [])
def test_parse_category_overrides__with_provider(self):
unit = TestPlanUnit({
}, provider=self.provider)
self.assertEqual(
unit.parse_category_overrides('apply "wireless" to "wireless/.*"'),
[(0, "ns::wireless", "^ns::wireless/.*$")])
self.assertEqual(
unit.parse_category_overrides(
'apply "other::wireless" to "wireless/.*"'),
[(0, "other::wireless", "^ns::wireless/.*$")])
self.assertEqual(
unit.parse_category_overrides(
'apply "wireless" to "other::wireless/.*"'),
[(0, "ns::wireless", "^other::wireless/.*$")])
self.assertEqual(
unit.parse_category_overrides(
'apply "first::wireless" to "second::wireless/.*"'),
[(0, "first::wireless", "^second::wireless/.*$")])
def test_parse_category_overrides__without_provider(self):
unit = TestPlanUnit({
}, provider=None)
self.assertEqual(
unit.parse_category_overrides('apply "wireless" to "wireless/.*"'),
[(0, "wireless", "^wireless/.*$")])
self.assertEqual(
unit.parse_category_overrides(
'apply "other::wireless" to "wireless/.*"'),
[(0, "other::wireless", "^wireless/.*$")])
self.assertEqual(
unit.parse_category_overrides(
'apply "wireless" to "other::wireless/.*"'),
[(0, "wireless", "^other::wireless/.*$")])
self.assertEqual(
unit.parse_category_overrides(
'apply "first::wireless" to "second::wireless/.*"'),
[(0, "first::wireless", "^second::wireless/.*$")])
def test_parse_category_overrides__errors(self):
unit = TestPlanUnit({}, provider=self.provider)
with self.assertRaisesRegex(ValueError, "expected override value"):
unit.parse_category_overrides('apply')
def test_get_bootstrap_job_ids__empty(self):
unit = TestPlanUnit({}, provider=None)
self.assertEqual(unit.get_bootstrap_job_ids(), set())
def test_get_bootstrap_job_ids__normal(self):
unit = TestPlanUnit({
'bootstrap_include': 'Foo\nBar'
}, provider=None)
self.assertEqual(unit.get_bootstrap_job_ids(), set(['Foo', 'Bar']))
def test_get_bootstrap_job_ids__qualified_ids(self):
unit = TestPlanUnit({
'bootstrap_include': 'Foo\nBar'
}, provider=self.provider)
self.assertEqual(unit.get_bootstrap_job_ids(),
set(['ns::Foo', 'ns::Bar']))
|