def get_dlrn_api_url(self, config): """ API url is the wild west of exceptions, when it's not specified in config files, we need an entire function to try to understand what we can use :param config: The existing configuration :return: the first API url that responds. """ # Try local staged api first api_host = "localhost" api_port = "58080" api_url = None if common.check_port(api_host, api_port): api_url = "http://{}:{}".format(api_host, api_port) else: distro_api_endpoint = config['distro_name'] if config['distro_version'] == '8': distro_api_endpoint += config['distro_version'] release_api_endpoint = config['release'] if config['release'] == "master": release_api_endpoint = "master-uc" api_endpoint = "api-{}-{}".format(distro_api_endpoint, release_api_endpoint) api_host = self.defaults['dlrn_api_host'] api_port = 443 if common.check_port(api_host, api_port, 5): api_url = "https://{}/{}".format(api_host, api_endpoint) if api_url is None: self.log.error("No valid API url found") else: self.log.debug("Assigning api_url %s", api_url) return api_url
def test_check_port_open_timeout(self, socket_connect_mock): socket_connect_mock.side_effect = ConnectionRefusedError timestamp_start = datetime.datetime.now() self.assertFalse(check_port("localhost", 100, timeout=2)) timestamp_end = datetime.datetime.now() timedelta_check = \ timestamp_end - timestamp_start >= datetime.timedelta(seconds=2) error_msg = "Timeout not honored" assert timedelta_check, error_msg
def test_check_port_closed_timeout(self, socket_connect_mock): socket_connect_mock.return_value = True timestamp_start = datetime.datetime.now() self.assertFalse( check_port("localhost", 100, timeout=2, port_mode="closed")) timestamp_end = datetime.datetime.now() timedelta_check = \ timestamp_end - timestamp_start >= datetime.timedelta(seconds=2) error_msg = "Timeout not honored" self.assertTrue(timedelta_check, error_msg)
def teardown(self, __): """ Cleans up resource created by staging DLRN server and repo Kills the server and removes the dlrn tree :param __: An unused parameter useful for other teardown methods :return: None """ if self.dry_run: return self.log.info("Shutting down DLRN server") try: subprocess.check_call(self.teardown_cmd.split()) except subprocess.CalledProcessError: self.log.warning("Cannot shut down DLRN: no" " process running") # if the server respawns too fast, the address may be still in use # Wait until server port is unavailable before moving on check_port(self.host, self.port, port_mode="closed") self.log.info("Removing dlrn server root dir") shutil.rmtree(self.server_root)
def run_server(self): """ Sets up the DLRN server working dir with launch script and configuration files, then launches the server :return: None """ self.log.debug("Launching DLRN server with command: '%s'", self.launch_cmd) # Create dlrn staging server script on dlrn root dir dlrn_server_path = os.path.join(self.server_root, "dlrn_staging_server.py") with open(dlrn_server_path, "w") as dlrn_staging_script: dlrn_staging_script.write(dlrn_staging_server) # Create server configuration in project.ini # It'optional for the single pipeline, but needed for the component # as we need to change the use_components variable if self.components_mode: with open(self.project_ini_path, 'w') as config_file: self.project_conf.write(config_file) # Launches the server working_dir = os.getcwd() os.chdir(self.server_root) try: # TODO: redirect stdout to logs subprocess.Popen(self.launch_cmd.split()) except OSError: self.log.error("Cannot launch DLRN server: requirements missing") raise os.chdir(working_dir) # Wait until server port is available before moving on if not check_port(self.host, self.port, 5): raise Exception("Dlrn server not listening to port")
def test_check_port_closed_true(self, socket_connect_mock): socket_connect_mock.side_effect = ConnectionRefusedError self.assertTrue(check_port("localhost", 100, port_mode="closed"))
def test_check_port_open_true(self, socket_connect_mock): socket_connect_mock.return_value = True self.assertTrue(check_port("localhost", 100))