import mock
import argparse
from unittest import TestCase
from devour import exceptions
from devour.bin.devour_commands import parse_args
from devour.validators import CONFIG_VALIDATOR
from devour.utils.helpers import validate_config

class TestScript(TestCase):
    def test_parse_args_success(self):
        args = ['consume', 'test']
        ret = parse_args(args)
        self.assertEqual(ret.method, args[0])
        self.assertEqual(ret.consumer_name, args[1])

    def test_parse_args_failure(self):
        arg_map = {}

        self.assertRaises(
            SystemExit,
            parse_args,
            arg_map.values()
        )

class TestConfigValidation(TestCase):
    def test_hosts(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport'
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['hosts'] = 1
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

        # missing / empty
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

        args_dict['hosts'] = None
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_zookeeper_hosts(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'zookeeper_hosts':'fakehost:fakeport'
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['zookeeper_hosts'] = 1
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_socket_timeout_ms(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'socket_timeout_ms': 1
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['socket_timeout_ms'] = 'invalid'
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_offsets_channel_socket_timeout_ms(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'offsets_channel_socket_timeout_ms': 1
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['offsets_channel_socket_timeout_ms'] = 'invalid'
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_use_greenlets(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'use_greenlets': True
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['use_greenlets'] = 'invalid'
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_exclude_internal_topics(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'exclude_internal_topics': True
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['exclude_internal_topics'] = 'invalid'
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_source_address(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'source_address': 'fakehost:fakeport'
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['source_address'] = 1
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

    def test_broker_version(self):
        # valid
        args_dict = {
            'hosts':'fakehost:fakeport',
            'broker_version': '0.8'
        }

        ret = validate_config(CONFIG_VALIDATOR, args_dict)
        self.assertTrue(ret)

        # invalid
        args_dict['broker_version'] = 1
        self.assertRaises(
            exceptions.DevourConfigException,
            validate_config,
            CONFIG_VALIDATOR,
            args_dict
        )

@@ -4915,28 +4915,875 @@
         args_dict%0A        )%0A
+%0A    def test_ssl_config(self):%0A        #valid%0A        args_dict = %7B%0A            'hosts':'fakehost:fakeport',%0A            'ssl_config': %7B%0A                'cafile': './fake/path/',%0A                'certfile': './fake/path/',%0A                'keyfile': './fake/path/',%0A                'password': './fake/path/'%0A            %7D%0A        %7D%0A%0A        ret = validate_config(CONFIG_VALIDATOR, args_dict)%0A        self.assertTrue(ret)%0A%0A        args_dict%5B'ssl_config'%5D%5B'certfile'%5D = 1%0A        self.assertRaises(%0A            exceptions.DevourConfigException,%0A            validate_config,%0A            CONFIG_VALIDATOR,%0A            args_dict%0A        )%0A%0A        del args_dict%5B'ssl_config'%5D%5B'cafile'%5D%0A        self.assertRaises(%0A            exceptions.DevourConfigException,%0A            validate_config,%0A            CONFIG_VALIDATOR,%0A            args_dict%0A        )%0A

Add test case for ssl config validation