Merge lp:~ack/landscape-client/mocker-replace-test-customgraph into lp:~landscape/landscape-client/trunk

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: 881
Merged at revision: 887
Proposed branch: lp:~ack/landscape-client/mocker-replace-test-customgraph
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 142 lines (+26/-27)
1 file modified
landscape/manager/tests/test_customgraph.py (+26/-27)
To merge this branch: bzr merge lp:~ack/landscape-client/mocker-replace-test-customgraph
Reviewer Review Type Date Requested Status
Chris Glass (community) Approve
Bogdana Vereha (community) Approve
🤖 Landscape Builder test results Approve
Review via email: mp+297578@code.launchpad.net

Commit message

Replace mocker uses in test_customgraph.

Description of the change

Replace mocker uses in test_customgraph.

Testing instructions:

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: Success
Revno: 881
Branch: lp:~ack/landscape-client/mocker-replace-test-customgraph
Jenkins: https://ci.lscape.net/job/latch-test/5079/

review: Approve (test results)
Revision history for this message
Bogdana Vereha (bogdana) wrote :

+1

review: Approve
Revision history for this message
Chris Glass (tribaal) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/manager/tests/test_customgraph.py'
2--- landscape/manager/tests/test_customgraph.py 2014-08-28 09:47:21 +0000
3+++ landscape/manager/tests/test_customgraph.py 2016-06-15 23:36:27 +0000
4@@ -2,6 +2,8 @@
5 import pwd
6 import logging
7
8+import mock
9+
10 from twisted.internet.error import ProcessDone
11 from twisted.python.failure import Failure
12
13@@ -10,10 +12,10 @@
14
15 from landscape.tests.helpers import (
16 LandscapeTest, ManagerHelper, StubProcessFactory, DummyProcess)
17-from landscape.tests.mocker import ANY
18
19
20 class CustomGraphManagerTests(LandscapeTest):
21+
22 helpers = [ManagerHelper]
23
24 def setUp(self):
25@@ -54,16 +56,14 @@
26 "graph-123"),
27 username)])
28
29- def test_add_graph_unknown_user(self):
30+ @mock.patch("pwd.getpwnam")
31+ def test_add_graph_unknown_user(self, mock_getpwnam):
32 """
33 Attempting to add a graph with an unknown user should not result in an
34 error, instead a message should be logged, the error will be picked up
35 when the graph executes.
36 """
37- mock_getpwnam = self.mocker.replace("pwd.getpwnam", passthrough=False)
38- mock_getpwnam("foo")
39- self.mocker.throw(KeyError("foo"))
40- self.mocker.replay()
41+ mock_getpwnam.side_effect = KeyError("foo")
42 error_message = "Attempt to add graph with unknown user foo"
43 self.log_helper.ignore_errors(error_message)
44 self.logger.setLevel(logging.ERROR)
45@@ -78,23 +78,20 @@
46 self.assertEqual(graph[0], 123)
47 self.assertEqual(graph[2], u"foo")
48 self.assertTrue(error_message in self.logfile.getvalue())
49-
50- def test_add_graph_for_user(self):
51- mock_chown = self.mocker.replace("os.chown", passthrough=False)
52- mock_chown(ANY, 1234, 5678)
53-
54- mock_chmod = self.mocker.replace("os.chmod", passthrough=False)
55- mock_chmod(ANY, 0700)
56-
57- mock_getpwnam = self.mocker.replace("pwd.getpwnam", passthrough=False)
58+ mock_getpwnam.assert_called_with("foo")
59+
60+ @mock.patch("os.chown")
61+ @mock.patch("os.chmod")
62+ @mock.patch("pwd.getpwnam")
63+ def test_add_graph_for_user(self, mock_getpwnam, mock_chmod, mock_chown):
64
65 class pwnam(object):
66 pw_uid = 1234
67 pw_gid = 5678
68 pw_dir = self.makeFile()
69
70- self.expect(mock_getpwnam("bar")).result(pwnam)
71- self.mocker.replay()
72+ mock_getpwnam.return_value = pwnam
73+
74 self.manager.dispatch_message(
75 {"type": "custom-graph-add",
76 "interpreter": "/bin/sh",
77@@ -107,6 +104,10 @@
78 "graph-123"),
79 "bar")])
80
81+ mock_chown.assert_called_with(mock.ANY, 1234, 5678)
82+ mock_chmod.assert_called_with(mock.ANY, 0700)
83+ mock_getpwnam.assert_called_with("bar")
84+
85 def test_remove_unknown_graph(self):
86 self.manager.dispatch_message(
87 {"type": "custom-graph-remove",
88@@ -307,21 +308,19 @@
89 "type": "custom-graph"}])
90 return result.addCallback(check)
91
92- def test_run_user(self):
93+ @mock.patch("pwd.getpwnam")
94+ def test_run_user(self, mock_getpwnam):
95 filename = self.makeFile("some content")
96 self.store.add_graph(123, filename, "bar")
97 factory = StubProcessFactory()
98 self.graph_manager.process_factory = factory
99
100- mock_getpwnam = self.mocker.replace("pwd.getpwnam", passthrough=False)
101-
102 class pwnam(object):
103 pw_uid = 1234
104 pw_gid = 5678
105 pw_dir = self.makeFile()
106
107- self.expect(mock_getpwnam("bar")).result(pwnam)
108- self.mocker.replay()
109+ mock_getpwnam.return_value = pwnam
110
111 result = self.graph_manager.run()
112
113@@ -333,6 +332,7 @@
114 self.assertEqual(spawn[4], "/")
115 self.assertEqual(spawn[5], 1234)
116 self.assertEqual(spawn[6], 5678)
117+ mock_getpwnam.assert_called_with("bar")
118
119 self._exit_process_protocol(spawn[0], "spam")
120
121@@ -365,11 +365,9 @@
122
123 return result.addCallback(check)
124
125- def test_run_unknown_user(self):
126- mock_getpwnam = self.mocker.replace("pwd.getpwnam", passthrough=False)
127- mock_getpwnam("foo")
128- self.mocker.throw(KeyError("foo"))
129- self.mocker.replay()
130+ @mock.patch("pwd.getpwnam")
131+ def test_run_unknown_user(self, mock_getpwnam):
132+ mock_getpwnam.side_effect = KeyError("foo")
133
134 self.manager.config.script_users = "foo"
135
136@@ -389,6 +387,7 @@
137 "script-hash": "",
138 "values": []}},
139 "type": "custom-graph"}])
140+ mock_getpwnam.assert_called_with("foo")
141
142 return result.addCallback(check)
143

Subscribers

People subscribed via source and target branches

to all changes: