#!/usr/bin/python3

import pytest
import yaml
import requests
import time


@pytest.fixture(scope="module", params=[{'config': {'rspec': '/vagrant/test/streaming/rspec.yml'}}])
def streaming_config(request):
    """
    Reads the service configuration deployed for the streaming simulation test.

    :param request: access the parameters of the fixture
    :return: the python object representing the read YAML file
    """

    with open(request.param['config']['rspec'], 'r') as stream:
        data_loaded = yaml.load(stream)
    return data_loaded


@pytest.fixture(scope="module", autouse=True,
                params=[{'config': {'kapacitor_url': 'http://localhost:8888/chronograf/v1/sources/1/kapacitors', 'kapacitor_file': '/vagrant/test/streaming/kapacitor.json'}}])
def kapacitor_config(request):

    kapacitor_configuration = request.param['config']['kapacitor_file']
    with open(kapacitor_configuration, "r") as rule_file:
        data = "".join(line.strip() for line in rule_file.readlines())

    kapacitor_url = request.param['config']['kapacitor_url']
    requests.post(url=kapacitor_url, data=data, headers={"Content-Type": "application/json"})
    time.sleep(1)