Skip to content
Snippets Groups Projects
conftest.py 8.71 KiB
Newer Older
Nikolay Stanchev's avatar
Nikolay Stanchev committed
#!/usr/bin/python3
"""
// © University of Southampton IT Innovation Centre, 2018
//
// Copyright in this software belongs to University of Southampton
// IT Innovation Centre of Gamma House, Enterprise Road,
// Chilworth Science Park, Southampton, SO16 7NS, UK.
//
// This software may not be used, sold, licensed, transferred, copied
// or reproduced in whole or in part in any manner or form or in or
// on any media by any person other than in accordance with the terms
// of the Licence Agreement supplied with the software, or otherwise
// without the prior written consent of the copyright owners.
//
// This software is distributed WITHOUT ANY WARRANTY, without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE, except where stated in the Licence Agreement supplied with
// the software.
//
//      Created By :            Nikolay Stanchev
//      Created Date :          09-07-2018
//      Created for Project :   FLAME
"""

import pytest
from influxdb import InfluxDBClient
from py2neo import Graph
from clmcservice.graphapi.utilities import build_network_graph
Nikolay Stanchev's avatar
Nikolay Stanchev committed

# static network configuration data used for testing cases, latencies reported in milliseconds
links = [
    {
        "src-switch": "dpid1",
        "dst-switch": "dpid2",
    }
]


switches = {
    "dpid1": "127.0.0.1",
    "dpid2": "127.0.0.2",
    "dpid3": "127.0.0.3",
    "dpid4": "127.0.0.4",
    "dpid5": "127.0.0.5",
    "dpid6": "127.0.0.6"
}


clusters = {
    "127.0.0.1": "DC1",
    "127.0.0.2": "DC2",
    "127.0.0.3": "DC3",
    "127.0.0.4": "DC4",
    "127.0.0.5": "DC5",
    "127.0.0.6": "DC6"
ues = {
    "127.0.0.1": "ue1",
    "127.0.0.3": "ue3",
    "127.0.0.6": "ue6"
}

Nikolay Stanchev's avatar
Nikolay Stanchev committed

@pytest.fixture(scope='module', autouse=True)
def db_testing_data():
    """
    This fixture generates testing data in influx to be used in the various test methods, after which it clears up the neo4j and influx databases.
Nikolay Stanchev's avatar
Nikolay Stanchev committed

    :return: pair of time stamps - the from-to range of the generated influx test data, test database name and the graph db client object
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    """

Nikolay Stanchev's avatar
Nikolay Stanchev committed

    test_sfc_name = "test_sfc"
    test_sfc_instance_1_name = "test_sfc_premium"
    test_sfc_instance_2_name = "test_sfc_non_premium"
    test_db_name = test_sfc_name
Nikolay Stanchev's avatar
Nikolay Stanchev committed

    # ASSUMES both Influx and Neo4j are running on localhost with default ports
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    influx = InfluxDBClient(host="localhost", port=8086, timeout=10)
    graph = Graph(host="localhost", password="admin")
    graph.delete_all()  # clear the graph db before testing
Nikolay Stanchev's avatar
Nikolay Stanchev committed

    # create the physical infrastructure subgraph
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    dbs = influx.get_list_database()
    switch_count, cluster_count, ues_count = build_network_graph(graph, switches, links, clusters, ues)
    assert switch_count == 6 and cluster_count == 6 and ues_count == 3, "Network graph build failure"
Nikolay Stanchev's avatar
Nikolay Stanchev committed

    # check if exists ( if so, clear ) or create the test DB in influx
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    if test_db_name in dbs:
        influx.drop_database(test_db_name)
    influx.create_database(test_db_name)
    influx.switch_database(test_db_name)

    # time range for which the data is reported
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    from_timestamp = 1528385860
    to_timestamp = 1528685860

    # nginx data to report to influx
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    data = [
        ("nginx_1_ep1", "DC4", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 5, 20, 1500, 15000, 1528385860),
        ("nginx_1_ep2", "DC6", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 8, 35, 1000, 11000, 1528385860),
        ("nginx_1_ep1", "DC4", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 7, 15, 2300, 10000, 1528389860),
        ("nginx_1_ep2", "DC6", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 10, 23, 98000, 1200, 1528389860),
        ("nginx_1_ep1", "DC4", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 12, 17, 2000, 7500, 1528395860),
        ("nginx_1_ep2", "DC6", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 15, 11, 1300, 6700, 1528395860),
        ("nginx_1_ep1", "DC4", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 17, 23, 3000, 8300, 1528485860),
        ("nginx_1_ep2", "DC6", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 19, 24, 76000, 1200, 1528485860),
        ("nginx_1_ep1", "DC4", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 11, 16, 2500, 7500, 1528545860),
        ("nginx_1_ep2", "DC6", "nginx", "nginx_1", test_sfc_name, test_sfc_instance_1_name, 20, 18, 1700, 12000, 1528545860)
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    ]
    influx.write_points([
        {"measurement": "nginx",
         "tags": {"flame_server": location, "flame_sfe": sfe, "flame_location": location, "flame_sfp": sfp, "flame_sf": sf, "flame_sfc": sfc, "flame_sfci": sfci},
         "fields": {"requests": num_requests, "avg_processing_time": processing_time, "avg_request_size": request_size, "avg_response_size": response_size},
Nikolay Stanchev's avatar
Nikolay Stanchev committed
         "time": timestamp * 10 ** 9
         } for sfe, location, sfp, sf, sfc, sfci, num_requests, processing_time, request_size, response_size, timestamp in data
    # minio data to report to influx
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    data = [
        ("minio_1_ep1", "DC4", "minio", "minio_1", test_sfc_name, test_sfc_instance_1_name, 12, 86, 101000, 4700, 1528386860),
        ("minio_2_ep1", "DC5", "minio", "minio_2", test_sfc_name, test_sfc_instance_2_name, 15, 75, 96000, 6300, 1528386860),
        ("minio_1_ep1", "DC4", "minio", "minio_1", test_sfc_name, test_sfc_instance_1_name, 7, 105, 5200, 89200, 1528388860),
        ("minio_2_ep1", "DC5", "minio", "minio_2", test_sfc_name, test_sfc_instance_2_name, 12, 60, 76900, 2100, 1528388860),
        ("minio_1_ep1", "DC4", "minio", "minio_1", test_sfc_name, test_sfc_instance_1_name, 11, 121, 99500, 3500, 1528410860),
        ("minio_2_ep1", "DC5", "minio", "minio_2", test_sfc_name, test_sfc_instance_2_name, 12, 154, 2700, 111000, 1528410860),
        ("minio_1_ep1", "DC4", "minio", "minio_1", test_sfc_name, test_sfc_instance_1_name, 14, 84, 1100, 4300, 1528412860),
        ("minio_2_ep1", "DC5", "minio", "minio_2", test_sfc_name, test_sfc_instance_2_name, 5, 45, 1200, 3200, 1528412860),
        ("minio_1_ep1", "DC4", "minio", "minio_1", test_sfc_name, test_sfc_instance_1_name, 7, 63, 87000, 2000, 1528414860),
        ("minio_2_ep1", "DC5", "minio", "minio_2", test_sfc_name, test_sfc_instance_2_name, 16, 86, 3100, 94000, 1528414860)
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    ]
    influx.write_points([
        {"measurement": "minio_http",
         "tags": {"flame_server": location, "flame_sfe": sfe, "flame_location": location, "flame_sfp": sfp, "flame_sf": sf, "flame_sfc": sfc, "flame_sfci": sfci},
         "fields": {"total_requests_count": num_requests, "total_processing_time": processing_time, "total_requests_size": request_size, "total_response_size": response_size},
Nikolay Stanchev's avatar
Nikolay Stanchev committed
         "time": timestamp * 10 ** 9
         } for sfe, location, sfp, sf, sfc, sfci, num_requests, processing_time, request_size, response_size, timestamp in data
    # apache data to report to influx
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    data = [
        ("apache_1_ep1", "DC5", "apache", "apache_1", test_sfc_name, test_sfc_instance_2_name, 15, 1400, 15600, 1528386860),
        ("apache_1_ep1", "DC5", "apache", "apache_1", test_sfc_name, test_sfc_instance_2_name, 17, 2200, 11200, 1528388860),
        ("apache_1_ep1", "DC5", "apache", "apache_1", test_sfc_name, test_sfc_instance_2_name, 19, 700, 5700, 1528410860),
        ("apache_1_ep1", "DC5", "apache", "apache_1", test_sfc_name, test_sfc_instance_2_name, 24, 1900, 4300, 1528412860),
        ("apache_1_ep1", "DC5", "apache", "apache_1", test_sfc_name, test_sfc_instance_2_name, 13, 1200, 2500, 1528414860),
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    ]
    influx.write_points([
        {"measurement": "apache",
         "tags": {"flame_server": location, "flame_sfe": sfe, "flame_location": location, "flame_sfp": sfp, "flame_sf": sf, "flame_sfc": sfc, "flame_sfci": sfci},
         "fields": {"avg_processing_time": processing_time, "avg_request_size": request_size, "avg_response_size": response_size},
Nikolay Stanchev's avatar
Nikolay Stanchev committed
         "time": timestamp * 10 ** 9
         } for sfe, location, sfp, sf, sfc, sfci, processing_time, request_size, response_size, timestamp in data
    yield from_timestamp, to_timestamp, graph
Nikolay Stanchev's avatar
Nikolay Stanchev committed

    # clean up after the test is over - delete the test databases and clear up the graph
    influx.drop_database("CLMCMetrics")
    influx.drop_database(test_db_name)
Nikolay Stanchev's avatar
Nikolay Stanchev committed
    graph.delete_all()