Merge lp:~allenap/maas/cannot-set-database-port--bug-1642200 into lp:~maas-committers/maas/trunk

Proposed by Gavin Panella
Status: Merged
Approved by: Gavin Panella
Approved revision: no longer in the source branch.
Merged at revision: 5557
Proposed branch: lp:~allenap/maas/cannot-set-database-port--bug-1642200
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 120 lines (+46/-6)
4 files modified
src/maasserver/config.py (+2/-2)
src/maasserver/management/commands/_config.py (+11/-2)
src/maasserver/management/commands/tests/test_config.py (+30/-1)
src/maasserver/tests/test_config.py (+3/-1)
To merge this branch: bzr merge lp:~allenap/maas/cannot-set-database-port--bug-1642200
Reviewer Review Type Date Requested Status
Brendan Donegan (community) Approve
Review via email: mp+310996@code.launchpad.net

Commit message

Deal with string input for database port.

Previously database ports passed in from the command-line were not being parsed as integers. This change also improves the display of validation errors when using local_config_set; previously a full traceback was being printed.

To post a comment you must log in.
Revision history for this message
Brendan Donegan (brendan-donegan) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/config.py'
2--- src/maasserver/config.py 2016-11-10 09:46:34 +0000
3+++ src/maasserver/config.py 2016-11-16 11:16:50 +0000
4@@ -9,7 +9,7 @@
5
6 from os import path
7
8-from formencode.validators import RangeValidator
9+from formencode.validators import Int
10 from provisioningserver.config import (
11 Configuration,
12 ConfigurationFile,
13@@ -44,7 +44,7 @@
14 UnicodeString(if_missing="localhost", accept_python=False))
15 database_port = ConfigurationOption(
16 "database_port", "The port of the PostgreSQL database.",
17- RangeValidator(if_missing=5432, accept_python=False, min=1, max=65535))
18+ Int(if_missing=5432, accept_python=False, min=1, max=65535))
19 database_name = ConfigurationOption(
20 "database_name", "The name of the PostgreSQL database.",
21 UnicodeString(if_missing="maasdb", accept_python=False))
22
23=== modified file 'src/maasserver/management/commands/_config.py'
24--- src/maasserver/management/commands/_config.py 2015-12-10 10:31:31 +0000
25+++ src/maasserver/management/commands/_config.py 2016-11-16 11:16:50 +0000
26@@ -14,7 +14,11 @@
27 from optparse import make_option
28 import sys
29
30-from django.core.management.base import BaseCommand
31+from django.core.management.base import (
32+ BaseCommand,
33+ CommandError,
34+)
35+import formencode
36 from maascli.utils import parse_docstring
37 from maasserver.config import RegionConfiguration
38 from provisioningserver.config import ConfigurationOption
39@@ -208,4 +212,9 @@
40 for name, option in gen_configuration_options():
41 value = options.get(name)
42 if value is not None:
43- setattr(config, name, value)
44+ try:
45+ setattr(config, name, value)
46+ except formencode.Invalid as error:
47+ message = str(error).rstrip(".")
48+ raise CommandError("%s: %s." % (
49+ name.replace("_", "-"), message))
50
51=== modified file 'src/maasserver/management/commands/tests/test_config.py'
52--- src/maasserver/management/commands/tests/test_config.py 2016-11-10 22:45:28 +0000
53+++ src/maasserver/management/commands/tests/test_config.py 2016-11-16 11:16:50 +0000
54@@ -9,6 +9,7 @@
55 import json
56
57 from django.core.management import call_command
58+from django.core.management.base import CommandError
59 from maasserver.config import RegionConfiguration
60 from maasserver.management.commands import _config as config
61 from maasserver.testing.config import RegionConfigurationFixture
62@@ -120,7 +121,8 @@
63 else:
64 value = factory.make_name("foobar")
65
66- stdio = call_set(**{self.option.dest: value})
67+ # Values are coming from the command-line so stringify.
68+ stdio = call_set(**{self.option.dest: str(value)})
69
70 # Nothing is echoed back to the user.
71 self.assertThat(stdio.getOutput(), Equals(""))
72@@ -136,6 +138,33 @@
73 Contains(str(value)))
74
75
76+class TestConfigurationSet_DatabasePort(MAASTestCase):
77+ """Tests for setting the database port.
78+
79+ Setting the port is slightly special because the other options are mostly
80+ (at the time of writing) defined using `UnicodeString` validators, roughly
81+ meaning that anything goes, but the port is defined with `Int`.
82+ """
83+
84+ def test__exception_when_port_is_not_an_integer(self):
85+ self.useFixture(RegionConfigurationFixture())
86+ error = self.assertRaises(CommandError, call_set, database_port="foo")
87+ self.assertThat(str(error), Equals(
88+ "database-port: Please enter an integer value."))
89+
90+ def test__exception_when_port_is_too_low(self):
91+ self.useFixture(RegionConfigurationFixture())
92+ error = self.assertRaises(CommandError, call_set, database_port=0)
93+ self.assertThat(str(error), Equals(
94+ "database-port: Please enter a number that is 1 or greater."))
95+
96+ def test__exception_when_port_is_too_high(self):
97+ self.useFixture(RegionConfigurationFixture())
98+ error = self.assertRaises(CommandError, call_set, database_port=2**16)
99+ self.assertThat(str(error), Equals(
100+ "database-port: Please enter a number that is 65535 or smaller."))
101+
102+
103 class TestConfigurationCommon(MAASTestCase):
104
105 is_string = IsInstance(str)
106
107=== modified file 'src/maasserver/tests/test_config.py'
108--- src/maasserver/tests/test_config.py 2016-11-10 09:46:34 +0000
109+++ src/maasserver/tests/test_config.py 2016-11-16 11:16:50 +0000
110@@ -84,7 +84,9 @@
111 example_value = factory.make_name(self.option)
112 else:
113 example_value = factory.pick_port()
114- setattr(config, self.option, example_value)
115+ # Argument values will most often be passed in from the command-line,
116+ # so convert to a string before use to reflect that usage.
117+ setattr(config, self.option, str(example_value))
118 self.assertEqual(example_value, getattr(config, self.option))
119 # It's also stored in the configuration database.
120 self.assertEqual({self.option: example_value}, config.store)