def test_get_hosts(self, instance): mgr.list_servers.return_value = [{ 'hostname': 'node1' }, { 'hostname': 'localhost' }] fake_client = mock.Mock() fake_client.available.return_value = True fake_client.hosts.list.return_value = [ InventoryNode('node1', []), InventoryNode('node2', []) ] instance.return_value = fake_client hosts = get_hosts() self.assertEqual(len(hosts), 3) check_sources = { 'localhost': { 'ceph': True, 'orchestrator': False }, 'node1': { 'ceph': True, 'orchestrator': True }, 'node2': { 'ceph': False, 'orchestrator': True } } for host in hosts: hostname = host['hostname'] sources = host['sources'] self.assertDictEqual(sources, check_sources[hostname])
def test_inventory(): json_data = { 'name': 'host0', 'addr': '1.2.3.4', 'devices': [{ 'sys_api': { 'rotational': '1', 'size': 1024, }, 'path': '/dev/sda', 'available': False, 'rejected_reasons': [], 'lvs': [] }] } _test_resource(json_data, InventoryNode, {'abc': False}) for devices in json_data['devices']: _test_resource(devices, inventory.Device) json_data = [{}, {'name': 'host0', 'addr': '1.2.3.4'}, {'devices': []}] for data in json_data: with pytest.raises(OrchestratorValidationError): InventoryNode.from_json(data)
def test_host(self, cephadm_module): with self._with_host(cephadm_module, 'test'): assert self._wait(cephadm_module, cephadm_module.get_hosts()) == [ InventoryNode('test') ] c = cephadm_module.get_hosts() assert self._wait(cephadm_module, c) == []
def test_inventory(): json_data = { 'name': 'host0', 'devices': [{ 'type': 'hdd', 'id': '/dev/sda', 'size': 1024, 'rotates': True }] } _test_resource(json_data, InventoryNode, {'abc': False}) for devices in json_data['devices']: _test_resource(devices, InventoryDevice, {'abc': False}) json_data = [{}, {'name': 'host0'}, {'devices': []}] for data in json_data: with pytest.raises(OrchestratorValidationError): InventoryNode.from_json(data)
def process(self, operation_id, raw_result): """ :param operation_id: Playbook uuid :param raw_result: events dict with the results Example: inventory_events = {'37-100564f1-9fed-48c2-bd62-4ae8636dfcdb': {'host': '192.168.121.254', 'task': 'list storage inventory', 'event': 'runner_on_ok'}, '36-2016b900-e38f-7dcd-a2e7-00000000000e': {'host': '192.168.121.252' 'task': 'list storage inventory', 'event': 'runner_on_ok'}} :return : list of InventoryNode """ # Just making more readable the method inventory_events = raw_result #Obtain the needed data for each result event inventory_nodes = [] # Loop over the result events and request the event data for event_key, dummy_data in inventory_events.items(): event_response = self.ar_client.http_get(EVENT_DATA_URL % (operation_id, event_key)) # self.pb_execution.play_uuid # Process the data for each event if event_response: event_data = json.loads( event_response.text)["data"]["event_data"] host = event_data["host"] devices = json.loads(event_data["res"]["stdout"]) devs = [] for storage_device in devices: dev = InventoryDevice.from_ceph_volume_inventory( storage_device) devs.append(dev) inventory_nodes.append(InventoryNode(host, devs)) return inventory_nodes
def process(self, operation_id, raw_result): """ Format the output of host ls call :param operation_id: Not used in this output wizard :param raw_result: In this case is like the following json: { "status": "OK", "msg": "", "data": { "hosts": [ "host_a", "host_b", ... "host_x", ] } } :return: list of InventoryNodes """ # Just making more readable the method host_ls_json = raw_result inventory_nodes = [] try: json_resp = json.loads(host_ls_json) for host in json_resp["data"]["hosts"]: inventory_nodes.append( InventoryNode(host, inventory.Devices([]))) except ValueError: logger.exception("Malformed json response") except KeyError: logger.exception("Unexpected content in Ansible Runner Service" " response") except TypeError: logger.exception("Hosts data must be iterable in Ansible Runner " "Service response") return inventory_nodes
def test_device_ls(self, cephadm_module): with self._with_host(cephadm_module, 'test'): c = cephadm_module.get_inventory() assert self._wait(cephadm_module, c) == [InventoryNode('test')]
def _list_inventory(hosts=None, refresh=False): nodes = [] for node in inventory: if hosts is None or node['name'] in hosts: nodes.append(InventoryNode.from_json(node)) return nodes
def test_host(self, _get_connection, _save_host, _rm_host, cephadm_module): with self._with_host(cephadm_module, 'test'): assert wait(cephadm_module, cephadm_module.get_hosts()) == [InventoryNode('test')] c = cephadm_module.get_hosts() assert wait(cephadm_module, c) == []
def _list_inventory(hosts=None, refresh=False): nodes = [] for node in inventory: if hosts is None or node['name'] in hosts: nodes.append(InventoryNode(node['name'], node['devices'])) return nodes