Esempio n. 1
0
async def test_app_metrics(aiohttp_client, loop):
    parser = get_parser()
    config = Config(parser, args=[])
    app = make_app(config)

    client = await aiohttp_client(app)

    resp = await client.get('/metrics')

    assert resp.status == 200

    text = await resp.text()
    lines = [line for line in text.split('\n') if not line.startswith('#')]
    text = "\n".join(lines)

    assert len(lines) == 80

    assert 'promsd_request_count' in text
    assert 'promsd_build_count' in text
    assert 'promsd_event_count' in text
    assert 'promsd_reinit_count' in text
    assert 'promsd_errors_count' in text
    assert 'promsd_build_seconds' in text
    assert 'promsd_config_size_bytes' in text
    assert 'promsd_configs_units' in text
async def test_save_configs():
    scrape_config = [
        {
            "labels": {
                "job": "job1",
            },
            "targets": [
                "example.com",
                "test.example.com",
            ]
        },
    ]

    parser = get_parser()
    file_out = '/tmp/services.json'
    config = Config(parser, args=['--out', file_out])

    await save_configs(config, scrape_config)

    async with AIOFile(file_out, 'r') as fin:
        data = await fin.read()
        obj = json.loads(data)

        assert len(obj) == len(scrape_config)
        assert obj[0]['labels']['job'] == scrape_config[0]['labels']['job']
def test_config_metrics_config():
    parser = get_parser()

    args = [
        '--out', 'text.json', '--metrics', '--metrics.path', '/met',
        '--metrics.host', '0.0.0.0', '--metrics.port', '9191'
    ]

    config = Config(parser, args, DockerClientMock)
    config.init()

    assert config.options.metrics == True
    assert config.options.metrics_path == '/met'
    assert config.options.metrics_host == '0.0.0.0'
    assert config.options.metrics_port == 9191

    args = [
        '--out',
        'text.json',
    ]

    config = Config(parser, args, DockerClientMock)
    config.init()

    assert config.options.metrics == False
    assert config.options.metrics_path == '/metrics'
    assert config.options.metrics_host == 'localhost'
    assert config.options.metrics_port == 9090
def test_config_minimal_self_init():
    parser = get_parser()
    config = Config(parser, ['--out', 'text.json'], DockerClientMock)

    assert config.validate() == True
    assert config.get_client().url == docker_url
    assert config.inited == True
def test_config_log_level_base():
    parser = get_parser()
    args = [
        '--out',
        'text.json',
    ]

    config = Config(parser, args, DockerClientMock)
    setup_logging(config)
    assert isinstance(config.log_handler, logging.StreamHandler)
async def test_config_deinit():
    success = True
    parser = get_parser()
    file_out = '/tmp/services.json'
    config = Config(parser, args=['--out', file_out])

    config.init()

    assert config.inited == True

    await config.deinit()

    assert config.inited == False
Esempio n. 7
0
async def test_server_metrics(aiohttp_client, loop):
    parser = get_parser()

    args = ['--out', 'text.json', '--metrics', '--metrics.host', '0.0.0.0']

    config = Config(parser, args, DockerClientMock)

    assert config.options.metrics == True

    webserver = loop.create_task(make_server(config))

    async with aiohttp.ClientSession() as session:
        async with session.get('http://localhost:9090/metrics') as resp:
            assert resp.status == 200
async def test_build_image():
    docker = aiodocker.Docker()
    await docker.swarm.init()

    with tarfile.open('context.tar.gz', 'w:gz') as fout:
        for name in os.listdir('.'):
            fout.add(name)

    with open('context.tar.gz', 'rb') as context:
        image = await docker.images.build(
            fileobj=context,
            tag="promsd:latest",
            encoding="gzip",
        )

        assert image is not None

    image_name = 'promsd:latest'

    service_config = {
        'ContainerSpec': {
            'Image':
            image_name,
            'Args': ['--out', '/tmp/test.json'],
            'Mounts': [{
                'Type': 'bind',
                'Source': '/var/run/docker.sock',
                'Target': '/var/run/docker.sock'
            }]
        }
    }

    labels = {
        "prometheus.enable": "true",
        "prometheus.jobs.main.port": "9090",
    }

    service = await docker.services.create(task_template=service_config,
                                           name="promsd-service",
                                           labels=labels)

    parser = get_parser()
    config = Config(parser, args=['--out', '/tmp/services.json'])

    # Could be done in a better way like waiting for service to be up
    await asyncio.sleep(10)

    service_inspect = await docker.services.inspect(service['ID'])

    configs = await load_service_configs(config, service_inspect)

    assert configs is not None

    configs2 = await load_existing_services(config)

    assert len(configs) == len(configs2)
    assert configs[0]['labels']['job'] == 'main'

    labels2 = labels.copy()
    labels2['prometheus.enable'] = 'false'

    service2 = await docker.services.create(task_template=service_config,
                                            name="promsd-service2",
                                            labels=labels2)

    configs3 = await load_existing_services(config)
    assert len(configs3) == 1

    config = Config(parser,
                    args=[
                        '--out', '/tmp/services.json', '--meta-labels',
                        '--service-labels'
                    ])

    configs3 = await load_existing_services(config)
    assert len(configs3) == 1

    assert '__meta_docker_service_label_prometheus_enable' in configs3[0][
        'labels']

    # load all possible labels
    config = Config(parser,
                    args=[
                        '--out', '/tmp/services.json', '--meta-labels',
                        '--service-labels', '--task-labels',
                        '--container-labels'
                    ])

    configs3 = await load_existing_services(config)
    assert len(configs3) == 1
    assert '__meta_docker_service_label_prometheus_enable' in configs3[0][
        'labels']
    assert '__meta_docker_container_label_com_docker_swarm_node_id' in configs3[
        0]['labels']
    # TODO add test for task labels, those are usually empty

    services = await docker.services.list()
    assert len(services) > 0
    await docker.services.delete(service['ID'])

    await docker.swarm.leave(force=True)
    await docker.close()
def test_config_no_args():
    parser = get_parser()
    config = Config(parser, [], DockerClientMock)

    assert config.validate() == False