# Path: servi/config.py
# def deep_update(orig_dict, new_dict):
#     # Given a new dict, update the orig dict deeply
#     # eg: {a: {a1: 1}} , {a: {a2: 2}} --> {a: {a1: 1, a2:2 }}
#     # Note - this means new_dict  >= orig_dict
#     # http://stackoverflow.com/a/3233356/1400991
#     for key, val in new_dict.items():
#         if isinstance(val, collections.Mapping):
#             tmp = deep_update(orig_dict.get(key, {}), val)
#             orig_dict[key] = tmp
#         else:
#             orig_dict[key] = new_dict[key]
#     return orig_dict
# 
# Path: servi/exceptions.py
# class ServiError(Exception):
#     # Expected errrors. Handle gracefully (ie: no stack dump)
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             '\n**Servi Aborting**'.format(self.msg))


from pprint import pprint
from jinja2 import Environment, DictLoader
from tests.fixtures import *
from servi.config import deep_update
from servi.exceptions import ServiError
import servi.config as c
  - "string1"
  - "string2"
MAIN_RSA_KEY_FILE: "{{ lookup('env', 'SERVI_TEST') }}"

"""


def test_process_config(setup_init):
    data = c.process_config(TEST_Servifile)

    print('loaded data: \n')
    pprint(data)
    assert data['HOST_NAME'] == 'servi_server'
    assert data['SERVI_IGNORE_FILES'] == ['string1', 'string2']
    assert data['MAIN_RSA_KEY_FILE'] == '12345'

    data = c.getconfig(c.SERVIFILE, c.TEMPLATE, c.MASTER, c.TMPL_DIR_SITE,
                       c.MASTER_DIR)
    print('Actual servifile:')
    pprint(data)
    assert c.load_user_config()


def test_deep_update():
    assert (deep_update({"a": {"a1": 1}}, {"a": {"a2": 2}}) ==
            {"a": {"a1": 1, "a2": 2}})

    assert (deep_update({"a": {"a1": 1}}, {"b": {"b1": 1}}) ==
            {"a": {"a1": 1}, "b": {"b1": 1}})


# Path: servi/config.py
# def deep_update(orig_dict, new_dict):
#     # Given a new dict, update the orig dict deeply
#     # eg: {a: {a1: 1}} , {a: {a2: 2}} --> {a: {a1: 1, a2:2 }}
#     # Note - this means new_dict  >= orig_dict
#     # http://stackoverflow.com/a/3233356/1400991
#     for key, val in new_dict.items():
#         if isinstance(val, collections.Mapping):
#             tmp = deep_update(orig_dict.get(key, {}), val)
#             orig_dict[key] = tmp
#         else:
#             orig_dict[key] = new_dict[key]
#     return orig_dict
# 
# Path: servi/exceptions.py
# class ServiError(Exception):
#     # Expected errrors. Handle gracefully (ie: no stack dump)
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             '\n**Servi Aborting**'.format(self.msg))


# Path: tests/test_config.py
from pprint import pprint
from jinja2 import Environment, DictLoader
from tests.fixtures import *
from servi.config import deep_update
from servi.exceptions import ServiError
import servi.config as c

  - "string1"
  - "string2"
MAIN_RSA_KEY_FILE: "{{ lookup('env', 'SERVI_TEST') }}"

"""


def test_process_config(setup_init):
    data = c.process_config(TEST_Servifile)

    print('loaded data: \n')
    pprint(data)
    assert data['HOST_NAME'] == 'servi_server'
    assert data['SERVI_IGNORE_FILES'] == ['string1', 'string2']
    assert data['MAIN_RSA_KEY_FILE'] == '12345'

    data = c.getconfig(c.SERVIFILE, c.TEMPLATE, c.MASTER, c.TMPL_DIR_SITE,
                       c.MASTER_DIR)
    print('Actual servifile:')
    pprint(data)
    assert c.load_user_config()


def test_deep_update():
    assert (deep_update({"a": {"a1": 1}}, {"a": {"a2": 2}}) ==
            {"a": {"a1": 1, "a2": 2}})

    assert (deep_update({"a": {"a1": 1}}, {"b": {"b1": 1}}) ==
            {"a": {"a1": 1}, "b": {"b1": 1}})


    assert (deep_update({}, {"b": {"b1": 1}}) ==