Merge lp:~milo/lava-tool/more-unit-tests into lp:~linaro-validation/lava-tool/trunk

Proposed by Milo Casagrande
Status: Merged
Merged at revision: 188
Proposed branch: lp:~milo/lava-tool/more-unit-tests
Merge into: lp:~linaro-validation/lava-tool/trunk
Prerequisite: lp:~milo/lava-tool/lava-169
Diff against target: 460 lines (+342/-2)
7 files modified
lava/helper/tests/test_command.py (+14/-1)
lava/job/tests/test_commands.py (+47/-0)
lava/script/tests/test_commands.py (+59/-0)
lava/script/tests/test_script.py (+80/-0)
lava/tests/test_parameter.py (+73/-0)
lava_tool/tests/__init__.py (+2/-0)
lava_tool/tests/test_utils.py (+67/-1)
To merge this branch: bzr merge lp:~milo/lava-tool/more-unit-tests
Reviewer Review Type Date Requested Status
Antonio Terceiro Needs Fixing
Linaro Validation Team Pending
Review via email: mp+175847@code.launchpad.net

Description of the change

More unit tests and some cleanups.

To post a comment you must log in.
lp:~milo/lava-tool/more-unit-tests updated
426. By Milo Casagrande

Merged from parent branch.

427. By Milo Casagrande

Added one more test for URL creation method.

428. By Milo Casagrande

Merged from parent, fixed conflicts.

Revision history for this message
Antonio Terceiro (terceiro) wrote :
Download full text (14.1 KiB)

Thanks for your work on this. Adding tests is really really nice, and I
hope you are able to do that with the other components as well. :-)

Some comments below.

 review needs-fixing

> === modified file 'lava/commands.py'
> --- lava/commands.py 2013-07-24 14:47:25 +0000
> +++ lava/commands.py 2013-07-24 14:47:25 +0000
> @@ -102,24 +102,31 @@
> raise CommandError("'{0}' already exists, and is a "
> "file.".format(self.args.DIR))
>
> - if not os.path.isdir(full_path):
> - try:
> - os.makedirs(full_path)
> - except OSError:
> - raise CommandError("Cannot create directory "
> - "'{0}'.".format(self.args.DIR))
> -
> - test_path = os.path.join(full_path, TESTS_DIR)
> - if not os.path.isdir(test_path):
> - try:
> - os.makedirs(test_path)
> - except OSError:
> - raise CommandError("Cannot create directory "
> - "'{0}'.".format(self.args.DIR))
> + self._create_dir(full_path)
> + test_path = self._create_dir(full_path, TESTS_DIR)
>
> data = self._update_data()
> self._create_files(data, full_path, test_path)
>
> + def _create_dir(self, full_path, dir_name=None):
> + """Checks if a directory does not exists, and creates it.
> +
> + :param full_path: The path where the directory should be created.
> + :param dir_name: An optional name for a directory to be created at
> + full_path (dir_name will be joined with full_path).
> + :return The path of the created directory."""
> + created_dir = full_path
> + if dir_name:
> + created_dir = os.path.join(full_path, dir_name)
> +
> + if not os.path.isdir(created_dir):
> + try:
> + os.makedirs(created_dir)
> + except OSError:
> + raise CommandError("Cannot create directory "
> + "'{0}'.".format(created_dir))
> + return created_dir
> +
> def _update_data(self):
> """Updates the template and ask values to the user.
>
>
> === modified file 'lava/helper/tests/test_command.py'
> --- lava/helper/tests/test_command.py 2013-07-24 14:47:25 +0000
> +++ lava/helper/tests/test_command.py 2013-07-24 14:47:25 +0000
> @@ -151,3 +151,47 @@
> obtained = BaseCommand.verify_file_extension(
> "/tmp/a_fake.extension", extension, supported)
> self.assertEquals(expected, obtained)
> +
> + def test_verify_and_create_url(self):
> + server = "www.example.org"
> + endpoint = "RPC"
> + expected = "https://www.example.org/RPC/"
> + obtained = BaseCommand.verify_and_create_url(server, endpoint)
> + self.assertEquals(expected, obtained)
> +
> + def test_verify_and_create_url_with_scheme(self):
> + server = "http://www.example.org"
> + endpoint = "RPC/"
> + expected = "http://www.example.org/RPC/"
> + obtained = BaseCommand.verify_and_create_url(server, endpoint)
> + self.ass...

review: Needs Fixing
Revision history for this message
Milo Casagrande (milo) wrote :

On Wed, Jul 24, 2013 at 11:07 PM, Antonio Terceiro
<email address hidden> wrote:
>
>
> this part is duplicated with other tests. can we extract it to the base class
> instead?

Hmmm... not so sure... It is tied to the "command" creation, so each
command will have its own initialization, plus there are (and might be
in the future) commands with more than two args that need to be
tested.
I actually do not mind duplicating that code since we are testing
functionalities. I would be more concerned with duplications happening
in the actual code.

--
Milo Casagrande | Automation Engineer
Linaro.org <www.linaro.org> │ Open source software for ARM SoCs

lp:~milo/lava-tool/more-unit-tests updated
429. By Milo Casagrande

Merged from parent branch.

430. By Milo Casagrande

Fixed test.

431. By Milo Casagrande

Added tests for the script commands.

432. By Milo Casagrande

More utils tests.

433. By Milo Casagrande

Added test for authenticated_server.

434. By Milo Casagrande

Merged from parent branch.

435. By Milo Casagrande

Merged from parent.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lava/helper/tests/test_command.py'
2--- lava/helper/tests/test_command.py 2013-07-29 08:49:24 +0000
3+++ lava/helper/tests/test_command.py 2013-07-29 08:49:25 +0000
4@@ -16,7 +16,9 @@
5 # You should have received a copy of the GNU Lesser General Public License
6 # along with lava-tool. If not, see <http://www.gnu.org/licenses/>.
7
8-"""lava.herlp.command module tests."""
9+"""lava.helper.command module tests."""
10+
11+from mock import MagicMock, patch
12
13
14 from lava.helper.command import BaseCommand
15@@ -32,3 +34,14 @@
16 command.register_arguments(self.parser)
17 name, args, kwargs = self.parser.method_calls[0]
18 self.assertIn("--non-interactive", args)
19+
20+ @patch("lava.helper.command.AuthenticatingServerProxy", create=True)
21+ def test_authenticated_server(self, mocked_auth_server):
22+ command = BaseCommand(self.parser, self.args)
23+ command.config = MagicMock()
24+ command.config.get = MagicMock()
25+ command.config.get.side_effect = ["www.example.org", "RPC"]
26+
27+ command.authenticated_server()
28+
29+ self.assertTrue(mocked_auth_server.called)
30
31=== modified file 'lava/job/tests/test_commands.py'
32--- lava/job/tests/test_commands.py 2013-07-29 08:49:24 +0000
33+++ lava/job/tests/test_commands.py 2013-07-29 08:49:25 +0000
34@@ -31,6 +31,7 @@
35 new,
36 run,
37 submit,
38+ status,
39 )
40 from lava.parameter import Parameter
41 from lava.tool.errors import CommandError
42@@ -64,6 +65,22 @@
43 if os.path.exists(self.args.FILE):
44 os.unlink(self.args.FILE)
45
46+ def test_register_arguments(self):
47+ new_cmd = new(self.parser, self.args)
48+ new_cmd.register_arguments(self.parser)
49+
50+ # Make sure we do not forget about this test.
51+ self.assertEqual(3, len(self.parser.method_calls))
52+
53+ _, args, _ = self.parser.method_calls[0]
54+ self.assertIn("--non-interactive", args)
55+
56+ _, args, _ = self.parser.method_calls[1]
57+ self.assertIn("FILE", args)
58+
59+ _, args, _ = self.parser.method_calls[2]
60+ self.assertIn("--type", args)
61+
62 def test_create_new_file(self):
63 self.new_command.invoke()
64 self.assertTrue(os.path.exists(self.args.FILE))
65@@ -93,6 +110,19 @@
66
67 class JobRunTest(CommandTest):
68
69+ def test_register_arguments(self):
70+ run_cmd = run(self.parser, self.args)
71+ run_cmd.register_arguments(self.parser)
72+
73+ # Make sure we do not forget about this test.
74+ self.assertEqual(2, len(self.parser.method_calls))
75+
76+ _, args, _ = self.parser.method_calls[0]
77+ self.assertIn("--non-interactive", args)
78+
79+ _, args, _ = self.parser.method_calls[1]
80+ self.assertIn("FILE", args)
81+
82 def test_invoke_raises_0(self):
83 # Users passes a non existing job file to the run command.
84 self.args.FILE = self.tmp("test_invoke_raises_0.json")
85@@ -106,3 +136,20 @@
86 mocked_has_command.return_value = False
87 command = run(self.parser, self.args)
88 self.assertRaises(CommandError, command.invoke)
89+
90+
91+class TestsStatusCommand(CommandTest):
92+
93+ def test_register_arguments(self):
94+ self.args.JOB_ID = "1"
95+ status_cmd = status(self.parser, self.args)
96+ status_cmd.register_arguments(self.parser)
97+
98+ # Make sure we do not forget about this test.
99+ self.assertEqual(2, len(self.parser.method_calls))
100+
101+ _, args, _ = self.parser.method_calls[0]
102+ self.assertIn("--non-interactive", args)
103+
104+ _, args, _ = self.parser.method_calls[1]
105+ self.assertIn("JOB_ID", args)
106
107=== added directory 'lava/script/tests'
108=== added file 'lava/script/tests/__init__.py'
109=== added file 'lava/script/tests/test_commands.py'
110--- lava/script/tests/test_commands.py 1970-01-01 00:00:00 +0000
111+++ lava/script/tests/test_commands.py 2013-07-29 08:49:25 +0000
112@@ -0,0 +1,59 @@
113+# Copyright (C) 2013 Linaro Limited
114+#
115+# Author: Milo Casagrande <milo.casagrande@linaro.org>
116+#
117+# This file is part of lava-tool.
118+#
119+# lava-tool is free software: you can redistribute it and/or modify
120+# it under the terms of the GNU Lesser General Public License version 3
121+# as published by the Free Software Foundation
122+#
123+# lava-tool is distributed in the hope that it will be useful,
124+# but WITHOUT ANY WARRANTY; without even the implied warranty of
125+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
126+# GNU General Public License for more details.
127+#
128+# You should have received a copy of the GNU Lesser General Public License
129+# along with lava-tool. If not, see <http://www.gnu.org/licenses/>.
130+
131+"""
132+Tests for lava.script.commands.
133+"""
134+
135+from lava.helper.tests.helper_test import HelperTest
136+from lava.script.commands import (
137+ run,
138+ submit,
139+)
140+
141+
142+class RunCommandTests(HelperTest):
143+
144+ def test_register_arguments(self):
145+ run_cmd = run(self.parser, self.args)
146+ run_cmd.register_arguments(self.parser)
147+
148+ # Make sure we do not forget about this test.
149+ self.assertEqual(2, len(self.parser.method_calls))
150+
151+ _, args, _ = self.parser.method_calls[0]
152+ self.assertIn("--non-interactive", args)
153+
154+ _, args, _ = self.parser.method_calls[1]
155+ self.assertIn("FILE", args)
156+
157+
158+class SubmitCommandTests(HelperTest):
159+
160+ def test_register_arguments(self):
161+ submit_cmd = submit(self.parser, self.args)
162+ submit_cmd.register_arguments(self.parser)
163+
164+ # Make sure we do not forget about this test.
165+ self.assertEqual(2, len(self.parser.method_calls))
166+
167+ _, args, _ = self.parser.method_calls[0]
168+ self.assertIn("--non-interactive", args)
169+
170+ _, args, _ = self.parser.method_calls[1]
171+ self.assertIn("FILE", args)
172
173=== added file 'lava/script/tests/test_script.py'
174--- lava/script/tests/test_script.py 1970-01-01 00:00:00 +0000
175+++ lava/script/tests/test_script.py 2013-07-29 08:49:25 +0000
176@@ -0,0 +1,80 @@
177+# Copyright (C) 2013 Linaro Limited
178+#
179+# Author: Milo Casagrande <milo.casagrande@linaro.org>
180+#
181+# This file is part of lava-tool.
182+#
183+# lava-tool is free software: you can redistribute it and/or modify
184+# it under the terms of the GNU Lesser General Public License version 3
185+# as published by the Free Software Foundation
186+#
187+# lava-tool is distributed in the hope that it will be useful,
188+# but WITHOUT ANY WARRANTY; without even the implied warranty of
189+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
190+# GNU General Public License for more details.
191+#
192+# You should have received a copy of the GNU Lesser General Public License
193+# along with lava-tool. If not, see <http://www.gnu.org/licenses/>.
194+
195+"""
196+Unittests for the ShellScript class.
197+"""
198+
199+import os
200+import stat
201+
202+from lava.helper.tests.helper_test import HelperTest
203+from lava.script import ShellScript
204+
205+
206+class ShellScriptTests(HelperTest):
207+
208+ """ShellScript tests."""
209+
210+ def test_create_file(self):
211+ # Tests that a shell script is actually written.
212+ try:
213+ temp_file = self.tmp("a_shell_test")
214+ script = ShellScript(temp_file)
215+ script.write()
216+
217+ self.assertTrue(os.path.isfile(temp_file))
218+ finally:
219+ os.unlink(temp_file)
220+
221+ def test_assure_executable(self):
222+ # Tests that the shell script created is executable.
223+ try:
224+ temp_file = self.tmp("a_shell_test")
225+ script = ShellScript(temp_file)
226+ script.write()
227+
228+ expected = (stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH |
229+ stat.S_IXOTH)
230+
231+ obtained = stat.S_IMODE(os.stat(temp_file).st_mode)
232+ self.assertEquals(expected, obtained)
233+ finally:
234+ os.unlink(temp_file)
235+
236+ def test_shell_script_content(self):
237+ # Tests that the shell script created contains the exepcted content.
238+ try:
239+ temp_file = self.tmp("a_shell_test")
240+ script = ShellScript(temp_file)
241+ script.write()
242+
243+ obtained = ""
244+ with open(temp_file) as read_file:
245+ obtained = read_file.read()
246+
247+ expected = ("#!/bin/sh\n# Automatic generated "
248+ "content by lava-tool.\n# Please add your own "
249+ "instructions.\n#\n# You can use all the avialable "
250+ "Bash commands.\n#\n# For the available LAVA "
251+ "commands, see:\n# http://lava.readthedocs.org/\n"
252+ "#\n")
253+
254+ self.assertEquals(expected, obtained)
255+ finally:
256+ os.unlink(temp_file)
257
258=== modified file 'lava/tests/test_parameter.py'
259--- lava/tests/test_parameter.py 2013-07-29 08:49:24 +0000
260+++ lava/tests/test_parameter.py 2013-07-29 08:49:25 +0000
261@@ -26,6 +26,7 @@
262 from lava.parameter import (
263 ListParameter,
264 Parameter,
265+ SingleChoiceParameter,
266 )
267 from lava_tool.utils import to_list
268
269@@ -131,3 +132,75 @@
270 expected = ["foo", 1, "", "bar"]
271 obtained = self.list_parameter.deserialize(expected)
272 self.assertEqual(expected, obtained)
273+
274+ def test_set_value_0(self):
275+ # Pass a string to a ListParameter, expect a list.
276+ set_value = "foo"
277+ expected = [set_value]
278+ self.list_parameter.set(set_value)
279+ self.assertEquals(expected, self.list_parameter.value)
280+
281+ def test_set_value_1(self):
282+ # Pass a list to a ListParameter, expect the same list.
283+ expected = ["foo", "bar"]
284+ self.list_parameter.set(expected)
285+ self.assertEquals(expected, self.list_parameter.value)
286+
287+ def test_add_value_0(self):
288+ # Add a value to a ListParameter, expect a list back.
289+ add_value = "foo"
290+ expected = [add_value]
291+ self.list_parameter.add(add_value)
292+ self.assertEquals(expected, self.list_parameter.value)
293+
294+ def test_add_value_1(self):
295+ # Add a list value to a ListParameter with already a value set, expect
296+ # a list with both values.
297+ # The ListParameter is initialized with a string.
298+ add_value = ["foo"]
299+ list_param = ListParameter("list", value="bar")
300+ expected = ["bar", "foo"]
301+ list_param.add(add_value)
302+ self.assertEquals(expected, list_param.value)
303+
304+ def test_add_value_2(self):
305+ # Add a list value to a ListParameter with already a value set, expect
306+ # a list with both values.
307+ # The ListParameter is initialized with a list.
308+ add_value = ["foo"]
309+ list_param = ListParameter("list", value=["bar", "baz"])
310+ expected = ["bar", "baz", "foo"]
311+ list_param.add(add_value)
312+ self.assertEquals(expected, list_param.value)
313+
314+
315+class TestsSingleChoiceParameter(GeneralParameterTest):
316+
317+ def setUp(self):
318+ super(TestsSingleChoiceParameter, self).setUp()
319+ self.choices = ["foo", "bar", "baz", "bam"]
320+ self.param_id = "single_choice"
321+ self.single_choice_param = SingleChoiceParameter(self.param_id,
322+ self.choices)
323+
324+ def test_with_old_value(self):
325+ # There is an old value for a single choice parameter, the user
326+ # is prompted to select from the list of values, but she presses
327+ # enter. The old value is returned.
328+ old_value = "bat"
329+ self.mocked_raw_input.side_effect = ["\n"]
330+ obtained = self.single_choice_param.prompt("", old_value=old_value)
331+ self.assertEquals(old_value, obtained)
332+
333+ def test_without_old_value(self):
334+ # There is no old value, user just select the first choice.
335+ self.mocked_raw_input.side_effect = ["1"]
336+ obtained = self.single_choice_param.prompt("")
337+ self.assertEquals("foo", obtained)
338+
339+ def test_with_wrong_user_input(self):
340+ # No old value, user inserts at least two wrong choices, and the select
341+ # the third one.
342+ self.mocked_raw_input.side_effect = ["1000", "0", "3"]
343+ obtained = self.single_choice_param.prompt("")
344+ self.assertEquals("baz", obtained)
345
346=== modified file 'lava_tool/tests/__init__.py'
347--- lava_tool/tests/__init__.py 2013-07-29 08:49:24 +0000
348+++ lava_tool/tests/__init__.py 2013-07-29 08:49:25 +0000
349@@ -42,6 +42,8 @@
350 'lava.helper.tests.test_template',
351 'lava.job.tests.test_commands',
352 'lava.job.tests.test_job',
353+ 'lava.script.tests.test_commands',
354+ 'lava.script.tests.test_script',
355 'lava.testdef.tests.test_commands',
356 'lava.tests.test_commands',
357 'lava.tests.test_config',
358
359=== modified file 'lava_tool/tests/test_utils.py'
360--- lava_tool/tests/test_utils.py 2013-07-29 08:49:24 +0000
361+++ lava_tool/tests/test_utils.py 2013-07-29 08:49:25 +0000
362@@ -18,9 +18,10 @@
363
364 """lava_tool.utils tests."""
365
366-import sys
367 import os
368+import shutil
369 import subprocess
370+import sys
371 import tempfile
372
373 from unittest import TestCase
374@@ -33,10 +34,12 @@
375 from lava.tool.errors import CommandError
376 from lava_tool.utils import (
377 can_edit_file,
378+ create_dir,
379 edit_file,
380 execute,
381 has_command,
382 retrieve_file,
383+ verify_and_create_url,
384 verify_file_extension,
385 )
386
387@@ -137,6 +140,29 @@
388 os.unlink(file_name_with_suffix.name)
389 os.unlink(hidden_file.name)
390
391+ def test_retrieve_job_file_2(self):
392+ # Pass a file with the valid extension.
393+ temp_file = tempfile.NamedTemporaryFile(suffix=".json")
394+ obtained = retrieve_file(temp_file.name, ["json"])
395+ self.assertEquals(temp_file.name, obtained)
396+
397+ def test_retrieve_job_file_3(self):
398+ # Pass a file with a non-valid extension.
399+ temp_file = tempfile.NamedTemporaryFile(suffix=".bork")
400+ self.assertRaises(
401+ CommandError, retrieve_file, temp_file.name, ["json"])
402+
403+ @patch("os.listdir")
404+ def test_retrieve_job_file_4(self, mocked_os_listdir):
405+ # Pass hidden and wrong files and make sure exception is thrown.
406+ a_hidden_file = ".a_hidden.json"
407+ b_hidden_file = ".b_hidden.json"
408+ c_wrong_file = "a_wrong_file.bork"
409+
410+ mocked_os_listdir.return_value = [a_hidden_file, b_hidden_file, c_wrong_file]
411+ self.assertRaises(
412+ CommandError, retrieve_file, tempfile.gettempdir(), ["json"])
413+
414 @patch("lava_tool.utils.subprocess")
415 def test_execute_0(self, mocked_subprocess):
416 mocked_subprocess.check_call = MagicMock()
417@@ -214,3 +240,43 @@
418 obtained = f.read()
419
420 self.assertEqual(expected, obtained)
421+
422+ def test_verify_and_create_url_0(self):
423+ expected = "https://www.example.org/"
424+ obtained = verify_and_create_url("www.example.org", "")
425+ self.assertEquals(expected, obtained)
426+
427+ def test_verify_and_create_url_1(self):
428+ expected = "http://www.example.org/"
429+ obtained = verify_and_create_url("http://www.example.org")
430+ self.assertEquals(expected, obtained)
431+
432+ def test_verify_and_create_url_2(self):
433+ expected = "http://www.example.org/RPC/"
434+ obtained = verify_and_create_url("http://www.example.org", "RPC")
435+ self.assertEquals(expected, obtained)
436+
437+ def test_verify_and_create_url_3(self):
438+ expected = "https://www.example.org/RPC/"
439+ obtained = verify_and_create_url("www.example.org/", "/RPC/")
440+ self.assertEquals(expected, obtained)
441+
442+ def test_create_dir_0(self):
443+ try:
444+ temp_dir = os.path.join(tempfile.gettempdir(), "a_dir")
445+ create_dir(temp_dir)
446+ self.assertTrue(os.path.isdir(temp_dir))
447+ finally:
448+ shutil.rmtree(temp_dir)
449+
450+ def test_create_dir_1(self):
451+ try:
452+ temp_dir = os.path.join(tempfile.gettempdir(), "a_dir")
453+ create_dir(temp_dir, "subdir")
454+ self.assertTrue(os.path.isdir(os.path.join(temp_dir, "subdir")))
455+ finally:
456+ shutil.rmtree(temp_dir)
457+
458+ def test_create_dir_2(self):
459+ temp_dir = os.path.join("/", "a_temp_dir")
460+ self.assertRaises(CommandError, create_dir, temp_dir)

Subscribers

People subscribed via source and target branches