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)
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)
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
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
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)
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)