Ejemplo n.º 1
0
def cse_server():
    """Fixture to ensure that CSE is installed and running before client tests.

    This fixture executes automatically for this module's setup and teardown.

    Setup tasks:
    - If templates do not exist, install CSE using `--upgrade`
    - Run `cse install` to ensure that CSE is registered and AMQP
        exchange exists.
    - Run CSE server as a subprocess

    Teardown tasks:
    - Stop CSE server
    """
    config = testutils.yaml_to_dict(env.BASE_CONFIG_FILEPATH)
    install_cmd = ['install', '--config', env.ACTIVE_CONFIG_FILEPATH,
                   '--ssh-key', env.SSH_KEY_FILEPATH]
    for template in config['broker']['templates']:
        if not env.catalog_item_exists(template['catalog_item']):
            install_cmd.append('--update')
            break

    env.setup_active_config()
    result = env.CLI_RUNNER.invoke(cli, install_cmd,
                                   input='y',
                                   catch_exceptions=False)
    assert result.exit_code == 0

    # start cse server as subprocess
    cmd = f"cse run -c {env.ACTIVE_CONFIG_FILEPATH}"
    p = subprocess.Popen(cmd.split(), stdout=subprocess.DEVNULL,
                         stderr=subprocess.STDOUT)
    time.sleep(env.WAIT_INTERVAL)  # server takes a little time to set up

    # enable kubernetes functionality on our ovdc
    # by default, an ovdc cannot deploy kubernetes clusters
    # TODO() this should be removed once this behavior is changed
    cmd = f"login {config['vcd']['host']} {utils.SYSTEM_ORG_NAME} " \
          f"{config['vcd']['username']} -iwp {config['vcd']['password']}"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0
    cmd = f"org use {config['broker']['org']}"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0
    cmd = f"cse ovdc enablek8s {config['broker']['vdc']} -c vcd"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0
    result = env.CLI_RUNNER.invoke(vcd, 'logout', catch_exceptions=False)

    yield

    # terminate cse server subprocess
    try:
        p.terminate()
    except OSError:
        pass
def cse_server():
    """Fixture to ensure that CSE is installed before client tests.

    This function will execute once for this module.

    Setup tasks:
    - If templates do not exist, install CSE using `--upgrade`
    - If CSE is not registered properly, install CSE normally
    """
    config = testutils.yaml_to_dict(env.BASE_CONFIG_FILEPATH)
    install_cmd = [
        'install', '--config', env.ACTIVE_CONFIG_FILEPATH, '--ssh-key',
        env.SSH_KEY_FILEPATH
    ]
    installation_exists = True
    for template in config['broker']['templates']:
        if not env.catalog_item_exists(template['catalog_item']):
            installation_exists = False
            install_cmd.append('--update')
            break

    env.setup_active_config()
    if not installation_exists \
            or not env.is_cse_registration_valid(config['amqp']['routing_key'],
                                                 config['amqp']['exchange']):
        result = env.CLI_RUNNER.invoke(cli,
                                       install_cmd,
                                       input='y',
                                       catch_exceptions=False)
        assert result.exit_code == 0

    # start cse server as subprocess
    cmd = f"cse run -c {env.ACTIVE_CONFIG_FILEPATH}"
    p = subprocess.Popen(cmd.split(),
                         stdout=subprocess.DEVNULL,
                         stderr=subprocess.STDOUT)
    time.sleep(10)

    yield

    # terminate cse server subprocess
    p.terminate()
Ejemplo n.º 3
0
def config():
    """Fixture to setup and teardown an active config file.

    Usage: add the parameter 'config' to the test function. This 'config'
        parameter is the dict representation of the config file, and can be
        used in the test function.

    Tasks:
    - create config dict from env.BASE_CONFIG_FILEPATH
    - create active config file at env.ACTIVE_CONFIG_FILEPATH
    - adjust active config file security

    yields config dict
    """
    config = env.setup_active_config()
    yield config
    env.teardown_active_config()
Ejemplo n.º 4
0
def cse_server():
    """Fixture to ensure that CSE is installed and running before client tests.

    This fixture executes automatically for this module's setup and teardown.

    Setup tasks:
    - If templates do not exist, install CSE using `--upgrade`
    - Run `cse install` to ensure that CSE is registered and AMQP
        exchange exists.
    - Run CSE server as a subprocess

    Teardown tasks:
    - Stop CSE server
    """
    config = testutils.yaml_to_dict(env.BASE_CONFIG_FILEPATH)
    install_cmd = [
        'install', '--config', env.ACTIVE_CONFIG_FILEPATH, '--ssh-key',
        env.SSH_KEY_FILEPATH
    ]
    env.setup_active_config()
    result = env.CLI_RUNNER.invoke(cli,
                                   install_cmd,
                                   input='y',
                                   catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('cse', install_cmd, result.exit_code,
                                      result.output)

    # start cse server as subprocess
    cmd = f"cse run -c {env.ACTIVE_CONFIG_FILEPATH}"
    p = None
    if os.name == 'nt':
        p = subprocess.Popen(cmd, shell=True)
    else:
        p = subprocess.Popen(cmd.split(),
                             stdout=subprocess.DEVNULL,
                             stderr=subprocess.STDOUT)
    time.sleep(env.WAIT_INTERVAL)  # server takes a little while to set up

    # enable kubernetes functionality on our ovdc
    # by default, an ovdc cannot deploy kubernetes clusters
    # TODO() this should be removed once this behavior is changed
    cmd = f"login {config['vcd']['host']} {constants.SYSTEM_ORG_NAME} " \
          f"{config['vcd']['username']} -iwp {config['vcd']['password']} " \
          f"-V {config['vcd']['api_version']}"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('vcd', cmd, result.exit_code,
                                      result.output)
    cmd = f"org use {config['broker']['org']}"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('vcd', cmd, result.exit_code,
                                      result.output)
    cmd = f"vdc use {config['broker']['vdc']}"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('vcd', cmd, result.exit_code,
                                      result.output)
    cmd = f"cse ovdc enable {config['broker']['vdc']} -k " \
          f"{constants.K8sProvider.NATIVE}"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('vcd', cmd, result.exit_code,
                                      result.output)
    cmd = f"catalog acl add {config['broker']['catalog']} " \
          f"\'org:{config['broker']['org']}:ReadOnly\'"
    result = env.CLI_RUNNER.invoke(vcd, cmd.split(), catch_exceptions=False)
    assert result.exit_code == 0, \
        testutils.format_command_info('vcd', cmd, result.exit_code,
                                      result.output)
    result = env.CLI_RUNNER.invoke(vcd, 'logout', catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('vcd', cmd, result.exit_code,
                                      result.output)

    yield

    # terminate cse server subprocess
    try:
        # check if the subprocess is running or not
        if p and p.poll() is None:
            if os.name == 'nt':
                subprocess.Popen(f"taskkill /f /pid {p.pid} /t")
            else:
                p.terminate()
    except OSError:
        pass
Ejemplo n.º 5
0
def cse_server():
    """Fixture to ensure that CSE is installed and running before client tests.

    This fixture executes automatically for this module's setup and teardown.

    Setup tasks:
    - If templates do not exist, install CSE using `--upgrade`
    - Run `cse install` to ensure that CSE is registered and AMQP
        exchange exists.
    - Run CSE server as a subprocess

    Teardown tasks:
    - Stop CSE server
    """
    env.setup_active_config()
    if env.is_cse_registered():
        cmd = [
            'upgrade', '--config', env.ACTIVE_CONFIG_FILEPATH, '--ssh-key',
            env.SSH_KEY_FILEPATH, '--skip-config-decryption',
            '--skip-template-creation'
        ]
    else:
        cmd = [
            'install', '--config', env.ACTIVE_CONFIG_FILEPATH, '--ssh-key',
            env.SSH_KEY_FILEPATH, '--skip-config-decryption',
            '--skip-template-creation'
        ]
    result = env.CLI_RUNNER.invoke(cli, cmd, input='y', catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('cse', cmd, result.exit_code,
                                      result.output)

    # Create missing templates
    for template_config in env.TEMPLATE_DEFINITIONS:
        cmd = f"template install {template_config['name']} " \
              f"{template_config['revision']} " \
              f"--config {env.ACTIVE_CONFIG_FILEPATH} " \
              f"--ssh-key {env.SSH_KEY_FILEPATH} " \
              f"--skip-config-decryption"
        result = env.CLI_RUNNER.invoke(cli,
                                       cmd.split(),
                                       catch_exceptions=False)
        assert result.exit_code == 0,\
            testutils.format_command_info('cse', cmd, result.exit_code,
                                          result.output)

    # start cse server as subprocess
    cmd = f"cse run -c {env.ACTIVE_CONFIG_FILEPATH} --skip-config-decryption"
    p = None
    if os.name == 'nt':
        p = subprocess.Popen(cmd, shell=True)
    else:
        p = subprocess.Popen(cmd.split(),
                             stdout=subprocess.DEVNULL,
                             stderr=subprocess.STDOUT)
    time.sleep(env.WAIT_INTERVAL * 3)  # server takes a little while to set up

    yield

    # terminate cse server subprocess
    try:
        # check if the subprocess is running or not
        if p and p.poll() is None:
            if os.name == 'nt':
                subprocess.Popen(f"taskkill /f /pid {p.pid} /t")
            else:
                p.terminate()
    except OSError:
        pass
def cse_server():
    """Fixture to ensure that CSE is installed and running before client tests.

    This fixture executes automatically for this module's setup and teardown.

    Setup tasks:
    - If templates do not exist, install CSE using `--upgrade`
    - Run `cse install` to ensure that CSE is registered and AMQP
        exchange exists.
    - Run CSE server as a subprocess

    Teardown tasks:
    - Stop CSE server
    """
    env.setup_active_config(logger=PYTEST_LOGGER)
    if env.is_cse_registered_as_mqtt_ext(logger=PYTEST_LOGGER):
        cmd = [
            'upgrade', '--config', env.ACTIVE_CONFIG_FILEPATH, '--ssh-key',
            env.SSH_KEY_FILEPATH, '--skip-config-decryption',
            '--skip-template-creation'
        ]
    else:
        cmd = [
            'install', '--config', env.ACTIVE_CONFIG_FILEPATH, '--ssh-key',
            env.SSH_KEY_FILEPATH, '--skip-config-decryption',
            '--skip-template-creation'
        ]
    result = env.CLI_RUNNER.invoke(cli, cmd, input='y', catch_exceptions=False)
    assert result.exit_code == 0,\
        testutils.format_command_info('cse', cmd, result.exit_code,
                                      result.output)

    # assign native right bundle to test org
    env.publish_right_bundle_to_deployment_org(logger=PYTEST_LOGGER)
    # assign rights to cluster admin role
    env.assign_native_rights(env.CLUSTER_ADMIN_ROLE_NAME, [
        "cse:nativeCluster: Full Access", "cse:nativeCluster: Modify",
        "cse:nativeCluster: View"
    ],
                             logger=PYTEST_LOGGER)
    # assign rights to cluster author role
    env.assign_native_rights(
        env.CLUSTER_AUTHOR_ROLE_NAME,
        ["cse:nativeCluster: Modify", "cse:nativeCluster: View"],
        logger=PYTEST_LOGGER)
    # Create missing templates
    PYTEST_LOGGER.debug("Creating missing templates")
    for template_config in env.TEMPLATE_DEFINITIONS:
        cmd = f"template install {template_config['name']} " \
              f"{template_config['revision']} " \
              f"--config {env.ACTIVE_CONFIG_FILEPATH} " \
              f"--ssh-key {env.SSH_KEY_FILEPATH} " \
              f"--skip-config-decryption"
        result = env.CLI_RUNNER.invoke(cli,
                                       cmd.split(),
                                       catch_exceptions=False)
        assert result.exit_code == 0,\
            testutils.format_command_info('cse', cmd, result.exit_code,
                                          result.output)
        PYTEST_LOGGER.debug("Successfully installed template "
                            f"{template_config['name']} at "
                            f"revision {template_config['revision']}")

    # start cse server as subprocess
    cmd = f"cse run -c {env.ACTIVE_CONFIG_FILEPATH} --skip-config-decryption"
    p = None
    if os.name == 'nt':
        p = subprocess.Popen(cmd, shell=True)
    else:
        p = subprocess.Popen(cmd.split(),
                             stdout=subprocess.DEVNULL,
                             stderr=subprocess.STDOUT)
    time.sleep(env.WAIT_INTERVAL * 3)  # server takes a little while to set up
    PYTEST_LOGGER.debug(p.stdout)
    PYTEST_LOGGER.debug("Successfully started the CSE server.")

    yield

    # terminate cse server subprocess
    try:
        # check if the subprocess is running or not
        if p and p.poll() is None:
            if os.name == 'nt':
                subprocess.Popen(f"taskkill /f /pid {p.pid} /t")
            else:
                p.terminate()
        PYTEST_LOGGER.debug("Killed CSE server")
    except OSError as e:
        PYTEST_LOGGER.warning(f"Failed to kill CSE server {e}")