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
=== modified file 'src/maasserver/models/node_constraint_filter.py'
--- src/maasserver/models/node_constraint_filter.py 2012-09-28 11:07:48 +0000
+++ src/maasserver/models/node_constraint_filter.py 2012-10-04 10:59:23 +0000
@@ -12,6 +12,8 @@
12 'constrain_nodes',12 'constrain_nodes',
13 ]13 ]
1414
15import math
16
15from maasserver.exceptions import (17from maasserver.exceptions import (
16 InvalidConstraint,18 InvalidConstraint,
17 )19 )
@@ -31,7 +33,7 @@
31 as an invalid constraint.33 as an invalid constraint.
32 """34 """
33 try:35 try:
34 int_value = int(str_value)36 int_value = int(math.ceil(float(str_value)))
35 except ValueError as e:37 except ValueError as e:
36 raise InvalidConstraint(key, str_value, e)38 raise InvalidConstraint(key, str_value, e)
37 return nodes.filter(**{'%s__gte' % (key,): int_value})39 return nodes.filter(**{'%s__gte' % (key,): int_value})
3840
=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py 2012-10-04 10:08:26 +0000
+++ src/maasserver/tests/test_api.py 2012-10-04 10:59:23 +0000
@@ -1796,6 +1796,17 @@
1796 response_json = json.loads(response.content)1796 response_json = json.loads(response.content)
1797 self.assertEqual(node.system_id, response_json['system_id'])1797 self.assertEqual(node.system_id, response_json['system_id'])
17981798
1799 def test_POST_acquire_allocates_node_by_float_cpu(self):
1800 # Asking for a needlessly precise number of cpus works.
1801 node = factory.make_node(status=NODE_STATUS.READY, cpu_count=1)
1802 response = self.client.post(self.get_uri('nodes/'), {
1803 'op': 'acquire',
1804 'cpu_count': '1.0',
1805 })
1806 self.assertResponseCode(httplib.OK, response)
1807 response_json = json.loads(response.content)
1808 self.assertEqual(node.system_id, response_json['system_id'])
1809
1799 def test_POST_acquire_fails_with_invalid_cpu(self):1810 def test_POST_acquire_fails_with_invalid_cpu(self):
1800 # Asking for an invalid amount of cpu returns a bad request.1811 # Asking for an invalid amount of cpu returns a bad request.
1801 factory.make_node(status=NODE_STATUS.READY)1812 factory.make_node(status=NODE_STATUS.READY)
18021813
=== modified file 'src/maasserver/tests/test_node_constraint_filter.py'
--- src/maasserver/tests/test_node_constraint_filter.py 2012-09-28 17:01:26 +0000
+++ src/maasserver/tests/test_node_constraint_filter.py 2012-10-04 10:59:23 +0000
@@ -55,6 +55,8 @@
55 self.assertConstrainedNodes([node1, node2], {'cpu_count': '1'})55 self.assertConstrainedNodes([node1, node2], {'cpu_count': '1'})
56 self.assertConstrainedNodes([node2], {'cpu_count': '2'})56 self.assertConstrainedNodes([node2], {'cpu_count': '2'})
57 self.assertConstrainedNodes([], {'cpu_count': '4'})57 self.assertConstrainedNodes([], {'cpu_count': '4'})
58 self.assertConstrainedNodes([node2], {'cpu_count': '2.0'})
59 self.assertConstrainedNodes([node2], {'cpu_count': '1.2'})
58 self.assertRaises(InvalidConstraint,60 self.assertRaises(InvalidConstraint,
59 self.assertConstrainedNodes, [], {'cpu_count': 'notint'})61 self.assertConstrainedNodes, [], {'cpu_count': 'notint'})
6062
@@ -66,6 +68,7 @@
66 self.assertConstrainedNodes([node2], {'memory': '2048'})68 self.assertConstrainedNodes([node2], {'memory': '2048'})
67 self.assertConstrainedNodes([node2], {'memory': '4096'})69 self.assertConstrainedNodes([node2], {'memory': '4096'})
68 self.assertConstrainedNodes([], {'memory': '8192'})70 self.assertConstrainedNodes([], {'memory': '8192'})
71 self.assertConstrainedNodes([node2], {'memory': '4096.0'})
69 self.assertRaises(InvalidConstraint,72 self.assertRaises(InvalidConstraint,
70 self.assertConstrainedNodes, [], {'memory': 'notint'})73 self.assertConstrainedNodes, [], {'memory': 'notint'})
7174