def get_test_versions(): test_versions = [] name = "mysql/mysql-server" for version in ("5.5", "5.6", "5.7", "8.0"): test_versions.append( MySQLImage( name=name, tag=version, container_id=random_string("mysql-{}-".format(version)), )) name = "mariadb" for version in ("10.1", "10.2", "10.3", "10.4", "10.5"): test_versions.append( MySQLImage( name=name, tag=version, container_id=random_string("mariadb-{}-".format(version)), )) name = "percona" for version in ("5.5", "5.6", "5.7", "8.0"): test_versions.append( MySQLImage( name=name, tag=version, container_id=random_string("percona-{}-".format(version)), )) return test_versions
def setup_teardown_vars(salt_call_cli): user_name = random_string("RS-", lowercase=False) group_name = random_string("RS-", lowercase=False) try: yield user_name, group_name finally: salt_call_cli.run("user.delete", user_name, True, True) salt_call_cli.run("group.delete", group_name) salt_call_cli.run("lgpo.set", "computer_policy={'Minimum Password Length': 0}")
def test_pytest_config(salt_factories, configure_kwargs): master_id = random_string("master-") master = salt_factories.salt_master_daemon(master_id) config = master.salt_proxy_minion_daemon(random_string("the-id-"), **configure_kwargs).config config_key = "pytest-minion" assert config_key in config assert "log" in config[config_key] for key in ("host", "level", "port", "prefix"): assert key in config[config_key]["log"] assert "master-id" in config[config_key] assert config[config_key]["master-id"] == master_id
def setUp(self): """ Get current settings """ self._password = self.run_function("shadow.gen_password", ["Password1234"]) if "ERROR" in self._password: self.fail("Failed to generate password: {}".format(self._password)) super().setUp() self._no_user = random_string("tu-", uppercase=False) self._test_user = random_string("tu-", uppercase=False) self._password = salt.modules.linux_shadow.gen_password("Password1234")
def salt_delta_proxy_factory(salt_factories, salt_master_factory): proxy_minion_id = random_string("proxytest-") root_dir = salt_factories.get_root_dir_for_daemon(proxy_minion_id) conf_dir = root_dir / "conf" conf_dir.mkdir(parents=True, exist_ok=True) config_defaults = { "root_dir": str(root_dir), "hosts.file": os.path.join(RUNTIME_VARS.TMP, "hosts"), # Do we really need this for these tests? "aliases.file": os.path.join(RUNTIME_VARS.TMP, "aliases"), # Do we really need this for these tests? "transport": salt_master_factory.config["transport"], "user": salt_master_factory.config["user"], "metaproxy": "deltaproxy", "master": "127.0.0.1", } factory = salt_master_factory.salt_proxy_minion_daemon( proxy_minion_id, defaults=config_defaults, extra_cli_arguments_after_first_start_failure=["--log-level=debug"], start_timeout=240, ) factory.after_terminate(pytest.helpers.remove_stale_minion_key, salt_master_factory, factory.id) return factory
def test_keyword_nested_overrides_override_defaults(salt_factories): minion_config = salt_factories.get_salt_minion_daemon( random_string("minion-"), config_defaults={ "zzzz": False, "user": "******", "colors": { "black": True, "white": False }, }, config_overrides={ "colors": { "white": True, "grey": False } }, ).config assert "zzzz" in minion_config assert minion_config["zzzz"] is False assert minion_config["colors"] == { "black": True, "white": True, "grey": False }
def __init__(self, docker_module, **kwargs): self.docker_module = docker_module self.network = Network(random_string("salt_net_"), **kwargs) if self.network.net is not None: if "enable_ipv6" not in kwargs: kwargs["enable_ipv6"] = self.network.net.version == 6 self.kwargs = kwargs
def setUp(self): """ Get current settings """ super().setUp() self._user = random_string("tg-", uppercase=False) self._user1 = random_string("tg-", uppercase=False) self._no_user = random_string("tg-", uppercase=False) self._group = random_string("tg-", uppercase=False) self._no_group = random_string("tg-", uppercase=False) _gid = _new_gid = None if not salt.utils.platform.is_windows(): _gid = 64989 _new_gid = 64998 self._gid = _gid self._new_gid = _new_gid
def _default_container_id(self): return random_string( "{}-{}-".format( self.mysql_name.replace("/", "-"), self.mysql_version, ) )
def salt_sub_minion_factory(salt_master_factory): with salt.utils.files.fopen( os.path.join(RUNTIME_VARS.CONF_DIR, "sub_minion")) as rfh: config_defaults = yaml.deserialize(rfh.read()) config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts") config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases") config_defaults["transport"] = salt_master_factory.config["transport"] config_overrides = { "file_roots": salt_master_factory.config["file_roots"].copy(), "pillar_roots": salt_master_factory.config["pillar_roots"].copy(), } virtualenv_binary = get_virtualenv_binary_path() if virtualenv_binary: config_overrides["venv_bin"] = virtualenv_binary factory = salt_master_factory.get_salt_minion_daemon( random_string("sub-minion-"), config_defaults=config_defaults, config_overrides=config_overrides, extra_cli_arguments_after_first_start_failure=["--log-level=debug"], ) factory.register_after_terminate_callback( pytest.helpers.remove_stale_minion_key, salt_master_factory, factory.id) return factory
def salt_proxy_factory(salt_factories, salt_master_factory): proxy_minion_id = random_string("proxytest-") root_dir = salt_factories.get_root_dir_for_daemon(proxy_minion_id) conf_dir = root_dir / "conf" conf_dir.mkdir(parents=True, exist_ok=True) RUNTIME_VARS.TMP_PROXY_CONF_DIR = str(conf_dir) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.CONF_DIR, "proxy")) as rfh: config_defaults = yaml.deserialize(rfh.read()) config_defaults["root_dir"] = str(root_dir) config_defaults["hosts.file"] = os.path.join(RUNTIME_VARS.TMP, "hosts") config_defaults["aliases.file"] = os.path.join(RUNTIME_VARS.TMP, "aliases") config_defaults["transport"] = salt_master_factory.config["transport"] config_defaults["user"] = salt_master_factory.config["user"] factory = salt_master_factory.get_salt_proxy_minion_daemon( proxy_minion_id, config_defaults=config_defaults, extra_cli_arguments_after_first_start_failure=["--log-level=debug"], start_timeout=240, ) factory.register_after_terminate_callback( pytest.helpers.remove_stale_minion_key, salt_master_factory, factory.id) return factory
def test_send(event_listener, salt_master, salt_minion, salt_call_cli): """ Test sending an event to the master event bus """ event_tag = random_string("salt/test/event/") data = {"event.fire": "just test it!!!!"} start_time = time.time() ret = salt_call_cli.run( "event.send", event_tag, data=data, with_grains=True, with_pillar=True, preload={"foo": "bar"}, ) assert ret.exitcode == 0 assert ret.json assert ret.json is True event_pattern = (salt_master.id, event_tag) matched_events = event_listener.wait_for_events( [event_pattern], after_time=start_time, timeout=30 ) assert matched_events.found_all_events for event in matched_events: assert event.data["id"] == salt_minion.id assert event.data["cmd"] == "_minion_event" assert "event.fire" in event.data["data"] assert event.data["foo"] == "bar" assert event.data["data"]["grains"]["test_grain"] == "cheese" assert event.data["data"]["pillar"]["ext_spam"] == "eggs"
def test_provide_root_dir(testdir, salt_factories): root_dir = testdir.mkdir("custom-root") config_defaults = {"root_dir": root_dir} proxy_minion_config = salt_factories.get_salt_proxy_minion_daemon( random_string("proxy_minion-"), config_defaults=config_defaults ).config assert proxy_minion_config["root_dir"] == root_dir
def test_provide_root_dir(testdir, mom): root_dir = testdir.mkdir("custom-root") config_defaults = {"root_dir": root_dir} syndic_id = random_string("syndic-") syndic = mom.get_salt_syndic_daemon(syndic_id, config_defaults=config_defaults) assert syndic.config["root_dir"] == root_dir
def consul_container(salt_factories, docker_client, consul_port, docker_consul_image): container = salt_factories.get_container( random_string("consul-server-"), image_name=docker_consul_image, docker_client=docker_client, check_ports=[consul_port], container_run_kwargs={"ports": { "8500/tcp": consul_port }}, ) with container.started() as factory: # TODO: May want to do the same thing for redis to ensure that service is up & running # TODO: THIS IS HORRIBLE. THERE ARE BETTER WAYS TO DETECT SERVICE IS UP -W. Werner, 2021-10-12 timer = Timer(timeout=10) sleeptime = 0.1 while not timer.expired: try: with socket.create_connection(("localhost", consul_port), timeout=1) as cli: cli.send(b"GET /v1/kv/fnord HTTP/1.1\n\n") cli.recv(2048) break except ConnectionResetError as e: if e.errno == 104: time.sleep(sleeptime) sleeptime += sleeptime else: assert False, "Timer expired before connecting to consul" yield factory
def test_provide_root_dir(pytester, salt_factories): root_dir = str(pytester.mkdir("custom-root")) defaults = {"root_dir": root_dir} master_config = salt_factories.salt_master_daemon( random_string("master-"), defaults=defaults ).config assert master_config["root_dir"] == root_dir
def test_interface_defaults(salt_factories): interface = "172.17.0.1" master_config = salt_factories.salt_master_daemon( random_string("master-"), defaults={"interface": interface} ).config assert master_config["interface"] != interface assert master_config["interface"] == "127.0.0.1"
def test_interface_overrides(salt_factories): interface = "172.17.0.1" master_config = salt_factories.salt_master_daemon( random_string("master-"), overrides={"interface": interface} ).config assert master_config["interface"] != "127.0.0.1" assert master_config["interface"] == interface
def __attrs_post_init__(self): if self.sminion is None: self.sminion = create_sminion() if self.username is None: self.username = random_string("account-", uppercase=False) if self.password is None: self.password = random_string("pwd-", size=8) if (self.hashed_password is None and not salt.utils.platform.is_darwin() and not salt.utils.platform.is_windows()): self.hashed_password = salt.utils.pycrypto.gen_hash( password=self.password) if self.create_group is True and self.group_name is None: self.group_name = "group-{}".format(self.username) if self.group_name is not None: self._group = TestGroup(sminion=self.sminion, name=self.group_name)
def test_keyword_nested_overrides_override_defaults(mom): defaults = { "zzzz": False, "user": "******", "colors": { "black": True, "white": False } } overrides = {"zzzz": True, "colors": {"white": True, "grey": False}} expected_colors = {"black": True, "white": True, "grey": False} syndic_id = random_string("syndic-") syndic = mom.get_salt_syndic_daemon( syndic_id, config_defaults=defaults, master_config_defaults=defaults.copy(), minion_config_defaults=defaults.copy(), config_overrides=overrides, master_config_overrides=overrides.copy(), minion_config_overrides=overrides.copy(), ) assert "zzzz" in syndic.config assert syndic.config["zzzz"] is True assert syndic.config["colors"] == expected_colors assert "zzzz" in syndic.master.config assert syndic.master.config["zzzz"] is True assert syndic.master.config["colors"] == expected_colors assert "zzzz" in syndic.minion.config assert syndic.minion.config["zzzz"] is True assert syndic.minion.config["colors"] == expected_colors
def logging_master(salt_factories): log_format = "|%(name)-17s:%(lineno)-4d|%(levelname)-8s|%(processName)s|PID:%(process)d|%(message)s" config_overrides = { "log_level": "debug", "log_fmt_console": log_format, "log_level_logfile": "debug", "log_fmt_logfile": log_format, } factory = salt_factories.salt_master_daemon( random_string("master-logging-"), overrides=config_overrides, extra_cli_arguments_after_first_start_failure=["--log-level=debug"], ) process_pid = None with factory.started("--log-level=debug"): process_pid = factory.pid # Wait a little after the master starts if not salt.utils.platform.spawning_platform(): time.sleep(2) else: time.sleep(10) ret = factory.terminate() return SimpleNamespace( process_pid=process_pid, ret=ret, log_file=pathlib.Path(factory.config["log_file"]), )
def get_temp_config(config_for, **config_overrides): rootdir = config_overrides.get( "root_dir", tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) ) if not os.path.exists(rootdir): os.makedirs(rootdir) conf_dir = config_overrides.pop("conf_dir", os.path.join(rootdir, "conf")) for key in ("cachedir", "pki_dir", "sock_dir"): if key not in config_overrides: config_overrides[key] = key if "log_file" not in config_overrides: config_overrides["log_file"] = "logs/{}.log".format(config_for) if "user" not in config_overrides: config_overrides["user"] = RUNTIME_VARS.RUNNING_TESTS_USER config_overrides["root_dir"] = rootdir cdict = AdaptedConfigurationTestCaseMixin.get_config( config_for, from_scratch=True ) if config_for in ("master", "client_config"): rdict = salt.config.apply_master_config(config_overrides, cdict) if config_for == "minion": minion_id = ( config_overrides.get("id") or config_overrides.get("minion_id") or cdict.get("id") or cdict.get("minion_id") or random_string("temp-minion-") ) config_overrides["minion_id"] = config_overrides["id"] = minion_id rdict = salt.config.apply_minion_config( config_overrides, cdict, cache_minion_id=False, minion_id=minion_id ) verify_env( [ os.path.join(rdict["pki_dir"], "minions"), os.path.join(rdict["pki_dir"], "minions_pre"), os.path.join(rdict["pki_dir"], "minions_rejected"), os.path.join(rdict["pki_dir"], "minions_denied"), os.path.join(rdict["cachedir"], "jobs"), os.path.join(rdict["cachedir"], "tokens"), os.path.join(rdict["root_dir"], "cache", "tokens"), os.path.join(rdict["pki_dir"], "accepted"), os.path.join(rdict["pki_dir"], "rejected"), os.path.join(rdict["pki_dir"], "pending"), os.path.dirname(rdict["log_file"]), rdict["sock_dir"], conf_dir, ], RUNTIME_VARS.RUNNING_TESTS_USER, root_dir=rdict["root_dir"], ) rdict["conf_file"] = os.path.join(conf_dir, config_for) with salt.utils.files.fopen(rdict["conf_file"], "w") as wfh: salt.utils.yaml.safe_dump(rdict, wfh, default_flow_style=False) return rdict
def proxy_minion_id(salt_factories, salt_master): _proxy_minion_id = random_string("proxy-minion-") try: yield _proxy_minion_id finally: # Remove stale key if it exists pytest.helpers.remove_stale_minion_key(salt_master, _proxy_minion_id)
def test_pytest_config(salt_factories, configure_kwargs): master_id = random_string("master-") config = salt_factories.salt_master_daemon(master_id, **configure_kwargs).config config_key = "pytest-master" assert config_key in config assert "log" in config[config_key] for key in ("host", "level", "port", "prefix"): assert key in config[config_key]["log"]
def test_keyword_simple_overrides_override_defaults(salt_factories): proxy_minion_config = salt_factories.get_salt_proxy_minion_daemon( random_string("proxy-minion-"), config_defaults={"zzzz": False}, config_overrides={"zzzz": True}, ).config assert "zzzz" in proxy_minion_config assert proxy_minion_config["zzzz"] is True
def test_missing_api_config(salt_factories): master = salt_factories.get_salt_master_daemon(random_string("master-")) with pytest.raises(RuntimeError) as exc: master.get_salt_api_daemon() assert str(exc.value) == ( "The salt-master configuration for this salt-api instance does not seem to have " "any api properly configured.")
def set_container_name_before_start(container): """ This is useful if the container has to be restared and the old container, under the same name was left running, but in a bad shape. """ container.name = random_string("{}-".format(container.name.rsplit("-", 1)[0])) container.display_name = None return container
def test_interface_config_overrides(salt_factories): interface = "172.17.0.1" minion_config = salt_factories.get_salt_minion_daemon( random_string("minion-"), config_overrides={ "interface": interface }).config assert minion_config["interface"] == interface assert minion_config["interface"] != "127.0.0.1"
def pillar_file_path(salt_master): pillar_dir = salt_master.pillar_tree.base.write_path testfile = pillar_dir / random_string("foo") try: yield testfile finally: if testfile.exists(): testfile.unlink()
def test_master_config_overrides(salt_factories): master = "172.17.0.1" minion_config = salt_factories.get_salt_minion_daemon( random_string("minion-"), config_overrides={ "master": master }).config assert minion_config["master"] == master assert minion_config["master"] != "127.0.0.1"