'Test _fill_tpl with mocked docker client'

@mock.patch('config.get_auto_confd_path', return_value=os.path.join(os.path.dirname(__file__), 'fixtures/auto_conf/'))
@mock.patch('utils.dockerutil.DockerUtil.client', return_value=None)
@mock.patch.object(EtcdStore, 'get_client', return_value=None)
@mock.patch.object(ConsulStore, 'get_client', return_value=None)
def test_fill_tpl(self, *args):

    valid_configs = [(({}, {'host': 'localhost'}, [], None), ({'host': 'localhost'}, {})), (({'NetworkSettings': {'IPAddress': ''}}, {'host': 'localhost'}, [], None), ({'host': 'localhost'}, {})), (({'NetworkSettings': {'Networks': {}}}, {'host': 'localhost'}, [], None), ({'host': 'localhost'}, {})), (({'NetworkSettings': {'Networks': {'bridge': {}}}}, {'host': 'localhost'}, [], None), ({'host': 'localhost'}, {})), (({'NetworkSettings': {'IPAddress': '127.0.0.1'}}, {'host': '%%host%%', 'port': 1337}, ['host'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host': '127.0.0.1'})), (({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Networks': {}}}, {'host': '%%host%%', 'port': 1337}, ['host'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host': '127.0.0.1'})), (({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Networks': {'bridge': {'IPAddress': '172.17.0.2'}}}}, {'host': '%%host%%', 'port': 1337}, ['host'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host': '172.17.0.2'})), (({'NetworkSettings': {'IPAddress': '', 'Networks': {'bridge': {'IPAddress': '172.17.0.2'}, 'foo': {'IPAddress': '192.168.0.2'}}}}, {'host': '%%host_bridge%%', 'port': 1337}, ['host_bridge'], ['foo', 'bar:baz']), ({'host': '%%host_bridge%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host_bridge': '172.17.0.2'})), (({'NetworkSettings': {'IPAddress': '', 'Networks': {'bridge': {'IPAddress': '172.17.0.2'}, 'foo': {'IPAddress': '192.168.0.2'}}}}, {'host': '%%host_foo%%', 'port': 1337}, ['host_foo'], ['foo', 'bar:baz']), ({'host': '%%host_foo%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host_foo': '192.168.0.2'})), (({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Ports': {'42/tcp': None, '22/tcp': None}}}, {'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test']}, ['host', 'port_1'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']}, {'host': '127.0.0.1', 'port_1': '42'})), (({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Ports': {'42/tcp': None, '22/tcp': None}}}, {'host': '%%host%%', 'port': '%%port_1%%', 'tags': {'env': 'test'}}, ['host', 'port_1'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']}, {'host': '127.0.0.1', 'port_1': '42'}))]
    edge_cases = [(({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Networks': {'bridge': {'IPAddress': '172.17.0.2'}}}}, {'host': '%%host_bridge%%', 'port': 1337}, ['host_bridge'], ['foo', 'bar:baz']), ({'host': '%%host_bridge%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host_bridge': '172.17.0.2'})), (({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Networks': {'bridge': {'IPAddress': '172.17.0.2'}}}}, {'host': '%%host_0%%', 'port': 1337}, ['host_0'], ['foo', 'bar:baz']), ({'host': '%%host_0%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host_0': '172.17.0.2'})), (({'NetworkSettings': {'Networks': {'bridge': {'IPAddress': '127.0.0.1'}, 'foo': {'IPAddress': '172.17.0.2'}}}}, {'host': '%%host_bar%%', 'port': 1337}, ['host_bar'], []), ({'host': '%%host_bar%%', 'port': 1337}, {'host_bar': '127.0.0.1'})), (({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Ports': {'42/tcp': None, '22/tcp': None}}}, {'host': '%%host%%', 'port': '%%port_2%%', 'tags': ['env:test']}, ['host', 'port_2'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': '%%port_2%%', 'tags': ['env:test', 'foo', 'bar:baz']}, {'host': '127.0.0.1', 'port_2': '42'}))]
    invalid_config = [(({'NetworkSettings': {'Networks': {}}}, {'host': '%%host%%', 'port': 1337}, ['host'], ['foo', 'bar:baz']), Exception), (({'NetworkSettings': {'Networks': {}}}, {'host': '%%host_0%%', 'port': 1337}, ['host_0'], ['foo', 'bar:baz']), Exception), (({'NetworkSettings': {'Networks': {}}}, {'host': '%%host_foo%%', 'port': 1337}, ['host_foo'], ['foo', 'bar:baz']), Exception), (({'NetworkSettings': {'Networks': {}}}, {'host': 'localhost', 'port': '%%port%%'}, ['port'], []), Exception), (({'NetworkSettings': {'Networks': {}}}, {'host': 'localhost', 'port_0': '%%port%%'}, ['port_0'], []), Exception), (({'NetworkSettings': {'Networks': {}}}, {'host': 'localhost', 'port': '%%port_foo%%'}, ['port_foo'], []), Exception)]
    for ac in self.agentConfigs:
        sd_backend = get_sd_backend(agentConfig=ac)
        try:
            for co in (valid_configs + edge_cases):
                (inspect, tpl, variables, tags) = co[0]
                state = _SDDockerBackendConfigFetchState((lambda _: inspect))
                (instance_tpl, var_values) = sd_backend._fill_tpl(state, 'c_id', tpl, variables, tags)
                for key in instance_tpl.keys():
                    if isinstance(instance_tpl[key], list):
                        self.assertEquals(len(instance_tpl[key]), len(co[1][0].get(key)))
                        for elem in instance_tpl[key]:
                            self.assertTrue((elem in co[1][0].get(key)))
                    else:
                        self.assertEquals(instance_tpl[key], co[1][0].get(key))
                self.assertEquals(var_values, co[1][1])
            for co in invalid_config:
                (inspect, tpl, variables, tags) = co[0]
                state = _SDDockerBackendConfigFetchState((lambda _: inspect))
                self.assertRaises(co[1], sd_backend._fill_tpl(state, 'c_id', tpl, variables, tags))
        finally:
            clear_singletons(ac)
