Ejemplo n.º 1
0
    def test_update_targets_dns(self):
        """
        test update_targets when host is dns
        """
        host = "localhost"
        user = "******"
        opts = self.opts
        opts["tgt"] = user + host

        with patch("salt.utils.network.is_reachable_host",
                   MagicMock(return_value=False)):
            client = ssh.SSH(opts)
        assert opts["tgt"] == user + host
        client._update_targets()
        assert opts["tgt"] == host
        assert client.targets[host]["user"] == user.split("@")[0]
Ejemplo n.º 2
0
    def test_parse_tgt_no_user(self):
        """
        test parse_tgt when only the host set on
        the ssh cli tgt
        """
        host = "localhost"
        opts = self.opts
        opts["ssh_user"] = "******"
        opts["tgt"] = host

        with patch("salt.utils.network.is_reachable_host",
                   MagicMock(return_value=False)):
            assert not self.opts.get("ssh_cli_tgt")
            client = ssh.SSH(opts)
            assert client.parse_tgt["hostname"] == host
            assert client.parse_tgt["user"] == opts["ssh_user"]
            assert self.opts.get("ssh_cli_tgt") == host
Ejemplo n.º 3
0
    def test_parse_tgt(self):
        """
        test parse_tgt when user and host set on
        the ssh cli tgt
        """
        host = "localhost"
        user = "******"
        opts = self.opts
        opts["tgt"] = user + host

        with patch("salt.utils.network.is_reachable_host",
                   MagicMock(return_value=False)):
            assert not self.opts.get("ssh_cli_tgt")
            client = ssh.SSH(opts)
            assert client.parse_tgt["hostname"] == host
            assert client.parse_tgt["user"] == user.split("@")[0]
            assert self.opts.get("ssh_cli_tgt") == user + host
Ejemplo n.º 4
0
    def test_expand_target_no_user(self):
        """
        test expand_target when no user defined
        """
        host = "127.0.0.1"
        opts = self.opts
        opts["tgt"] = host

        with patch("salt.utils.network.is_reachable_host",
                   MagicMock(return_value=False)):
            client = ssh.SSH(opts)
        assert opts["tgt"] == host

        with patch(
                "salt.roster.get_roster_file",
                MagicMock(return_value="/etc/salt/roster")), patch(
                    "salt.client.ssh.compile_template",
                    MagicMock(
                        return_value=salt.utils.yaml.safe_load(self.roster)),
                ):
            client._expand_target()
        assert opts["tgt"] == host
Ejemplo n.º 5
0
    def test_expand_target_dns(self):
        """
        test expand_target when target is root@<dns>
        """
        host = "localhost"
        user = "******"
        opts = self.opts
        opts["tgt"] = user + host

        with patch("salt.utils.network.is_reachable_host",
                   MagicMock(return_value=False)):
            client = ssh.SSH(opts)
        assert opts["tgt"] == user + host
        with patch(
                "salt.roster.get_roster_file",
                MagicMock(return_value="/etc/salt/roster")), patch(
                    "salt.client.ssh.compile_template",
                    MagicMock(
                        return_value=salt.utils.yaml.safe_load(self.roster)),
                ):
            client._expand_target()
        assert opts["tgt"] == host
Ejemplo n.º 6
0
 def test_password_failure(self):
     """
     Check password failures when trying to deploy keys
     """
     opts = salt.config.client_config(self.get_config_file_path("master"))
     opts["list_hosts"] = False
     opts["argv"] = ["test.ping"]
     opts["selected_target_option"] = "glob"
     opts["tgt"] = "localhost"
     opts["arg"] = []
     roster = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "roster")
     handle_ssh_ret = [
         {
             "localhost": {
                 "retcode": 255,
                 "stderr": "Permission denied (publickey).\r\n",
                 "stdout": "",
             }
         },
     ]
     expected = {"localhost": "Permission denied (publickey)"}
     display_output = MagicMock()
     with patch(
         "salt.roster.get_roster_file", MagicMock(return_value=roster)
     ), patch(
         "salt.client.ssh.SSH.handle_ssh", MagicMock(return_value=handle_ssh_ret)
     ), patch(
         "salt.client.ssh.SSH.key_deploy", MagicMock(return_value=expected)
     ), patch(
         "salt.output.display_output", display_output
     ):
         client = ssh.SSH(opts)
         ret = next(client.run_iter())
         with self.assertRaises(SystemExit):
             client.run()
     display_output.assert_called_once_with(expected, "nested", opts)
     self.assertIs(ret, handle_ssh_ret[0])
Ejemplo n.º 7
0
    def test_expand_target_no_host(self):
        """
        test expand_target when host is not included in the rosterdata
        """
        host = "127.0.0.1"
        user = "******"
        opts = self.opts
        opts["tgt"] = user + host

        roster = """
            localhost: 127.0.0.1
            """
        roster_file = os.path.join(RUNTIME_VARS.TMP, "test_roster_no_host")
        with salt.utils.files.fopen(roster_file, "w") as fp:
            salt.utils.yaml.safe_dump(salt.utils.yaml.safe_load(roster), fp)

        with patch(
            "salt.utils.network.is_reachable_host", MagicMock(return_value=False)
        ):
            client = ssh.SSH(opts)
        assert opts["tgt"] == user + host
        with patch("salt.roster.get_roster_file", MagicMock(return_value=roster_file)):
            client._expand_target()
        assert opts["tgt"] == host