diff options
| -rw-r--r-- | neutronclient/neutron/v2_0/subnet.py | 18 | ||||
| -rw-r--r-- | neutronclient/tests/unit/test_cli20_subnet.py | 68 |
2 files changed, 74 insertions, 12 deletions
diff --git a/neutronclient/neutron/v2_0/subnet.py b/neutronclient/neutron/v2_0/subnet.py index 60b899c..86c9f18 100644 --- a/neutronclient/neutron/v2_0/subnet.py +++ b/neutronclient/neutron/v2_0/subnet.py @@ -85,6 +85,20 @@ def add_updatable_arguments(parser): '--enable-dhcp', action='store_true', help=_('Enable DHCP for this subnet.')) + # NOTE(ihrachys): yes, that's awful, but should be left as-is for + # backwards compatibility for versions <=2.3.4 that passed the + # boolean values through to the server without any argument + # validation. + parser.add_argument( + '--enable-dhcp=True', + action='store_true', + dest='enable_dhcp', + help=argparse.SUPPRESS) + parser.add_argument( + '--enable-dhcp=False', + action='store_true', + dest='disable_dhcp', + help=argparse.SUPPRESS) def updatable_args2body(parsed_args, body, for_create=True): @@ -93,8 +107,8 @@ def updatable_args2body(parsed_args, body, for_create=True): "--no-gateway option can " "not be used same time")) if parsed_args.disable_dhcp and parsed_args.enable_dhcp: - raise exceptions.CommandError(_("--enable-dhcp and --disable-dhcp can " - "not be used in the same command.")) + raise exceptions.CommandError(_( + "You cannot enable and disable DHCP at the same time.")) if parsed_args.no_gateway: body['subnet'].update({'gateway_ip': None}) diff --git a/neutronclient/tests/unit/test_cli20_subnet.py b/neutronclient/tests/unit/test_cli20_subnet.py index fb34003..a98af01 100644 --- a/neutronclient/tests/unit/test_cli20_subnet.py +++ b/neutronclient/tests/unit/test_cli20_subnet.py @@ -75,23 +75,71 @@ class CLITestV20SubnetJSON(test_cli20.CLITestV20Base): return self.fail('No exception for bad gateway option') + def _test_create_resource_and_catch_command_error(self, tested_args, + should_fail, + *args): + _j = lambda args: ' '.join(args) + try: + self._test_create_resource(*args) + except exceptions.CommandError: + if not should_fail: + self.fail( + 'Unexpected exception raised for %s options' % + _j(tested_args)) + self.mox.UnsetStubs() + else: + if should_fail: + self.fail( + 'No exception for %s options' % _j(tested_args)) + def test_create_subnet_with_enable_and_disable_dhcp(self): - """Create sbunet: --enable-dhcp and --disable-dhcp.""" + """Create subnet: --enable-dhcp and --disable-dhcp.""" resource = 'subnet' cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = 'myname' myid = 'myid' netid = 'netid' cidr = 'cidrvalue' - args = ['--enable-dhcp', '--disable-dhcp', netid, cidr] - position_names = ['ip_version', 'network_id', 'cidr', 'gateway_ip'] - position_values = [4, netid, cidr, None] - try: - self._test_create_resource(resource, cmd, name, myid, args, - position_names, position_values) - except exceptions.CommandError: - return - self.fail('No exception for --enable-dhcp --disable-dhcp option') + position_names = ['ip_version', 'network_id', 'cidr', 'enable_dhcp'] + # enable_dhcp value is appended later inside the loop + position_values = [4, netid, cidr] + for enable_dhcp_arg, should_fail in ( + ('--enable-dhcp=False', False), + ('--enable-dhcp=True', True), + ('--enable-dhcp', True) + ): + tested_args = [enable_dhcp_arg, '--disable-dhcp'] + args = tested_args + [netid, cidr] + pos_values = position_values + [should_fail] + self._test_create_resource_and_catch_command_error( + tested_args, should_fail, + resource, cmd, name, myid, args, position_names, pos_values) + + def test_create_subnet_with_multiple_enable_dhcp(self): + """Create subnet with multiple --enable-dhcp arguments passed.""" + resource = 'subnet' + cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) + name = 'myname' + myid = 'myid' + netid = 'netid' + cidr = 'cidrvalue' + position_names = ['ip_version', 'network_id', 'cidr', 'enable_dhcp'] + # enable_dhcp value is appended later inside the loop + position_values = [4, netid, cidr] + + _ = 'UNUSED_MARKER' + for tested_args, should_fail, pos_value in ( + (['--enable-dhcp', '--enable-dhcp=True'], False, True), + (['--enable-dhcp', '--enable-dhcp=False'], True, _), + (['--enable-dhcp=False', '--enable-dhcp'], True, _), + (['--enable-dhcp=True', '--enable-dhcp=False'], True, _), + (['--enable-dhcp=False', '--enable-dhcp=True'], True, _) + ): + args = tested_args + [netid, cidr] + pos_values = position_values + [pos_value] + self._test_create_resource_and_catch_command_error( + tested_args, should_fail, + resource, cmd, name, myid, args, position_names, pos_values) def test_create_subnet_tenant(self): """Create subnet: --tenant_id tenantid netid cidr.""" |
