#!/usr/bin/env python3

"""Test config parsing"""

import logging
import os
import shutil
import tempfile
import unittest
from faucet import config_parser as cp

LOGNAME = '/dev/null'


class TestGaugeConfig(unittest.TestCase):
    """Test gauge.yaml config parsing."""

    DEFAULT_FAUCET_CONFIG = """
dps:
    dp1:
        dp_id: 1
        interfaces:
            1:
                native_vlan: v1
    dp2:
        dp_id: 2
        interfaces:
            1:
                native_vlan: v1
vlans:
    v1:
        vid: 1
"""

    GAUGE_CONFIG_HEADER = """
faucet_configs:
    - '{}'
"""

    def setUp(self):
        logging.disable(logging.CRITICAL)
        self.tmpdir = tempfile.mkdtemp()

    def tearDown(self):
        logging.disable(logging.NOTSET)
        shutil.rmtree(self.tmpdir)

    def conf_file_name(self, faucet=False):
        if faucet:
            return os.path.join(self.tmpdir, 'faucet.yaml')
        else:
            return os.path.join(self.tmpdir, 'gauge.yaml')

    def create_config_files(self, config, faucet_config=None):
        """Returns file path to file containing the config parameter."""
        gauge_file_name = self.conf_file_name()
        faucet_file_name = self.conf_file_name(faucet=True)
        with open(gauge_file_name, 'w') as conf_file:
            conf_file.write(config.format(faucet_file_name))
        with open(faucet_file_name, 'w') as conf_file:
            if faucet_config:
                conf_file.write(faucet_config)
            else:
                conf_file.write(self.DEFAULT_FAUCET_CONFIG)
        return (gauge_file_name, faucet_file_name)

    def get_config(self, conf_suffix):
        return self.GAUGE_CONFIG_HEADER + conf_suffix


    def test_all_dps(self):
        GAUGE_CONF = """
watchers:
    port_stats_poller:
        type: 'port_stats'
        all_dps: True
        interval: 10
        db: 'prometheus'
dbs:
    prometheus:
        type: 'prometheus'
"""
        conf = self.get_config(GAUGE_CONF)
        gauge_file, faucet_file = self.create_config_files(conf)
        watcher_confs = cp.watcher_parser(gauge_file, 'gauge_config_test', None)
        self.assertEqual(len(watcher_confs), 2, 'failed to create config for each dp')
        for watcher_conf in watcher_confs:
            msg = 'all_dps config not applied to each dp'
            self.assertEqual(watcher_conf.type, 'port_stats', msg)
            self.assertEqual(watcher_conf.interval, 10, msg)
            self.assertEqual(watcher_conf.db_type, 'prometheus', msg)

if __name__ == "__main__":
    unittest.main()

Add unit tests for gauge config
Add unit tests for gauge config

    Add test coverage for https://github.com/faucetsdn/faucet/issues/1441
