def test_salt_cli_display_name(config_file): factory_id = "TheID" proc = SaltDaemonFactory( start_timeout=1, cli_script_name="foo-bar", config={"id": factory_id, "conf_file": config_file}, ) assert proc.get_display_name() == "{}(id={!r})".format(SaltDaemonFactory.__name__, factory_id)
def test_default_cli_flags(config_dir, config_file, cli_script_name): config = {"conf_file": config_file, "id": "the-id"} expected = [ sys.executable, cli_script_name, "--config-dir={}".format(config_dir.strpath), "--log-level=quiet", ] proc = SaltDaemonFactory(start_timeout=1, cli_script_name=cli_script_name, config=config) cmdline = proc.build_cmdline() assert cmdline == expected
def test_extra_cli_arguments_after_first_failure(request, config_dir, config_file, tempfiles, master_id, tmpdir): """ This test asserts that after the first start failure, the extra_cli_arguments_after_first_start_failure arguments are added """ output_file = tmpdir.join("output.txt") config = {"conf_file": config_file, "id": master_id} script = tempfiles.makepyfile( r""" # coding=utf-8 import sys import multiprocessing def main(): with open({!r}, "a") as wfh: wfh.write(" ".join(sys.argv)) wfh.write("\n") sys.exit(1) # Support for windows test runs if __name__ == '__main__': multiprocessing.freeze_support() main() """.format(output_file.strpath), executable=True, ) daemon = SaltDaemonFactory( cli_script_name=script, config=config, start_timeout=0.25, max_start_attempts=2, check_ports=[12345], extra_cli_arguments_after_first_start_failure=["--log-level=debug"], ) # Make sure the daemon is terminated no matter what request.addfinalizer(daemon.terminate) with pytest.raises(FactoryNotStarted) as exc: daemon.start() output_file_contents = output_file.read().splitlines() expected = [ "{} --config-dir={} --log-level=quiet".format(script, config_dir.strpath), "{} --config-dir={} --log-level=debug".format(script, config_dir.strpath), ] assert output_file_contents == expected
def test_override_config_dir(config_dir, config_file, cli_script_name, flag): passed_config_dir = config_dir.strpath + ".new" if flag is None: args = ["--config-dir={}".format(config_dir.strpath)] elif flag.endswith("="): args = [flag + passed_config_dir] else: args = [flag, passed_config_dir] default_timeout = 10 config = {"conf_file": config_file, "id": "the-id"} expected = [sys.executable, cli_script_name, "--log-level=quiet"] + args proc = SaltDaemonFactory(start_timeout=1, cli_script_name=cli_script_name, config=config) cmdline = proc.build_cmdline(*args) assert cmdline == expected
def test_override_log_level(config_dir, config_file, cli_script_name, flag): config = {"conf_file": config_file, "id": "the-id"} if flag.endswith("="): args = [flag + "info"] else: args = [flag, "info"] expected = [ sys.executable, cli_script_name, "--config-dir={}".format(config_dir.strpath), ] + args proc = SaltDaemonFactory(start_timeout=1, cli_script_name=cli_script_name, config=config) cmdline = proc.build_cmdline(*args) assert cmdline == expected
def is_running(self): running = ContainerFactory.is_running(self) if running is False: return running if self.daemon_starting or self.daemon_started: return SaltDaemonFactory.is_running(self) return running
def test_salt_daemon_factory_id_attr_comes_first_in_repr(config_file): proc = SaltDaemonFactory( start_timeout=1, cli_script_name="foo-bar", config={"id": "TheID", "conf_file": config_file} ) regex = r"{}(id='TheID'".format(proc.__class__.__name__) assert repr(proc).startswith(regex) assert str(proc).startswith(regex)
def __attrs_post_init__(self): if self.python_executable is None: # Default to whatever is the default python in the container self.python_executable = "python" SaltDaemonFactory.__attrs_post_init__(self) ContainerFactory.__attrs_post_init__(self) # There are some volumes which NEED to exist on the container # so that configs are in the right place and also our custom # salt plugins root_dir = os.path.dirname(self.config["root_dir"]) volumes = { root_dir: {"bind": root_dir, "mode": "z"}, str(CODE_ROOT_DIR): {"bind": str(CODE_ROOT_DIR), "mode": "z"}, } if "volumes" not in self.container_run_kwargs: self.container_run_kwargs["volumes"] = {} self.container_run_kwargs["volumes"].update(volumes)
def __attrs_post_init__(self): self.daemon_started = self.daemon_starting = False if self.python_executable is None: # Default to whatever is the default python in the container self.python_executable = "python" SaltDaemonFactory.__attrs_post_init__(self) ContainerFactory.__attrs_post_init__(self) # There are some volumes which NEED to exist on the container so # that configs are in the right place and also our custom salt # plugins along with the custom scripts to start the daemons. root_dir = os.path.dirname(self.config["root_dir"]) config_dir = str(self.config_dir) scripts_dir = str(self.factories_manager.scripts_dir) volumes = { root_dir: { "bind": root_dir, "mode": "z" }, scripts_dir: { "bind": scripts_dir, "mode": "z" }, config_dir: { "bind": self.config_dir, "mode": "z" }, str(CODE_ROOT_DIR): { "bind": str(CODE_ROOT_DIR), "mode": "z" }, } if "volumes" not in self.container_run_kwargs: self.container_run_kwargs["volumes"] = {} self.container_run_kwargs["volumes"].update(volumes) self.container_run_kwargs.setdefault("hostname", self.name) self.container_run_kwargs.setdefault("auto_remove", True)
def start(self, *extra_cli_arguments, max_start_attempts=None, start_timeout=None): # Start the container ContainerFactory.start(self, max_start_attempts=max_start_attempts, start_timeout=start_timeout) self.daemon_starting = True # Now that the container is up, let's start the daemon self.daemon_started = SaltDaemonFactory.start( self, *extra_cli_arguments, max_start_attempts=max_start_attempts, start_timeout=start_timeout) return self.daemon_started
def terminate(self): self.daemon_started = self.daemon_starting = False ret = SaltDaemonFactory.terminate(self) ContainerFactory.terminate(self) return ret
def terminate(self): ret = SaltDaemonFactory.terminate(self) ContainerFactory.terminate(self) return ret
def start(self): # Start the container ContainerFactory.start(self) # Now that the container is up, let's start the daemon return SaltDaemonFactory.start(self)