def __get_experimenters():
    """
    Get the ue-test experimenter defined in the integration test configuration file
    :return:
    """
    experimenter_name = get_config_value('ue-test', 'username')
    experimenter_password = get_config_value('ue-test', 'password')
    create_experimenter = get_config_value('ue-test', 'create-user', 'True')
    delete_experimenter = get_config_value('ue-test', 'delete-user', 'True')
    experiment_file = get_config_value('ue-test', 'experiment')
    experiment_name = get_config_value('ue-test', 'experiment-name')
    experimenters = [
        (experimenter_name, experimenter_password, create_experimenter,
         delete_experimenter, experiment_file, experiment_name)
    ]
    return experimenters
    def validate(self, resource, resource_id, used_resource_id, session):
        log.debug('Validate MonitoringResource with resource_id: {}'.format(resource_id))
        log.debug('Validate MonitoringResource with resource: {}'.format(resource))

        attempts = int(get_config_value('monitoring-resource', 'attempts', '10'))

        try:
            res = json.loads(resource)
        except JSONDecodeError as e:
            raise MonitoringResourceValidationException(e.msg)

        if not res["floatingIp"]:
            raise MonitoringResourceValidationException("Floating ip not available: {}".format(res))
            
        cnt = 1
        while cnt <= attempts:

            log.debug('Validate attempt: {}'.format(cnt))

            try:
                r = requests.get(res["url"], timeout=10)
                if r.status_code == 200:
                    if "zabbix.php" in r.text:
                        log.debug('********SUCCESSS*********')
                        return
            except Exception as e:
                if cnt > attempts:
                    log.error("after %d attempts zabbix is not started yet, considering it failed..." % attempts)
                    exception_data = traceback.format_exc().splitlines()
                    exception_text = "Error: {}".format(exception_data[-1])
                    log.error(exception_text)
                    raise e  # raise exceptions only after X attempts, to allow test passing in slow environments

            cnt += 1

            time.sleep(5)

        raise MonitoringResourceValidationException(res)
def start_integration_test():
    log.info("Starting the SoftFIRE integration tests.")
    test_results = OrderedDict()

    # get config values
    exp_mngr_admin_name = get_config_value('experiment-manager',
                                           'admin-username', 'admin')
    exp_mngr_admin_pwd = get_config_value('experiment-manager',
                                          'admin-password', 'admin')
    admin_session = log_in(exp_mngr_admin_name, exp_mngr_admin_pwd)
    experimenters = __get_experimenters()

    # validate experiment files (preparation phase)
    preparation_failed = False
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experiment_file = experimenter[4]
        try:
            __validate_experiment_file(experiment_file)
        except Exception as e:
            traceback.print_exc()
            add_result(test_results, 'Preparation', 'FAILED',
                       '{}: {}'.format(experimenter_name, str(e)))
            preparation_failed = True
    log.info('Finished preparation phase.')
    if preparation_failed:
        log.error('Preparation phase failed.')
        time.sleep(1)
        print()
        print_results([[r[0], r[1].get('status'), r[1].get('details')]
                       for r in test_results.items()])
        __exit_on_failure([[r[0], r[1].get('status'), r[1].get('details')]
                           for r in test_results.items()])
        return

    # create experimenter(s)
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        create_experimenter = experimenter[2]
        if create_experimenter in ['True', 'true']:
            try:
                create_user(experimenter_name, experimenter_pwd,
                            'experimenter', admin_session)
                log.info(
                    'Triggered the creation of a new experimenter named \'{}\'.'
                    .format(experimenter_name))
            except Exception as e:
                log.error(
                    'Could not trigger the creation of a new experimenter named {}.'
                    .format(experimenter_pwd))
                traceback.print_exc()
                add_result(test_results, 'Create User', 'FAILED',
                           '{}: {}'.format(experimenter_name, str(e)))

    # check if the users were created correctly
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        create_experimenter = experimenter[2]
        if create_experimenter in ['True', 'true']:
            log.debug(
                'Trying to log in as user {} to check if the user was created successfully.'
                .format(experimenter_name))
            for i in range(0, 18):
                time.sleep(5)
                try:
                    log_in(experimenter_name, experimenter_pwd)
                    add_result(test_results, 'Create User', 'OK', '')
                    break
                except:
                    pass
            else:
                log.error(
                    'Not able to log in as experimenter {}. Assuming the creation failed.'
                )
                add_result(
                    test_results, 'Create User', 'FAILED',
                    '{}: The user seems not to exist.'.format(
                        experimenter_name))

    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        experiment_file = experimenter[4]
        user_session = log_in(experimenter_name, experimenter_pwd)
        try:
            upload_experiment(experiment_file, user_session)
            log.info('Experimenter {} uploaded experiment {}.'.format(
                experimenter_name, experiment_file))
            add_result(test_results, 'Upload Experiment', 'OK', '')
        except Exception as e:
            log.error('Experimenter {} could not upload experiment {}.'.format(
                experimenter_name, experiment_file))
            traceback.print_exc()
            add_result(test_results, 'Upload Experiment', 'FAILED',
                       '{}: {}'.format(experimenter_name, str(e)))

    deployment_threads_queues = []
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        experiment_name = experimenter[5]
        experiment_id = '{}_{}'.format(experimenter_name, experiment_name)
        user_session = log_in(experimenter_name, experimenter_pwd)
        q = queue.Queue()
        t = threading.Thread(target=deploy_experiment,
                             args=(
                                 user_session,
                                 experiment_id,
                                 q,
                             ),
                             name=experimenter_name)
        deployment_threads_queues.append((t, q))
    for (t, q) in deployment_threads_queues:
        log.info('Deploy experiment of experimenter {}'.format(t.name))
        t.start()
    time.sleep(5)
    for (t, q) in deployment_threads_queues:
        t.join()
        exception = q.get()
        if exception is not None:
            log.error(
                'The experiment\'s deployment failed for experimenter {}.'.
                format(t.name))
            add_result(test_results, 'Deploy Experiment', 'FAILED',
                       '{}: {}'.format(t.name, str(exception)))
        else:
            add_result(test_results, 'Deploy Experiment', 'OK', '')

    # validate deployments
    validated_resources = []
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        experiment_name = experimenter[5]
        experiment_id = '{}_{}'.format(experimenter_name, experiment_name)
        failed_resources = []
        user_session = log_in(experimenter_name, experimenter_pwd)
        deployed_experiment = get_experiment_status(
            user_session, experiment_id=experiment_id)
        for resource in deployed_experiment:
            used_resource_id = resource.get('used_resource_id')
            resource_id = resource.get('resource_id')
            node_type = resource.get('node_type')
            try:
                log.info("Starting to validate resource of node type: %s" %
                         node_type)
                validator = get_validator(node_type)
                log.debug("Got validator %s" % validator)
                validator.validate(
                    get_resource_from_id(used_resource_id,
                                         session=user_session), resource_id,
                    used_resource_id, user_session)
                log.info('\n\n\n')
                log.info(
                    'Validation of resource {}: {}-{} succeeded.\n\n\n'.format(
                        experimenter_name, resource_id, used_resource_id))
                time.sleep(5)
                validated_resources.append([
                    '   - {}: {}-{}'.format(experimenter_name, resource_id,
                                            used_resource_id), 'OK', ''
                ])
            except Exception as e:
                error_message = e.message if isinstance(
                    e, IntegrationTestException) else str(e)
                log.error('Validation of resource {}: {}-{} failed: {}'.format(
                    experimenter_name, resource_id, used_resource_id,
                    error_message))
                traceback.print_exc()
                validated_resources.append([
                    '   - {}: {}-{}'.format(experimenter_name, resource_id,
                                            used_resource_id), 'FAILED',
                    '{}: {}'.format(experimenter_name, error_message)
                ])
                failed_resources.append(resource_id)
        if len(failed_resources) == 0:
            add_result(test_results, 'Validate Resources', 'OK', '')
        else:
            add_result(
                test_results, 'Validate Resources', 'FAILED',
                '{}: {}'.format(experimenter_name,
                                ', '.join(failed_resources)))
        log.info('Resource validation phase for {} finished.'.format(
            experimenter_name))
    for resource in validated_resources:
        add_result(test_results, resource[0], resource[1], resource[2])

    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        experiment_name = experimenter[5]
        experiment_id = '{}_{}'.format(experimenter_name, experiment_name)
        user_session = log_in(experimenter_name, experimenter_pwd)
        try:
            log.info('\n\n\n')
            log.info("Removing experiment {} of {}".format(
                experiment_id, experimenter_name))
            delete_experiment(user_session, experiment_id)
            log.info('Removed experiment {} of {}.\n\n\n'.format(
                experiment_id, experimenter_name))
            add_result(test_results, 'Delete Experiment', 'OK', '')
        except Exception as e:
            log.error('Failure during removal of experiment {} of {}.'.format(
                experiment_id, experimenter_name))
            traceback.print_exc()
            add_result(test_results, 'Delete Experiment', 'FAILED',
                       '{}: {}'.format(experimenter_name, str(e)))

    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        delete_experimenter = experimenter[3]
        if delete_experimenter in ['True', 'true']:
            try:
                delete_user(experimenter_name, admin_session)
                log.info(
                    'Successfully removed experimenter named \'{}\'.'.format(
                        experimenter_name))
                add_result(test_results, 'Delete User', 'OK', '')
            except Exception as e:
                log.error('Could not remove experimenter named {}.'.format(
                    experimenter_pwd))
                traceback.print_exc()
                add_result(test_results, 'Delete User', 'FAILED',
                           '{}: {}'.format(experimenter_name, str(e)))

    time.sleep(
        1
    )  # otherwise the results were printed in the middle of the stack traces
    print()
    print_results([[r[0], r[1].get('status'), r[1].get('details')]
                   for r in test_results.items()])
    __exit_on_failure([[r[0], r[1].get('status'), r[1].get('details')]
                       for r in test_results.items()])
def __get_experimenters():
    """
    Get the experimenters defined in the integration test configuration file.
    You can specify just one experimenter with a section called [experimenter]
    or you can add additional ones by using sections called [experimenter-x] where x is an integer between 0 and 99.
    In any case the experimenter in section [experimenter] has to be present always.
    :return:
    """
    experimenter_name = get_config_value('experimenter', 'username')
    experimenter_password = get_config_value('experimenter', 'password')
    create_experimenter = get_config_value('experimenter', 'create-user',
                                           'True')
    delete_experimenter = get_config_value('experimenter', 'delete-user',
                                           'True')
    experiment_file = get_config_value('experimenter', 'experiment')
    experiment_name = get_config_value('experimenter', 'experiment-name')
    experimenters = [
        (experimenter_name, experimenter_password, create_experimenter,
         delete_experimenter, experiment_file, experiment_name)
    ]
    for i in range(0, 100):
        try:
            experimenter_name = get_config_value('experimenter-{}'.format(i),
                                                 'username')
            experimenter_password = get_config_value(
                'experimenter-{}'.format(i), 'password')
            create_experimenter = get_config_value('experimenter-{}'.format(i),
                                                   'create-user', 'True')
            delete_experimenter = get_config_value('experimenter-{}'.format(i),
                                                   'delete-user', 'True')
            experiment_file = get_config_value('experimenter-{}'.format(i),
                                               'experiment')
            experiment_name = get_config_value('experimenter-{}',
                                               'experiment-name')
            experimenters.append(
                (experimenter_name, experimenter_password, create_experimenter,
                 delete_experimenter, experiment_file, experiment_name))
        except (configparser.NoSectionError, configparser.NoOptionError):
            break
    return experimenters
import json
import os
import traceback

import requests

from eu.softfire.integrationtest.utils.exceptions import IntegrationTestException
from eu.softfire.integrationtest.utils.utils import get_config_value, get_logger

experiment_manager_ip = get_config_value('experiment-manager', 'ip',
                                         'localhost')
experiment_manager_port = get_config_value('experiment-manager', 'port',
                                           '5080')
experiment_manager_login_url = 'http://{}:{}/login'.format(
    experiment_manager_ip, experiment_manager_port)
experiment_manager_create_user_url = 'http://{}:{}/create_user'.format(
    experiment_manager_ip, experiment_manager_port)
experiment_manager_delete_user_url = 'http://{}:{}/delete_user'.format(
    experiment_manager_ip, experiment_manager_port)
experiment_manager_upload_experiment_url = 'http://{}:{}/reserve_resources'.format(
    experiment_manager_ip, experiment_manager_port)
experiment_manager_deploy_experiment_url = 'http://{}:{}/provide_resources'.format(
    experiment_manager_ip, experiment_manager_port)
experiment_manager_delete_experiment_url = 'http://{}:{}/release_resources'.format(
    experiment_manager_ip, experiment_manager_port)
experiment_manager_get_status_url = 'http://{}:{}/get_status'.format(
    experiment_manager_ip, experiment_manager_port)

log = get_logger(__name__)

import json
import subprocess
import time

from eu.softfire.integrationtest.main.experiment_manager_client import get_resource_from_id
from eu.softfire.integrationtest.utils.exceptions import NfvValidationException
from eu.softfire.integrationtest.utils.utils import get_config_value, get_logger
from eu.softfire.integrationtest.validators.validators import AbstractValidator

log = get_logger(__name__)

wait_nfv_resource_minuties = int(
    get_config_value('nfv-resource', 'wait-nfv-res-min', '7'))


class NfvResourceValidator(AbstractValidator):
    def validate(self, resource, resource_id, used_resource_id, session):
        log.debug(
            'Validate NfvResource with resource_id: {}'.format(resource_id))
        # wait at most about 7 minutes for the NSR to reach active state
        nsr = None
        for i in range(wait_nfv_resource_minuties * 20):
            time.sleep(3)
            resource = get_resource_from_id(used_resource_id, session)
            try:
                nsr = json.loads(resource)
            except Exception as e:
                log.error('Not able to parse resource: {}'.format(resource))
                raise NfvValidationException(
                    'Not able to parse resource: {}'.format(resource))
            nsr_status = nsr.get('status')
import json
import sys
import time
import traceback

from eu.softfire.integrationtest.main.experiment_manager_client import create_user, upload_experiment, \
    delete_experiment, deploy_experiment, get_resource_from_id, get_experiment_status, delete_user, \
    log_in
from eu.softfire.integrationtest.utils.utils import get_config_value
from eu.softfire.integrationtest.utils.utils import get_logger

log = get_logger(__name__)

# get config values
exp_mngr_admin_name = get_config_value('experiment-manager', 'admin-username',
                                       'admin')
exp_mngr_admin_pwd = get_config_value('experiment-manager', 'admin-password',
                                      'admin')
experimenter_name = get_config_value('connectivity-test', 'username')
experimenter_pwd = get_config_value('connectivity-test', 'password')
create_experimenter = get_config_value('connectivity-test', 'create-user',
                                       'True')
delete_experimenter = get_config_value('connectivity-test', 'delete-user',
                                       'True')
experiment_file_path = get_config_value('connectivity-test', 'experiment')
wait_nfv_resource_minuties = int(
    get_config_value('nfv-resource', 'wait-nfv-res-min', '10'))
experiment_name = get_config_value('connectivity-test', 'experiment-name')
experiment_id = '{}_{}'.format(experimenter_name, experiment_name)

def start_ue_resv_engine_test():
    log.info("Starting the SoftFIRE UE reservation engine test.")
    test_results = OrderedDict()

    # get config values
    exp_mngr_admin_name = get_config_value('experiment-manager',
                                           'admin-username', 'admin')
    exp_mngr_admin_pwd = get_config_value('experiment-manager',
                                          'admin-password', 'admin')
    admin_session = log_in(exp_mngr_admin_name, exp_mngr_admin_pwd)
    experimenters = __get_experimenters()

    # validate experiment files (preparation phase)
    preparation_failed = False
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experiment_file = experimenter[4]
        try:
            __validate_experiment_file(experiment_file)
        except Exception as e:
            traceback.print_exc()
            add_result(test_results, 'Preparation', 'FAILED',
                       '{}: {}'.format(experimenter_name, str(e)))
            preparation_failed = True
    log.info('Finished preparation phase.')
    if preparation_failed:
        log.error('Preparation phase failed.')
        time.sleep(1)
        print()
        print_results([[r[0], r[1].get('status'), r[1].get('details')]
                       for r in test_results.items()])
        __exit_on_failure([[r[0], r[1].get('status'), r[1].get('details')]
                           for r in test_results.items()])
        return

    # create experimenter(s)
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        create_experimenter = experimenter[2]
        if create_experimenter in ['True', 'true']:
            try:
                create_user(experimenter_name, experimenter_pwd,
                            'experimenter', admin_session)
                log.info(
                    'Triggered the creation of a new experimenter named \'{}\'.'
                    .format(experimenter_name))
            except Exception as e:
                log.error(
                    'Could not trigger the creation of a new experimenter named {}.'
                    .format(experimenter_pwd))
                traceback.print_exc()
                add_result(test_results, 'Create User', 'FAILED',
                           '{}: {}'.format(experimenter_name, str(e)))

    # check if the users were created correctly
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        create_experimenter = experimenter[2]
        if create_experimenter in ['True', 'true']:
            log.debug(
                'Trying to log in as user {} to check if the user was created successfully.'
                .format(experimenter_name))
            for i in range(0, 18):
                time.sleep(5)
                try:
                    log_in(experimenter_name, experimenter_pwd)
                    add_result(test_results, 'Create User', 'OK', '')
                    break
                except:
                    pass
            else:
                log.error(
                    'Not able to log in as experimenter {}. Assuming the creation failed.'
                )
                add_result(
                    test_results, 'Create User', 'FAILED',
                    '{}: The user seems not to exist.'.format(
                        experimenter_name))

    # upload experiment
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        experiment_file = experimenter[4]  # defined in config
        experiment_name = experimenter[5]
        user_session = log_in(experimenter_name, experimenter_pwd)
        try:
            upload_experiment(experiment_file, user_session)
            log.info('Experimenter {} uploaded experiment {}.'.format(
                experimenter_name, experiment_file))
            add_result(test_results, 'Upload Experiment', 'OK', '')
        except Exception as e:
            log.error('Experimenter {} could not upload experiment {}.'.format(
                experimenter_name, experiment_file))
            traceback.print_exc()
            add_result(test_results, 'Upload Experiment', 'FAILED',
                       '{}: {}'.format(experimenter_name, str(e)))

    # delete experiment
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        experiment_name = experimenter[5]
        experiment_id = '{}_{}'.format(experimenter_name, experiment_name)
        user_session = log_in(experimenter_name, experimenter_pwd)
        try:
            log.info('\n\n\n')
            log.info("Removing Experiment of {}".format(experimenter_name))
            delete_experiment(user_session, experiment_id)
            log.info(
                'Removed experiment of {}.\n\n\n'.format(experimenter_name))
            add_result(test_results, 'Delete Experiment', 'OK', '')
        except Exception as e:
            log.error('Failure during removal of experiment of {}.'.format(
                experimenter_name))
            traceback.print_exc()
            add_result(test_results, 'Delete Experiment', 'FAILED',
                       '{}: {}'.format(experimenter_name, str(e)))

    # delete experimenter
    for experimenter in experimenters:
        experimenter_name = experimenter[0]
        experimenter_pwd = experimenter[1]
        delete_experimenter = experimenter[3]
        try_count = 3
        if delete_experimenter in ['True', 'true']:
            while try_count > 0:
                try:
                    delete_user(experimenter_name, admin_session)
                    log.info('Successfully removed experimenter named \'{}\'.'.
                             format(experimenter_name))
                    add_result(test_results, 'Delete User', 'OK', '')
                    try_count = 0
                except Exception as e:
                    log.error('Could not remove experimenter named {}.'.format(
                        experimenter_pwd))
                    traceback.print_exc()
                    add_result(test_results, 'Delete User', 'FAILED',
                               '{}: {}'.format(experimenter_name, str(e)))
                    try_count -= 1

    time.sleep(
        1
    )  # otherwise the results were printed in the middle of the stack traces
    print()
    print_results([[r[0], r[1].get('status'), r[1].get('details')]
                   for r in test_results.items()])
    __exit_on_failure([[r[0], r[1].get('status'), r[1].get('details')]
                       for r in test_results.items()])