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_init_block_id(self): host = "test_host" block = AllocationBlock(network, host) assert_equal(block.host_affinity, host) assert_equal(block.cidr, network) assert_equal(block.count_free_addresses(), BLOCK_SIZE)
def _claim_block_affinity(self, host, block_cidr): """ Claim a block we think is free. """ block_id = str(block_cidr) path = IPAM_HOST_AFFINITY_PATH % { "host": host, "version": block_cidr.version } key = path + block_id.replace("/", "-") self.etcd_client.write(key, "") # Create the block. block = AllocationBlock(block_cidr, host) try: self._compare_and_swap_block(block) except CASError: # Block exists. Read it back to find out its host affinity block = self._read_block(block_cidr) if block.host_affinity == host: # Block is now claimed by us. Some other process on this host # must have claimed it. _log.debug("Block %s already claimed by us. Success.", block_cidr) return # Some other host beat us to claiming this block. Clean up. try: self.etcd_client.delete(key) except EtcdKeyNotFound: # A race exists where another process on the same host could # have already deleted the key. This is fine as long as the key # no longer exists. pass # Throw a key error to let the caller know the block wasn't free # after all. raise HostAffinityClaimedError("Block %s already claimed by %s", block_id, block.host_affinity) # successfully created the block. Done. return
def _test_block_empty_v6(): block = AllocationBlock(BLOCK_V6_1, "test_host1") return block
def test_auto_assign_v6(self): block0 = _test_block_empty_v6() attr = {"key21": "value1", "key22": "value2"} ips = block0.auto_assign(1, "key2", attr, TEST_HOST) assert_list_equal([BLOCK_V6_1[0]], ips) assert_equal(block0.attributes[0][AllocationBlock.ATTR_HANDLE_ID], "key2") assert_dict_equal(block0.attributes[0][AllocationBlock.ATTR_SECONDARY], attr) assert_equal(block0.count_free_addresses(), BLOCK_SIZE - 1) # Allocate again from the first block, with a different key. ips = block0.auto_assign(3, "key3", attr, TEST_HOST) assert_list_equal([BLOCK_V6_1[1], BLOCK_V6_1[2], BLOCK_V6_1[3]], ips) assert_equal(block0.attributes[1][AllocationBlock.ATTR_HANDLE_ID], "key3") assert_dict_equal(block0.attributes[1][AllocationBlock.ATTR_SECONDARY], attr) assert_equal(block0.count_free_addresses(), BLOCK_SIZE - 4) # Allocate with different attributes. ips = block0.auto_assign(3, "key3", {}, TEST_HOST) assert_list_equal([BLOCK_V6_1[4], BLOCK_V6_1[5], BLOCK_V6_1[6]], ips) assert_equal(block0.attributes[2][AllocationBlock.ATTR_HANDLE_ID], "key3") assert_dict_equal(block0.attributes[2][AllocationBlock.ATTR_SECONDARY], {}) assert_equal(block0.count_free_addresses(), BLOCK_SIZE - 7) # Allocate 3 from a new block. block1 = _test_block_empty_v6() ips = block1.auto_assign(3, "key2", attr, TEST_HOST) assert_list_equal([BLOCK_V6_1[0], BLOCK_V6_1[1], BLOCK_V6_1[2]], ips) assert_equal(block1.count_free_addresses(), BLOCK_SIZE - 3) # Allocate again with same keys. ips = block1.auto_assign(3, "key2", attr, TEST_HOST) assert_list_equal([BLOCK_V6_1[3], BLOCK_V6_1[4], BLOCK_V6_1[5]], ips) assert_equal(block1.count_free_addresses(), BLOCK_SIZE - 6) # Assert we didn't create another attribute entry. assert_equal(len(block1.attributes), 1) # Test allocating 0 IPs with a new key. ips = block1.auto_assign(0, "key3", attr, TEST_HOST) assert_list_equal(ips, []) assert_equal(len(block1.attributes), 1) assert_equal(block1.count_free_addresses(), BLOCK_SIZE - 6) # Allocate addresses, so the block is nearly full ips = block1.auto_assign(BLOCK_SIZE - 8, None, {}, TEST_HOST) assert_equal(len(ips), BLOCK_SIZE - 8) assert_equal(block1.count_free_addresses(), 2) # Allocate 4 addresses. 248+3+3 = 254, so only 2 addresses left ips = block1.auto_assign(4, None, {}, TEST_HOST) assert_list_equal([BLOCK_V6_1[-2], BLOCK_V6_1[-1]], ips) assert_equal(block1.count_free_addresses(), 0) # Block is now full, further attempts return no addresses ips = block1.auto_assign(4, None, {}, TEST_HOST) assert_list_equal([], ips) # Test that we can cope with already allocated addresses that aren't # sequential. block2 = _test_block_not_empty_v6() assert_equal(block2.count_free_addresses(), BLOCK_SIZE - 2) ips = block2.auto_assign(4, None, {}, TEST_HOST) assert_list_equal( [BLOCK_V6_1[0], BLOCK_V6_1[1], BLOCK_V6_1[3], BLOCK_V6_1[5]], ips) assert_equal(block2.count_free_addresses(), BLOCK_SIZE - 6) # Test ordinal math still works for small IPv6 addresses sm_cidr = IPNetwork("::1234:5600/122") block3 = AllocationBlock(sm_cidr, "test_host1") ips = block3.auto_assign(4, None, {}, TEST_HOST) assert_list_equal([sm_cidr[0], sm_cidr[1], sm_cidr[2], sm_cidr[3]], ips) assert_equal(block3.count_free_addresses(), BLOCK_SIZE - 4)
def _test_block_empty_v4(): block = AllocationBlock(BLOCK_V4_1, "test_host1", False) return block
def _test_block_empty_v6(): block = AllocationBlock(IPNetwork("2001:abcd:def0::/120"), "test_host1") return block
def _test_block_empty_v4(): block = AllocationBlock(IPNetwork("10.11.12.0/24"), "test_host1") return block
def test_auto_assign_v6(self): block0 = _test_block_empty_v6() attr = {"key21": "value1", "key22": "value2"} ips = block0.auto_assign(1, "key2", attr) assert_list_equal([IPAddress("2001:abcd:def0::")], ips) assert_equal(block0.attributes[0][AllocationBlock.ATTR_HANDLE_ID], "key2") assert_dict_equal(block0.attributes[0][AllocationBlock.ATTR_SECONDARY], attr) assert_equal(block0.count_free_addresses(), BLOCK_SIZE - 1) # Allocate again from the first block, with a different key. ips = block0.auto_assign(3, "key3", attr) assert_list_equal([IPAddress("2001:abcd:def0::1"), IPAddress("2001:abcd:def0::2"), IPAddress("2001:abcd:def0::3")], ips) assert_equal(block0.attributes[1][AllocationBlock.ATTR_HANDLE_ID], "key3") assert_dict_equal(block0.attributes[1][AllocationBlock.ATTR_SECONDARY], attr) assert_equal(block0.count_free_addresses(), BLOCK_SIZE - 4) # Allocate with different attributes. ips = block0.auto_assign(3, "key3", {}) assert_list_equal([IPAddress("2001:abcd:def0::4"), IPAddress("2001:abcd:def0::5"), IPAddress("2001:abcd:def0::6")], ips) assert_equal(block0.attributes[2][AllocationBlock.ATTR_HANDLE_ID], "key3") assert_dict_equal(block0.attributes[2][AllocationBlock.ATTR_SECONDARY], {}) assert_equal(block0.count_free_addresses(), BLOCK_SIZE - 7) # Allocate 3 from a new block. block1 = _test_block_empty_v6() ips = block1.auto_assign(3, "key2", attr) assert_list_equal([IPAddress("2001:abcd:def0::"), IPAddress("2001:abcd:def0::1"), IPAddress("2001:abcd:def0::2")], ips) assert_equal(block1.count_free_addresses(), BLOCK_SIZE - 3) # Allocate again with same keys. ips = block1.auto_assign(3, "key2", attr) assert_list_equal([IPAddress("2001:abcd:def0::3"), IPAddress("2001:abcd:def0::4"), IPAddress("2001:abcd:def0::5")], ips) assert_equal(block1.count_free_addresses(), BLOCK_SIZE - 6) # Assert we didn't create another attribute entry. assert_equal(len(block1.attributes), 1) # Test allocating 0 IPs with a new key. ips = block1.auto_assign(0, "key3", attr) assert_list_equal(ips, []) assert_equal(len(block1.attributes), 1) assert_equal(block1.count_free_addresses(), BLOCK_SIZE - 6) # Allocate another 248 addresses, so the block is nearly full ips = block1.auto_assign(248, None, {}) assert_equal(len(ips), 248) assert_equal(block1.count_free_addresses(), 2) # Allocate 4 addresses. 248+3+3 = 254, so only 2 addresses left ips = block1.auto_assign(4, None, {}) assert_list_equal([IPAddress("2001:abcd:def0::fe"), IPAddress("2001:abcd:def0::ff")], ips) assert_equal(block1.count_free_addresses(), 0) # Block is now full, further attempts return no addresses ips = block1.auto_assign(4, None, {}) assert_list_equal([], ips) # Test that we can cope with already allocated addresses that aren't # sequential. block2 = _test_block_not_empty_v6() assert_equal(block2.count_free_addresses(), BLOCK_SIZE - 2) ips = block2.auto_assign(4, None, {}) assert_list_equal([IPAddress("2001:abcd:def0::"), IPAddress("2001:abcd:def0::1"), IPAddress("2001:abcd:def0::3"), IPAddress("2001:abcd:def0::5")], ips) assert_equal(block2.count_free_addresses(), BLOCK_SIZE - 6) # Test ordinal math still works for small IPv6 addresses block3 = AllocationBlock(IPNetwork("::1234:5600/120"), "test_host1") ips = block3.auto_assign(4, None, {}) assert_list_equal([IPAddress("::1234:5600"), IPAddress("::1234:5601"), IPAddress("::1234:5602"), IPAddress("::1234:5603")], ips) assert_equal(block3.count_free_addresses(), BLOCK_SIZE - 4)