示例#1
0
    def test_to_json(self):

        handle = AllocationHandle("test_id")

        expected_json = {AllocationHandle.HANDLE_ID: "test_id",
                         AllocationHandle.BLOCK: {}}
        json_str = handle.to_json()
        json_result = json.loads(json_str)
        assert_dict_equal(expected_json, json_result)

        block_cidr = IPNetwork("10.11.12.0/24")
        handle.increment_block(block_cidr, 5)

        expected_json[AllocationHandle.BLOCK]["10.11.12.0/24"] = 5
        json_str = handle.to_json()
        json_result = json.loads(json_str)
        assert_dict_equal(expected_json, json_result)

        block_cidr2 = IPNetwork("10.11.45.0/24")
        handle.increment_block(block_cidr2, 20)

        expected_json[AllocationHandle.BLOCK]["10.11.45.0/24"] = 20
        json_str = handle.to_json()
        json_result = json.loads(json_str)
        assert_dict_equal(expected_json, json_result)
示例#2
0
    def test_to_json(self):

        handle = AllocationHandle("test_id")

        expected_json = {
            AllocationHandle.HANDLE_ID: "test_id",
            AllocationHandle.BLOCK: {}
        }
        json_str = handle.to_json()
        json_result = json.loads(json_str)
        assert_dict_equal(expected_json, json_result)

        block_cidr = IPNetwork("10.11.12.0/24")
        handle.increment_block(block_cidr, 5)

        expected_json[AllocationHandle.BLOCK]["10.11.12.0/24"] = 5
        json_str = handle.to_json()
        json_result = json.loads(json_str)
        assert_dict_equal(expected_json, json_result)

        block_cidr2 = IPNetwork("10.11.45.0/24")
        handle.increment_block(block_cidr2, 20)

        expected_json[AllocationHandle.BLOCK]["10.11.45.0/24"] = 20
        json_str = handle.to_json()
        json_result = json.loads(json_str)
        assert_dict_equal(expected_json, json_result)
示例#3
0
    def test_inc_dec_block(self):
        block = [IPNetwork("10.11.12.0/24"),
                 IPNetwork("2001:abcd:def0::/120"),
                 IPNetwork("192.168.1.0")]

        handle = AllocationHandle("tst_id1")

        result = handle.increment_block(block[0], 20)
        assert_equal(result, 20)

        result = handle.decrement_block(block[0], 15)
        assert_equal(result, 5)

        assert_raises(AddressCountTooLow,
                      handle.decrement_block, block[1], 1)

        result = handle.increment_block(block[1], 1)
        assert_equal(result, 1)

        result = handle.increment_block(block[2], 10)
        assert_equal(result, 10)

        result = handle.decrement_block(block[1], 1)
        assert_equal(result, 0)
        assert_false(str(block[1]) in handle.block)

        assert_raises(AddressCountTooLow,
                      handle.decrement_block, block[2], 11)

        result = handle.decrement_block(block[2], 10)
        assert_equal(result, 0)
        assert_false(str(block[2]) in handle.block)

        result = handle.decrement_block(block[0], 5)
        assert_equal(result, 0)
        assert_false(str(block[0]) in handle.block)
示例#4
0
    def test_inc_dec_block(self):
        block = [
            IPNetwork("10.11.12.0/24"),
            IPNetwork("2001:abcd:def0::/120"),
            IPNetwork("192.168.1.0")
        ]

        handle = AllocationHandle("tst_id1")

        result = handle.increment_block(block[0], 20)
        assert_equal(result, 20)

        result = handle.decrement_block(block[0], 15)
        assert_equal(result, 5)

        assert_raises(AddressCountTooLow, handle.decrement_block, block[1], 1)

        result = handle.increment_block(block[1], 1)
        assert_equal(result, 1)

        result = handle.increment_block(block[2], 10)
        assert_equal(result, 10)

        result = handle.decrement_block(block[1], 1)
        assert_equal(result, 0)
        assert_false(str(block[1]) in handle.block)

        assert_raises(AddressCountTooLow, handle.decrement_block, block[2], 11)

        result = handle.decrement_block(block[2], 10)
        assert_equal(result, 0)
        assert_false(str(block[2]) in handle.block)

        result = handle.decrement_block(block[0], 5)
        assert_equal(result, 0)
        assert_false(str(block[0]) in handle.block)
示例#5
0
    def _increment_handle(self, handle_id, block_cidr, amount):
        """
        Increment the allocation count on the given handle for the given block
        by the given amount.
        """
        for _ in xrange(RETRIES):
            try:
                handle = self._read_handle(handle_id)
            except KeyError:
                # handle doesn't exist.  Create it.
                handle = AllocationHandle(handle_id)

            _ = handle.increment_block(block_cidr, amount)

            try:
                self._compare_and_swap_handle(handle)
            except CASError:
                # CAS failed.  Retry.
                continue
            else:
                # success!
                return
        raise RuntimeError("Max retries hit.")  # pragma: no cover
示例#6
0
    def _increment_handle(self, handle_id, block_cidr, amount):
        """
        Increment the allocation count on the given handle for the given block
        by the given amount.
        """
        for _ in xrange(RETRIES):
            try:
                handle = self._read_handle(handle_id)
            except KeyError:
                # handle doesn't exist.  Create it.
                handle = AllocationHandle(handle_id)

            _ = handle.increment_block(block_cidr, amount)

            try:
                self._compare_and_swap_handle(handle)
            except CASError:
                # CAS failed.  Retry.
                continue
            else:
                # success!
                return
        raise RuntimeError("Max retries hit.")  # pragma: no cover