def test_show_lowstate(self): """ state.show_lowstate with salt-ssh """ top_sls = """ base: '*': - core """ core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format( RUNTIME_VARS.TMP ) with temp_file( "top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE ), temp_file("core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): low = self.run_function("state.show_lowstate") self.assertIsInstance(low, list) self.assertIsInstance(low[0], dict)
def test_state_show_top(self): """ test state.show_top with salt-ssh """ top_sls = """ base: '*': - core """ core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format( RUNTIME_VARS.TMP ) with temp_file( "top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE ), temp_file("core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): ret = self.run_function("state.show_top") self.assertEqual(ret, {"base": ["core", "master_tops_test"]})
def test_show_highstate(self): """ state.show_highstate with salt-ssh """ top_sls = """ base: '*': - core """ core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format( RUNTIME_VARS.TMP ) with temp_file( "top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE ), temp_file("core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): high = self.run_function("state.show_highstate") destpath = os.path.join(RUNTIME_VARS.TMP, "testfile") self.assertIsInstance(high, dict) self.assertIn(destpath, high) self.assertEqual(high[destpath]["__env__"], "base")
def test_list_states(self): """ cp.list_states """ top_sls = """ base: '*': - core """ core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format(RUNTIME_VARS.TMP) with temp_file("top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE), temp_file( "core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): ret = self.run_function("cp.list_states", ) self.assertIn("core", ret) self.assertIn("top", ret)
def test_docker_highstate(self): """ check that docker.highstate works, and works with a container not running as root """ top_sls = """ base: '*': - core """ core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format( RUNTIME_VARS.TMP ) with temp_file( "top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE ), temp_file("core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): ret = self.run_function("docker.apply", [self.random_name]) self.assertSaltTrueReturn(ret)
def test_temp_file_with_name(tmp_path, name): expected_path = tmp_path / name assert expected_path.is_file() is False with tempfiles.temp_file(name=name, directory=tmp_path) as tpath: assert tpath.is_file() assert str(tpath) == str(expected_path) assert expected_path.is_file() is False
def test_temp_file_without_name_no_directory(): try: expected_parent_path = pathlib.Path(tempfile.gettempdir()) with tempfiles.temp_file() as tpath: assert tpath.is_file() assert str(tpath.parent) == str(expected_parent_path) assert tpath.is_file() is False finally: shutil.rmtree(str(tpath), ignore_errors=True)
def test_output_highstate(self): """ Regression tests for the highstate outputter. Calls a basic state with various flags. Each comparison should be identical when successful. """ simple_ping_sls = """ simple-ping: module.run: - name: test.ping """ with temp_file("simple-ping.sls", simple_ping_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): # Test basic highstate output. No frills. expected = [ "minion:", " ID: simple-ping", " Function: module.run", " Name: test.ping", " Result: True", " Comment: Module function test.ping executed", " Changes: ", " ret:", " True", "Summary for minion", "Succeeded: 1 (changed=1)", "Failed: 0", "Total states run: 1", ] state_run = self.run_salt('"minion" state.sls simple-ping') for expected_item in expected: self.assertIn(expected_item, state_run) # Test highstate output while also passing --out=highstate. # This is a regression test for Issue #29796 state_run = self.run_salt( '"minion" state.sls simple-ping --out=highstate') for expected_item in expected: self.assertIn(expected_item, state_run) # Test highstate output when passing --static and running a state function. # See Issue #44556. state_run = self.run_salt( '"minion" state.sls simple-ping --static') for expected_item in expected: self.assertIn(expected_item, state_run) # Test highstate output when passing --static and --out=highstate. # See Issue #44556. state_run = self.run_salt( '"minion" state.sls simple-ping --static --out=highstate') for expected_item in expected: self.assertIn(expected_item, state_run)
def test_temp_file_does_not_delete_non_empty_directories(tmp_path): expected_parent_path = tmp_path level1_path = expected_parent_path / "level1" level2_path = level1_path / "level2" assert not level1_path.is_dir() assert not level2_path.is_dir() with tempfiles.temp_file("level1/foo.txt", directory=expected_parent_path) as tpath1: assert tpath1.is_file() assert level1_path.is_dir() assert not level2_path.is_dir() with tempfiles.temp_file("level1/level2/foo.txt", directory=expected_parent_path) as tpath2: assert tpath2.is_file() assert level1_path.is_dir() assert level2_path.is_dir() assert not tpath2.is_file() assert not level2_path.is_dir() assert tpath1.is_file() assert level1_path.is_dir() assert not level1_path.is_dir() assert not level2_path.is_dir()
def test_temp_file_contents(strip_first_newline): contents = """ These are the contents, first line Second line """ if strip_first_newline: expected_contents = "These are the contents, first line\n Second line\n" else: expected_contents = "\nThese are the contents, first line\n Second line\n" with tempfiles.temp_file(contents=contents, strip_first_newline=strip_first_newline) as tpath: assert tpath.is_file() assert tpath.read_text() == expected_contents
def test_state_highstate(self): top_sls = """ base: '*': - core """ core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format(RUNTIME_VARS.TMP) with temp_file("top.sls", top_sls, RUNTIME_VARS.TMP_BASEENV_STATE_TREE), temp_file( "core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): ret = self.run_function("state.highstate") for key, value in ret.items(): self.assertTrue(value["result"])
def test_state_apply(self): core_state = """ {}/testfile: file: - managed - source: salt://testfile - makedirs: true """.format(RUNTIME_VARS.TMP) with temp_file("core.sls", core_state, RUNTIME_VARS.TMP_BASEENV_STATE_TREE): ret = self.run_function("state.apply", ["core"]) for key, value in ret.items(): self.assertTrue(value["result"])
def test_managed_multiple_comps(self): state_file = """ ubuntu-backports: pkgrepo.managed: - name: 'deb http://fi.archive.ubuntu.com/ubuntu focal-backports' - comps: main, restricted, universe, multiverse - refresh: false - disabled: false - clean_file: true - file: /etc/apt/sources.list.d/99-salt-archive-ubuntu-focal-backports.list - require_in: - pkgrepo: canonical-ubuntu canonical-ubuntu: pkgrepo.managed: - name: 'deb http://archive.canonical.com/ubuntu {{ salt['grains.get']('oscodename') }}' - comps: partner - refresh: false - disabled: false - clean_file: true - file: /etc/apt/sources.list.d/99-salt-canonical-ubuntu.list """ def remove_apt_list_file(path): if os.path.exists(path): os.unlink(path) self.addCleanup( remove_apt_list_file, "/etc/apt/sources.list.d/99-salt-canonical-ubuntu.list", ) self.addCleanup( remove_apt_list_file, "/etc/apt/sources.list.d/99-salt-archive-ubuntu-focal-backports.list", ) with temp_file( "multiple-comps-repos.sls", state_file, RUNTIME_VARS.TMP_BASEENV_STATE_TREE ): ret = self.run_function("state.sls", ["multiple-comps-repos"]) for state_run in ret.values(): # On the first run, we must have changes assert state_run["changes"] ret = self.run_function("state.sls", ["multiple-comps-repos"]) for state_run in ret.values(): # On the second run though, we shouldn't have changes made assert not state_run["changes"]
def temp_pillar_file(name, contents, saltenv="base", strip_first_newline=True): """ This helper creates a temporary pillar file. It should be used as a context manager which returns the temporary pillar file path, and, once out of context, deletes it. Can be directly imported and used, or, it can be used as a pytest helper function if ``pytest-helpers-namespace`` is installed. .. code-block:: python import os import pytest def test_blah(): with pytest.helpers.temp_pillar_file("blah.sls") as tpath: print(tpath) assert os.path.exists(tpath) assert not os.path.exists(tpath) Depending on the saltenv, it will be created under ``RUNTIME_VARS.TMP_PILLAR_TREE`` or ``RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE``. Args: name(str): The temporary state file name contents(str): The contents of the temporary file saltenv(str): The salt env to use. Either ``base`` or ``prod`` strip_first_newline(bool): Wether to strip the initial first new line char or not. """ if saltenv == "base": directory = RUNTIME_VARS.TMP_BASEENV_PILLAR_TREE elif saltenv == "prod": directory = RUNTIME_VARS.TMP_PRODENV_PILLAR_TREE else: raise RuntimeError( '"saltenv" can only be "base" or "prod", not "{}"'.format(saltenv)) return temp_file(name, contents, directory=directory, strip_first_newline=strip_first_newline)
def test_backends_decode_body_true(self): """ test all backends when using decode_body=True that it returns string and decodes it. """ core_state = """ {}: file: - managed - source: salt://testfile - makedirs: true """.format(RUNTIME_VARS.TMP) with temp_file("core.sls", core_state, self.get_webserver.root): for backend in ["tornado", "requests", "urllib2"]: ret = http.query(self.get_webserver.url("core.sls"), backend=backend) body = ret.get("body", "") assert isinstance(body, str)
def test_temp_file_without_name(tmp_path): expected_parent_path = tmp_path with tempfiles.temp_file(directory=tmp_path) as tpath: assert tpath.is_file() assert str(tpath.parent) == str(expected_parent_path) assert tpath.is_file() is False