Beispiel #1
0
def test_fact_store_route_health(openshift: OpenShift, factstore: list,
                                 config: dict):
    """Test Facstore route."""
    check_skip_test(config=config,
                    spec=Spec.FACTSTORE,
                    test_name=test_fact_store_route_health.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(factstore) > 0

    timeout = config['spec'][Spec.FACTSTORE]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=factstore,
        kind="Route",
    )
    template_route = resources.pop()
    route_name = template_route['metadata']['name']

    route_is_healthy = wait_for_condition(
        success_cond=lambda: openshift.get_route_status_code(route_name
                                                             ) == 200,
        min_to_wait=timeout)

    assert route_is_healthy

    db_route_connection_is_healthy = wait_for_condition(
        success_cond=lambda: openshift.get_route_status_code(
            route_name, path_suffix='/api/false_positive') == 200,
        min_to_wait=timeout)

    assert db_route_connection_is_healthy
def lad(openshift: OpenShift, config: dict, factstore: list) -> list:
    """Provide deployed lad objects."""
    es_endpoint = "lad-elasticsearch-service.{}.svc:9200".format(
        openshift.namespace
    )

    if not factstore:
        host = "no-host"
    else:
        resources = openshift.extract_resource_from_response(
            resp=factstore,
            kind="Route",
        )
        template_route = resources.pop()
        route_name = template_route['metadata']['name']
        try:
            oc_fs_route = openshift.get_route_by_name(route_name)
            host = oc_fs_route['spec']['host']
        except Exception as e:
            logging.warning("Could not retrieve factstore route.")
            host = "no-host"
    factstore_url = "http://{}".format(host)

    lad = template_deployer(openshift=openshift,
                            spec=Spec.LAD,
                            config=config,
                            FACT_STORE_URL=factstore_url,
                            ES_ENDPOINT=es_endpoint)

    yield lad
    if config['openshift']['clean'].lower() == "true":
        delete_objects(openshift, config, Spec.LAD,
                       FACT_STORE_URL=factstore_url,
                       ES_ENDPOINT=es_endpoint)
        delete_template(openshift, config, Spec.LAD)
def openshift(config: dict):
    """Provide openshift instance."""
    token = config['openshift']['authToken']
    host = config['openshift']['host']
    namespace = config['openshift']['namespace']

    configuration = kubeclient.Configuration()

    token_auth = dict(
        api_key={'authorization': 'Bearer {}'.format(token)},
        host='https://{}'.format(host),
        verify_ssl=False
    )

    for k, v in token_auth.items():
        setattr(configuration, k, v)

    kubeclient.Configuration.set_default(configuration)

    k8s_client = kubeclient.ApiClient(configuration)
    client = None
    try:
        client = DynamicClient(k8s_client)
    except UnauthorizedError:
        logging.error("Token is not authorized. Please supply another token.")
        exit(1)
    except Exception:
        logging.error("Could not connect to openshift cluster, "
                      "please ensure host settings are accurate.")

    ocp_url = "https://{}".format(host)

    openshift = OpenShift(
        namespace=namespace,
        openshift_api_url=ocp_url,
        token=token,
        client=client
    )

    openshift.create_project()

    # The fixture return value
    return openshift
Beispiel #4
0
def test_ad_demo_order_placement(openshift: OpenShift, ad_demo: list,
                                 config: dict):
    """Test Anomaly Detection Demo Order Placement."""
    check_skip_test(config=config,
                    spec=Spec.AD_DEMO,
                    test_name=test_ad_demo_order_placement.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(ad_demo) > 0
    resources = openshift.extract_resource_from_response(
        resp=ad_demo,
        kind="Route",
    )
    resources = list(
        filter(lambda route: not route['metadata']['name'].endswith('metrics'),
               resources))
    template_route = resources.pop()
    route_name = template_route['metadata']['name']
    route = openshift.get_route_by_name(route_name)
    route_url = "http://{}/mock/orderService".format(route['spec']['host'])
    form_data = {
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[id]": "1",
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[image]": "bananas.jpg",
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[pname]": "bananas",
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[pprice]": "5",
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[pquant]": "1",
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[ptype]": "fruit",
        "79161a98-30e0-11e7-b4e8-9801a798fc8f[newQuant]": "1",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[id]": "2",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[image]": "onions.jpg",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[pname]": "onions",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[pprice]": "3",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[pquant]": "1",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[ptype]": "vegetables",
        "7915f0cc-30e0-11e7-91c7-9801a798fc8f[newQuant]": "1"
    }
    with requests.Session() as s:
        s.headers = {"User-Agent": "Mozilla/5.0"}
        res = s.post(route_url, data=form_data)
    assert res.status_code == 200
Beispiel #5
0
def test_lad_deployment(openshift: OpenShift, lad: list, config: dict):
    """Test Lad Deployment."""
    check_skip_test(config=config,
                    spec=Spec.LAD,
                    test_name=test_lad_deployment.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(lad) > 0
    timeout = config['spec'][Spec.LAD]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=lad,
        kind="DeploymentConfig",
    )
    deploy_cfg = resources.pop()
    is_running = deployment_running(openshift, deploy_cfg, timeout)
    assert is_running
Beispiel #6
0
def test_ad_demo_deployment(openshift: OpenShift, ad_demo: list, config: dict):
    """Test Anomaly Detection Demo Deployment."""
    check_skip_test(config=config,
                    spec=Spec.AD_DEMO,
                    test_name=test_ad_demo_deployment.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(ad_demo) > 0
    timeout = config['spec'][Spec.AD_DEMO]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=ad_demo,
        kind="DeploymentConfig",
    )
    deploy_cfg = resources.pop()
    is_running = deployment_running(openshift, deploy_cfg, timeout)
    assert is_running
Beispiel #7
0
def test_es_deployment(openshift: OpenShift, elasticsearch: list,
                       config: dict):
    """Test Elastic Search Deployment."""
    check_skip_test(config=config,
                    spec=Spec.ELASTICSEARCH,
                    test_name=test_es_deployment.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(elasticsearch) > 0
    timeout = config['spec'][Spec.ELASTICSEARCH]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=elasticsearch,
        kind="DeploymentConfig",
    )
    deploy_cfg = resources.pop()
    is_running = deployment_running(openshift, deploy_cfg, timeout)
    assert is_running
Beispiel #8
0
def test_fact_store_deployment(openshift: OpenShift, factstore: list,
                               config: dict):
    """Test factstore deployment."""
    check_skip_test(config=config,
                    spec=Spec.FACTSTORE,
                    test_name=test_fact_store_deployment.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(factstore) > 0

    timeout_amount = config['spec'][Spec.FACTSTORE]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=factstore,
        kind="DeploymentConfig",
    )
    deploy_cfg = resources.pop()
    is_running = deployment_running(openshift, deploy_cfg, timeout_amount)
    assert is_running
Beispiel #9
0
def test_ad_demo_build(openshift: OpenShift, ad_demo: list, config: dict):
    """Test Anomaly Detection Demo Build."""
    check_skip_test(config=config,
                    spec=Spec.AD_DEMO,
                    test_name=test_ad_demo_build.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(ad_demo) > 0

    timeout = config['spec'][Spec.AD_DEMO]['timeout']
    build_log_file = config['logs']["build_log_file"]
    resources = openshift.extract_resource_from_response(
        resp=ad_demo,
        kind="BuildConfig",
    )
    build_cfg = resources.pop()

    build_sucessfully_completed = build_finished_successfully(
        openshift, build_cfg, timeout, build_log_file)

    assert build_sucessfully_completed
Beispiel #10
0
def test_lad_container_started(openshift: OpenShift, lad: list, config: dict):
    """Test Lad container status."""
    check_skip_test(config=config,
                    spec=Spec.LAD,
                    test_name=test_lad_container_started.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(lad) > 0
    timeout = config['spec'][Spec.LAD]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=lad,
        kind="DeploymentConfig",
    )

    deploy_cfg = resources.pop()
    is_running = container_running(
        openshift=openshift,
        deploy_cfg=deploy_cfg,
        timeout_amount=timeout,
        container_id=0,
        pod_log_file_prefix=config['logs']['pod_log_file'])
    assert is_running
Beispiel #11
0
def test_fact_store_build(openshift: OpenShift, factstore: list, config: dict):
    """Test factstore build."""
    check_skip_test(config=config,
                    spec=Spec.FACTSTORE,
                    test_name=test_fact_store_build.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(factstore) > 0

    timeout_amount = config['spec'][Spec.FACTSTORE]['timeout']
    build_log_file = config['logs']["build_log_file"]

    resources = openshift.extract_resource_from_response(
        resp=factstore,
        kind="BuildConfig",
    )
    build_cfg = resources.pop()

    build_sucessfully_completed = build_finished_successfully(
        openshift, build_cfg, timeout_amount, build_log_file)

    assert build_sucessfully_completed
Beispiel #12
0
def test_ad_demo_route_health(openshift: OpenShift, ad_demo: list,
                              config: dict):
    """Test Anomaly Detection Demo Route Health."""
    check_skip_test(config=config,
                    spec=Spec.AD_DEMO,
                    test_name=test_ad_demo_route_health.__name__)
    # Ensure we successfully deployed (if indicated) or retrieved template.
    assert len(ad_demo) > 0
    timeout = config['spec'][Spec.AD_DEMO]['timeout']
    resources = openshift.extract_resource_from_response(
        resp=ad_demo,
        kind="Route",
    )

    resources = list(
        filter(lambda route: not route['metadata']['name'].endswith('metrics'),
               resources))
    template_route = resources.pop()
    route_name = template_route['metadata']['name']

    is_healthy = wait_for_condition(success_cond=lambda: openshift.
                                    get_route_status_code(route_name) == 200,
                                    min_to_wait=timeout)
    assert is_healthy