def test_phone_home(self): instance_id = data_factory.generate_random_string(12) address = '127.0.0.1' port = network.find_free_port(address=address) server = cloudinit.PhoneHomeServer((address, port), instance_id) self.assertFalse(server.instance_phoned_back) server_thread = threading.Thread(target=server.handle_request) server_thread.start() conn = http_client.HTTPConnection(address, port) # contact the wrong url, and check the server does not # consider it a call back from the expected caller conn.request('POST', '/BAD_INSTANCE_ID') try: conn.getresponse() except: pass self.assertFalse(server.instance_phoned_back) conn.close() # now the right url server_thread = threading.Thread(target=server.handle_request) server_thread.start() conn = http_client.HTTPConnection(address, port) conn.request('POST', '/' + instance_id) try: conn.getresponse() except: pass self.assertTrue(server.instance_phoned_back) conn.close() server.server_close()
def setUp(self): super(JsonResultTest, self).setUp() status_server = '127.0.0.1:%u' % find_free_port() self.config_file = script.TemporaryScript( 'avocado.conf', ("[nrunner]\n" "status_server_listen = %s\n" "status_server_uri = %s\n") % (status_server, status_server)) self.config_file.save()
def __init__(self, path='/usr/bin/gdbserver', port=None, wait_until_running=True, *extra_args): """ Initializes a new gdbserver instance :param path: location of the gdbserver binary :type path: str :param port: tcp port number to listen on for incoming connections :type port: int :param wait_until_running: wait until the gdbserver is running and accepting connections. It may take a little after the process is started and it is actually bound to the aloccated port :type wait_until_running: bool :param extra_args: optional extra arguments to be passed to gdbserver """ self.path = path args = [self.path] args += self.REQUIRED_ARGS # The output on stderr generated by gdbserver itself is of no use to # avocado. Quite on the contrary, it gets mixed with the "inferior" # process stderr. Support is being added to gdbserver to send its own # stderr output somewhere else. If available, use it. output = commands.getoutput("%s --help" % self.path) if '--server-stderr=' in output: self.redirected_stderr = True args.append('--server-stderr=/dev/null') else: self.redirected_stderr = False if port is None: self.port = network.find_free_port(*self.PORT_RANGE) else: self.port = port args.append(":%s" % self.port) prefix = 'avocado_gdbserver_%s_' % self.port _, self.stdout_path = tempfile.mkstemp(prefix=prefix + 'stdout_') self.stdout = open(self.stdout_path, 'w') _, self.stderr_path = tempfile.mkstemp(prefix=prefix + 'stderr_') self.stderr = open(self.stderr_path, 'w') self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr, close_fds=True) if wait_until_running: self._wait_until_running()
def __init__(self, path='/usr/bin/gdbserver', port=None, wait_until_running=True, *extra_args): """ Initializes a new gdbserver instance :param path: location of the gdbserver binary :type path: str :param port: tcp port number to listen on for incoming connections :type port: int :param wait_until_running: wait until the gdbserver is running and accepting connections. It may take a little after the process is started and it is actually bound to the aloccated port :type wait_until_running: bool :param extra_args: optional extra arguments to be passed to gdbserver """ self.path = path args = [self.path] args += self.REQUIRED_ARGS # The output on stderr generated by gdbserver itself is of no use to # avocado. Quite on the contrary, it gets mixed with the "inferior" # process stderr. Support is being added to gdbserver to send its own # stderr output somewhere else. If available, use it. output = commands.getoutput("%s --help" % self.path) if '--server-stderr=' in output: self.redirected_stderr = True args.append('--server-stderr=/dev/null') else: self.redirected_stderr = False if port is None: self.port = network.find_free_port(*self.PORT_RANGE) else: self.port = port args.append(":%s" % self.port) prefix = 'avocado_gdbserver_%s_' % self.port _, self.stdout_path = tempfile.mkstemp(prefix=prefix + 'stdout_') self.stdout = open(self.stdout_path, 'w') _, self.stderr_path = tempfile.mkstemp(prefix=prefix + 'stderr_') self.stderr = open(self.stderr_path, 'w') self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr, close_fds=True) if wait_until_running: self._wait_until_running()
def download_cloudinit(self): self.log.info('Preparing cloudinit image') try: cloudinit_iso = os.path.join(self.workdir, 'cloudinit.iso') self.phone_home_port = network.find_free_port() cloudinit.iso(cloudinit_iso, self.name, username='******', password='******', # QEMU's hard coded usermode router address phone_home_host='10.0.2.2', phone_home_port=self.phone_home_port) except Exception: self.cancel('Failed to prepared cloudinit image') return cloudinit_iso
def prepare_cloudinit(self): self.log.info('Preparing cloudinit image') try: cloudinit_iso = os.path.join(self.workdir, 'cloudinit.iso') self.phone_home_port = network.find_free_port() cloudinit.iso(cloudinit_iso, self.name, username='******', password='******', # QEMU's hard coded usermode router address phone_home_host='10.0.2.2', phone_home_port=self.phone_home_port) self.vm.add_args('-drive', 'file=%s,format=raw' % cloudinit_iso) except Exception: self.cancel('Failed to prepared cloudinit image')
def __init__(self): self.port = network.find_free_port(*self.PORT_RANGE) prefix = 'avocado_gdbserver_%s_' % self.port _, self.stdout_path = tempfile.mkstemp(prefix=prefix + 'stdout_') self.stdout = open(self.stdout_path, 'w') _, self.stderr_path = tempfile.mkstemp(prefix=prefix + 'stderr_') self.stderr = open(self.stderr_path, 'w') args = self.ARGS[:] args.append(":%s" % self.port) self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr, close_fds=True)
def start_metrics_server(port=9389): """ https://github.com/prometheus/prometheus/wiki/Default-port-allocations Occupied port 9389 for SCT """ hostname = socket.gethostname() if not network.is_port_free(port, hostname): port = network.find_free_port(8001, 10000) try: logger.debug('Try to start prometheus API server on port: %s', port) prometheus_client.start_http_server(port) ip = socket.gethostbyname(hostname) return '{}:{}'.format(ip, port) except Exception as ex: logger.error('Cannot start local http metrics server: %s', ex) return None
def start_metrics_server(port=9389): """ https://github.com/prometheus/prometheus/wiki/Default-port-allocations Occupied port 9389 for SCT """ hostname = socket.gethostname() if not network.is_port_free(port, hostname): port = network.find_free_port(8001, 10000) try: logger.debug('Try to start prometheus API server on port: %s', port) prometheus_client.start_http_server(port) ip = socket.gethostbyname(hostname) return '{}:{}'.format(ip, port) except Exception as ex: logger.error('Cannot start local http metrics server: %s', ex) return None
def prepare_cloudinit(self, ssh_pubkey=None): self.log.info('Preparing cloudinit image') try: cloudinit_iso = os.path.join(self.workdir, 'cloudinit.iso') self.phone_home_port = network.find_free_port() pubkey_content = None if ssh_pubkey: with open(ssh_pubkey) as pubkey: pubkey_content = pubkey.read() cloudinit.iso(cloudinit_iso, self.name, username=self.username, password=self.password, # QEMU's hard coded usermode router address phone_home_host='10.0.2.2', phone_home_port=self.phone_home_port, authorized_key=pubkey_content) except Exception: self.cancel('Failed to prepare the cloudinit image') return cloudinit_iso
def test_is_port_free(self): port = network.find_free_port(sequent=False) self.assertTrue(network.is_port_free(port, "localhost")) local_addrs = get_all_local_addrs() ipv4_addrs = ["localhost", ""] + list(local_addrs[0]) ipv6_addrs = ["localhost", ""] + list(local_addrs[1]) good = [] bad = [] skip = [] sock = None for family in network.FAMILIES: if family == socket.AF_INET: addrs = ipv4_addrs else: addrs = ipv6_addrs for addr in addrs: for protocol in network.PROTOCOLS: try: sock = socket.socket(family, protocol) sock.bind((addr, port)) if network.is_port_free(port, "localhost"): bad.append("%s, %s, %s: reports free" % (family, protocol, addr)) else: good.append("%s, %s, %s" % (family, protocol, addr)) except Exception as exc: if getattr(exc, 'errno', None) in (-2, 2, 22, 94): skip.append("%s, %s, %s: Not supported: %s" % (family, protocol, addr, exc)) else: bad.append("%s, %s, %s: Failed to bind: %s" % (family, protocol, addr, exc)) finally: if sock is not None: sock.close() self.assertFalse( bad, "Following combinations failed:\n%s\n\n" "Following combinations passed:\n%s\n\n" "Following combinations were skipped:\n%s" % ("\n".join(bad), "\n".join(good), "\n".join(skip)))
def __init__(self, path='/usr/bin/gdbserver', port=None, wait_until_running=True, *extra_args): """ Initializes a new gdbserver instance :param path: location of the gdbserver binary :type path: str :param port: tcp port number to listen on for incoming connections :type port: int :param wait_until_running: wait until the gdbserver is running and accepting connections. It may take a little after the process is started and it is actually bound to the aloccated port :type wait_until_running: bool :param extra_args: optional extra arguments to be passed to gdbserver """ self.path = path args = [self.path] args += self.REQUIRED_ARGS if port is None: self.port = network.find_free_port(*self.PORT_RANGE) else: self.port = port args.append(":%s" % self.port) prefix = 'avocado_gdbserver_%s_' % self.port _, self.stdout_path = tempfile.mkstemp(prefix=prefix + 'stdout_') self.stdout = open(self.stdout_path, 'w') _, self.stderr_path = tempfile.mkstemp(prefix=prefix + 'stderr_') self.stderr = open(self.stderr_path, 'w') self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr, close_fds=True) if wait_until_running: self._wait_until_running()
def test_is_port_free(self): port = network.find_free_port(sequent=False) self.assertTrue(network.is_port_free(port, "localhost")) local_addrs = get_all_local_addrs() ipv4_addrs = ["localhost", ""] + list(local_addrs[0]) ipv6_addrs = ["localhost", ""] + list(local_addrs[1]) good = [] bad = [] skip = [] sock = None for family in network.FAMILIES: if family == socket.AF_INET: addrs = ipv4_addrs else: addrs = ipv6_addrs for addr in addrs: for protocol in network.PROTOCOLS: try: sock = socket.socket(family, protocol) sock.bind((addr, port)) if network.is_port_free(port, "localhost"): bad.append("%s, %s, %s: reports free" % (family, protocol, addr)) else: good.append("%s, %s, %s" % (family, protocol, addr)) except Exception as exc: if getattr(exc, 'errno', None) in (-2, 2, 22, 94): skip.append("%s, %s, %s: Not supported: %s" % (family, protocol, addr, exc)) else: bad.append("%s, %s, %s: Failed to bind: %s" % (family, protocol, addr, exc)) finally: if sock is not None: sock.close() self.assertFalse(bad, "Following combinations failed:\n%s\n\n" "Following combinations passed:\n%s\n\n" "Following combinations were skipped:\n%s" % ("\n".join(bad), "\n".join(good), "\n".join(skip)))
def setUp(self): self.port = network.find_free_port(address=self.ADDRESS, sequent=False) self.instance_id = data_factory.generate_random_string(12) self.server = cloudinit.PhoneHomeServer((self.ADDRESS, self.port), self.instance_id)
def _get_free_port(self): port = network.find_free_port() if port is None: self.cancel('Failed to find a free port') return port
def setUp(self): self.port = network.find_free_port(address=self.ADDRESS) self.instance_id = data_factory.generate_random_string(12) self.server = cloudinit.PhoneHomeServer((self.ADDRESS, self.port), self.instance_id)
def _get_free_port(self): port = network.find_free_port() if port is None: self.cancel('Failed to find a free port') return port