def list_podman_containers(client=None, **kwargs): try: containers = podman_client(client).containers.list(**kwargs) except _exception.DockerUrlNotFoundError: return tobiko.Selection() else: return tobiko.select(containers)
def _add_node(self, addresses: typing.List[netaddr.IPAddress], hostname: str = None, ssh_client: typing.Optional[ssh.SSHClientFixture] = None): if ssh_client is None: ssh_client = self._ssh_connect(addresses=addresses) addresses.extend(self._list_addresses_from_host(ssh_client=ssh_client)) addresses = tobiko.select(remove_duplications(addresses)) hostname = hostname or sh.get_hostname(ssh_client=ssh_client) name = node_name_from_hostname(hostname) try: node = self._names[name] except KeyError: LOG.debug("Add topology node:\n" f" - name: {name}\n" f" - hostname: {hostname}\n" f" - login: {ssh_client.login}\n" f" - addresses: {addresses}\n") self._names[name] = node = self.create_node(name=name, hostname=hostname, ssh_client=ssh_client, addresses=addresses) for address in addresses: address_node = self._addresses.setdefault(address, node) if address_node is not node: LOG.error(f"Address '{address}' of node '{name}' is already " f"used by node '{address_node.name}'") return node
def _ips(self, obj): if isinstance(obj, tobiko.Selection): ips = obj elif isinstance(obj, netaddr.IPAddress): ips = tobiko.select([obj]) elif isinstance(obj, six.string_types): try: ips = tobiko.select([netaddr.IPAddress(obj)]) except (netaddr.AddrFormatError, ValueError): ips = resolve_host_ips(obj) else: for item in iter(obj): tobiko.check_valid_type(item, netaddr.IPAddress) ips = tobiko.select(obj) if ips and self.ip_version: ips = ips.with_attributes(version=self.ip_version) return ips
def list_subnets(client=None, ip_version: typing.Optional[int] = None, **params) -> tobiko.Selection[NeutronSubnetType]: if ip_version is not None: params['ip_version'] = ip_version subnets = neutron_client(client).list_subnets(**params) if isinstance(subnets, abc.Mapping): subnets = subnets['subnets'] return tobiko.select(subnets)
def list_docker_containers(client=None, **kwargs): try: containers = docker_client(client).containers.list(all=True, sparse=True, **kwargs) except _exception.DockerUrlNotFoundError: return tobiko.Selection() else: return tobiko.select(containers)
def list_addresses(obj, ip_version: typing.Optional[int] = None, port: typing.Union[int, str, None] = None, ssh_config: bool = False) -> \ tobiko.Selection[netaddr.IPAddress]: if isinstance(obj, tobiko.Selection): addresses = obj elif isinstance(obj, netaddr.IPAddress): addresses = tobiko.select([obj]) elif isinstance(obj, str): addresses = tobiko.select( list_host_addresses(obj, ip_version=ip_version, port=port, ssh_config=ssh_config)) elif isinstance(obj, abc.Sequence): addresses = tobiko.Selection() for item in iter(obj): addresses.extend(list_addresses(item)) if addresses and ip_version is not None: addresses = addresses.with_attributes(version=ip_version) return addresses
def list_services(client=None, name=None, service_type=None, **attributes): client = keystone_client(client) service_type = service_type or attributes.pop('type', None) if service_type: attributes['type'] = base.getid(service_type) if name: attributes['name'] = name if client.version == 'v2.0': services = client.services.list() else: services = client.services.list(name=name, service_type=service_type) services = tobiko.select(services) if attributes: services = services.with_attributes(**attributes) return services
def test_servers_creation(stack=TestServerCreationStack, number_of_servers=2) -> \ tobiko.Selection[_nova.ServerStackFixture]: initial_servers_ids = {server.id for server in nova.list_servers()} pid = os.getpid() fixture_obj = tobiko.get_fixture_class(stack) # Get list of server stack instances fixtures: tobiko.Selection[_nova.ServerStackFixture] = tobiko.select( tobiko.get_fixture(fixture_obj, fixture_id=f'{pid}-{i}') for i in range(number_of_servers or 1)) test_case = tobiko.get_test_case() # Check fixtures types for fixture in fixtures: test_case.assertIsInstance(fixture, _nova.ServerStackFixture) # Delete all servers stacks for fixture in fixtures: tobiko.cleanup_fixture(fixture) # Create all servers stacks for fixture in fixtures: tobiko.use_fixture(fixture) # Check every server ID is unique and new server_ids = {fixture.server_id for fixture in fixtures} test_case.assertEqual(number_of_servers or 1, len(server_ids)) test_case.assertFalse(server_ids & initial_servers_ids) # sleep for 20 sec , ensure no race condition with ssh time.sleep(20) # Test SSH connectivity to floating IP address for fixture in fixtures: test_case.assertTrue(sh.get_hostname(ssh_client=fixture.ssh_client)) # Test pinging to floating IP address ping.assert_reachable_hosts(fixture.floating_ip_address for fixture in fixtures) return fixtures
def add_node(self, hostname: typing.Optional[str] = None, address: typing.Optional[str] = None, group: typing.Optional[str] = None, ssh_client: typing.Optional[ssh.SSHClientFixture] = None) \ -> OpenStackTopologyNode: if ssh_client is not None: # detect all global addresses from remote server try: hostname = sh.get_hostname(ssh_client=ssh_client) except Exception: LOG.exception("Unable to get node hostname from " f"{ssh_client}") ssh_client = None name = hostname and node_name_from_hostname(hostname) or None addresses: typing.List[netaddr.IPAddress] = [] if address: # add manually configure addresses first addresses.extend(self._list_addresses(address)) if hostname: # detect more addresses from the hostname addresses.extend(self._list_addresses(hostname)) addresses = tobiko.select(remove_duplications(addresses)) try: node = self.get_node(name=name, address=addresses) except _exception.NoSuchOpenStackTopologyNode: node = None node = node or self._add_node( addresses=addresses, hostname=hostname, ssh_client=ssh_client) if group: # Add group anyway even if the node hasn't been added group_nodes = self.add_group(group=group) if node and node not in group_nodes: group_nodes.append(node) node.add_group(group=group) return node
def list_endpoints(client=None, service=None, interface=None, region=None, translate=True, **attributes): client = keystone_client(client) service = service or attributes.pop('service_id', None) if service: attributes['service_id'] = base.getid(service) region = region or attributes.pop('region_id', None) if region: attributes['region_id'] = base.getid(region) try: if client.version == 'v2.0': endpoints = client.endpoints.list() if translate: endpoints = translate_v2_endpoints(v2_endpoints=endpoints, interface=interface) else: endpoints = client.endpoints.list(service=service, interface=interface, region=region) except exceptions.ClientException: LOG.exception('Error listing services endpoints:\n' f' client: {client}\n' f' service: {service}\n' f' interface: {interface}\n' f' region: {region}\n' f' translate: {translate}\n' f' attributes: {attributes}\n') raise endpoints = tobiko.select(endpoints) if attributes: endpoints = endpoints.with_attributes(**attributes) return endpoints
def add_node(self, hostname=None, address=None, group=None, ssh_client=None): name = hostname and node_name_from_hostname(hostname) or None ips = set() if address: ips.update(self._ips(address)) if hostname: ips.update(self._ips(hostname)) ips = tobiko.select(ips) try: node = self.get_node(name=name, address=ips) except _exception.NoSuchOpenStackTopologyNode: node = self._add_node(hostname=hostname, ips=ips, ssh_client=ssh_client) if node and group: self.add_group(group=group).append(node) node.add_group(group=group) return node
def router_ips(self): return tobiko.select([self.ipv4_subnet_gateway_ip, self.ipv6_subnet_gateway_ip] + self.external_gateway_ips)
def list_services(client: NovaClientType = None, **params) -> tobiko.Selection: client = nova_client(client) services = client.services.list() return tobiko.select(services).with_attributes(**params)
def list_floating_ips(client=None, retrieve_all=True, **params): floating_ips = neutron_client(client).list_floatingips( retrieve_all=retrieve_all, **params)['floatingips'] return tobiko.select(floating_ips)
def list_hypervisors(client=None, detailed=True, **params): client = nova_client(client) hypervisors = client.hypervisors.list(detailed=detailed) return tobiko.select(hypervisors).with_attributes(**params)
def nodes(self): return tobiko.select( self.get_node(name) for name in self._nodes_by_name)
def list_servers(client=None, **params): client = nova_client(client) servers = client.servers.list() return tobiko.select(servers).with_attributes(**params)
def create_selection(*args, **kwargs): return tobiko.select(*args, **kwargs)
def list_subnet_cidrs(client=None, **params): return tobiko.select( netaddr.IPNetwork(subnet['cidr']) for subnet in list_subnets(client=client, **params))
def list_subnets(client=None, **params): subnets = neutron_client(client).list_subnets(**params) if isinstance(subnets, collections.Mapping): subnets = subnets['subnets'] return tobiko.select(subnets)
def list_networks(client=None, **params): networks = neutron_client(client).list_networks(**params)['networks'] return tobiko.select(networks)
def external_gateway_ips(self): fixed_ips = self.gateway_details['external_gateway_info'][ 'external_fixed_ips'] return tobiko.select( netaddr.IPAddress(fixed_ip['ip_address']) for fixed_ip in fixed_ips)
def nodes(self) -> tobiko.Selection[OpenStackTopologyNode]: return tobiko.select(self.get_node(name) for name in self._names)
def list_hypervisors(client: NovaClientType = None, detailed=True, **params) \ -> tobiko.Selection[NovaHypervisor]: client = nova_client(client) hypervisors = client.hypervisors.list(detailed=detailed) return tobiko.select(hypervisors).with_attributes(**params)
def list_ports(client=None, **params): ports = neutron_client(client).list_ports(**params)['ports'] return tobiko.select(ports)
def list_servers(client: NovaClientType = None, **params) -> \ tobiko.Selection[NovaServer]: servers = nova_client(client).servers.list() return tobiko.select(servers).with_attributes(**params)
def list_agents(client=None, **params): agents = neutron_client(client).list_agents(**params) if isinstance(agents, collections.Mapping): agents = agents['agents'] return tobiko.select(agents)
def get_hypervisors(**params): hypervisors = tobiko.setup_fixture(HypervisorListFixture).hypervisors return tobiko.select(hypervisors).with_attributes(**params)
def list_l3_agent_hosting_routers(router, client=None, **params): agents = neutron_client(client).list_l3_agent_hosting_routers( router, **params) if isinstance(agents, collections.Mapping): agents = agents['agents'] return tobiko.select(agents)
def list_images(client=None, limit=None, **filters): images = glance_client(client).images.list(limit=limit, filters=filters) return tobiko.select(images)