def assess_update_mongo(client, series, bootstrap_host):
    log.info('series={}, bootstrap_host={}'.format(series, bootstrap_host))
    return_code = 1
    charm = local_charm_path(charm='ubuntu',
                             juju_ver=client.version,
                             series=series)
    log.info("Setting up test.")
    client.deploy(charm, series=series)
    client.wait_for_started()
    log.info("Setup complete.")
    log.info("Test started.")
    # Instrument the case where Juju can install the new mongo packages from
    # Ubuntu.
    remote = remote_from_address(bootstrap_host, series=series)
    remote.run(DEP_SCRIPT)
    # upgrade-mongo returns 0 if all is well. status will work but not
    # explicitly show that mongo3 is running.
    client.upgrade_mongo()
    client.show_status()
    log.info("Checking bootstrap host for mongo3:")
    mongo_proc = remote.run(VERIFY_SCRIPT)
    log.info(mongo_proc)
    if '--port 37017' in mongo_proc and '--replSet juju' in mongo_proc:
        return_code = 0
    log.info("Controller upgraded to MongoDB 3.")
    log.info("Test complete.")
    return return_code
Exemple #2
0
def assess_update_mongo(client, series, bootstrap_host):
    log.info('series={}, bootstrap_host={}'.format(series, bootstrap_host))
    return_code = 1
    charm = local_charm_path(
        charm='ubuntu', juju_ver=client.version, series=series)
    log.info("Setting up test.")
    client.deploy(charm, series=series)
    client.wait_for_started()
    log.info("Setup complete.")
    log.info("Test started.")
    # Instrument the case where Juju can install the new mongo packages from
    # Ubuntu.
    remote = remote_from_address(bootstrap_host, series=series)
    remote.run(DEP_SCRIPT)
    # upgrade-mongo returns 0 if all is well. status will work but not
    # explicitly show that mongo3 is running.
    client.upgrade_mongo()
    client.show_status()
    log.info("Checking bootstrap host for mongo3:")
    mongo_proc = remote.run(VERIFY_SCRIPT)
    log.info(mongo_proc)
    if '--port 37017' in mongo_proc and '--replSet juju' in mongo_proc:
        return_code = 0
    log.info("Controller upgraded to MongoDB 3.")
    log.info("Test complete.")
    return return_code
 def test_run_subprocess_timeout(self):
     remote = remote_from_address("10.55.60.1")
     remote.timeout = 63
     with patch("subprocess.check_output", autospec=True) as mock_co:
         remote.cat("/a/file")
     mock_co.assert_called_once_with(
         (
             sys.executable,
             get_timeout_path(),
             "63.00",
             "--",
             "ssh",
             "-o",
             "User ubuntu",
             "-o",
             "UserKnownHostsFile /dev/null",
             "-o",
             "StrictHostKeyChecking no",
             "-o",
             "PasswordAuthentication no",
             "10.55.60.1",
             "cat",
             "/a/file",
         ),
         stdin=subprocess.PIPE,
     )
    def handle_bootstrap_exceptions(self):
        """If an exception is raised during bootstrap, handle it.

        Log the exception, re-raise as a LoggedException.
        Copy logs for the bootstrap host
        Tear down.  (self.keep_env is ignored.)
        """
        try:
            # If an exception is raised that indicates an error, log it
            # before tearing down so that the error is closely tied to
            # the failed operation.
            with logged_exception(logging):
                yield
        except:
            # If run from a windows machine may not have ssh to get
            # logs
            with self.client.ignore_soft_deadline():
                with self.tear_down_client.ignore_soft_deadline():
                    if self.bootstrap_host is not None and _can_run_ssh():
                        remote = remote_from_address(self.bootstrap_host,
                                                     series=self.series)
                        copy_remote_logs(remote, self.log_dir)
                        archive_logs(self.log_dir)
                    self.controller_strategy.prepare()
            raise
Exemple #5
0
def get_remote_for_controller(admin_client):
    """Get a remote client to the controller machine of `admin_client`.

    :return: remote.SSHRemote object for the controller machine.
    """
    status = admin_client.get_status()
    controller_ip = status.get_machine_dns_name('0')
    return remote_from_address(controller_ip)
def get_remote_for_controller(admin_client):
    """Get a remote client to the controller machine of `admin_client`.

    :return: remote.SSHRemote object for the controller machine.
    """
    status = admin_client.get_status()
    controller_ip = status.get_machine_dns_name('0')
    return remote_from_address(controller_ip)
def iter_remote_machines(client):
    try:
        status = client.get_status()
    except Exception as err:
        logging.warning("Failed to retrieve status for dumping logs: %s", err)
        return

    for machine_id, machine in status.iter_machines():
        hostname = machine.get('dns-name')
        if hostname:
            remote = remote_from_address(hostname, machine.get('series'))
            yield machine_id, remote
Exemple #8
0
 def test_cat(self):
     remote = remote_from_address("10.55.60.1")
     with patch.object(remote, "_run_subprocess") as mock_run:
         remote.cat("/a/file")
     mock_run.assert_called_once_with([
         "ssh",
         "-o", "User ubuntu",
         "-o", "UserKnownHostsFile /dev/null",
         "-o", "StrictHostKeyChecking no",
         "-o", "PasswordAuthentication no",
         "10.55.60.1",
         "cat", "/a/file",
     ])
Exemple #9
0
 def test_run_with_address(self):
     remote = remote_from_address("10.55.60.1")
     with patch.object(remote, "_run_subprocess") as mock_run:
         mock_run.return_value = "contents of /a/file"
         output = remote.run("cat /a/file")
         self.assertEqual(output, "contents of /a/file")
     mock_run.assert_called_once_with([
         "ssh",
         "-o", "User ubuntu",
         "-o", "UserKnownHostsFile /dev/null",
         "-o", "StrictHostKeyChecking no",
         "-o", "PasswordAuthentication no",
         "10.55.60.1",
         "cat /a/file",
     ])
def get_remote_machines(client, known_hosts):
    """Return a dict of machine_id to remote machines.

    A bootstrap_host address may be provided as a fallback for machine 0 if
    status fails. For some providers such as MAAS the dns-name will be
    resolved to a real ip address using the substrate api.
    """
    # Try to get machine details from environment if possible.
    machines = dict(iter_remote_machines(client))
    # The bootstrap host is added as a fallback in case status failed.
    for machine_id, address in known_hosts.items():
        if machine_id not in machines:
            machines[machine_id] = remote_from_address(address)
    # Update remote machines in place with real addresses if substrate needs.
    resolve_remote_dns_names(client.env, machines.values())
    return machines
Exemple #11
0
 def test_copy(self):
     remote = remote_from_address("10.55.60.1")
     dest = "/local/path"
     with patch.object(remote, "_run_subprocess") as mock_run:
         remote.copy(dest, ["/var/log/*", "~/.config"])
     mock_run.assert_called_once_with([
         "scp",
         "-rC",
         "-o", "User ubuntu",
         "-o", "UserKnownHostsFile /dev/null",
         "-o", "StrictHostKeyChecking no",
         "-o", "PasswordAuthentication no",
         "10.55.60.1:/var/log/*",
         "10.55.60.1:~/.config",
         "/local/path",
     ])
 def test_cat(self):
     remote = remote_from_address("10.55.60.1")
     with patch.object(remote, "_run_subprocess") as mock_run:
         remote.cat("/a/file")
     mock_run.assert_called_once_with([
         "ssh",
         "-o",
         "User ubuntu",
         "-o",
         "UserKnownHostsFile /dev/null",
         "-o",
         "StrictHostKeyChecking no",
         "-o",
         "PasswordAuthentication no",
         "10.55.60.1",
         "cat",
         "/a/file",
     ])
 def test_run_with_address(self):
     remote = remote_from_address("10.55.60.1")
     with patch.object(remote, "_run_subprocess") as mock_run:
         mock_run.return_value = "contents of /a/file"
         output = remote.run("cat /a/file")
         self.assertEqual(output, "contents of /a/file")
     mock_run.assert_called_once_with([
         "ssh",
         "-o",
         "User ubuntu",
         "-o",
         "UserKnownHostsFile /dev/null",
         "-o",
         "StrictHostKeyChecking no",
         "-o",
         "PasswordAuthentication no",
         "10.55.60.1",
         "cat /a/file",
     ])
Exemple #14
0
 def test_run_subprocess_timeout(self):
     remote = remote_from_address("10.55.60.1")
     remote.timeout = 63
     with patch("subprocess.check_output", autospec=True) as mock_co:
         remote.cat("/a/file")
     mock_co.assert_called_once_with((
         sys.executable,
         get_timeout_path(),
         "63.00",
         "--",
         "ssh",
         "-o", "User ubuntu",
         "-o", "UserKnownHostsFile /dev/null",
         "-o", "StrictHostKeyChecking no",
         "-o", "PasswordAuthentication no",
         "10.55.60.1",
         "cat", "/a/file",
         ),
         stdin=subprocess.PIPE,
     )
 def test_copy(self):
     remote = remote_from_address("10.55.60.1")
     dest = "/local/path"
     with patch.object(remote, "_run_subprocess") as mock_run:
         remote.copy(dest, ["/var/log/*", "~/.config"])
     mock_run.assert_called_once_with([
         "scp",
         "-rC",
         "-o",
         "User ubuntu",
         "-o",
         "UserKnownHostsFile /dev/null",
         "-o",
         "StrictHostKeyChecking no",
         "-o",
         "PasswordAuthentication no",
         "10.55.60.1:/var/log/*",
         "10.55.60.1:~/.config",
         "/local/path",
     ])
Exemple #16
0
def deploy_machine_and_verify(client, series="xenial"):
    """Deploy machine using juju add-machine of specified series
    and verify that it make use of same agent-file used by the
    controller.

    :param client: Juju client
    :param series: The charm series to deploy
    """
    old_status = client.get_status()
    client.juju('add-machine', ('--series', series))
    new_status = client.wait_for_started()

    # This will iterate only once because we just added only one
    # machine after old_status to new_status.
    for unit, machine in new_status.iter_new_machines(old_status):
        hostname = machine.get('dns-name')
        if hostname:
            remote = remote_from_address(hostname, machine.get('series'))
            verify_deployed_charm(client, remote, unit)
        else:
            raise JujuAssertionError(
                'Unable to get information about added machine')

    log.info("add-machine verification done successfully")
Exemple #17
0
 def test_remote_from_address(self):
     remote = remote_from_address("10.55.60.1")
     self.assertEqual(repr(remote), "<SSHRemote addr='10.55.60.1'>")
     self.assertIs(None, remote.is_windows())
Exemple #18
0
 def test_remote_from_address_and_series(self):
     remote = remote_from_address("10.55.60.2", series="trusty")
     self.assertEqual(repr(remote), "<SSHRemote addr='10.55.60.2'>")
     self.assertIs(False, remote.is_windows())
Exemple #19
0
 def test_remote_from_address_and_win_series(self):
     remote = remote_from_address("10.55.60.3", series="win2012hvr2")
     self.assertEqual(repr(remote), "<WinRmRemote addr='10.55.60.3'>")
     self.assertIs(True, remote.is_windows())
 def test_remote_from_address_and_win_series(self):
     remote = remote_from_address("10.55.60.3", series="win2012hvr2")
     self.assertEqual(repr(remote), "<WinRmRemote addr='10.55.60.3'>")
     self.assertIs(True, remote.is_windows())
 def test_remote_from_address_and_series(self):
     remote = remote_from_address("10.55.60.2", series="trusty")
     self.assertEqual(repr(remote), "<SSHRemote addr='10.55.60.2'>")
     self.assertIs(False, remote.is_windows())
 def test_remote_from_address(self):
     remote = remote_from_address("10.55.60.1")
     self.assertEqual(repr(remote), "<SSHRemote addr='10.55.60.1'>")
     self.assertIs(None, remote.is_windows())