Debian Codegraph @

neutronclient/tests/unit/test_casual_args.py

raw
1 # Copyright 2012 OpenStack Foundation.
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 import testtools
18
19 from neutronclient.common import exceptions
20 from neutronclient.neutron import v2_0 as neutronV20
21
22
23 class CLITestArgs(testtools.TestCase):
24
25 def test_empty(self):
26 _mydict = neutronV20.parse_args_to_dict([])
27 self.assertEqual({}, _mydict)
28
29 def test_default_bool(self):
30 _specs = ['--my_bool', '--arg1', 'value1']
31 _mydict = neutronV20.parse_args_to_dict(_specs)
32 self.assertTrue(_mydict['my_bool'])
33
34 def test_bool_true(self):
35 _specs = ['--my-bool', 'type=bool', 'true', '--arg1', 'value1']
36 _mydict = neutronV20.parse_args_to_dict(_specs)
37 self.assertTrue(_mydict['my_bool'])
38
39 def test_bool_false(self):
40 _specs = ['--my_bool', 'type=bool', 'false', '--arg1', 'value1']
41 _mydict = neutronV20.parse_args_to_dict(_specs)
42 self.assertFalse(_mydict['my_bool'])
43
44 def test_int_and_str(self):
45 _specs = ['--my-int', 'type=int', '10',
46 '--my-str', 'type=str', 'value1']
47 _mydict = neutronV20.parse_args_to_dict(_specs)
48 self.assertEqual(10, _mydict['my_int'])
49 self.assertEqual('value1', _mydict['my_str'])
50
51 def test_nargs(self):
52 _specs = ['--tag', 'x', 'y', '--arg1', 'value1']
53 _mydict = neutronV20.parse_args_to_dict(_specs)
54 self.assertIn('x', _mydict['tag'])
55 self.assertIn('y', _mydict['tag'])
56
57 def test_badarg(self):
58 _specs = ['--tag=t', 'x', 'y', '--arg1', 'value1']
59 self.assertRaises(exceptions.CommandError,
60 neutronV20.parse_args_to_dict, _specs)
61
62 def test_badarg_with_minus(self):
63 _specs = ['--arg1', 'value1', '-D']
64 self.assertRaises(exceptions.CommandError,
65 neutronV20.parse_args_to_dict, _specs)
66
67 def test_goodarg_with_minus_number(self):
68 _specs = ['--arg1', 'value1', '-1', '-1.0']
69 _mydict = neutronV20.parse_args_to_dict(_specs)
70 self.assertEqual(['value1', '-1', '-1.0'],
71 _mydict['arg1'])
72
73 def test_badarg_duplicate(self):
74 _specs = ['--tag=t', '--arg1', 'value1', '--arg1', 'value1']
75 self.assertRaises(exceptions.CommandError,
76 neutronV20.parse_args_to_dict, _specs)
77
78 def test_badarg_early_type_specification(self):
79 _specs = ['type=dict', 'key=value']
80 self.assertRaises(exceptions.CommandError,
81 neutronV20.parse_args_to_dict, _specs)
82
83 def test_arg(self):
84 _specs = ['--tag=t', '--arg1', 'value1']
85 self.assertEqual('value1',
86 neutronV20.parse_args_to_dict(_specs)['arg1'])
87
88 def test_arg_invalid_syntax(self):
89 _specs = ['--tag=t', '---arg1', 'value1']
90 self.assertRaises(exceptions.CommandError,
91 neutronV20.parse_args_to_dict, _specs)
92
93 def test_dict_arg(self):
94 _specs = ['--tag=t', '--arg1', 'type=dict', 'key1=value1,key2=value2']
95 arg1 = neutronV20.parse_args_to_dict(_specs)['arg1']
96 self.assertEqual('value1', arg1['key1'])
97 self.assertEqual('value2', arg1['key2'])
98
99 def test_dict_arg_with_attribute_named_type(self):
100 _specs = ['--tag=t', '--arg1', 'type=dict', 'type=value1,key2=value2']
101 arg1 = neutronV20.parse_args_to_dict(_specs)['arg1']
102 self.assertEqual('value1', arg1['type'])
103 self.assertEqual('value2', arg1['key2'])
104
105 def test_list_of_dict_arg(self):
106 _specs = ['--tag=t', '--arg1', 'type=dict',
107 'list=true', 'key1=value1,key2=value2']
108 arg1 = neutronV20.parse_args_to_dict(_specs)['arg1']
109 self.assertEqual('value1', arg1[0]['key1'])
110 self.assertEqual('value2', arg1[0]['key2'])
111
112 def test_parse_args_to_dict_bad_type(self):
113 _specs = ['--badtypearg', 'type=badtype', 'val1']
114 ex = self.assertRaises(exceptions.CommandError,
115 neutronV20.parse_args_to_dict, _specs)
116 self.assertEqual('Invalid value_specs --badtypearg type=badtype val1: '
117 'type badtype is not supported',
118 str(ex))
119
120 def test_clear_action(self):
121 _specs = ['--anyarg', 'action=clear']
122 args = neutronV20.parse_args_to_dict(_specs)
123 self.assertIsNone(args['anyarg'])
124
125 def test_bad_values_str_without_value(self):
126 _specs = ['--strarg', 'type=str']
127 ex = self.assertRaises(exceptions.CommandError,
128 neutronV20.parse_args_to_dict, _specs)
129 self.assertEqual('Invalid values_specs --strarg type=str',
130 str(ex))
131
132 def test_bad_values_list(self):
133 _specs = ['--listarg', 'list=true', 'type=str']
134 self.assertRaises(exceptions.CommandError,
135 neutronV20.parse_args_to_dict, _specs)
136 _specs = ['--listarg', 'type=list']
137 self.assertRaises(exceptions.CommandError,
138 neutronV20.parse_args_to_dict, _specs)
139 _specs = ['--listarg', 'type=list', 'action=clear']
140 self.assertRaises(exceptions.CommandError,
141 neutronV20.parse_args_to_dict, _specs)