Merge lp:~danilo/landscape-client/mock-tests-sysinfo-deployment into lp:~landscape/landscape-client/trunk

Proposed by Данило Шеган
Status: Merged
Approved by: Данило Шеган
Approved revision: 849
Merged at revision: 891
Proposed branch: lp:~danilo/landscape-client/mock-tests-sysinfo-deployment
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 180 lines (+64/-68)
1 file modified
landscape/sysinfo/tests/test_deployment.py (+64/-68)
To merge this branch: bzr merge lp:~danilo/landscape-client/mock-tests-sysinfo-deployment
Reviewer Review Type Date Requested Status
🤖 Landscape Builder test results Approve
Benji York (community) Approve
Adam Collard (community) Approve
Review via email: mp+297579@code.launchpad.net

Commit message

Migrate landscape/sysinfo/tests/test_deployment.py from mocker to mock.

Description of the change

Migrate landscape/sysinfo/tests/test_deployment.py from mocker to mock.

The trickier one was test_output_is_only_displayed_once_deferred_fires.

To post a comment you must log in.
Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: TRIAL_ARGS=-j4 make check
Result: Fail
Revno: 848
Branch: lp:~danilo/landscape-client/mock-tests-sysinfo-deployment
Jenkins: https://ci.lscape.net/job/latch-test/5081/

review: Needs Fixing (test results)
Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: TRIAL_ARGS=-j4 make check
Result: Success
Revno: 848
Branch: lp:~danilo/landscape-client/mock-tests-sysinfo-deployment
Jenkins: https://ci.lscape.net/job/latch-test/5088/

review: Approve (test results)
Revision history for this message
Adam Collard (adam-collard) wrote :

Small nit inline

review: Approve
Revision history for this message
Benji York (benji) wrote :

This looks good.

review: Approve
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :
Download full text (187.0 KiB)

The attempt to merge lp:~danilo/landscape-client/mock-tests-sysinfo-deployment into lp:landscape-client failed. Below is the output from the failed tests.

python setup.py build_ext -i
running build_ext
Running 2170 tests.
landscape.broker.tests.test_amp
  RemoteBrokerTest
    test_call_if_accepted ... [OK]
    test_fire_event ... [OK]
    test_get_accepted_message_types ... [OK]
    test_call_if_accepted_with_not_accepted ... [OK]
    test_is_message_pending ... [OK]
    test_listen_events ... [OK]
    test_get_server_uuid ... [OK]
    test_ping ... [OK]
    test_method_call_error ... [OK]
    test_register_client ... [OK]
    test_register ... [OK]
    test_register_client_accepted_message_type ... [OK]
    test_reload_configuration ... [OK]
    test_send_message ... [OK]
    test_send_message_with_urgent ... [OK]
    test_stop_clients ... [OK]
  RemoteClientTest
    test_exit ... [OK]
    test_fire_event ... [OK]
    test_message ... [OK]
    test_method_call_error ... [OK]
    test_ping ... [OK]
landscape.broker.tests.test_client
  BrokerClientTest
    test_add ... [OK]
    test_dispatch_message ... [OK]
    test_dispatch_message_with_exception ... [OK]
    test_dispatch_message_with_no_handler ... [OK]
    test_exchange ... [OK]
    test_exchange_logs_errors_and_continues ... [OK]
    test_exchange_on_plugin_without_exchange_method ... [OK]
    test_exit ... [OK]
    test_fire_event ... [OK]
    test_fire_event_with_acceptance_changed ... [OK]
    test_fire_event_with_arguments ... [OK]
    test_fire_event_with_mixed_results ... [OK]
    test_get_named_plugin ... [OK]
    test_get_plugins_returns_a_copy ... [OK]
    test_get_plugins ... ...

Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

No approved revision specified.

Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: TRIAL_ARGS=-j4 make check
Result: Success
Revno: 849
Branch: lp:~danilo/landscape-client/mock-tests-sysinfo-deployment
Jenkins: https://ci.lscape.net/job/latch-test/5093/

review: Approve (test results)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/sysinfo/tests/test_deployment.py'
2--- landscape/sysinfo/tests/test_deployment.py 2014-03-27 13:52:48 +0000
3+++ landscape/sysinfo/tests/test_deployment.py 2016-06-16 15:42:59 +0000
4@@ -1,3 +1,4 @@
5+import mock
6 import os
7
8 from logging.handlers import RotatingFileHandler
9@@ -15,7 +16,6 @@
10 from landscape.sysinfo.load import Load
11
12 from landscape.tests.helpers import LandscapeTest, StandardIOHelper
13-from landscape.tests.mocker import ARGS, KWARGS
14
15
16 class DeploymentTest(LandscapeTest):
17@@ -109,38 +109,41 @@
18 self.assertEqual(sysinfo.get_notes(), ["Test note"])
19 self.assertEqual(sysinfo.get_footnotes(), ["Test footnote"])
20
21- def test_format_sysinfo_gets_correct_information(self):
22- format_sysinfo = self.mocker.replace("landscape.sysinfo.sysinfo."
23- "format_sysinfo")
24- format_sysinfo([("Test header", "Test value")],
25- ["Test note"], ["Test footnote"],
26- indent=" ")
27- format_sysinfo(ARGS, KWARGS)
28- self.mocker.count(0)
29- self.mocker.replay()
30-
31+ @mock.patch("landscape.sysinfo.deployment.format_sysinfo")
32+ def test_format_sysinfo_gets_correct_information(self, format_sysinfo):
33 run(["--sysinfo-plugins", "TestPlugin"])
34+ format_sysinfo.assert_called_once_with(
35+ [("Test header", "Test value")],
36+ ["Test note"], ["Test footnote"],
37+ indent=" ")
38
39 def test_format_sysinfo_output_is_printed(self):
40- format_sysinfo = self.mocker.replace("landscape.sysinfo.sysinfo."
41- "format_sysinfo")
42- format_sysinfo(ARGS, KWARGS)
43- self.mocker.result("Hello there!")
44- self.mocker.replay()
45-
46- run(["--sysinfo-plugins", "TestPlugin"])
47-
48+ with mock.patch(
49+ "landscape.sysinfo.deployment.format_sysinfo",
50+ return_value="Hello there!") as format_sysinfo:
51+ run(["--sysinfo-plugins", "TestPlugin"])
52+
53+ self.assertTrue(format_sysinfo.called)
54 self.assertEqual(self.stdout.getvalue(), "Hello there!\n")
55
56 def test_output_is_only_displayed_once_deferred_fires(self):
57 deferred = Deferred()
58- sysinfo = self.mocker.patch(SysInfoPluginRegistry)
59- sysinfo.run()
60- self.mocker.passthrough()
61- self.mocker.result(deferred)
62- self.mocker.replay()
63-
64- run(["--sysinfo-plugins", "TestPlugin"])
65+
66+ # We mock the sysinfo.run() to return a Deferred but still
67+ # run the actual sysinfo.run() to gather the results from all
68+ # the plugins. We cannot easily combine return_value and
69+ # side_effect because side_effect operates on the return_value,
70+ # thus firing the callback and writing sysinfo out to stdout.
71+ sysinfo = SysInfoPluginRegistry()
72+ original_sysinfo_run = sysinfo.run
73+ def wrapped_sysinfo_run(*args, **kwargs):
74+ original_sysinfo_run(*args, **kwargs)
75+ return deferred
76+ sysinfo.run = mock.Mock(side_effect=wrapped_sysinfo_run)
77+
78+ run(["--sysinfo-plugins", "TestPlugin"], sysinfo=sysinfo)
79+
80+ sysinfo.run.assert_called_once_with()
81
82 self.assertNotIn("Test note", self.stdout.getvalue())
83 deferred.callback(None)
84@@ -226,11 +229,10 @@
85 If landscape-sysinfo is running as a privileged user, then the logs
86 should be stored in the system-wide log directory.
87 """
88- uid_mock = self.mocker.replace("os.getuid")
89- uid_mock()
90- self.mocker.result(0)
91- self.mocker.replay()
92- self.assertEqual(get_landscape_log_directory(), "/var/log/landscape")
93+ with mock.patch("os.getuid", return_value=0) as uid_mock:
94+ self.assertEqual(
95+ get_landscape_log_directory(), "/var/log/landscape")
96+ uid_mock.assert_called_once_with()
97
98 def test_wb_logging_setup(self):
99 """
100@@ -250,28 +252,24 @@
101 self.assertFalse(logger.propagate)
102
103 def test_setup_logging_logs_to_var_log_if_run_as_root(self):
104- mock_os = self.mocker.replace("os")
105- mock_os.getuid()
106- self.mocker.result(0)
107-
108- # Ugh, sorry
109- mock_os.path.isdir("/var/log/landscape")
110- self.mocker.result(False)
111- mock_os.mkdir("/var/log/landscape")
112-
113- self.mocker.replace("__builtin__.open", passthrough=False)(
114- "/var/log/landscape/sysinfo.log", "a")
115-
116- self.mocker.replay()
117-
118- logger = getLogger("landscape-sysinfo")
119- self.assertEqual(logger.handlers, [])
120-
121- setup_logging()
122- handler = logger.handlers[0]
123- self.assertTrue(isinstance(handler, RotatingFileHandler))
124- self.assertEqual(handler.baseFilename,
125- "/var/log/landscape/sysinfo.log")
126+ with mock.patch.object(os, "getuid", return_value=0) as mock_getuid, \
127+ mock.patch.object(
128+ os.path, "isdir", return_value=False) as mock_isdir, \
129+ mock.patch.object(os, "mkdir") as mock_mkdir, \
130+ mock.patch("__builtin__.open") as mock_open:
131+ logger = getLogger("landscape-sysinfo")
132+ self.assertEqual(logger.handlers, [])
133+
134+ setup_logging()
135+ mock_getuid.assert_called_with()
136+ mock_isdir.assert_called_with("/var/log/landscape")
137+ mock_mkdir.assert_called_with("/var/log/landscape")
138+ mock_open.assert_called_with("/var/log/landscape/sysinfo.log", "a")
139+
140+ handler = logger.handlers[0]
141+ self.assertTrue(isinstance(handler, RotatingFileHandler))
142+ self.assertEqual(handler.baseFilename,
143+ "/var/log/landscape/sysinfo.log")
144
145 def test_create_log_dir(self):
146 log_dir = self.makeFile()
147@@ -280,20 +278,18 @@
148 self.assertTrue(os.path.exists(log_dir))
149
150 def test_run_sets_up_logging(self):
151- setup_logging_mock = self.mocker.replace(
152- "landscape.sysinfo.deployment.setup_logging")
153- setup_logging_mock()
154- self.mocker.replay()
155-
156- run(["--sysinfo-plugins", "TestPlugin"])
157+ with mock.patch(
158+ "landscape.sysinfo.deployment"
159+ ".setup_logging") as setup_logging_mock:
160+ run(["--sysinfo-plugins", "TestPlugin"])
161+ setup_logging_mock.assert_called_once_with()
162
163 def test_run_setup_logging_exits_gracefully(self):
164- setup_logging_mock = self.mocker.replace(
165- "landscape.sysinfo.deployment.setup_logging")
166- setup_logging_mock()
167- self.mocker.throw(IOError("Read-only filesystem."))
168- self.mocker.replay()
169- error = self.assertRaises(SystemExit, run,
170- ["--sysinfo-plugins", "TestPlugin"])
171- self.assertEqual(error.message,
172- "Unable to setup logging. Read-only filesystem.")
173+ io_error = IOError("Read-only filesystem.")
174+ with mock.patch(
175+ "landscape.sysinfo.deployment.setup_logging",
176+ side_effect=io_error) as setup_logging_mock:
177+ error = self.assertRaises(
178+ SystemExit, run, ["--sysinfo-plugins", "TestPlugin"])
179+ self.assertEqual(
180+ error.message, "Unable to setup logging. Read-only filesystem.")

Subscribers

People subscribed via source and target branches

to all changes: