root / kamaki / cli / test.py @ bb50c4ec
History | View | Annotate | Download (18.3 kB)
1 | 2fde8651 | Stavros Sachtouris | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 2fde8651 | Stavros Sachtouris | #
|
3 | 2fde8651 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 2fde8651 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 2fde8651 | Stavros Sachtouris | # conditions are met:
|
6 | 2fde8651 | Stavros Sachtouris | #
|
7 | 2fde8651 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 2fde8651 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 2fde8651 | Stavros Sachtouris | # disclaimer.
|
10 | 2fde8651 | Stavros Sachtouris | #
|
11 | 2fde8651 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 2fde8651 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 2fde8651 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 2fde8651 | Stavros Sachtouris | # provided with the distribution.
|
15 | 2fde8651 | Stavros Sachtouris | #
|
16 | 2fde8651 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 2fde8651 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 2fde8651 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 2fde8651 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 2fde8651 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 2fde8651 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 2fde8651 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 2fde8651 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 2fde8651 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 2fde8651 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 2fde8651 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 2fde8651 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 2fde8651 | Stavros Sachtouris | #
|
29 | 2fde8651 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 2fde8651 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 2fde8651 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 2fde8651 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 2fde8651 | Stavros Sachtouris | |
34 | 2fde8651 | Stavros Sachtouris | from unittest import makeSuite, TestSuite, TextTestRunner, TestCase |
35 | 2fde8651 | Stavros Sachtouris | from inspect import getmembers, isclass |
36 | 98093aac | Stavros Sachtouris | from tempfile import NamedTemporaryFile |
37 | 4769da6b | Stavros Sachtouris | from mock import patch, call |
38 | 4769da6b | Stavros Sachtouris | from itertools import product |
39 | 2fde8651 | Stavros Sachtouris | |
40 | 2fde8651 | Stavros Sachtouris | |
41 | 98093aac | Stavros Sachtouris | class History(TestCase): |
42 | 98093aac | Stavros Sachtouris | |
43 | 98093aac | Stavros Sachtouris | def setUp(self): |
44 | 98093aac | Stavros Sachtouris | from kamaki.cli.history import History as HClass |
45 | 98093aac | Stavros Sachtouris | self.HCLASS = HClass
|
46 | 98093aac | Stavros Sachtouris | self.file = NamedTemporaryFile()
|
47 | 98093aac | Stavros Sachtouris | |
48 | 98093aac | Stavros Sachtouris | def tearDown(self): |
49 | 98093aac | Stavros Sachtouris | self.file.close()
|
50 | 98093aac | Stavros Sachtouris | |
51 | 98093aac | Stavros Sachtouris | def test__match(self): |
52 | 98093aac | Stavros Sachtouris | self.assertRaises(AttributeError, self.HCLASS._match, 'ok', 42) |
53 | 98093aac | Stavros Sachtouris | self.assertRaises(TypeError, self.HCLASS._match, 2.71, 'ok') |
54 | 98093aac | Stavros Sachtouris | for args, expected in ( |
55 | 98093aac | Stavros Sachtouris | (('XXX', None), True), |
56 | 98093aac | Stavros Sachtouris | ((None, None), True), |
57 | 98093aac | Stavros Sachtouris | (('this line has some terms', 'some terms'), True), |
58 | 98093aac | Stavros Sachtouris | (('this line has some terms', 'some bad terms'), False), |
59 | 98093aac | Stavros Sachtouris | (('small line', 'not so small line terms'), False), |
60 | 98093aac | Stavros Sachtouris | ((['line', 'with', 'some', 'terms'], 'some terms'), True), |
61 | 98093aac | Stavros Sachtouris | ((['line', 'with', 'some terms'], 'some terms'), False)): |
62 | 98093aac | Stavros Sachtouris | self.assertEqual(self.HCLASS._match(*args), expected) |
63 | 98093aac | Stavros Sachtouris | |
64 | 98093aac | Stavros Sachtouris | def test_add(self): |
65 | 98093aac | Stavros Sachtouris | history = self.HCLASS(self.file.name) |
66 | 98093aac | Stavros Sachtouris | some_strings = ('a brick', 'two bricks', 'another brick', 'A wall!') |
67 | 98093aac | Stavros Sachtouris | for i, line in enumerate(some_strings): |
68 | 98093aac | Stavros Sachtouris | history.add(line) |
69 | 98093aac | Stavros Sachtouris | self.file.seek(0) |
70 | 98093aac | Stavros Sachtouris | self.assertEqual(
|
71 | 98093aac | Stavros Sachtouris | self.file.read(), '\n'.join(some_strings[:(i + 1)]) + '\n') |
72 | 98093aac | Stavros Sachtouris | |
73 | edaf3ba6 | Stavros Sachtouris | def test_empty(self): |
74 | 98093aac | Stavros Sachtouris | content = 'a brick\ntwo bricks\nanother brick\nA wall!\n'
|
75 | 98093aac | Stavros Sachtouris | self.file.write(content)
|
76 | 98093aac | Stavros Sachtouris | self.file.flush()
|
77 | 98093aac | Stavros Sachtouris | self.file.seek(0) |
78 | 98093aac | Stavros Sachtouris | self.assertEqual(self.file.read(), content) |
79 | 98093aac | Stavros Sachtouris | history = self.HCLASS(self.file.name) |
80 | edaf3ba6 | Stavros Sachtouris | history.empty() |
81 | 98093aac | Stavros Sachtouris | self.file.seek(0) |
82 | 5e383dd4 | Stavros Sachtouris | self.assertEqual(self.file.read(), '0\n') |
83 | 98093aac | Stavros Sachtouris | |
84 | 98093aac | Stavros Sachtouris | def test_retrieve(self): |
85 | 98093aac | Stavros Sachtouris | sample_history = ( |
86 | 98093aac | Stavros Sachtouris | 'kamaki history show\n',
|
87 | 98093aac | Stavros Sachtouris | 'kamaki file list\n',
|
88 | a44a9d97 | Stavros Sachtouris | 'kamaki file create /pithos/f1\n',
|
89 | a44a9d97 | Stavros Sachtouris | 'kamaki file info /pithos/f1\n',
|
90 | a44a9d97 | Stavros Sachtouris | 'last command is always excluded')
|
91 | 98093aac | Stavros Sachtouris | self.file.write(''.join(sample_history)) |
92 | 98093aac | Stavros Sachtouris | self.file.flush()
|
93 | 98093aac | Stavros Sachtouris | |
94 | 98093aac | Stavros Sachtouris | history = self.HCLASS(self.file.name) |
95 | a44a9d97 | Stavros Sachtouris | self.assertRaises(ValueError, history.retrieve, 'must be number') |
96 | a44a9d97 | Stavros Sachtouris | self.assertRaises(TypeError, history.retrieve, [1, 2, 3]) |
97 | 98093aac | Stavros Sachtouris | |
98 | a44a9d97 | Stavros Sachtouris | for i in (0, len(sample_history) + 1, - len(sample_history) - 1): |
99 | a44a9d97 | Stavros Sachtouris | self.assertEqual(history.retrieve(i), None) |
100 | 98093aac | Stavros Sachtouris | for i in range(1, len(sample_history)): |
101 | 5e383dd4 | Stavros Sachtouris | self.assertEqual(history.retrieve(i), sample_history[i])
|
102 | a44a9d97 | Stavros Sachtouris | self.assertEqual(history.retrieve(- i), sample_history[- i])
|
103 | 98093aac | Stavros Sachtouris | |
104 | 5e383dd4 | Stavros Sachtouris | def test_limit(self): |
105 | 5e383dd4 | Stavros Sachtouris | sample_history = ( |
106 | 5e383dd4 | Stavros Sachtouris | 'kamaki history show\n',
|
107 | 5e383dd4 | Stavros Sachtouris | 'kamaki file list\n',
|
108 | 5e383dd4 | Stavros Sachtouris | 'kamaki file create /pithos/f1\n',
|
109 | 5e383dd4 | Stavros Sachtouris | 'kamaki file info /pithos/f1\n',
|
110 | 5e383dd4 | Stavros Sachtouris | 'last command is always excluded')
|
111 | 5e383dd4 | Stavros Sachtouris | sample_len = len(sample_history)
|
112 | 5e383dd4 | Stavros Sachtouris | self.file.write(''.join(sample_history)) |
113 | 5e383dd4 | Stavros Sachtouris | self.file.flush()
|
114 | 5e383dd4 | Stavros Sachtouris | history = self.HCLASS(self.file.name) |
115 | 5e383dd4 | Stavros Sachtouris | |
116 | 5e383dd4 | Stavros Sachtouris | for value, exp_e in ( |
117 | 5e383dd4 | Stavros Sachtouris | (-2, ValueError), |
118 | 5e383dd4 | Stavros Sachtouris | ('non int', ValueError), |
119 | 5e383dd4 | Stavros Sachtouris | (None, TypeError)): |
120 | 5e383dd4 | Stavros Sachtouris | try:
|
121 | 5e383dd4 | Stavros Sachtouris | history.limit = value |
122 | 5e383dd4 | Stavros Sachtouris | except Exception as e: |
123 | 5e383dd4 | Stavros Sachtouris | self.assertTrue(isinstance(e, exp_e)) |
124 | 5e383dd4 | Stavros Sachtouris | |
125 | 5e383dd4 | Stavros Sachtouris | history.limit = 10
|
126 | 5e383dd4 | Stavros Sachtouris | self.assertEqual(history.limit, 10) |
127 | 5e383dd4 | Stavros Sachtouris | self.file.seek(0) |
128 | 5e383dd4 | Stavros Sachtouris | self.assertEqual(len(self.file.readlines()), sample_len) |
129 | 5e383dd4 | Stavros Sachtouris | |
130 | 5e383dd4 | Stavros Sachtouris | history.limit = sample_len - 1
|
131 | 5e383dd4 | Stavros Sachtouris | self.assertEqual(history.limit, sample_len - 1) |
132 | 5e383dd4 | Stavros Sachtouris | self.file.seek(0) |
133 | 5e383dd4 | Stavros Sachtouris | self.assertEqual(len(self.file.readlines()), sample_len) |
134 | 5e383dd4 | Stavros Sachtouris | |
135 | 98093aac | Stavros Sachtouris | |
136 | 4769da6b | Stavros Sachtouris | class LoggerMethods(TestCase): |
137 | 4769da6b | Stavros Sachtouris | |
138 | 4769da6b | Stavros Sachtouris | class PseudoLogger(object): |
139 | 4769da6b | Stavros Sachtouris | level = 'some level'
|
140 | 4769da6b | Stavros Sachtouris | _setLevel_calls = [] |
141 | 4769da6b | Stavros Sachtouris | _addHandler_calls = [] |
142 | 4769da6b | Stavros Sachtouris | |
143 | 4769da6b | Stavros Sachtouris | def setLevel(self, *args): |
144 | 4769da6b | Stavros Sachtouris | self._setLevel_calls.append(args)
|
145 | 4769da6b | Stavros Sachtouris | |
146 | 4769da6b | Stavros Sachtouris | def addHandler(self, *args): |
147 | 4769da6b | Stavros Sachtouris | self._addHandler_calls.append(args)
|
148 | 4769da6b | Stavros Sachtouris | |
149 | 4769da6b | Stavros Sachtouris | class PseudoHandler(object): |
150 | 4769da6b | Stavros Sachtouris | _setFormatter_calls = [] |
151 | 4769da6b | Stavros Sachtouris | |
152 | 4769da6b | Stavros Sachtouris | def setFormatter(self, *args): |
153 | 4769da6b | Stavros Sachtouris | self._setFormatter_calls.append(args)
|
154 | 4769da6b | Stavros Sachtouris | |
155 | 4769da6b | Stavros Sachtouris | def setUp(self): |
156 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import LOG_FILE, _blacklist |
157 | 4769da6b | Stavros Sachtouris | self.LF, self.BL = list(LOG_FILE), dict(_blacklist) |
158 | 4769da6b | Stavros Sachtouris | |
159 | 4769da6b | Stavros Sachtouris | def tearDown(self): |
160 | 4769da6b | Stavros Sachtouris | self.PseudoLogger._setLevel_calls = []
|
161 | 4769da6b | Stavros Sachtouris | self.PseudoLogger._addHandler_calls = []
|
162 | 4769da6b | Stavros Sachtouris | self.PseudoLogger._setFormatter_calls = []
|
163 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import LOG_FILE, _blacklist |
164 | 4769da6b | Stavros Sachtouris | for e in LOG_FILE: |
165 | 4769da6b | Stavros Sachtouris | LOG_FILE.pop() |
166 | 4769da6b | Stavros Sachtouris | for e in self.LF: |
167 | 4769da6b | Stavros Sachtouris | LOG_FILE.append(e) |
168 | 4769da6b | Stavros Sachtouris | _blacklist.clear() |
169 | 4769da6b | Stavros Sachtouris | _blacklist.update(self.BL)
|
170 | 4769da6b | Stavros Sachtouris | |
171 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.logging.getLogger', return_value=PseudoLogger()) |
172 | 4769da6b | Stavros Sachtouris | def test_deactivate(self, GL): |
173 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import deactivate, _blacklist |
174 | 4769da6b | Stavros Sachtouris | self.assertEqual(_blacklist, {})
|
175 | 4769da6b | Stavros Sachtouris | deactivate('some logger')
|
176 | 4769da6b | Stavros Sachtouris | GL.assert_called_once_with('some logger')
|
177 | 4769da6b | Stavros Sachtouris | self.assertEqual(
|
178 | 4769da6b | Stavros Sachtouris | _blacklist.get('some logger', None), self.PseudoLogger.level) |
179 | 4769da6b | Stavros Sachtouris | from logging import CRITICAL |
180 | 4769da6b | Stavros Sachtouris | self.assertEqual(self.PseudoLogger._setLevel_calls[-1], (CRITICAL, )) |
181 | 4769da6b | Stavros Sachtouris | |
182 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.logging.getLogger', return_value=PseudoLogger()) |
183 | 4769da6b | Stavros Sachtouris | def test_activate(self, GL): |
184 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import activate |
185 | 4769da6b | Stavros Sachtouris | activate('another logger')
|
186 | 4769da6b | Stavros Sachtouris | GL.assert_called_once_with('another logger')
|
187 | 4769da6b | Stavros Sachtouris | self.assertEqual(
|
188 | 4769da6b | Stavros Sachtouris | self.PseudoLogger._setLevel_calls[-1], (self.PseudoLogger.level, )) |
189 | 4769da6b | Stavros Sachtouris | |
190 | 4769da6b | Stavros Sachtouris | def test_get_log_filename(self): |
191 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import get_log_filename, LOG_FILE |
192 | 4769da6b | Stavros Sachtouris | f = NamedTemporaryFile() |
193 | 4769da6b | Stavros Sachtouris | for e in LOG_FILE: |
194 | 4769da6b | Stavros Sachtouris | LOG_FILE.pop() |
195 | 4769da6b | Stavros Sachtouris | LOG_FILE.append(f.name) |
196 | 4769da6b | Stavros Sachtouris | self.assertEqual(get_log_filename(), f.name)
|
197 | 4769da6b | Stavros Sachtouris | LOG_FILE.pop() |
198 | 4769da6b | Stavros Sachtouris | LOG_FILE.append(2 * f.name)
|
199 | 4769da6b | Stavros Sachtouris | print('\n Should print error msg here: ')
|
200 | 4769da6b | Stavros Sachtouris | self.assertEqual(get_log_filename(), None) |
201 | 4769da6b | Stavros Sachtouris | |
202 | 4769da6b | Stavros Sachtouris | def test_set_log_filename(self): |
203 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import set_log_filename, LOG_FILE |
204 | 4769da6b | Stavros Sachtouris | for n in ('some name', 'some other name'): |
205 | 4769da6b | Stavros Sachtouris | set_log_filename(n) |
206 | 4769da6b | Stavros Sachtouris | self.assertEqual(LOG_FILE[0], n) |
207 | 4769da6b | Stavros Sachtouris | |
208 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.get_logger', return_value=PseudoLogger()) |
209 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.logging.Formatter', return_value='f0rm4t') |
210 | 4769da6b | Stavros Sachtouris | @patch(
|
211 | 4769da6b | Stavros Sachtouris | 'kamaki.cli.logger.logging.StreamHandler',
|
212 | 4769da6b | Stavros Sachtouris | return_value=PseudoHandler()) |
213 | 4769da6b | Stavros Sachtouris | @patch(
|
214 | 4769da6b | Stavros Sachtouris | 'kamaki.cli.logger.logging.FileHandler',
|
215 | 4769da6b | Stavros Sachtouris | return_value=PseudoHandler()) |
216 | 4769da6b | Stavros Sachtouris | def test__add_logger(self, FH, SH, F, GL): |
217 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import _add_logger |
218 | 4769da6b | Stavros Sachtouris | from logging import DEBUG |
219 | 4769da6b | Stavros Sachtouris | stdf, cnt = '%(name)s\n %(message)s', 0 |
220 | 4769da6b | Stavros Sachtouris | for name, level, filename, fmt in product( |
221 | 4769da6b | Stavros Sachtouris | ('my logger', ),
|
222 | 4769da6b | Stavros Sachtouris | ('my level', None), |
223 | 4769da6b | Stavros Sachtouris | ('my filename', None), |
224 | 4769da6b | Stavros Sachtouris | ('my fmt', None)): |
225 | 4769da6b | Stavros Sachtouris | log = _add_logger(name, level, filename, fmt) |
226 | 4769da6b | Stavros Sachtouris | self.assertTrue(isinstance(log, self.PseudoLogger)) |
227 | 4769da6b | Stavros Sachtouris | self.assertEqual(GL.mock_calls[-1], call(name)) |
228 | 4769da6b | Stavros Sachtouris | if filename:
|
229 | 4769da6b | Stavros Sachtouris | self.assertEqual(FH.mock_calls[-1], call(filename)) |
230 | 4769da6b | Stavros Sachtouris | else:
|
231 | 4769da6b | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-1], call()) |
232 | 4769da6b | Stavros Sachtouris | self.assertEqual(F.mock_calls[-1], call(fmt or stdf)) |
233 | 4769da6b | Stavros Sachtouris | self.assertEqual(
|
234 | 4769da6b | Stavros Sachtouris | self.PseudoHandler._setFormatter_calls[-1], ('f0rm4t', )) |
235 | 4769da6b | Stavros Sachtouris | cnt += 1
|
236 | 4769da6b | Stavros Sachtouris | self.assertEqual(len(self.PseudoLogger._addHandler_calls), cnt) |
237 | 4769da6b | Stavros Sachtouris | h = self.PseudoLogger._addHandler_calls[-1] |
238 | 4769da6b | Stavros Sachtouris | self.assertTrue(isinstance(h[0], self.PseudoHandler)) |
239 | 4769da6b | Stavros Sachtouris | l = self.PseudoLogger._setLevel_calls[-1] |
240 | 4769da6b | Stavros Sachtouris | self.assertEqual(l, (level or DEBUG, )) |
241 | 4769da6b | Stavros Sachtouris | |
242 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.get_log_filename', return_value='my log fname') |
243 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.get_logger', return_value='my get logger ret') |
244 | 4769da6b | Stavros Sachtouris | def test_add_file_logger(self, GL, GLF): |
245 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import add_file_logger |
246 | 4769da6b | Stavros Sachtouris | with patch('kamaki.cli.logger._add_logger', return_value='AL') as AL: |
247 | 4769da6b | Stavros Sachtouris | GLFcount = GLF.call_count |
248 | 4769da6b | Stavros Sachtouris | for name, level, filename in product( |
249 | 4769da6b | Stavros Sachtouris | ('my name'), ('my level', None), ('my filename', None)): |
250 | 4769da6b | Stavros Sachtouris | self.assertEqual(add_file_logger(name, level, filename), 'AL') |
251 | 4769da6b | Stavros Sachtouris | self.assertEqual(AL.mock_calls[-1], call( |
252 | 4769da6b | Stavros Sachtouris | name, level, filename or 'my log fname', |
253 | 4769da6b | Stavros Sachtouris | fmt='%(name)s(%(levelname)s) %(asctime)s\n\t%(message)s'))
|
254 | 4769da6b | Stavros Sachtouris | if filename:
|
255 | 4769da6b | Stavros Sachtouris | self.assertEqual(GLFcount, GLF.call_count)
|
256 | 4769da6b | Stavros Sachtouris | else:
|
257 | 4769da6b | Stavros Sachtouris | GLFcount = GLF.call_count |
258 | 4769da6b | Stavros Sachtouris | self.assertEqual(GLF.mock_calls[-1], call()) |
259 | 4769da6b | Stavros Sachtouris | with patch('kamaki.cli.logger._add_logger', side_effect=Exception): |
260 | 4769da6b | Stavros Sachtouris | self.assertEqual(add_file_logger('X'), 'my get logger ret') |
261 | 4769da6b | Stavros Sachtouris | GL.assert_called_once_with('X')
|
262 | 4769da6b | Stavros Sachtouris | |
263 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.get_logger', return_value='my get logger ret') |
264 | 4769da6b | Stavros Sachtouris | def test_add_stream_logger(self, GL): |
265 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import add_stream_logger |
266 | 4769da6b | Stavros Sachtouris | with patch('kamaki.cli.logger._add_logger', return_value='AL') as AL: |
267 | 4769da6b | Stavros Sachtouris | for name, level, fmt in product( |
268 | 4769da6b | Stavros Sachtouris | ('my name'), ('my level', None), ('my fmt', None)): |
269 | 4769da6b | Stavros Sachtouris | self.assertEqual(add_stream_logger(name, level, fmt), 'AL') |
270 | 4769da6b | Stavros Sachtouris | self.assertEqual(AL.mock_calls[-1], call(name, level, fmt=fmt)) |
271 | 4769da6b | Stavros Sachtouris | with patch('kamaki.cli.logger._add_logger', side_effect=Exception): |
272 | 4769da6b | Stavros Sachtouris | self.assertEqual(add_stream_logger('X'), 'my get logger ret') |
273 | 4769da6b | Stavros Sachtouris | GL.assert_called_once_with('X')
|
274 | 4769da6b | Stavros Sachtouris | |
275 | 4769da6b | Stavros Sachtouris | @patch('kamaki.cli.logger.logging.getLogger', return_value=PseudoLogger()) |
276 | 4769da6b | Stavros Sachtouris | def test_get_logger(self, GL): |
277 | 4769da6b | Stavros Sachtouris | from kamaki.cli.logger import get_logger |
278 | 4769da6b | Stavros Sachtouris | get_logger('my logger name')
|
279 | 4769da6b | Stavros Sachtouris | GL.assert_called_once_with('my logger name')
|
280 | 4769da6b | Stavros Sachtouris | |
281 | 4769da6b | Stavros Sachtouris | |
282 | f10cef01 | Stavros Sachtouris | _RET = None
|
283 | f10cef01 | Stavros Sachtouris | |
284 | f10cef01 | Stavros Sachtouris | |
285 | f10cef01 | Stavros Sachtouris | class PseudoException(object): |
286 | f10cef01 | Stavros Sachtouris | |
287 | f10cef01 | Stavros Sachtouris | def __init__(self, *args): |
288 | f10cef01 | Stavros Sachtouris | global _RET
|
289 | f10cef01 | Stavros Sachtouris | _RET = args |
290 | f10cef01 | Stavros Sachtouris | |
291 | f10cef01 | Stavros Sachtouris | |
292 | f10cef01 | Stavros Sachtouris | class CLIError(TestCase): |
293 | f10cef01 | Stavros Sachtouris | |
294 | f10cef01 | Stavros Sachtouris | @patch('__builtin__.super', return_value=PseudoException()) |
295 | f10cef01 | Stavros Sachtouris | def test___init__(self, S): |
296 | f10cef01 | Stavros Sachtouris | from kamaki.cli.errors import CLIError |
297 | f10cef01 | Stavros Sachtouris | global _RET
|
298 | f10cef01 | Stavros Sachtouris | for message, details, importance in ( |
299 | f10cef01 | Stavros Sachtouris | ('some msg', [], 0), |
300 | f10cef01 | Stavros Sachtouris | ('some msg\n', 'details', 0), |
301 | f10cef01 | Stavros Sachtouris | ('some msg', ['details1', 'details2'], 10)): |
302 | f10cef01 | Stavros Sachtouris | clie = CLIError(message, details, importance) |
303 | f10cef01 | Stavros Sachtouris | self.assertEqual(S.mock_calls[-1], call(CLIError, clie)) |
304 | f10cef01 | Stavros Sachtouris | self.assertEqual(_RET[0], (message + '\n') if ( |
305 | f10cef01 | Stavros Sachtouris | message and not message.endswith('\n')) else message) |
306 | f10cef01 | Stavros Sachtouris | self.assertEqual(clie.details, (list(details) if ( |
307 | f10cef01 | Stavros Sachtouris | isinstance(details, list)) else ['%s' % details]) if ( |
308 | f10cef01 | Stavros Sachtouris | details) else [])
|
309 | f10cef01 | Stavros Sachtouris | self.assertEqual(clie.importance, int(importance)) |
310 | f10cef01 | Stavros Sachtouris | clie = CLIError(message, details, 'non int')
|
311 | f10cef01 | Stavros Sachtouris | self.assertEqual(clie.importance, 0) |
312 | f10cef01 | Stavros Sachtouris | |
313 | cfebddb5 | Stavros Sachtouris | def test_raiseCLIError(self): |
314 | cfebddb5 | Stavros Sachtouris | from kamaki.cli.errors import raiseCLIError, CLIError |
315 | cfebddb5 | Stavros Sachtouris | for err, message, importance, details in ( |
316 | cfebddb5 | Stavros Sachtouris | (Exception('msg'), '', 0, []), |
317 | cfebddb5 | Stavros Sachtouris | (Exception('msg'), 'orther msg', 0, []), |
318 | cfebddb5 | Stavros Sachtouris | (Exception('msg'), 'orther msg', 0, ['d1', 'd2']), |
319 | cfebddb5 | Stavros Sachtouris | (Exception('msg'), '', 10, []), |
320 | cfebddb5 | Stavros Sachtouris | (Exception('msg'), '', None, []), |
321 | cfebddb5 | Stavros Sachtouris | (CLIError('some msg'), '', None, ['d1', 'd2']) |
322 | cfebddb5 | Stavros Sachtouris | ): |
323 | cfebddb5 | Stavros Sachtouris | try:
|
324 | cfebddb5 | Stavros Sachtouris | raiseCLIError(err, message, importance, details) |
325 | cfebddb5 | Stavros Sachtouris | except CLIError as clie: |
326 | cfebddb5 | Stavros Sachtouris | exp_msg = '%s' % (message or err) |
327 | cfebddb5 | Stavros Sachtouris | exp_msg += '' if exp_msg.endswith('\n') else '\n' |
328 | cfebddb5 | Stavros Sachtouris | self.assertEqual('%s' % clie, exp_msg) |
329 | cfebddb5 | Stavros Sachtouris | self.assertEqual(clie.importance, importance or 0) |
330 | cfebddb5 | Stavros Sachtouris | exp_d = list(details) if isinstance(details, list) else [ |
331 | cfebddb5 | Stavros Sachtouris | '%s' % (details or '')] |
332 | cfebddb5 | Stavros Sachtouris | base_msg = '%s' % err
|
333 | cfebddb5 | Stavros Sachtouris | if message and base_msg != message: |
334 | cfebddb5 | Stavros Sachtouris | exp_d.append(base_msg) |
335 | cfebddb5 | Stavros Sachtouris | self.assertEqual(clie.details, exp_d)
|
336 | cfebddb5 | Stavros Sachtouris | |
337 | f10cef01 | Stavros Sachtouris | |
338 | f10cef01 | Stavros Sachtouris | class CLIUnimplemented(TestCase): |
339 | f10cef01 | Stavros Sachtouris | |
340 | f10cef01 | Stavros Sachtouris | def test___init__(self): |
341 | f10cef01 | Stavros Sachtouris | from kamaki.cli.errors import CLIUnimplemented |
342 | f10cef01 | Stavros Sachtouris | cliu = CLIUnimplemented() |
343 | f10cef01 | Stavros Sachtouris | self.assertEqual(
|
344 | f10cef01 | Stavros Sachtouris | '%s' % cliu,
|
345 | f10cef01 | Stavros Sachtouris | 'I \'M SORRY, DAVE.\nI \'M AFRAID I CAN\'T DO THAT.\n')
|
346 | f10cef01 | Stavros Sachtouris | self.assertEqual(cliu.details, [
|
347 | f10cef01 | Stavros Sachtouris | ' _ |',
|
348 | f10cef01 | Stavros Sachtouris | ' _-- --_ |',
|
349 | f10cef01 | Stavros Sachtouris | ' -- -- |',
|
350 | f10cef01 | Stavros Sachtouris | ' -- . -- |',
|
351 | f10cef01 | Stavros Sachtouris | ' -_ _- |',
|
352 | f10cef01 | Stavros Sachtouris | ' -_ _- |',
|
353 | f10cef01 | Stavros Sachtouris | ' - |'])
|
354 | f10cef01 | Stavros Sachtouris | self.assertEqual(cliu.importance, 3) |
355 | f10cef01 | Stavros Sachtouris | |
356 | f10cef01 | Stavros Sachtouris | |
357 | f10cef01 | Stavros Sachtouris | class CLIBaseUrlError(TestCase): |
358 | f10cef01 | Stavros Sachtouris | |
359 | f10cef01 | Stavros Sachtouris | def test___init__(self): |
360 | f10cef01 | Stavros Sachtouris | from kamaki.cli.errors import CLIBaseUrlError |
361 | f10cef01 | Stavros Sachtouris | for service in ('', 'some service'): |
362 | f10cef01 | Stavros Sachtouris | clibue = CLIBaseUrlError(service=service) |
363 | f10cef01 | Stavros Sachtouris | self.assertEqual('%s' % clibue, 'No URL for %s\n' % service) |
364 | f10cef01 | Stavros Sachtouris | self.assertEqual(clibue.details, [
|
365 | f10cef01 | Stavros Sachtouris | 'Two ways to resolve this:',
|
366 | f10cef01 | Stavros Sachtouris | '(Use the correct cloud name, instead of "default")',
|
367 | f10cef01 | Stavros Sachtouris | 'A. (recommended) Let kamaki discover endpoint URLs for all',
|
368 | f10cef01 | Stavros Sachtouris | 'services by setting a single Authentication URL and token:',
|
369 | f10cef01 | Stavros Sachtouris | ' /config set cloud.default.url <AUTH_URL>',
|
370 | f10cef01 | Stavros Sachtouris | ' /config set cloud.default.token <t0k3n>',
|
371 | f10cef01 | Stavros Sachtouris | 'B. (advanced users) Explicitly set an %s endpoint URL' % (
|
372 | f10cef01 | Stavros Sachtouris | service.upper()), |
373 | f10cef01 | Stavros Sachtouris | 'Note: URL option has a higher priority, so delete it to',
|
374 | f10cef01 | Stavros Sachtouris | 'make that work',
|
375 | f10cef01 | Stavros Sachtouris | ' /config delete cloud.default.url',
|
376 | f10cef01 | Stavros Sachtouris | ' /config set cloud.%s.url <%s_URL>' % (
|
377 | f10cef01 | Stavros Sachtouris | service, service.upper())]) |
378 | f10cef01 | Stavros Sachtouris | self.assertEqual(clibue.importance, 2) |
379 | f10cef01 | Stavros Sachtouris | |
380 | f10cef01 | Stavros Sachtouris | |
381 | f10cef01 | Stavros Sachtouris | class CLISyntaxError(TestCase): |
382 | f10cef01 | Stavros Sachtouris | |
383 | f10cef01 | Stavros Sachtouris | def test___init__(self): |
384 | f10cef01 | Stavros Sachtouris | from kamaki.cli.errors import CLISyntaxError |
385 | f10cef01 | Stavros Sachtouris | clise = CLISyntaxError() |
386 | f10cef01 | Stavros Sachtouris | self.assertEqual('%s' % clise, 'Syntax Error\n') |
387 | f10cef01 | Stavros Sachtouris | self.assertEqual(clise.details, [])
|
388 | f10cef01 | Stavros Sachtouris | self.assertEqual(clise.importance, 1) |
389 | f10cef01 | Stavros Sachtouris | |
390 | f10cef01 | Stavros Sachtouris | |
391 | 8694bdad | Stavros Sachtouris | class CLIInvalidArgument(TestCase): |
392 | 8694bdad | Stavros Sachtouris | |
393 | 8694bdad | Stavros Sachtouris | def test___init__(self): |
394 | 8694bdad | Stavros Sachtouris | from kamaki.cli.errors import CLIInvalidArgument |
395 | 8694bdad | Stavros Sachtouris | cliia = CLIInvalidArgument() |
396 | 8694bdad | Stavros Sachtouris | self.assertEqual('%s' % cliia, 'Invalid Argument\n') |
397 | 8694bdad | Stavros Sachtouris | self.assertEqual(cliia.details, [])
|
398 | 8694bdad | Stavros Sachtouris | self.assertEqual(cliia.importance, 1) |
399 | 8694bdad | Stavros Sachtouris | |
400 | 8694bdad | Stavros Sachtouris | |
401 | f10cef01 | Stavros Sachtouris | class CLIUnknownCommand(TestCase): |
402 | f10cef01 | Stavros Sachtouris | |
403 | f10cef01 | Stavros Sachtouris | def test___init__(self): |
404 | f10cef01 | Stavros Sachtouris | from kamaki.cli.errors import CLIUnknownCommand |
405 | f10cef01 | Stavros Sachtouris | cliec = CLIUnknownCommand() |
406 | f10cef01 | Stavros Sachtouris | self.assertEqual('%s' % cliec, 'Unknown Command\n') |
407 | f10cef01 | Stavros Sachtouris | self.assertEqual(cliec.details, [])
|
408 | f10cef01 | Stavros Sachtouris | self.assertEqual(cliec.importance, 1) |
409 | f10cef01 | Stavros Sachtouris | |
410 | f10cef01 | Stavros Sachtouris | |
411 | f10cef01 | Stavros Sachtouris | class CLICmdSpecError(TestCase): |
412 | f10cef01 | Stavros Sachtouris | |
413 | f10cef01 | Stavros Sachtouris | def test___init__(self): |
414 | f10cef01 | Stavros Sachtouris | from kamaki.cli.errors import CLICmdSpecError |
415 | f10cef01 | Stavros Sachtouris | clicse = CLICmdSpecError() |
416 | f10cef01 | Stavros Sachtouris | self.assertEqual('%s' % clicse, 'Command Specification Error\n') |
417 | f10cef01 | Stavros Sachtouris | self.assertEqual(clicse.details, [])
|
418 | f10cef01 | Stavros Sachtouris | self.assertEqual(clicse.importance, 0) |
419 | f10cef01 | Stavros Sachtouris | |
420 | f10cef01 | Stavros Sachtouris | |
421 | 2fde8651 | Stavros Sachtouris | # TestCase auxiliary methods
|
422 | 2fde8651 | Stavros Sachtouris | |
423 | 2fde8651 | Stavros Sachtouris | def runTestCase(cls, test_name, args=[], failure_collector=[]): |
424 | 2fde8651 | Stavros Sachtouris | """
|
425 | 2fde8651 | Stavros Sachtouris | :param cls: (TestCase) a set of Tests
|
426 | 2fde8651 | Stavros Sachtouris |
|
427 | 2fde8651 | Stavros Sachtouris | :param test_name: (str)
|
428 | 2fde8651 | Stavros Sachtouris |
|
429 | 2fde8651 | Stavros Sachtouris | :param args: (list) these are prefixed with test_ and used as params when
|
430 | 2fde8651 | Stavros Sachtouris | instantiating cls
|
431 | 2fde8651 | Stavros Sachtouris |
|
432 | 2fde8651 | Stavros Sachtouris | :param failure_collector: (list) collects info of test failures
|
433 | 2fde8651 | Stavros Sachtouris |
|
434 | 2fde8651 | Stavros Sachtouris | :returns: (int) total # of run tests
|
435 | 2fde8651 | Stavros Sachtouris | """
|
436 | 2fde8651 | Stavros Sachtouris | suite = TestSuite() |
437 | 2fde8651 | Stavros Sachtouris | if args:
|
438 | 2fde8651 | Stavros Sachtouris | suite.addTest(cls('_'.join(['test'] + args))) |
439 | 2fde8651 | Stavros Sachtouris | else:
|
440 | 2fde8651 | Stavros Sachtouris | suite.addTest(makeSuite(cls)) |
441 | 2fde8651 | Stavros Sachtouris | print('* Test * %s *' % test_name)
|
442 | 2fde8651 | Stavros Sachtouris | r = TextTestRunner(verbosity=2).run(suite)
|
443 | 2fde8651 | Stavros Sachtouris | failure_collector += r.failures |
444 | 2fde8651 | Stavros Sachtouris | return r.testsRun
|
445 | 2fde8651 | Stavros Sachtouris | |
446 | 2fde8651 | Stavros Sachtouris | |
447 | 2fde8651 | Stavros Sachtouris | def get_test_classes(module=__import__(__name__), name=''): |
448 | 2fde8651 | Stavros Sachtouris | module_stack = [module] |
449 | 2fde8651 | Stavros Sachtouris | while module_stack:
|
450 | 2fde8651 | Stavros Sachtouris | module = module_stack[-1]
|
451 | 2fde8651 | Stavros Sachtouris | module_stack = module_stack[:-1]
|
452 | 2fde8651 | Stavros Sachtouris | for objname, obj in getmembers(module): |
453 | 2fde8651 | Stavros Sachtouris | if (objname == name or not name): |
454 | 2fde8651 | Stavros Sachtouris | if isclass(obj) and objname != 'TestCase' and ( |
455 | 2fde8651 | Stavros Sachtouris | issubclass(obj, TestCase)):
|
456 | 2fde8651 | Stavros Sachtouris | yield (obj, objname)
|
457 | 2fde8651 | Stavros Sachtouris | |
458 | 2fde8651 | Stavros Sachtouris | |
459 | 2fde8651 | Stavros Sachtouris | def main(argv): |
460 | 2fde8651 | Stavros Sachtouris | found = False
|
461 | 2fde8651 | Stavros Sachtouris | failure_collector = list()
|
462 | 2fde8651 | Stavros Sachtouris | num_of_tests = 0
|
463 | 2fde8651 | Stavros Sachtouris | for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''): |
464 | 2fde8651 | Stavros Sachtouris | found = True
|
465 | 2fde8651 | Stavros Sachtouris | num_of_tests += runTestCase(cls, name, argv[2:], failure_collector)
|
466 | 2fde8651 | Stavros Sachtouris | if not found: |
467 | 2fde8651 | Stavros Sachtouris | print('Test "%s" not found' % ' '.join(argv[1:])) |
468 | 2fde8651 | Stavros Sachtouris | else:
|
469 | 2fde8651 | Stavros Sachtouris | for i, failure in enumerate(failure_collector): |
470 | 2fde8651 | Stavros Sachtouris | print('Failure %s: ' % (i + 1)) |
471 | 2fde8651 | Stavros Sachtouris | for field in failure: |
472 | 2fde8651 | Stavros Sachtouris | print('\t%s' % field)
|
473 | 2fde8651 | Stavros Sachtouris | print('\nTotal tests run: %s' % num_of_tests)
|
474 | 2fde8651 | Stavros Sachtouris | print('Total failures: %s' % len(failure_collector)) |
475 | 2fde8651 | Stavros Sachtouris | |
476 | 2fde8651 | Stavros Sachtouris | |
477 | 2fde8651 | Stavros Sachtouris | if __name__ == '__main__': |
478 | 2fde8651 | Stavros Sachtouris | from sys import argv |
479 | 2fde8651 | Stavros Sachtouris | main(argv) |