Merge lp:~gz/maas/accept_float_constraints_1061286 into lp:~maas-committers/maas/trunk

Proposed by Martin Packman
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merged at revision: 1165
Proposed branch: lp:~gz/maas/accept_float_constraints_1061286
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 63 lines (+17/-1)
3 files modified
src/maasserver/models/node_constraint_filter.py (+3/-1)
src/maasserver/tests/test_api.py (+11/-0)
src/maasserver/tests/test_node_constraint_filter.py (+3/-0)
To merge this branch: bzr merge lp:~gz/maas/accept_float_constraints_1061286
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) Approve
Review via email: mp+127983@code.launchpad.net

Commit message

Tolerate decimal values for cpu_count and mem constraints

Description of the change

Simple change to make acquire work when a decimal is given for one of the integer constraints.

The default value juju uses for the 'cpu' constraint is 1.0 as EC2 has compute unit measures, and int() does not like stringified floats. There's no reason not to be a little more liberal here, even though float values don't make much sense for the current definitions of mem and cpu.

To post a comment you must log in.
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

Land it.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/node_constraint_filter.py'
2--- src/maasserver/models/node_constraint_filter.py 2012-09-28 11:07:48 +0000
3+++ src/maasserver/models/node_constraint_filter.py 2012-10-04 10:59:23 +0000
4@@ -12,6 +12,8 @@
5 'constrain_nodes',
6 ]
7
8+import math
9+
10 from maasserver.exceptions import (
11 InvalidConstraint,
12 )
13@@ -31,7 +33,7 @@
14 as an invalid constraint.
15 """
16 try:
17- int_value = int(str_value)
18+ int_value = int(math.ceil(float(str_value)))
19 except ValueError as e:
20 raise InvalidConstraint(key, str_value, e)
21 return nodes.filter(**{'%s__gte' % (key,): int_value})
22
23=== modified file 'src/maasserver/tests/test_api.py'
24--- src/maasserver/tests/test_api.py 2012-10-04 10:08:26 +0000
25+++ src/maasserver/tests/test_api.py 2012-10-04 10:59:23 +0000
26@@ -1796,6 +1796,17 @@
27 response_json = json.loads(response.content)
28 self.assertEqual(node.system_id, response_json['system_id'])
29
30+ def test_POST_acquire_allocates_node_by_float_cpu(self):
31+ # Asking for a needlessly precise number of cpus works.
32+ node = factory.make_node(status=NODE_STATUS.READY, cpu_count=1)
33+ response = self.client.post(self.get_uri('nodes/'), {
34+ 'op': 'acquire',
35+ 'cpu_count': '1.0',
36+ })
37+ self.assertResponseCode(httplib.OK, response)
38+ response_json = json.loads(response.content)
39+ self.assertEqual(node.system_id, response_json['system_id'])
40+
41 def test_POST_acquire_fails_with_invalid_cpu(self):
42 # Asking for an invalid amount of cpu returns a bad request.
43 factory.make_node(status=NODE_STATUS.READY)
44
45=== modified file 'src/maasserver/tests/test_node_constraint_filter.py'
46--- src/maasserver/tests/test_node_constraint_filter.py 2012-09-28 17:01:26 +0000
47+++ src/maasserver/tests/test_node_constraint_filter.py 2012-10-04 10:59:23 +0000
48@@ -55,6 +55,8 @@
49 self.assertConstrainedNodes([node1, node2], {'cpu_count': '1'})
50 self.assertConstrainedNodes([node2], {'cpu_count': '2'})
51 self.assertConstrainedNodes([], {'cpu_count': '4'})
52+ self.assertConstrainedNodes([node2], {'cpu_count': '2.0'})
53+ self.assertConstrainedNodes([node2], {'cpu_count': '1.2'})
54 self.assertRaises(InvalidConstraint,
55 self.assertConstrainedNodes, [], {'cpu_count': 'notint'})
56
57@@ -66,6 +68,7 @@
58 self.assertConstrainedNodes([node2], {'memory': '2048'})
59 self.assertConstrainedNodes([node2], {'memory': '4096'})
60 self.assertConstrainedNodes([], {'memory': '8192'})
61+ self.assertConstrainedNodes([node2], {'memory': '4096.0'})
62 self.assertRaises(InvalidConstraint,
63 self.assertConstrainedNodes, [], {'memory': 'notint'})
64