Esempio n. 1
0
def test_model(app):
    """
    Test whether the model field is correct
    :param app: test Flask(__name__)
    """
    model_init_app(app)

    # insert data
    cpu = psutil.cpu_percent(interval=config.Constant.CPU_PERCENT_INTERVAL)
    memory = psutil.virtual_memory().percent
    load_avg = ','.join([str(float('%.2f' % x)) for x in os.getloadavg()])
    disk_usage = psutil.disk_usage('/').percent
    boot_ts = psutil.boot_time()
    result_conf = {
        'ts': date.get_current_ms(),
        'cpu': cpu,
        'memory': memory,
        'load_avg': load_avg,
        'disk_usage': disk_usage,
        'boot_seconds': int(date.get_current_s() - boot_ts)
    }
    with app.app_context():
        host_status.create_host_status(result_conf)

    # query data
    with app.app_context():
        result = host_status.retrieve_host_status(1)[0]
        assert isinstance(result, tuple)

    # clear data
    with app.app_context():
        result = host_status.FlaskStateHost.query.delete(
            synchronize_session=False)
        if result:
            host_status.db.session.commit()
Esempio n. 2
0
def test_init_app(app):
    model_init_app(app)
    app.add_url_rule('/v0/state/hoststatus',
                     endpoint='state_host_status',
                     view_func=manager.query_flask_state,
                     methods=[config.HttpMethod.POST.value])
    c = app.test_client()
    response = c.post('/v0/state/hoststatus',
                      data=json.dumps({'timeQuantum': 1}),
                      content_type='application/json')
    assert 200 == response.status_code
Esempio n. 3
0
def test_init_app(app):
    model_init_app(app)
    app.add_url_rule(
        "/v0/state/hoststatus",
        endpoint="state_host_status",
        view_func=manager.query_flask_state,
        methods=[constants.HttpMethod.POST.value],
    )
    c = app.test_client()
    response = c.post(
        "/v0/state/hoststatus",
        data=json.dumps({"timeQuantum": 1}),
        content_type="application/json",
    )
    assert response.status_code == 200
Esempio n. 4
0
def test_clear_expire_data(app):
    """
    Test deleting expired data
    :param app: test Flask(__name__)
    """
    model_init_app(app)
    expire_data_length = 5
    thirty_day = 30

    # insert expire data
    for _ in range(expire_data_length):
        cpu = psutil.cpu_percent(interval=config.Config.CPU_PERCENT_INTERVAL)
        memory = psutil.virtual_memory().percent
        try:
            load_avg = ",".join(
                [str(float("%.2f" % x)) for x in os.getloadavg()])
        except AttributeError:
            load_avg = "0, 0, 0"
        disk_usage = psutil.disk_usage("/").percent
        boot_ts = psutil.boot_time()
        result_conf = {
            "ts": 0,
            "cpu": cpu,
            "memory": memory,
            "load_avg": load_avg,
            "disk_usage": disk_usage,
            "boot_seconds": int(date.get_current_s() - boot_ts),
        }
        with app.app_context():
            host_status.create_host_status(result_conf)

    # clear expire data
    with app.app_context():
        target_time = date.get_current_ms() - date.get_query_ms(thirty_day)
        result = host_status.FlaskStateHost.query.filter(
            host_status.FlaskStateHost.ts < target_time).delete(
                synchronize_session=False)
        assert result == expire_data_length
        if result:
            host_status.db.session.commit()
Esempio n. 5
0
def test_query_flask_state_host(app):
    """
    Tests whether the query data returns to the specified format
    """
    model_init_app(app)
    test_right_day = [1, 3, 7, 30]
    test_error_day = [5, 10, 31, 100]
    with app.app_context():
        for day in test_right_day:
            response_content = host_status.query_flask_state_host(day)
            assert 200 == response_content.get_code()
            assert 'Search success' == response_content.get_msg()
            assert isinstance(response_content.data.get('currentStatistic'),
                              dict)
            assert isinstance(response_content.data.get('items'), list)
            assert isinstance(response_content, SuccessResponse)

        for day in test_error_day:
            response_content = host_status.query_flask_state_host(day)
            assert isinstance(response_content.get_code(), int)
            assert isinstance(response_content.get_msg(), str)
            assert isinstance(response_content, ErrorResponse)
Esempio n. 6
0
def test_model(app):
    """
    Test whether the model field is correct
    :param app: test Flask(__name__)
    """
    model_init_app(app)

    # insert data
    cpu = psutil.cpu_percent(interval=config.Config.CPU_PERCENT_INTERVAL)
    memory = psutil.virtual_memory().percent
    try:
        load_avg = ",".join([str(float("%.2f" % x)) for x in os.getloadavg()])
    except AttributeError:
        load_avg = "0, 0, 0"
    disk_usage = psutil.disk_usage("/").percent
    boot_ts = psutil.boot_time()
    result_conf = {
        "ts": date.get_current_ms(),
        "cpu": cpu,
        "memory": memory,
        "load_avg": load_avg,
        "disk_usage": disk_usage,
        "boot_seconds": int(date.get_current_s() - boot_ts),
    }
    with app.app_context():
        host_status.create_host_status(result_conf)

    # query data
    with app.app_context():
        result = host_status.retrieve_host_status(1)[0]
        assert result is not None

    # clear data
    with app.app_context():
        result = host_status.FlaskStateHost.query.delete(
            synchronize_session=False)
        if result:
            host_status.db.session.commit()
Esempio n. 7
0
def test_query_flask_state_host(app):
    """
    Tests whether the query data returns to the specified format
    """
    model_init_app(app)
    test_right_day = [1, 3, 7, 30]
    test_error_day = [100, "hello"]
    with app.app_context():
        for day in test_right_day:
            response_content = host_status.query_flask_state_host(day)
            assert response_content.get_code() == 200
            assert "Search success" == response_content.get_msg()
            assert isinstance(response_content.data.get("currentStatistic"),
                              dict)
            assert isinstance(response_content.data.get("items"), list)
            assert isinstance(response_content, SuccessResponse)

        for day in test_error_day:
            with raises(FlaskStateError):
                response_content = host_status.query_flask_state_host(day)
                assert isinstance(response_content.get_code(), int)
                assert isinstance(response_content.get_msg(), str)
                assert isinstance(response_content, ErrorResponse)
Esempio n. 8
0
def test_clear_expire_data(app):
    """
    Test deleting expired data
    :param app: test Flask(__name__)
    """
    model_init_app(app)
    expire_data_length = 5
    thirty_day = 30

    # insert expire data
    for i in range(expire_data_length):
        cpu = psutil.cpu_percent(interval=config.Constant.CPU_PERCENT_INTERVAL)
        memory = psutil.virtual_memory().percent
        load_avg = ','.join([str(float('%.2f' % x)) for x in os.getloadavg()])
        disk_usage = psutil.disk_usage('/').percent
        boot_ts = psutil.boot_time()
        result_conf = {
            'ts': 0,
            'cpu': cpu,
            'memory': memory,
            'load_avg': load_avg,
            'disk_usage': disk_usage,
            'boot_seconds': int(date.get_current_s() - boot_ts)
        }
        with app.app_context():
            host_status.create_host_status(result_conf)

    # clear expire data
    with app.app_context():
        target_time = date.get_current_ms() - date.get_query_ms(thirty_day)
        result = host_status.FlaskStateHost.query.filter(
            host_status.FlaskStateHost.ts < target_time).delete(
                synchronize_session=False)
        assert result == expire_data_length
        if result:
            host_status.db.session.commit()