# Path: canaryd/plugin.py
# def get_plugin_by_name(plugin_name):
#     if plugin_name not in NAME_TO_PLUGIN:
#         raise NoPluginError('Missing plugin: {0}'.format(plugin_name))
# 
#     return NAME_TO_PLUGIN[plugin_name]
# 
# Path: canaryd/settings.py
# class CanarydSettings(object):
#     api_base = API_BASE
#     api_version = API_VERSION
# 
#     log_file = None
#     # Rotate logs by this size (in bytes!) or a TimedRotatingFileHandler "when"
#     log_file_rotation = None
#     # Number of files to rotate
#     log_file_rotation_count = 5
# 
#     # syslog facility to log to
#     syslog_facility = None
# 
#     debug = False
# 
#     collect_interval_s = 30
#     # Only collect slow plugin data this often
#     slow_collect_interval_s = 900
# 
#     # API access key
#     api_key = API_KEY
# 
#     # ID of (hopefully this) server attached to this key
#     server_id = SERVER_ID
# 
#     def __init__(self, **kwargs):
#         self.update(kwargs)
# 
#         self.plugin_settings = defaultdict(dict)
# 
#         # If no log file specified, we're root and /var/log exists, use that
#         if (
#             self.log_file is None
#             and geteuid() <= 0
#             and path.exists(path.join('/', 'var', 'log'))
#         ):
#             logger.debug('Root user, so setting log file to /var/log/canaryd.log')
#             self.log_file = path.join('/', 'var', 'log', 'canaryd.log')
# 
#     def __getattr__(self, key):
#         try:
#             return super(CanarydSettings, self).__getattr__(key)
#         except AttributeError:
#             pass
# 
#     def to_dict(self):
#         return dict(
#             (key, value)
#             for key, value in self.__dict__.items()
#             if key != 'plugin_settings'
#         )
# 
#     def update(self, data):
#         changed_keys = []
# 
#         for key, value in six.iteritems(data):
#             if key == 'plugin_settings':
#                 raise ValueError('Cannot update plugin_settings directly!')
# 
#             if getattr(self, key, None) != value:
#                 setattr(self, key, value)
#                 changed_keys.append(key)
# 
#         logger.debug('Settings updated: {0} <= {1}'.format(changed_keys, data))
#         return changed_keys
# 
#     def update_plugin_settings(self, plugin_name, data):
#         self.plugin_settings[plugin_name].update(data)
#         logger.debug('Plugin settings updated: {0} <= {1}'.format(plugin_name, data))
# 
#     def get_plugin_settings(self, plugin_name):
#         return self.plugin_settings.get(plugin_name, {})


from contextlib import contextmanager
from os import path
from unittest import TestCase
from dictdiffer import diff
from jsontest import JsonTest
from mock import patch
from canaryd_packages import six
from canaryd.plugin import get_plugin_by_name
from canaryd.settings import CanarydSettings


@six.add_metaclass(JsonTest)
class TestPluginStates(TestCase):
    jsontest_files = path.join('tests/plugins')
    test_settings = CanarydSettings()

    @contextmanager
    def patch_commands(self, commands):
        def handle_command(command, *args, **kwargs):
            command = command[0]

            if command not in commands:
                raise ValueError('Broken tests: {0} not in commands: {1}'.format(
                    command, commands.keys(),
                ))

            return '\n'.join(commands[command])

        check_output_patch = patch(
            'canaryd.subprocess.check_output',
            handle_command,
        )
        check_output_patch.start()

        yield

        check_output_patch.stop()

    def jsontest_function(self, test_name, test_data):

# Path: canaryd/plugin.py
# def get_plugin_by_name(plugin_name):
#     if plugin_name not in NAME_TO_PLUGIN:
#         raise NoPluginError('Missing plugin: {0}'.format(plugin_name))
# 
#     return NAME_TO_PLUGIN[plugin_name]
# 
# Path: canaryd/settings.py
# class CanarydSettings(object):
#     api_base = API_BASE
#     api_version = API_VERSION
# 
#     log_file = None
#     # Rotate logs by this size (in bytes!) or a TimedRotatingFileHandler "when"
#     log_file_rotation = None
#     # Number of files to rotate
#     log_file_rotation_count = 5
# 
#     # syslog facility to log to
#     syslog_facility = None
# 
#     debug = False
# 
#     collect_interval_s = 30
#     # Only collect slow plugin data this often
#     slow_collect_interval_s = 900
# 
#     # API access key
#     api_key = API_KEY
# 
#     # ID of (hopefully this) server attached to this key
#     server_id = SERVER_ID
# 
#     def __init__(self, **kwargs):
#         self.update(kwargs)
# 
#         self.plugin_settings = defaultdict(dict)
# 
#         # If no log file specified, we're root and /var/log exists, use that
#         if (
#             self.log_file is None
#             and geteuid() <= 0
#             and path.exists(path.join('/', 'var', 'log'))
#         ):
#             logger.debug('Root user, so setting log file to /var/log/canaryd.log')
#             self.log_file = path.join('/', 'var', 'log', 'canaryd.log')
# 
#     def __getattr__(self, key):
#         try:
#             return super(CanarydSettings, self).__getattr__(key)
#         except AttributeError:
#             pass
# 
#     def to_dict(self):
#         return dict(
#             (key, value)
#             for key, value in self.__dict__.items()
#             if key != 'plugin_settings'
#         )
# 
#     def update(self, data):
#         changed_keys = []
# 
#         for key, value in six.iteritems(data):
#             if key == 'plugin_settings':
#                 raise ValueError('Cannot update plugin_settings directly!')
# 
#             if getattr(self, key, None) != value:
#                 setattr(self, key, value)
#                 changed_keys.append(key)
# 
#         logger.debug('Settings updated: {0} <= {1}'.format(changed_keys, data))
#         return changed_keys
# 
#     def update_plugin_settings(self, plugin_name, data):
#         self.plugin_settings[plugin_name].update(data)
#         logger.debug('Plugin settings updated: {0} <= {1}'.format(plugin_name, data))
# 
#     def get_plugin_settings(self, plugin_name):
#         return self.plugin_settings.get(plugin_name, {})


# Path: tests/test_plugin_states.py
from contextlib import contextmanager
from os import path
from unittest import TestCase
from dictdiffer import diff
from jsontest import JsonTest
from mock import patch
from canaryd_packages import six
from canaryd.plugin import get_plugin_by_name
from canaryd.settings import CanarydSettings



@six.add_metaclass(JsonTest)
class TestPluginStates(TestCase):
    jsontest_files = path.join('tests/plugins')
    test_settings = CanarydSettings()

    @contextmanager
    def patch_commands(self, commands):
        def handle_command(command, *args, **kwargs):
            command = command[0]

            if command not in commands:
                raise ValueError('Broken tests: {0} not in commands: {1}'.format(
                    command, commands.keys(),
                ))

            return '\n'.join(commands[command])

        check_output_patch = patch(
            'canaryd.subprocess.check_output',
            handle_command,
        )
        check_output_patch.start()

        yield

        check_output_patch.stop()

    def jsontest_function(self, test_name, test_data):

        plugin = get_plugin_by_name(test_data['plugin'])