Merge lp:~jtv/maas-test/pidfiles-run into lp:maas-test

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: 108
Merged at revision: 106
Proposed branch: lp:~jtv/maas-test/pidfiles-run
Merge into: lp:maas-test
Diff against target: 236 lines (+66/-33)
3 files modified
maastest/proxyfixture.py (+13/-3)
maastest/tests/test_proxyfixture.py (+49/-30)
maastest/utils.py (+4/-0)
To merge this branch: bzr merge lp:~jtv/maas-test/pidfiles-run
Reviewer Review Type Date Requested Status
Julian Edwards (community) Approve
Review via email: mp+200954@code.launchpad.net

Commit message

Move pidfiles into /run/maas-test.

Description of the change

As discussed with Julian, pidfiles belong in /run. As a simple solution to keeping the name meaningful ("/run/proxy.pid" would be a bit too generic) and keep room for other pidfiles we may want to add later, I created a directory /run/maas-test.

This wasn't quite as trivial as updating a path and making sure it's created, because existing tests also needed updating. Don't want those writing to /run!

Next: moving the logs into /var/log/maas-test.

Jeroen

To post a comment you must log in.
Revision history for this message
Julian Edwards (julian-edwards) wrote :

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

 review: approve
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlLOPskACgkQWhGlTF8G/HfifACfdc01DXHhEyqZheAvuu9zPTi0
ExwAn33VNd2jDyz9z683Fq91dCHra8Xv
=E+N3
-----END PGP SIGNATURE-----

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'maastest/proxyfixture.py'
2--- maastest/proxyfixture.py 2014-01-09 03:51:20 +0000
3+++ maastest/proxyfixture.py 2014-01-09 06:08:12 +0000
4@@ -58,7 +58,7 @@
5
6 #TODO: port should be determined at runtime from the set of
7 # available ports rather than being an arbitrary one as it is here.
8- def __init__(self, config_dir=None, port=42676):
9+ def __init__(self, config_dir=None, port=42676, pidfile_dir=None):
10 """Create a `LocalProxyFixture` object.
11
12 :param url: The URL of the proxy to use. If None, LocalProxyFixture
13@@ -68,15 +68,20 @@
14 If URL is specified, this parameter is ignored.
15 :type port: int
16 :param config_dir: The directory in which to put the proxy's
17- config, pid file and cache.
18+ configuration and cache.
19 :type config_dir: string
20+ :param pid_dir: Optional directory in which to write the proxy's
21+ pidfile. Will be created if not present. Defaults to
22+ `/run/maas-test`.
23 """
24 super(LocalProxyFixture, self).__init__()
25 self.port = port
26 if config_dir is None:
27 config_dir = utils.DEFAULT_STATE_DIR
28 self.config_dir = config_dir
29- self.pid_file = os.path.join(self.config_dir, 'proxy.pid')
30+ if pidfile_dir is None:
31+ pidfile_dir = utils.DEFAULT_PIDFILE_DIR
32+ self.pid_file = os.path.join(pidfile_dir, 'proxy.pid')
33 self._network_address = None
34
35 def _get_config_arguments(self):
36@@ -119,6 +124,11 @@
37 # From this point, if further setup fails, we'll need to clean up.
38 self.addCleanup(self.kill_running_proxy)
39
40+ # Create pidfile directory if it did not already exist.
41+ pidfile_dir = os.path.dirname(self.pid_file)
42+ if not os.path.isdir(pidfile_dir):
43+ os.makedirs(pidfile_dir, 0o755)
44+
45 self.start_proxy()
46
47 def start_proxy(self):
48
49=== modified file 'maastest/tests/test_proxyfixture.py'
50--- maastest/tests/test_proxyfixture.py 2014-01-08 17:31:43 +0000
51+++ maastest/tests/test_proxyfixture.py 2014-01-09 06:08:12 +0000
52@@ -39,23 +39,52 @@
53 'netmask': '255.255.255.0',
54 }]}
55 self.patch(netifaces, 'ifaddresses', mock_ifaddresses)
56- self.temp_dir = self.useFixture(TempDir()).path
57+
58+ def make_dir(self):
59+ """Create a temporary directory for the duration of this test."""
60+ return self.useFixture(TempDir()).path
61+
62+ def make_fixture(self, port=None, config_dir=None, pidfile_dir=None):
63+ """Create a `LocalProxyFixture`.
64+
65+ :param port: Optional port. Defaults to a random choice.
66+ :param config_dir: Optional configuration directory. Defaults to a
67+ temporary directory.
68+ :param pidfile_dir: Optional pidfile directory. Defaults to a
69+ temporary directory.
70+ """
71+ if port is None:
72+ port = self.get_random_port_number()
73+ if config_dir is None:
74+ config_dir = self.make_dir()
75+ if pidfile_dir is None:
76+ pidfile_dir = self.make_dir()
77+ return LocalProxyFixture(
78+ port=port, config_dir=config_dir, pidfile_dir=pidfile_dir)
79
80 def get_random_port_number(self):
81 return random.randint(1, 65535)
82
83 def test_init_sets_properties(self):
84 port = self.get_random_port_number()
85- proxy_fixture = LocalProxyFixture(port=port, config_dir=self.temp_dir)
86+ config_dir = self.make_dir()
87+ pidfile_dir = self.make_dir()
88+ proxy_fixture = LocalProxyFixture(
89+ port=port, config_dir=config_dir, pidfile_dir=pidfile_dir)
90 self.assertEqual(port, proxy_fixture.port)
91- self.assertEqual(self.temp_dir, proxy_fixture.config_dir)
92+ self.assertEqual(config_dir, proxy_fixture.config_dir)
93 self.assertEqual(
94- os.path.join(self.temp_dir, 'proxy.pid'),
95+ os.path.join(pidfile_dir, 'proxy.pid'),
96 proxy_fixture.pid_file)
97
98+ def test_creates_pidfile_dir(self):
99+ pidfile_dir = os.path.join(self.make_dir(), "maas-test-pidfiles")
100+ with self.make_fixture(pidfile_dir=pidfile_dir):
101+ self.assertThat(pidfile_dir, DirExists())
102+
103 def test_get_url_returns_proxy_url(self):
104 port = self.get_random_port_number()
105- proxy_fixture = LocalProxyFixture(config_dir=self.temp_dir, port=port)
106+ proxy_fixture = self.make_fixture(port=port)
107 self.assertEqual(
108 "http://192.168.0.1:%s" % port,
109 proxy_fixture.get_url())
110@@ -64,9 +93,7 @@
111 self.patch(LocalProxyFixture, 'create_cache_root', mock.MagicMock())
112 self.patch(LocalProxyFixture, 'start_proxy', mock.MagicMock())
113
114- port = self.get_random_port_number()
115- config_dir = self.temp_dir
116- with LocalProxyFixture(config_dir=config_dir, port=port) as fixture:
117+ with self.make_fixture() as fixture:
118 pass
119
120 self.assertEqual(
121@@ -79,8 +106,7 @@
122 def test_start_proxy_starts_proxy(self):
123 mock_run_command = mock.MagicMock()
124 self.patch(utils, 'run_command', mock_run_command)
125- proxy_fixture = LocalProxyFixture(
126- config_dir=self.temp_dir, port=self.get_random_port_number())
127+ proxy_fixture = self.make_fixture()
128
129 proxy_fixture.start_proxy()
130
131@@ -91,28 +117,25 @@
132 mock_run_command.mock_calls[-1])
133
134 def test_create_cache_root_creates_directory(self):
135- proxy_fixture = LocalProxyFixture(
136- config_dir=self.temp_dir, port=self.get_random_port_number())
137+ config_dir = self.make_dir()
138+ proxy_fixture = self.make_fixture(config_dir=config_dir)
139
140 proxy_fixture.create_cache_root()
141 expected_cache_path = proxyfixture.DISK_CACHE_ROOT % {
142- 'config_dir': self.temp_dir}
143+ 'config_dir': config_dir}
144 self.assertThat(expected_cache_path, DirExists())
145
146 def test_create_cache_root_ignores_existing_directory(self):
147- temp_dir = self.useFixture(TempDir()).path
148- self.patch(proxyfixture, 'DISK_CACHE_ROOT', temp_dir)
149- proxy_fixture = LocalProxyFixture(
150- config_dir=self.temp_dir, port=self.get_random_port_number())
151+ disk_cache_root = self.make_dir()
152+ self.patch(proxyfixture, 'DISK_CACHE_ROOT', disk_cache_root)
153+ proxy_fixture = self.make_fixture()
154
155 proxy_fixture.create_cache_root()
156
157- self.assertThat(temp_dir, DirExists())
158+ self.assertThat(disk_cache_root, DirExists())
159
160 def test_get_network_address(self):
161- proxy_fixture = LocalProxyFixture(
162- config_dir=self.temp_dir, port=self.get_random_port_number())
163- proxy_adddress = proxy_fixture._get_network_address()
164+ proxy_adddress = self.make_fixture()._get_network_address()
165 self.assertEqual('192.168.0.1', proxy_adddress.ip.format())
166 self.assertEqual(24, proxy_adddress.prefixlen)
167
168@@ -122,8 +145,7 @@
169 mock_read_file = mock.MagicMock()
170 mock_read_file.return_value = bytes(self.getUniqueInteger())
171 self.patch(utils, 'read_file', mock_read_file)
172- proxy_fixture = LocalProxyFixture(config_dir=self.temp_dir)
173- proxy_fixture.kill_running_proxy()
174+ self.make_fixture().kill_running_proxy()
175 self.assertEqual(
176 mock.call(['kill', mock_read_file.return_value], check_call=True),
177 mock_run_command.mock_calls[0])
178@@ -134,8 +156,7 @@
179 mock_read_file = mock.MagicMock()
180 mock_read_file.side_effect = Exception(self.getUniqueString())
181 self.patch(utils, 'read_file', mock_read_file)
182- proxy_fixture = LocalProxyFixture(config_dir=self.temp_dir)
183- proxy_fixture.kill_running_proxy()
184+ self.make_fixture().kill_running_proxy()
185 mock_log_error.assert_has_calls([
186 mock.call("Unable to kill proxy:"),
187 mock.call(mock_read_file.side_effect.message)])
188@@ -149,8 +170,7 @@
189 mock_read_file = mock.MagicMock()
190 mock_read_file.return_value = bytes(self.getUniqueInteger())
191 self.patch(utils, 'read_file', mock_read_file)
192- proxy_fixture = LocalProxyFixture(config_dir=self.temp_dir)
193- proxy_fixture.kill_running_proxy()
194+ self.make_fixture().kill_running_proxy()
195 mock_log_error.assert_has_calls([
196 mock.call("Unable to kill proxy:"),
197 mock.call(mock_run_command.side_effect.message)])
198@@ -163,8 +183,7 @@
199 self.patch(utils, 'read_file', mock_read_file)
200 mock_kill_running_proxy = mock.MagicMock()
201
202- port = self.get_random_port_number()
203- proxy_fixture = LocalProxyFixture(config_dir=self.temp_dir, port=port)
204+ proxy_fixture = self.make_fixture()
205 self.patch(
206 proxy_fixture, 'kill_running_proxy', mock_kill_running_proxy)
207
208@@ -176,5 +195,5 @@
209 mock_exists = mock.MagicMock()
210 mock_exists.return_value = True
211 self.patch(os.path, 'exists', mock_exists)
212- proxy_fixture = LocalProxyFixture(config_dir=self.temp_dir)
213+ proxy_fixture = self.make_fixture()
214 self.assertRaises(Exception, proxy_fixture.setUp)
215
216=== modified file 'maastest/utils.py'
217--- maastest/utils.py 2014-01-09 03:51:20 +0000
218+++ maastest/utils.py 2014-01-09 06:08:12 +0000
219@@ -17,6 +17,7 @@
220 "determine_vm_series",
221 "determine_vm_architecture",
222 "get_uri",
223+ "DEFAULT_PIDFILE_DIR",
224 "read_file",
225 "retries",
226 "run_command",
227@@ -50,6 +51,9 @@
228 # machine, and the http proxy cache.
229 DEFAULT_STATE_DIR = '/var/cache/maas-test'
230
231+# Default location for pidfiles.
232+DEFAULT_PIDFILE_DIR = '/run/maas-test'
233+
234
235 def read_file(path):
236 """Return a given file's contents, as `bytes`."""

Subscribers

People subscribed via source and target branches