Exemple #1
0
def test_syslog_file_not_found(salt_minion, salt_call_cli, tmp_path):
    """
    test when log_file is set to a syslog file that does not exist
    """
    config_dir = tmp_path / "log_file_incorrect"
    config_dir.mkdir()
    with change_cwd(str(config_dir)):
        minion_config = copy.deepcopy(salt_minion.config)
        minion_config["log_file"] = "file:///dev/doesnotexist"
        with salt.utils.files.fopen(str(config_dir / "minion"), "w") as fh_:
            fh_.write(
                salt.utils.yaml.dump(minion_config, default_flow_style=False))
        ret = salt_call_cli.run(
            "--config-dir={}".format(config_dir),
            "--log-level=debug",
            "cmd.run",
            "echo foo",
        )
        if sys.version_info >= (3, 5, 4):
            assert ret.exitcode == 0
            assert (
                "[WARNING ] The log_file does not exist. Logging not setup correctly or"
                " syslog service not started." in ret.stderr)
            assert ret.json == "foo", ret
        else:
            assert ret.exitcode == salt.defaults.exitcodes.EX_UNAVAILABLE
            assert "Failed to setup the Syslog logging handler" in ret.stderr
Exemple #2
0
    def test_syslog_file_not_found(self):
        """
        test when log_file is set to a syslog file that does not exist
        """
        config_dir = os.path.join(RUNTIME_VARS.TMP, "log_file_incorrect")
        if not os.path.isdir(config_dir):
            os.makedirs(config_dir)

        with change_cwd(config_dir):
            with salt.utils.files.fopen(self.get_config_file_path("minion"),
                                        "r") as fh_:
                minion_config = salt.utils.yaml.load(fh_.read())
                minion_config["log_file"] = "file:///dev/doesnotexist"
                with salt.utils.files.fopen(os.path.join(config_dir, "minion"),
                                            "w") as fh_:
                    fh_.write(
                        salt.utils.yaml.dump(minion_config,
                                             default_flow_style=False))
            ret = self.run_script(
                "salt-call",
                '--config-dir {0} cmd.run "echo foo"'.format(config_dir),
                timeout=120,
                catch_stderr=True,
                with_retcode=True,
            )
            try:
                if sys.version_info >= (3, 5, 4):
                    self.assertIn("local:", ret[0])
                    self.assertIn(
                        "[WARNING ] The log_file does not exist. Logging not setup correctly or syslog service not started.",
                        ret[1],
                    )
                    self.assertEqual(ret[2], 0)
                else:
                    self.assertIn("Failed to setup the Syslog logging handler",
                                  "\n".join(ret[1]))
                    self.assertEqual(ret[2], 2)
            finally:
                if os.path.isdir(config_dir):
                    shutil.rmtree(config_dir)
Exemple #3
0
    def setUp(self):
        super().setUp()
        self.repo = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
        self.addCleanup(shutil.rmtree, self.repo, ignore_errors=True)
        self.addCleanup(delattr, self, "repo")
        self.files = ("foo", "bar", "baz", "питон")
        self.addCleanup(delattr, self, "files")
        self.dirs = ("", "qux")
        self.addCleanup(delattr, self, "dirs")
        self.branches = ("master", "iamanewbranch")
        self.addCleanup(delattr, self, "branches")
        self.tags = ("git_testing",)
        self.addCleanup(delattr, self, "tags")
        for dirname in self.dirs:
            dir_path = pathlib.Path(self.repo) / dirname
            dir_path.mkdir(parents=True, exist_ok=True)
            for filename in self.files:
                with salt.utils.files.fopen(str(dir_path / filename), "wb") as fp_:
                    fp_.write(
                        "This is a test file named {}.".format(filename).encode("utf-8")
                    )
        # Navigate to the root of the repo to init, stage, and commit
        with change_cwd(self.repo):
            # Initialize a new git repository
            subprocess.check_call(["git", "init", "--quiet", self.repo])

            # Set user.name and user.email config attributes if not present
            for key, value in (
                ("user.name", "Jenkins"),
                ("user.email", "*****@*****.**"),
            ):
                # Check if key is missing
                keycheck = subprocess.Popen(
                    ["git", "config", "--get", "--global", key],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )
                if keycheck.wait() != 0:
                    # Set the key if it is not present
                    subprocess.check_call(["git", "config", "--global", key, value])

            subprocess.check_call(["git", "add", "."])
            subprocess.check_call(
                ["git", "commit", "--quiet", "--message", "Initial commit"]
            )
            # Add a tag
            subprocess.check_call(["git", "tag", "-a", self.tags[0], "-m", "Add tag"])
            # Checkout a second branch
            subprocess.check_call(
                ["git", "checkout", "--quiet", "-b", self.branches[1]]
            )
            # Add a line to the file
            with salt.utils.files.fopen(self.files[0], "a") as fp_:
                fp_.write(salt.utils.stringutils.to_str("Added a line\n"))
            # Commit the updated file
            subprocess.check_call(
                [
                    "git",
                    "commit",
                    "--quiet",
                    "--message",
                    "Added a line to " + self.files[0],
                    self.files[0],
                ]
            )
            # Switch back to master
            subprocess.check_call(["git", "checkout", "--quiet", "master"])