示例#1
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)
示例#2
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)
示例#3
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
示例#4
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
示例#5
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)
示例#6
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)