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
|
"""test builtin option types."""
import argparse
from mock import patch
from Bcfg2.Options import Option, Types, Parser
from testsuite.common import Bcfg2TestCase
class TestOptionTypes(Bcfg2TestCase):
"""test builtin option types."""
def setUp(self):
self.options = None
def _test_options(self, options):
"""helper to test option types.
this expects that self.options is set to a single option named
test. The value of that option is returned.
"""
result = argparse.Namespace()
parser = Parser(components=[self], namespace=result)
parser.parse(options)
return result.test
def test_comma_list(self):
"""parse comma-list values."""
self.options = [Option("--test", type=Types.comma_list)]
expected = ["one", "two", "three"]
self.assertItemsEqual(self._test_options(["--test", "one,two,three"]),
expected)
self.assertItemsEqual(self._test_options(["--test",
"one, two, three"]),
expected)
self.assertItemsEqual(self._test_options(["--test",
"one , two ,three"]),
expected)
self.assertItemsEqual(self._test_options(["--test", "one two, three"]),
["one two", "three"])
def test_colon_list(self):
"""parse colon-list values."""
self.options = [Option("--test", type=Types.colon_list)]
self.assertItemsEqual(self._test_options(["--test", "one:two three"]),
["one", "two three"])
def test_comma_dict(self):
"""parse comma-dict values."""
self.options = [Option("--test", type=Types.comma_dict)]
expected = {
"one": True,
"two": 2,
"three": "three",
"four": False}
self.assertDictEqual(
self._test_options(["--test",
"one=yes, two=2 , three=three,four=no"]),
expected)
self.assertDictEqual(
self._test_options(["--test", "one,two=2,three=three,four=off"]),
expected)
def test_anchored_regex_list(self):
"""parse regex lists."""
self.options = [Option("--test", type=Types.anchored_regex_list)]
self.assertItemsEqual(
[r.pattern for r in self._test_options(["--test", r'\d+ \s*'])],
[r'^\d+$', r'^\s*$'])
self.assertRaises(SystemExit,
self._test_options, ["--test", '(]'])
def test_octal(self):
"""parse octal options."""
self.options = [Option("--test", type=Types.octal)]
self.assertEqual(self._test_options(["--test", "0777"]), 511)
self.assertEqual(self._test_options(["--test", "133114255"]), 23894189)
@patch("pwd.getpwnam")
def test_username(self, mock_getpwnam):
"""parse username options."""
self.options = [Option("--test", type=Types.username)]
mock_getpwnam.return_value = ("test", '********', 1001, 1001,
"Test user", "/home/test", "/bin/bash")
self.assertEqual(self._test_options(["--test", "1001"]), 1001)
self.assertEqual(self._test_options(["--test", "test"]), 1001)
@patch("grp.getgrnam")
def test_groupname(self, mock_getpwnam):
"""parse group name options."""
self.options = [Option("--test", type=Types.groupname)]
mock_getpwnam.return_value = ("test", '*', 1001, ["test"])
self.assertEqual(self._test_options(["--test", "1001"]), 1001)
self.assertEqual(self._test_options(["--test", "test"]), 1001)
def test_timeout(self):
"""parse timeout options."""
self.options = [Option("--test", type=Types.timeout)]
self.assertEqual(self._test_options(["--test", "1.0"]), 1.0)
self.assertEqual(self._test_options(["--test", "1"]), 1.0)
self.assertEqual(self._test_options(["--test", "0"]), None)
def test_size(self):
"""parse human-readable size options."""
self.options = [Option("--test", type=Types.size)]
self.assertEqual(self._test_options(["--test", "5k"]), 5120)
self.assertEqual(self._test_options(["--test", "5"]), 5)
self.assertRaises(SystemExit,
self._test_options, ["--test", "g5m"])
|