Пример #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_from_etcd_result(self):

        block_dict = {"10.23.24.0/24": 50, "10.23.35.0/24": 60}
        json_dict = {
            AllocationHandle.HANDLE_ID: "test_id2",
            AllocationHandle.BLOCK: block_dict
        }
        m_result = Mock(spec=EtcdResult)
        m_result.value = json.dumps(json_dict)

        handle = AllocationHandle.from_etcd_result(m_result)

        assert_dict_equal(block_dict, handle.block)
        assert_equal(m_result, handle.db_result)

        # Convert to JSON and back
        m_result.value = handle.to_json()
        handle2 = AllocationHandle.from_etcd_result(m_result)
        assert_equal(block_dict, handle2.block)
Пример #4
0
    def test_from_etcd_result(self):

        block_dict = {
            "10.23.24.0/24": 50,
            "10.23.35.0/24": 60
        }
        json_dict = {
            AllocationHandle.HANDLE_ID: "test_id2",
            AllocationHandle.BLOCK: block_dict
        }
        m_result = Mock(spec=EtcdResult)
        m_result.value = json.dumps(json_dict)

        handle = AllocationHandle.from_etcd_result(m_result)

        assert_dict_equal(block_dict, handle.block)
        assert_equal(m_result, handle.db_result)

        # Convert to JSON and back
        m_result.value = handle.to_json()
        handle2 = AllocationHandle.from_etcd_result(m_result)
        assert_equal(block_dict, handle2.block)
Пример #5
0
 def _read_handle(self, handle_id):
     """
     Read the handle with the given handle ID from the data store.
     :param handle_id: The handle ID to read.
     :return: AllocationHandle object.
     """
     key = _handle_datastore_key(handle_id)
     try:
         result = self.etcd_client.read(key, quorum=True)
     except EtcdKeyNotFound:
         raise KeyError(handle_id)
     handle = AllocationHandle.from_etcd_result(result)
     return handle
Пример #6
0
 def _read_handle(self, handle_id):
     """
     Read the handle with the given handle ID from the data store.
     :param handle_id: The handle ID to read.
     :return: AllocationHandle object.
     """
     key = _handle_datastore_key(handle_id)
     try:
         result = self.etcd_client.read(key)
     except EtcdKeyNotFound:
         raise KeyError(handle_id)
     handle = AllocationHandle.from_etcd_result(result)
     return handle
Пример #7
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
Пример #8
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
Пример #9
0
    def test_update_result(self):

        block_dict = {"10.23.24.0/24": 50, "10.23.35.0/24": 60}
        json_dict = {
            AllocationHandle.HANDLE_ID: "test_id2",
            AllocationHandle.BLOCK: block_dict
        }
        m_result = Mock(spec=EtcdResult)
        m_result.value = json.dumps(json_dict)

        handle = AllocationHandle.from_etcd_result(m_result)
        handle.decrement_block(IPNetwork("10.23.35.0/24"), 15)

        result = handle.update_result()
        assert_equal(result, m_result)
        result_json = json.loads(result.value)
        assert_equal(result_json[AllocationHandle.BLOCK]["10.23.35.0/24"], 45)
Пример #10
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)
Пример #11
0
    def test_update_result(self):

        block_dict = {
            "10.23.24.0/24": 50,
            "10.23.35.0/24": 60
        }
        json_dict = {
            AllocationHandle.HANDLE_ID: "test_id2",
            AllocationHandle.BLOCK: block_dict
        }
        m_result = Mock(spec=EtcdResult)
        m_result.value = json.dumps(json_dict)

        handle = AllocationHandle.from_etcd_result(m_result)
        handle.decrement_block(IPNetwork("10.23.35.0/24"), 15)

        result = handle.update_result()
        assert_equal(result, m_result)
        result_json = json.loads(result.value)
        assert_equal(result_json[AllocationHandle.BLOCK]["10.23.35.0/24"],
                     45)
Пример #12
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)