def ensure_migration_with_resources_succeeds(source_client, dest_client):
    """Test simple migration of a model to another controller.

    Ensure that migration a model that has an application, that uses resources,
    deployed upon it is able to continue it's operation after the migration
    process. This includes assertion that the resources are migrated correctly
    too.

    Given 2 bootstrapped environments:
      - Deploy an application with a resource
        - ensure it's operating as expected
      - Migrate that model to the other environment
        - Ensure it's operating as expected
        - Add a new unit to the application to ensure the model is functional

    :return: Tuple containing migrated client object and the resource string
      that the charm deployed to it outputs.

    """
    resource_contents = get_random_string()
    test_model, application = deploy_simple_server_to_new_model(
        source_client, 'example-model-resource', resource_contents)
    migration_target_client = migrate_model_to_controller(
        test_model, dest_client)
    assert_model_migrated_successfully(migration_target_client, application,
                                       resource_contents)

    log.info('SUCCESS: resources migrated')
    return migration_target_client, application, resource_contents
Esempio n. 2
0
def assess_network_traffic(client, targets):
    """Test that all containers in target can talk to target[0]
    :param client: Juju client
    :param targets: machine IDs of machines to test
    :return: None;
    """
    status = client.wait_for_started().status
    log.info('Assessing network traffic.')
    source = targets[0]
    dests = targets[1:]

    with tempfile.NamedTemporaryFile(delete=False) as f:
        f.write('tmux new-session -d -s test "nc -l 6778 > nc_listen.out"')
    client.juju('scp', ('--proxy', f.name, source + ':/home/ubuntu/listen.sh'))
    os.remove(f.name)

    # Containers are named 'x/type/y' where x is the host of the container. We
    host = source.split('/')[0]
    address = status['machines'][host]['containers'][source]['dns-name']

    for dest in dests:
        log.info('Assessing network traffic for {}.'.format(dest))
        msg = get_random_string()
        ssh(client, source, 'rm nc_listen.out; bash ./listen.sh')
        ssh(client, dest,
            'echo "{msg}" | nc {addr} 6778'.format(msg=msg, addr=address))
        result = ssh(client, source, 'more nc_listen.out')
        if result.rstrip() != msg:
            raise ValueError("Wrong or missing message: %r" % result.rstrip())
        log.info('SUCCESS.')
Esempio n. 3
0
def ensure_migration_with_resources_succeeds(source_client, dest_client):
    """Test simple migration of a model to another controller.

    Ensure that migration a model that has an application, that uses resources,
    deployed upon it is able to continue it's operation after the migration
    process. This includes assertion that the resources are migrated correctly
    too.

    Given 2 bootstrapped environments:
      - Deploy an application with a resource
        - ensure it's operating as expected
      - Migrate that model to the other environment
        - Ensure it's operating as expected
        - Add a new unit to the application to ensure the model is functional

    :return: Tuple containing migrated client object and the resource string
      that the charm deployed to it outputs.

    """
    resource_contents = get_random_string()
    test_model, application = deploy_simple_server_to_new_model(
        source_client, 'example-model-resource', resource_contents)
    migration_target_client = migrate_model_to_controller(
        test_model, dest_client)
    assert_model_migrated_successfully(
        migration_target_client, application, resource_contents)

    log.info('SUCCESS: resources migrated')
    return migration_target_client, application, resource_contents
Esempio n. 4
0
def prepare_dummy_env(client):
    """Use a client to prepare a dummy environment."""
    charm_source = local_charm_path(
        charm='dummy-source', juju_ver=client.version)
    client.deploy(charm_source)
    charm_sink = local_charm_path(charm='dummy-sink', juju_ver=client.version)
    client.deploy(charm_sink)
    token = get_random_string()
    client.set_config('dummy-source', {'token': token})
    client.juju('add-relation', ('dummy-source', 'dummy-sink'))
    client.juju('expose', ('dummy-sink',))
    return token
Esempio n. 5
0
    def test_passes_when_charm_is_responding_correctly(self):
        expected_output = get_random_string()
        client = Mock()

        with patch.object(
                amm, 'get_unit_ipaddress',
                autospec=True, return_value='192.168.1.2') as m_gui:
            with patch.object(
                    amm, 'get_server_response',
                    autospec=True, return_value=expected_output) as m_gsr:
                amm.assert_deployed_charm_is_responding(
                    client, expected_output)
        m_gui.assert_called_once_with(client, 'simple-resource-http/0')
        m_gsr.assert_called_once_with('192.168.1.2')
Esempio n. 6
0
    def test_passes_when_charm_is_responding_correctly(self):
        expected_output = get_random_string()
        client = Mock()

        with patch.object(amm,
                          'get_unit_ipaddress',
                          autospec=True,
                          return_value='192.168.1.2') as m_gui:
            with patch.object(amm,
                              'get_server_response',
                              autospec=True,
                              return_value=expected_output) as m_gsr:
                amm.assert_deployed_charm_is_responding(
                    client, expected_output)
        m_gui.assert_called_once_with(client, 'simple-resource-http/0')
        m_gsr.assert_called_once_with('192.168.1.2')
Esempio n. 7
0
    def test_raises_when_charm_does_not_respond_correctly(self):
        expected_output = get_random_string()
        client = Mock()

        with patch.object(
                amm, 'get_unit_ipaddress',
                autospec=True, return_value='192.168.1.2'):
            with patch.object(
                    amm, 'get_server_response',
                    autospec=True, return_value='abc'):

                with self.assertRaises(AssertionError) as ex:
                    amm.assert_deployed_charm_is_responding(
                        client, expected_output)
                self.assertEqual(
                    ex.exception.message,
                    'Server charm is not responding as expected.')
Esempio n. 8
0
    def test_raises_when_charm_does_not_respond_correctly(self):
        expected_output = get_random_string()
        client = Mock()

        with patch.object(amm,
                          'get_unit_ipaddress',
                          autospec=True,
                          return_value='192.168.1.2'):
            with patch.object(amm,
                              'get_server_response',
                              autospec=True,
                              return_value='abc'):

                with self.assertRaises(AssertionError) as ex:
                    amm.assert_deployed_charm_is_responding(
                        client, expected_output)
                self.assertEqual(
                    ex.exception.message,
                    'Server charm is not responding as expected.')
Esempio n. 9
0
def ensure_storage_remains_after_application_removal(client):
    """Storage created during a deploy must persist after application removal.

    Steps taken to test:
      - Deploy the dummy storage charm
      - Set config and ensure data is stored on storage.
      - Remove application, taking note of the remaining storage name
      - Re-deploy new charm using persisted storage
      - Ensure data has remained.

    :param client: ModelClient object to deploy the charm on.
    """
    random_token = get_random_string()
    expected_token_values = {'single-fs-token': random_token}
    charm_path = local_charm_path(
        charm='dummy-storage', juju_ver=client.version)
    client.deploy(charm_path, storage='single-fs=rootfs')
    client.wait_for_started()
    client.set_config('dummy-storage', {'single-fs-token': random_token})
    client.wait_for_workloads()

    assert_storage_is_intact(client, expected_results=expected_token_values)

    try:
        single_filesystem_name = get_storage_filesystems(
            client, 'single-fs')[0]
    except IndexError:
        raise JujuAssertionError('Storage was not found.')

    client.remove_service('dummy-storage')

    # Wait for application to be removed then re-deploy with existing storage.

    storage_command = '--attach-storage single-fs={}'.format(
        single_filesystem_name)
    client.deploy(charm_path, alias=storage_command)
    client.wait_for_started()
    client.wait_for_workloads()

    assert_storage_is_intact(client, expected_token_values)
Esempio n. 10
0
def ensure_storage_remains_after_application_removal(client):
    """Storage created during a deploy must persist after application removal.

    Steps taken to test:
      - Deploy the dummy storage charm
      - Set config and ensure data is stored on storage.
      - Remove application, taking note of the remaining storage name
      - Re-deploy new charm using persisted storage
      - Ensure data has remained.

    :param client: ModelClient object to deploy the charm on.
    """
    random_token = get_random_string()
    expected_token_values = {'single-fs-token': random_token}
    charm_path = local_charm_path(charm='dummy-storage',
                                  juju_ver=client.version)
    client.deploy(charm_path, storage='single-fs=rootfs')
    client.wait_for_started()
    client.set_config('dummy-storage', {'single-fs-token': random_token})
    client.wait_for_workloads()

    assert_storage_is_intact(client, expected_results=expected_token_values)

    try:
        single_filesystem_name = get_storage_filesystems(client,
                                                         'single-fs')[0]
    except IndexError:
        raise JujuAssertionError('Storage was not found.')

    client.remove_service('dummy-storage')

    # Wait for application to be removed then re-deploy with existing storage.

    storage_command = '--attach-storage single-fs={}'.format(
        single_filesystem_name)
    client.deploy(charm_path, alias=storage_command)
    client.wait_for_started()
    client.wait_for_workloads()

    assert_storage_is_intact(client, expected_token_values)
def env_token(env_name):
    return env_name + get_random_string()
Esempio n. 12
0
def env_token(env_name):
    return env_name + get_random_string()