Merge lp:~doanac/utah/bug1158743 into lp:utah/trunk

Proposed by Andy Doan
Status: Rejected
Rejected by: Javier Collado
Proposed branch: lp:~doanac/utah/bug1158743
Merge into: lp:utah/trunk
Diff against target: 135 lines (+80/-10)
4 files modified
debian/changelog (+3/-0)
tests/test_process.py (+44/-0)
utah/process.py (+25/-0)
utah/provisioning/baremetal/inventory.py (+8/-10)
To merge this branch: bzr merge lp:~doanac/utah/bug1158743
Reviewer Review Type Date Requested Status
Javier Collado (community) Approve
Max Brustkern (community) Approve
Review via email: mp+155264@code.launchpad.net

This proposal supersedes a proposal from 2013-03-22.

Description of the change

This should add the logging Javier was suggesting to understand what processes are actually using a machine.

Changed to proper branch

To post a comment you must log in.
Revision history for this message
Javier Collado (javier.collado) wrote : Posted in a previous version of this proposal

I see that you fixed the bug in a branch based on trunk and created the merge
for dev (which actually reveals an entry in debian/changelog that isn't in
dev). Is this the expected way to work with bugs?

Revision history for this message
Javier Collado (javier.collado) wrote : Posted in a previous version of this proposal

The use of a static private method suggests that `_in_use` is a utility method,
so I believe it should be moved to the `process` module which is expected to
have that kind of methods.

Revision history for this message
Andy Doan (doanac) wrote : Posted in a previous version of this proposal

On 03/25/2013 04:16 AM, Javier Collado wrote:
> I see that you fixed the bug in a branch based on trunk and created the merge
> for dev (which actually reveals an entry in debian/changelog that isn't in
> dev). Is this the expected way to work with bugs?
>
Nope - that was a mechanical error. I'll correct

lp:~doanac/utah/bug1158743 updated
270. By Andy Doan

move pid _in_use to process.py

As per Javier's review comment this moves the function to become a
generic helper. I also added some unit-testing logic for it

Revision history for this message
Andy Doan (doanac) wrote :

updated as per Javier's suggestion

Revision history for this message
Javier Collado (javier.collado) wrote :

Thanks for the update (specially for the test cases).

I've got a couple of commments:

- Exceptions

When getting the process object, TypeError and ValueError exceptions are
caught:
except (psutil.error.NoSuchProcess, TypeError, ValueError):

However, I think they shouldn't be caught since I'd like to get an exception
when I don't pass the right parameter type as in:
pid_in_use('string')

Otherwise, I misuse is not noticed by the end user.

- Documentation

I've seen you've made the effor to document everything. However the
documentation strings aren't pep257 (http://www.python.org/dev/peps/pep-0257/)
compliant and they don't follow the sphinx markup. If you could fix those
problems as well (just on the new code), that would be great.

To check documentation strings, I use the following tool and works quite well:
https://pypi.python.org/pypi/pep257/

With regard sphinx markup, please have a look at:
http://sphinx-doc.org/domains.html#info-field-lists

In particular, the fields that we've been using so far are:
:param <param>: to describe a parameter
:type <type>: to annotate the parameter type
:raises <exception>: to add exceptions that might be raised
:returns: to describe what is being returned
:rtype: to annotate the type of the return value

Revision history for this message
Andy Doan (doanac) wrote :

On 03/26/2013 12:35 PM, Javier Collado wrote:
> - Exceptions
>
> When getting the process object, TypeError and ValueError exceptions are
> caught:
> except (psutil.error.NoSuchProcess, TypeError, ValueError):
>
> However, I think they shouldn't be caught since I'd like to get an exception
> when I don't pass the right parameter type as in:
> pid_in_use('string')
>
> Otherwise, I misuse is not noticed by the end user.

Is this going to cause a possible regression that we need to test for?

@max - I think you suggested this, but i don't recall why

Revision history for this message
Max Brustkern (nuclearbob) wrote :

I think originally we caught TypeError and ValueError to allow is flexibility in the transition from a database without a pid column to a database with a pid column. As long as we do sufficient testing of an actual Inventory and verify things work, leaving those out should be fine.

Revision history for this message
Max Brustkern (nuclearbob) wrote :

I've tested this with an actual physical machine inventory on magners-orchestra, and I'm satisfied that it works correctly. Unfortunately, we're still not printing info to console by default, so the message won't be seen by some jobs. That's a different fix, however, so I'm in favor of merging this.

review: Approve
Revision history for this message
Max Brustkern (nuclearbob) wrote :

Can you add a debian changelog entry for this so it's sourced properly? I use
dch "Made a totally sweet change"
for that.

lp:~doanac/utah/bug1158743 updated
271. By Andy Doan

fix pep257 issues

272. By Andy Doan

add changelog entry

Revision history for this message
Max Brustkern (nuclearbob) wrote :

I removed TypeError and ValueError from the list of exceptions caught, and things still test out fine. I think those were probably mainly useful during the transition period when the database might or might not have a pid column.

Revision history for this message
Max Brustkern (nuclearbob) wrote :

This looks good to me.

review: Approve
Revision history for this message
Javier Collado (javier.collado) wrote :

My comment has been addressed in:
https://code.launchpad.net/~doanac/utah/bug11588743-part2/+merge/160498

Hence, I think these changes are fine.

review: Approve
Revision history for this message
Max Brustkern (nuclearbob) wrote :

This appears to already be in dev, but the target of this proposal is trunk. Is this something we want to release as a bugfix, or can we just wait until the next release?

Revision history for this message
Andy Doan (doanac) wrote :

hmm - i think i originally was going to target this as a fix, but then we started using dev for everything. So i think we are good here.

Revision history for this message
Javier Collado (javier.collado) wrote :

Rejecting the merge request as per the previous comment.

Unmerged revisions

272. By Andy Doan

add changelog entry

271. By Andy Doan

fix pep257 issues

270. By Andy Doan

move pid _in_use to process.py

As per Javier's review comment this moves the function to become a
generic helper. I also added some unit-testing logic for it

269. By Andy Doan

add logging for unavailable machines

this should help us understand who is using a machine in the event
one isn't available.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'debian/changelog'
--- debian/changelog 2013-03-27 15:33:54 +0000
+++ debian/changelog 2013-04-10 15:39:35 +0000
@@ -28,6 +28,9 @@
28 * Overhauled exception handling based on code review28 * Overhauled exception handling based on code review
29 * Made all shell scripts POSIX-compliant29 * Made all shell scripts POSIX-compliant
3030
31 [ Andy Doan ]
32 * Log process that has machine reserved. (LP: #1158743)
33
31 -- Max Brustkern <max@canonical.com> Fri, 15 Mar 2013 13:55:24 -040034 -- Max Brustkern <max@canonical.com> Fri, 15 Mar 2013 13:55:24 -0400
3235
33utah (0.8ubuntu1) quantal; urgency=low36utah (0.8ubuntu1) quantal; urgency=low
3437
=== added file 'tests/test_process.py'
--- tests/test_process.py 1970-01-01 00:00:00 +0000
+++ tests/test_process.py 2013-04-10 15:39:35 +0000
@@ -0,0 +1,44 @@
1# Ubuntu Testing Automation Harness
2# Copyright 2013 Canonical Ltd.
3
4# This program is free software: you can redistribute it and/or modify it
5# under the terms of the GNU General Public License version 3, as published
6# by the Free Software Foundation.
7
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranties of
10# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11# PURPOSE. See the GNU General Public License for more details.
12
13# You should have received a copy of the GNU General Public License along
14# with this program. If not, see <http://www.gnu.org/licenses/>.
15
16"""Unit tests for the utah.process module."""
17
18import os
19import unittest
20
21from utah.process import pid_in_use
22
23
24class TestProcess(unittest.TestCase):
25
26 """Tests the functions of the utah.process module."""
27
28 def test_is_running(self):
29 """Ensure simple test of pid_in_use works for a pid running."""
30 pid = os.getpid()
31 self.assertIsNotNone(pid_in_use(pid))
32 self.assertIsNotNone(pid_in_use(pid, ['test_']))
33 # make sure we don't return when the command line doesn't match
34 self.assertIsNone(pid_in_use(pid, ['blah']))
35
36 def test_is_not_running(self):
37 """Ensure simple test of pid_in_use works for a pid not running."""
38 pid = os.fork()
39 if pid == 0:
40 os.execv('/bin/true', ['/bin/true'])
41 os.waitpid(pid, 0)
42 # the child should be dead so this will return None
43 self.assertIsNone(pid_in_use(pid))
44 self.assertIsNone(pid_in_use(pid, ['true']))
045
=== modified file 'utah/process.py'
--- utah/process.py 2012-12-08 02:10:12 +0000
+++ utah/process.py 2013-04-10 15:39:35 +0000
@@ -19,6 +19,31 @@
19import psutil19import psutil
2020
2121
22def pid_in_use(pid, arg_patterns=None):
23 """Test if the given pid is running.
24
25 If arg_patterns is provided then the pid's command line will be compared
26 to make sure it matches an expected value.
27
28 :returns: proc object if the pid is active and optionally matches one of
29 the arg_patterns, otherwise None
30 :rtype: psutil.Process
31
32 """
33 try:
34 proc = psutil.Process(pid)
35 except (psutil.error.NoSuchProcess, TypeError, ValueError):
36 return None
37
38 cmdline = ' '.join(proc.cmdline)
39 if arg_patterns:
40 for p in arg_patterns:
41 if p in cmdline:
42 return proc
43 return None
44 return proc
45
46
22class ProcessChecker(object):47class ProcessChecker(object):
23 """48 """
24 Check all running process looking for a given pattern49 Check all running process looking for a given pattern
2550
=== modified file 'utah/provisioning/baremetal/inventory.py'
--- utah/provisioning/baremetal/inventory.py 2013-01-22 14:13:17 +0000
+++ utah/provisioning/baremetal/inventory.py 2013-04-10 15:39:35 +0000
@@ -17,8 +17,10 @@
17Provide inventory functions specific to bare metal deployments.17Provide inventory functions specific to bare metal deployments.
18"""18"""
1919
20import logging
20import os21import os
21import psutil22
23from utah.process import pid_in_use
22from utah.provisioning.inventory.exceptions import \24from utah.provisioning.inventory.exceptions import \
23 UTAHProvisioningInventoryException25 UTAHProvisioningInventoryException
24from utah.provisioning.inventory.sqlite import SQLiteInventory26from utah.provisioning.inventory.sqlite import SQLiteInventory
@@ -65,15 +67,11 @@
65 for minfo in result:67 for minfo in result:
66 machineinfo = dict(minfo)68 machineinfo = dict(minfo)
67 pid = machineinfo['pid']69 pid = machineinfo['pid']
68 try:70 proc = pid_in_use(pid, ['utah', 'run_test'])
69 if not (psutil.pid_exists(pid) and ('utah' in71 if not proc:
70 ' '.join(psutil.Process(pid).cmdline)72 return self._take(machineinfo, machinetype, *args, **kw)
71 or 'run_test' in73 else:
72 ' '.join(psutil.Process(pid).cmdline))):74 logging.info('machine in use by: %s', proc.cmdline)
73 return self._take(machineinfo, machinetype,
74 *args, **kw)
75 except ValueError:
76 continue
7775
78 raise UTAHProvisioningInventoryException(76 raise UTAHProvisioningInventoryException(
79 'All machines meeting criteria are currently unavailable')77 'All machines meeting criteria are currently unavailable')

Subscribers

People subscribed via source and target branches

to all changes: