def test_replication_defaults(self): args = '-r 1 -z 1 -i 127.0.0.1 -p 6010 -d d1 -w 100'.split() opts, _ = parse_args(args) device = build_dev_from_opts(opts) expected = { 'device': 'd1', 'ip': '127.0.0.1', 'meta': '', 'port': 6010, 'region': 1, 'replication_ip': '127.0.0.1', 'replication_port': 6010, 'weight': 100.0, 'zone': 1, } self.assertEqual(device, expected) args = '-r 1 -z 1 -i test.com -p 6010 -d d1 -w 100'.split() opts, _ = parse_args(args) device = build_dev_from_opts(opts) expected = { 'device': 'd1', 'ip': 'test.com', 'meta': '', 'port': 6010, 'region': 1, 'replication_ip': 'test.com', 'replication_port': 6010, 'weight': 100.0, 'zone': 1, } self.assertEqual(device, expected)
def test_replication_defaults(self): args = '-r 1 -z 1 -i 127.0.0.1 -p 6010 -d d1 -w 100'.split() opts, _ = parse_args(args) device = build_dev_from_opts(opts) expected = { 'device': 'd1', 'ip': '127.0.0.1', 'meta': '', 'port': 6010, 'region': 1, 'replication_ip': '127.0.0.1', 'replication_port': 6010, 'weight': 100.0, 'zone': 1, } self.assertEquals(device, expected) args = '-r 1 -z 1 -i test.com -p 6010 -d d1 -w 100'.split() opts, _ = parse_args(args) device = build_dev_from_opts(opts) expected = { 'device': 'd1', 'ip': 'test.com', 'meta': '', 'port': 6010, 'region': 1, 'replication_ip': 'test.com', 'replication_port': 6010, 'weight': 100.0, 'zone': 1, } self.assertEquals(device, expected)
def test_replication_defaults(self): args = "-r 1 -z 1 -i 127.0.0.1 -p 6010 -d d1 -w 100".split() opts, _ = parse_args(args) device = build_dev_from_opts(opts) expected = { "device": "d1", "ip": "127.0.0.1", "meta": "", "port": 6010, "region": 1, "replication_ip": "127.0.0.1", "replication_port": 6010, "weight": 100.0, "zone": 1, } self.assertEquals(device, expected)
def test_build_dev_from_opts(self): argv = \ ["--region", "0", "--zone", "3", "--ip", "test.test.com", "--port", "6200", "--replication-ip", "r.test.com", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] expected = { 'region': 0, 'zone': 3, 'ip': "test.test.com", 'port': 6200, 'replication_ip': "r.test.com", 'replication_port': 7000, 'device': "sda3", 'meta': "some meta data", 'weight': 3.14159265359, } opts, args = parse_args(argv) device = build_dev_from_opts(opts) self.assertEqual(device, expected) argv = \ ["--region", "2", "--zone", "3", "--ip", "[test.test.com]", "--port", "6200", "--replication-ip", "[r.test.com]", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] opts, args = parse_args(argv) self.assertRaises(ValueError, build_dev_from_opts, opts) argv = \ ["--region", "2", "--zone", "3", "--ip", "[test.test.com]", "--port", "6200", "--replication-ip", "[r.test.com]", "--replication-port", "7000", "--meta", "some meta data", "--weight", "3.14159265359"] opts, args = parse_args(argv) self.assertRaises(ValueError, build_dev_from_opts, opts)
def test_build_dev_from_opts(self): argv = \ ["--region", "2", "--zone", "3", "--ip", "test.test.com", "--port", "6000", "--replication-ip", "r.test.com", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] expected = { 'region': 2, 'zone': 3, 'ip': "test.test.com", 'port': 6000, 'replication_ip': "r.test.com", 'replication_port': 7000, 'device': "sda3", 'meta': "some meta data", 'weight': 3.14159265359, } opts, args = parse_args(argv) device = build_dev_from_opts(opts) self.assertEquals(device, expected) argv = \ ["--region", "2", "--zone", "3", "--ip", "[test.test.com]", "--port", "6000", "--replication-ip", "[r.test.com]", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] opts, args = parse_args(argv) self.assertRaises(ValueError, build_dev_from_opts, opts) argv = \ ["--region", "2", "--zone", "3", "--ip", "[test.test.com]", "--port", "6000", "--replication-ip", "[r.test.com]", "--replication-port", "7000", "--meta", "some meta data", "--weight", "3.14159265359"] opts, args = parse_args(argv) self.assertRaises(ValueError, build_dev_from_opts, opts)
def _parse_add_values(argvish): """ Parse devices to add as specified on the command line. Will exit on error and spew warnings. :returns: array of device dicts """ new_cmd_format, opts, args = validate_args(argvish) # We'll either parse the all-in-one-string format or the # --options format, # but not both. If both are specified, raise an error. parsed_devs = [] if len(args) > 0: if new_cmd_format or len(args) % 2 != 0: print(Commands.add.__doc__.strip()) exit(EXIT_ERROR) devs_and_weights = izip(islice(args, 0, len(args), 2), islice(args, 1, len(args), 2)) for devstr, weightstr in devs_and_weights: dev_dict = parse_add_value(devstr) if dev_dict['region'] is None: stderr.write('WARNING: No region specified for %s. ' 'Defaulting to region 1.\n' % devstr) dev_dict['region'] = 1 if dev_dict['replication_ip'] is None: dev_dict['replication_ip'] = dev_dict['ip'] if dev_dict['replication_port'] is None: dev_dict['replication_port'] = dev_dict['port'] weight = float(weightstr) if weight < 0: raise ValueError('Invalid weight value: %s' % devstr) dev_dict['weight'] = weight parsed_devs.append(dev_dict) else: parsed_devs.append(build_dev_from_opts(opts)) return parsed_devs
def _parse_add_values(argvish): """ Parse devices to add as specified on the command line. Will exit on error and spew warnings. :returns: array of device dicts """ opts, args = parse_args(argvish) # We'll either parse the all-in-one-string format or the --options format, # but not both. If both are specified, raise an error. opts_used = opts.region or opts.zone or opts.ip or opts.port or \ opts.device or opts.weight or opts.meta if len(args) > 0 and opts_used: print Commands.add.__doc__.strip() exit(EXIT_ERROR) elif len(args) > 0: if len(args) % 2 != 0: print Commands.add.__doc__.strip() exit(EXIT_ERROR) parsed_devs = [] devs_and_weights = izip(islice(args, 0, len(args), 2), islice(args, 1, len(args), 2)) for devstr, weightstr in devs_and_weights: region = 1 rest = devstr if devstr.startswith('r'): i = 1 while i < len(devstr) and devstr[i].isdigit(): i += 1 region = int(devstr[1:i]) rest = devstr[i:] else: stderr.write("WARNING: No region specified for %s. " "Defaulting to region 1.\n" % devstr) if not rest.startswith('z'): print 'Invalid add value: %s' % devstr exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 zone = int(rest[1:i]) rest = rest[i:] if not rest.startswith('-'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 if rest[i] == '[': i += 1 while i < len(rest) and rest[i] != ']': i += 1 i += 1 ip = rest[1:i].lstrip('[').rstrip(']') rest = rest[i:] else: while i < len(rest) and rest[i] in '0123456789.': i += 1 ip = rest[1:i] rest = rest[i:] if not rest.startswith(':'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 port = int(rest[1:i]) rest = rest[i:] replication_ip = ip replication_port = port if rest.startswith('R'): i = 1 if rest[i] == '[': i += 1 while i < len(rest) and rest[i] != ']': i += 1 i += 1 replication_ip = rest[1:i].lstrip('[').rstrip(']') rest = rest[i:] else: while i < len(rest) and rest[i] in '0123456789.': i += 1 replication_ip = rest[1:i] rest = rest[i:] if not rest.startswith(':'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 replication_port = int(rest[1:i]) rest = rest[i:] if not rest.startswith('/'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i] != '_': i += 1 device_name = rest[1:i] rest = rest[i:] meta = '' if rest.startswith('_'): meta = rest[1:] try: weight = float(weightstr) except ValueError: print 'Invalid weight value: %s' % weightstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) if weight < 0: print 'Invalid weight value (must be positive): %s' % weightstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) parsed_devs.append({'region': region, 'zone': zone, 'ip': ip, 'port': port, 'device': device_name, 'replication_ip': replication_ip, 'replication_port': replication_port, 'weight': weight, 'meta': meta}) return parsed_devs else: try: dev = build_dev_from_opts(opts) except ValueError as e: print e print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) return [dev]
def _parse_add_values(argvish): """ Parse devices to add as specified on the command line. Will exit on error and spew warnings. :returns: array of device dicts """ opts, args = parse_args(argvish) # We'll either parse the all-in-one-string format or the --options format, # but not both. If both are specified, raise an error. opts_used = opts.region or opts.zone or opts.ip or opts.port or \ opts.device or opts.weight or opts.meta if len(args) > 0 and opts_used: print Commands.add.__doc__.strip() exit(EXIT_ERROR) elif len(args) > 0: if len(args) % 2 != 0: print Commands.add.__doc__.strip() exit(EXIT_ERROR) parsed_devs = [] devs_and_weights = izip(islice(args, 0, len(args), 2), islice(args, 1, len(args), 2)) for devstr, weightstr in devs_and_weights: region = 1 rest = devstr if devstr.startswith('r'): i = 1 while i < len(devstr) and devstr[i].isdigit(): i += 1 region = int(devstr[1:i]) rest = devstr[i:] else: stderr.write("WARNING: No region specified for %s. " "Defaulting to region 1.\n" % devstr) if not rest.startswith('z'): print 'Invalid add value: %s' % devstr exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 zone = int(rest[1:i]) rest = rest[i:] if not rest.startswith('-'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 if rest[i] == '[': i += 1 while i < len(rest) and rest[i] != ']': i += 1 i += 1 ip = rest[1:i].lstrip('[').rstrip(']') rest = rest[i:] else: while i < len(rest) and rest[i] in '0123456789.': i += 1 ip = rest[1:i] rest = rest[i:] if not rest.startswith(':'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 port = int(rest[1:i]) rest = rest[i:] replication_ip = ip replication_port = port if rest.startswith('R'): i = 1 if rest[i] == '[': i += 1 while i < len(rest) and rest[i] != ']': i += 1 i += 1 replication_ip = rest[1:i].lstrip('[').rstrip(']') rest = rest[i:] else: while i < len(rest) and rest[i] in '0123456789.': i += 1 replication_ip = rest[1:i] rest = rest[i:] if not rest.startswith(':'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 replication_port = int(rest[1:i]) rest = rest[i:] if not rest.startswith('/'): print 'Invalid add value: %s' % devstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) i = 1 while i < len(rest) and rest[i] != '_': i += 1 device_name = rest[1:i] rest = rest[i:] meta = '' if rest.startswith('_'): meta = rest[1:] try: weight = float(weightstr) except ValueError: print 'Invalid weight value: %s' % weightstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) if weight < 0: print 'Invalid weight value (must be positive): %s' % weightstr print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) parsed_devs.append({ 'region': region, 'zone': zone, 'ip': ip, 'port': port, 'device': device_name, 'replication_ip': replication_ip, 'replication_port': replication_port, 'weight': weight, 'meta': meta }) return parsed_devs else: try: dev = build_dev_from_opts(opts) except ValueError as e: print e print "The on-disk ring builder is unchanged.\n" exit(EXIT_ERROR) return [dev]
def _parse_add_values(argvish): """ Parse devices to add as specified on the command line. Will exit on error and spew warnings. :returns: array of device dicts """ new_cmd_format, opts, args = validate_args(argvish) # We'll either parse the all-in-one-string format or the # --options format, # but not both. If both are specified, raise an error. parsed_devs = [] if len(args) > 0: if new_cmd_format or len(args) % 2 != 0: print Commands.add.__doc__.strip() exit(EXIT_ERROR) devs_and_weights = izip(islice(args, 0, len(args), 2), islice(args, 1, len(args), 2)) for devstr, weightstr in devs_and_weights: region = 1 rest = devstr if devstr.startswith('r'): i = 1 while i < len(devstr) and devstr[i].isdigit(): i += 1 region = int(devstr[1:i]) rest = devstr[i:] else: stderr.write('WARNING: No region specified for %s. ' 'Defaulting to region 1.\n' % devstr) if not rest.startswith('z'): raise ValueError('Invalid add value: %s' % devstr) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 zone = int(rest[1:i]) rest = rest[i:] if not rest.startswith('-'): raise ValueError('Invalid add value: %s' % devstr) ip, port, rest = _parse_address(rest[1:]) replication_ip = ip replication_port = port if rest.startswith('R'): replication_ip, replication_port, rest = \ _parse_address(rest[1:]) if not rest.startswith('/'): raise ValueError( 'Invalid add value: %s' % devstr) i = 1 while i < len(rest) and rest[i] != '_': i += 1 device_name = rest[1:i] rest = rest[i:] meta = '' if rest.startswith('_'): meta = rest[1:] weight = float(weightstr) if weight < 0: raise ValueError('Invalid weight value: %s' % devstr) parsed_devs.append({'region': region, 'zone': zone, 'ip': ip, 'port': port, 'device': device_name, 'replication_ip': replication_ip, 'replication_port': replication_port, 'weight': weight, 'meta': meta}) else: parsed_devs.append(build_dev_from_opts(opts)) return parsed_devs
def _parse_add_values(argvish): """ Parse devices to add as specified on the command line. Will exit on error and spew warnings. :returns: array of device dicts """ new_cmd_format, opts, args = validate_args(argvish) # We'll either parse the all-in-one-string format or the # --options format, # but not both. If both are specified, raise an error. parsed_devs = [] if len(args) > 0: if new_cmd_format or len(args) % 2 != 0: print Commands.add.__doc__.strip() exit(EXIT_ERROR) devs_and_weights = izip(islice(args, 0, len(args), 2), islice(args, 1, len(args), 2)) for devstr, weightstr in devs_and_weights: region = 1 rest = devstr if devstr.startswith('r'): i = 1 while i < len(devstr) and devstr[i].isdigit(): i += 1 region = int(devstr[1:i]) rest = devstr[i:] else: stderr.write('WARNING: No region specified for %s. ' 'Defaulting to region 1.\n' % devstr) if not rest.startswith('z'): raise ValueError('Invalid add value: %s' % devstr) i = 1 while i < len(rest) and rest[i].isdigit(): i += 1 zone = int(rest[1:i]) rest = rest[i:] if not rest.startswith('-'): raise ValueError('Invalid add value: %s' % devstr) ip, port, rest = _parse_address(rest[1:]) replication_ip = ip replication_port = port if rest.startswith('R'): replication_ip, replication_port, rest = \ _parse_address(rest[1:]) if not rest.startswith('/'): raise ValueError('Invalid add value: %s' % devstr) i = 1 while i < len(rest) and rest[i] != '_': i += 1 device_name = rest[1:i] if not validate_device_name(device_name): raise ValueError('Invalid device name') rest = rest[i:] meta = '' if rest.startswith('_'): meta = rest[1:] weight = float(weightstr) if weight < 0: raise ValueError('Invalid weight value: %s' % devstr) parsed_devs.append({ 'region': region, 'zone': zone, 'ip': ip, 'port': port, 'device': device_name, 'replication_ip': replication_ip, 'replication_port': replication_port, 'weight': weight, 'meta': meta }) else: parsed_devs.append(build_dev_from_opts(opts)) return parsed_devs