def test_to_json(self): host = "test_host" block = AllocationBlock(network, host) # Set up an allocation attr = { AllocationBlock.ATTR_HANDLE_ID: "test_key", AllocationBlock.ATTR_SECONDARY: { "key1": "value1", "key2": "value2" } } block.attributes.append(attr) block.allocations[5] = 0 assert_equal(block.count_free_addresses(), BLOCK_SIZE - 1) # Read out the JSON json_str = block.to_json() json_dict = json.loads(json_str) assert_equal(json_dict[AllocationBlock.CIDR], str(network)) assert_equal(json_dict[AllocationBlock.AFFINITY], "host:test_host") assert_dict_equal(json_dict[AllocationBlock.ATTRIBUTES][0], attr) expected_allocations = [None] * BLOCK_SIZE expected_allocations[5] = 0 assert_list_equal(json_dict[AllocationBlock.ALLOCATIONS], expected_allocations) # Verify we can read the JSON back in. result = Mock(spec=EtcdResult) result.value = json_str block2 = AllocationBlock.from_etcd_result(result) assert_equal(block2.to_json(), json_str)
def test_from_etcd_result_no_unallocated(self): """ Test the from_etcd_result processing when the allocation order is missing (this is allowed since it is a new field). """ result = Mock(spec=EtcdResult) # Build a JSON object for the Block attr0 = { AllocationBlock.ATTR_HANDLE_ID: "test_key1", AllocationBlock.ATTR_SECONDARY: { "key1": "value11", "key2": "value21" } } attr1 = { AllocationBlock.ATTR_HANDLE_ID: "test_key2", AllocationBlock.ATTR_SECONDARY: { "key1": "value12", "key2": "value22" } } allocations = [None] * BLOCK_SIZE allocations[0] = 0 allocations[1] = 0 allocations[4] = 1 json_dict = { AllocationBlock.CIDR: str(network), AllocationBlock.AFFINITY: "host:Sammy Davis, Jr.", AllocationBlock.STRICT_AFFINITY: True, AllocationBlock.ALLOCATIONS: allocations, AllocationBlock.ATTRIBUTES: [attr0, attr1] } result.value = json.dumps(json_dict) block = AllocationBlock.from_etcd_result(result) assert_equal(block.count_free_addresses(), BLOCK_SIZE - 3) assert_equal(block.db_result, result) assert_equal(block.cidr, network) assert_equal(block.host_affinity, "Sammy Davis, Jr.") assert_true(block.strict_affinity) assert_list_equal(block.allocations[:5], [0, 0, None, None, 1]) assert_dict_equal(block.attributes[0], attr0) assert_dict_equal(block.attributes[1], attr1) # Verify the allocation order is correctly calculated from the # unassigned entries. unallocated = [o for o in range(BLOCK_SIZE) if o not in (0, 1, 4)] assert_list_equal(block.unallocated, unallocated) # Verify we can get JSON back out. json_dict[AllocationBlock.UNALLOCATED] = unallocated assert_equal(json.dumps(json_dict), block.to_json())
def _read_block(self, block_cidr): """ Read the block from the data store. :param block_cidr: The IPNetwork identifier for a block. :return: An AllocationBlock object """ key = _block_datastore_key(block_cidr) try: result = self.etcd_client.read(key) except EtcdKeyNotFound: raise KeyError(str(block_cidr)) block = AllocationBlock.from_etcd_result(result) return block
def _read_block(self, block_cidr): """ Read the block from the data store. :param block_cidr: The IPNetwork identifier for a block. :return: An AllocationBlock object """ key = _block_datastore_key(block_cidr) try: # Use quorum=True to ensure we don't get stale reads. Without this # we allow many subtle race conditions, such as creating a block, # then later reading it and finding it doesn't exist. result = self.etcd_client.read(key, quorum=True) except EtcdKeyNotFound: raise KeyError(str(block_cidr)) block = AllocationBlock.from_etcd_result(result) return block
def test_update_result(self): result = Mock(spec=EtcdResult) # Build a JSON object for the Block attr0 = { AllocationBlock.ATTR_HANDLE_ID: "test_key1", AllocationBlock.ATTR_SECONDARY: { "key1": "value11", "key2": "value21" } } attr1 = { AllocationBlock.ATTR_HANDLE_ID: "test_key2", AllocationBlock.ATTR_SECONDARY: { "key1": "value12", "key2": "value22" } } allocations = [None] * BLOCK_SIZE allocations[0] = 0 allocations[1] = 0 allocations[2] = 1 json_dict = { AllocationBlock.CIDR: str(network), AllocationBlock.AFFINITY: "host:Sammy Davis, Jr.", AllocationBlock.ALLOCATIONS: allocations, AllocationBlock.ATTRIBUTES: [attr0, attr1] } result.value = json.dumps(json_dict) block = AllocationBlock.from_etcd_result(result) # Modify the block. block.allocations[3] = 1 # Get the update. It should be the same result object, but with the # value set to the new JSON. block_json_str = block.to_json() updated = block.update_result() assert_equal(updated, result) assert_equal(result.value, block_json_str) # Verify the update appears in the JSON block_json_dict = json.loads(block_json_str) json_dict[AllocationBlock.ALLOCATIONS][3] = 1 assert_dict_equal(block_json_dict, json_dict)
def test_from_etcd_result_no_affinity(self): """ Mainline test of from_etcd_result() """ result = Mock(spec=EtcdResult) # Build a JSON object for the Block. Assume the strict_affinity flag is # not present so that we default the value (to False). attr0 = { AllocationBlock.ATTR_HANDLE_ID: "test_key1", AllocationBlock.ATTR_SECONDARY: { "key1": "value11", "key2": "value21" } } attr1 = { AllocationBlock.ATTR_HANDLE_ID: "test_key2", AllocationBlock.ATTR_SECONDARY: { "key1": "value12", "key2": "value22" } } allocations = [None] * BLOCK_SIZE allocations[0] = 0 allocations[1] = 0 allocations[2] = 1 unallocated = list(range(3, BLOCK_SIZE)) json_dict = { AllocationBlock.CIDR: str(network), AllocationBlock.ALLOCATIONS: allocations, AllocationBlock.ATTRIBUTES: [attr0, attr1], AllocationBlock.UNALLOCATED: unallocated } result.value = json.dumps(json_dict) block = AllocationBlock.from_etcd_result(result) assert_equal(block.count_free_addresses(), BLOCK_SIZE - 3) assert_equal(block.db_result, result) assert_equal(block.cidr, network) assert_is_none(block.host_affinity) assert_false(block.strict_affinity) assert_list_equal(block.allocations[:3], [0, 0, 1]) assert_dict_equal(block.attributes[0], attr0) assert_dict_equal(block.attributes[1], attr1)
def test_from_etcd_result(self): result = Mock(spec=EtcdResult) # Build a JSON object for the Block attr0 = { AllocationBlock.ATTR_HANDLE_ID: "test_key1", AllocationBlock.ATTR_SECONDARY: { "key1": "value11", "key2": "value21" } } attr1 = { AllocationBlock.ATTR_HANDLE_ID: "test_key2", AllocationBlock.ATTR_SECONDARY: { "key1": "value12", "key2": "value22" } } allocations = [None] * BLOCK_SIZE allocations[0] = 0 allocations[1] = 0 allocations[2] = 1 json_dict = { AllocationBlock.CIDR: str(network), AllocationBlock.AFFINITY: "host:Sammy Davis, Jr.", AllocationBlock.ALLOCATIONS: allocations, AllocationBlock.ATTRIBUTES: [attr0, attr1] } result.value = json.dumps(json_dict) block = AllocationBlock.from_etcd_result(result) assert_equal(block.count_free_addresses(), BLOCK_SIZE - 3) assert_equal(block.db_result, result) assert_equal(block.cidr, network) assert_equal(block.host_affinity, "Sammy Davis, Jr.") assert_list_equal(block.allocations[:3], [0, 0, 1]) assert_dict_equal(block.attributes[0], attr0) assert_dict_equal(block.attributes[1], attr1) # Verify we can get JSON back out. json_str = block.to_json() assert_equal(result.value, json_str)
def test_update_result_from_pre_unallocated(self): """ Test mainline update_result() processing. This includes a check to ensure that updating a block that is stored without the allocation order is then correctly updated to include the allocation order. """ result = Mock(spec=EtcdResult) # Build a JSON object for the Block attr0 = { AllocationBlock.ATTR_HANDLE_ID: "test_key1", AllocationBlock.ATTR_SECONDARY: { "key1": "value11", "key2": "value21" } } attr1 = { AllocationBlock.ATTR_HANDLE_ID: "test_key2", AllocationBlock.ATTR_SECONDARY: { "key1": "value12", "key2": "value22" } } allocations = [None] * BLOCK_SIZE allocations[0] = 0 allocations[1] = 0 allocations[2] = 1 json_dict = { AllocationBlock.CIDR: str(network), AllocationBlock.AFFINITY: "host:Sammy Davis, Jr.", AllocationBlock.ALLOCATIONS: allocations, AllocationBlock.ATTRIBUTES: [attr0, attr1] } result.value = json.dumps(json_dict) block = AllocationBlock.from_etcd_result(result) # Verify that the allocation order is correctly initialised. unallocated = list(range(3, BLOCK_SIZE)) assert_list_equal(block.unallocated, unallocated) # Modify the block (and the expected allocation order) block.allocations[3] = 1 block.unallocated.remove(3) unallocated.remove(3) # Get the update. It should be the same result object, but with the # value set to the new JSON. block_json_str = block.to_json() updated = block.update_result() assert_equal(updated, result) assert_equal(result.value, block_json_str) # Verify the update appears in the JSON and that the JSON now includes # the allocation order. json_dict[AllocationBlock.UNALLOCATED] = unallocated block_json_dict = json.loads(block_json_str) json_dict[AllocationBlock.ALLOCATIONS][3] = 1 assert_dict_equal(block_json_dict, json_dict)
def test_from_etcd_result(self): """ Mainline test of from_etcd_result() """ result = Mock(spec=EtcdResult) # Build a JSON object for the Block attr0 = { AllocationBlock.ATTR_HANDLE_ID: "test_key1", AllocationBlock.ATTR_SECONDARY: { "key1": "value11", "key2": "value21" } } attr1 = { AllocationBlock.ATTR_HANDLE_ID: "test_key2", AllocationBlock.ATTR_SECONDARY: { "key1": "value12", "key2": "value22" } } allocations = [None] * BLOCK_SIZE allocations[0] = 0 allocations[1] = 0 allocations[2] = 1 unallocated = list(range(3, BLOCK_SIZE)) json_dict = { AllocationBlock.CIDR: str(network), AllocationBlock.AFFINITY: "host:Sammy Davis, Jr.", AllocationBlock.ALLOCATIONS: allocations, AllocationBlock.ATTRIBUTES: [attr0, attr1], AllocationBlock.UNALLOCATED: unallocated } result.value = json.dumps(json_dict) block = AllocationBlock.from_etcd_result(result) assert_equal(block.count_free_addresses(), BLOCK_SIZE - 3) assert_equal(block.db_result, result) assert_equal(block.cidr, network) assert_equal(block.host_affinity, "Sammy Davis, Jr.") assert_list_equal(block.allocations[:3], [0, 0, 1]) assert_dict_equal(block.attributes[0], attr0) assert_dict_equal(block.attributes[1], attr1) # Verify we can get JSON back out. json_str = block.to_json() assert_equal(result.value, json_str) # Modify the allocation order in the JSON so that it does not match # the allocations, and check the various unallocated asserts. # Check repeats json_dict[AllocationBlock.UNALLOCATED] = unallocated + [3] result.value = json.dumps(json_dict) self.assertRaises(AssertionError, AllocationBlock.from_etcd_result, result) # Check invalid entry json_dict[AllocationBlock.UNALLOCATED] = unallocated + [0] result.value = json.dumps(json_dict) self.assertRaises(AssertionError, AllocationBlock.from_etcd_result, result) # Check missing entry json_dict[AllocationBlock.UNALLOCATED] = unallocated[1:] result.value = json.dumps(json_dict) self.assertRaises(AssertionError, AllocationBlock.from_etcd_result, result)