def test_create_network(self):
        """
        Create a network then a container, attache one to the other
        And verify everything is OK

        """
        try:
            exec_cmd(['docker', 'stop', 'pytest'])
            exec_cmd(['docker', 'rm', 'pytest'])
            exec_cmd(['docker', 'network', 'prune', '-f'])
        except Exception:
            pass

        if docker_actions.network_exists('nw_pytest'):
            exec_cmd(['docker', 'network', 'rm', 'nw_pytest'])

        cmd = ['docker', 'run', '-d', '--rm', '--name', 'pytest', 'nginx:stable-alpine']
        subprocess.call(cmd)

        self.assertTrue(docker_actions.container_running('pytest'))
        self.assertFalse(docker_actions._container_in_network('pytest', 'pytest'))
        self.assertFalse(docker_actions.network_exists('nw_pytest'))

        network_created = docker_actions.create_network('nw_pytest')
        self.assertNotEqual(False, network_created)
        self.assertIs(str, type(network_created))

        self.assertFalse(docker_actions.create_network('nw_pytest'))
        self.assertTrue(docker_actions.add_container_to_network('pytest', 'nw_pytest'))
        self.assertFalse(docker_actions.add_container_to_network('pytest', 'nw_pytest'))
        self.assertTrue(docker_actions._container_in_network('pytest', 'nw_pytest'))
        exec_cmd(['docker', 'stop', 'pytest'])

        if docker_actions.network_exists('nw_pytest'):
            exec_cmd(['docker', 'network', 'rm', 'nw_pytest'])
    def test_get_container_info(self):
        """
        Start docker compose with another configuration file, then
        extract VM Info

        """
        try:
            exec_cmd(['docker', 'stop', 'test_php'])
            exec_cmd(['docker', 'rm', 'test_php'])
        except Exception:
            pass

        cmd = [
            'stakkr-compose', '-c', base_dir + '/static/config_valid.ini',
            'up', '-d'
        ]
        exec_cmd(cmd)
        numcts, cts = docker_actions.get_running_containers('test')
        self.assertIs(len(cts), 3)
        for ct_id, ct_info in cts.items():
            if ct_info['name'] in ('test_maildev', 'test_portainer'):
                continue

            self.assertTrue('name' in ct_info)
            self.assertTrue('compose_name' in ct_info)
            self.assertTrue('ports' in ct_info)
            self.assertTrue('running' in ct_info)
            self.assertTrue('ip' in ct_info)
            self.assertTrue('image' in ct_info)

            self.assertEqual(ct_info['name'], 'test_php')
            self.assertEqual(ct_info['compose_name'], 'php')
            self.assertTrue(ct_info['running'])
            self.assertEqual(ct_info['ip'][:8], '192.168.')
            self.assertEqual(ct_info['image'], 'edyan/php:7.0')

        self.assertTrue(
            docker_actions._container_in_network('test_php', 'test_stakkr'))
        self.assertTrue(docker_actions.network_exists('test_stakkr'))
        self.assertFalse(
            docker_actions._container_in_network('test_php', 'bridge'))

        cmd = [
            'stakkr-compose', '-c', base_dir + '/static/config_valid.ini',
            'stop'
        ]
        exec_cmd(cmd)
        exec_cmd(['docker', 'stop', 'test_php'])
        exec_cmd(['docker', 'rm', 'test_php'])

        with self.assertRaisesRegex(
                LookupError, 'Container test_php does not seem to exist'):
            docker_actions._container_in_network('test_php', 'bridge')

        exec_cmd(['stakkr', 'stop'])
        exec_cmd(['stakkr', 'dns', 'stop'])
        exec_cmd(['docker', 'network', 'rm', 'test_stakkr'])
        self.assertFalse(docker_actions.network_exists('test_stakkr'))
    def test_get_container_info(self):
        """
        Start docker compose with another configuration fileself.
        Then extract VM Info.
        """

        config = base_dir + '/static/config_valid.ini'

        # Clean
        exec_cmd(['stakkr-compose', '-c', config, 'stop'])
        remove_network('test_stakkr')

        # Start again
        cmd = ['stakkr-compose', '-c', config, 'up', '-d', '--force-recreate']
        exec_cmd(cmd)
        numcts, cts = docker_actions.get_running_containers('test')
        self.assertIs(len(cts), 3)
        for ct_id, ct_info in cts.items():
            if ct_info['name'] in ('test_maildev', 'test_portainer'):
                continue

            self.assertTrue('name' in ct_info)
            self.assertTrue('compose_name' in ct_info)
            self.assertTrue('ports' in ct_info)
            self.assertTrue('running' in ct_info)
            self.assertTrue('ip' in ct_info)
            self.assertTrue('image' in ct_info)

            self.assertEqual(ct_info['name'], 'test_php')
            self.assertEqual(ct_info['compose_name'], 'php')
            self.assertTrue(ct_info['running'])
            self.assertNotEqual(ct_info['ip'][:10], '192.168.23')
            self.assertEqual(ct_info['image'], 'edyan/php:7.2')

        self.assertTrue(
            docker_actions._container_in_network('test_php', 'test_stakkr'))
        self.assertTrue(docker_actions.network_exists('test_stakkr'))
        self.assertFalse(
            docker_actions._container_in_network('test_php', 'bridge'))

        exec_cmd(['stakkr-compose', '-c', config, 'stop'])
        stop_remove_container('test_php')

        with self.assertRaisesRegex(
                LookupError, 'Container test_php does not seem to exist'):
            docker_actions._container_in_network('test_php', 'bridge')

        exec_cmd(['stakkr', 'stop'])
        remove_network('test_stakkr')
        self.assertFalse(docker_actions.network_exists('test_stakkr'))
    def test_get_container_info(self):
        """
        Start docker compose with another configuration file, then
        extract VM Info

        """
        try:
            exec_cmd(['docker', 'stop', 'test_php'])
            exec_cmd(['docker', 'rm', 'test_php'])
        except Exception:
            pass

        cmd = ['stakkr-compose', '-c', base_dir + '/static/config_valid.ini', 'up', '-d']
        exec_cmd(cmd)
        numcts, cts = docker_actions.get_running_containers('test')
        self.assertIs(len(cts), 3)
        for ct_id, ct_info in cts.items():
            if ct_info['name'] in ('test_maildev', 'test_portainer'):
                continue

            self.assertTrue('name' in ct_info)
            self.assertTrue('compose_name' in ct_info)
            self.assertTrue('ports' in ct_info)
            self.assertTrue('running' in ct_info)
            self.assertTrue('ip' in ct_info)
            self.assertTrue('image' in ct_info)

            self.assertEqual(ct_info['name'], 'test_php')
            self.assertEqual(ct_info['compose_name'], 'php')
            self.assertTrue(ct_info['running'])
            self.assertEqual(ct_info['ip'][:8], '192.168.')
            self.assertEqual(ct_info['image'], 'edyan/php:7.0')

        self.assertTrue(docker_actions._container_in_network('test_php', 'test_stakkr'))
        self.assertTrue(docker_actions.network_exists('test_stakkr'))
        self.assertFalse(docker_actions._container_in_network('test_php', 'bridge'))

        cmd = ['stakkr-compose', '-c', base_dir + '/static/config_valid.ini', 'stop']
        exec_cmd(cmd)
        exec_cmd(['docker', 'stop', 'test_php'])
        exec_cmd(['docker', 'rm', 'test_php'])

        with self.assertRaisesRegex(LookupError, 'Container test_php does not seem to exist'):
            docker_actions._container_in_network('test_php', 'bridge')

        exec_cmd(['stakkr', 'stop'])
        exec_cmd(['stakkr', 'dns', 'stop'])
        exec_cmd(['docker', 'network', 'rm', 'test_stakkr'])
        self.assertFalse(docker_actions.network_exists('test_stakkr'))
    def test_get_container_info(self):
        """
        Start docker compose with another configuration fileself.
        Then extract VM Info.
        """

        config = base_dir + '/static/stakkr.yml'

        # Clean
        exec_cmd(['stakkr-compose', '-c', config, 'stop'])
        remove_network('static_stakkr')

        # Start again
        cmd = ['stakkr-compose', '-c', config, 'up', '-d', '--force-recreate']
        exec_cmd(cmd)

        numcts, cts = docker_actions.get_running_containers('static')
        self.assertIs(len(cts), 3)
        for ct_id, ct_info in cts.items():
            if ct_info['name'] in ('static_maildev', 'static_portainer'):
                continue

            self.assertTrue('name' in ct_info)
            self.assertTrue('compose_name' in ct_info)
            self.assertTrue('ports' in ct_info)
            self.assertTrue('running' in ct_info)
            self.assertTrue('ip' in ct_info)
            self.assertTrue('image' in ct_info)

            self.assertEqual(ct_info['name'], 'static_php')
            self.assertEqual(ct_info['compose_name'], 'php')
            self.assertTrue(ct_info['running'])
            self.assertNotEqual(ct_info['ip'][:10], '192.168.23')
            self.assertEqual(ct_info['image'], 'edyan/php:7.2')

        self.assertTrue(docker_actions._container_in_network('static_php', 'static_stakkr'))
        self.assertTrue(docker_actions.network_exists('static_stakkr'))
        self.assertFalse(docker_actions._container_in_network('static_php', 'bridge'))

        exec_cmd(['stakkr-compose', '-c', config, 'stop'])
        stop_remove_container('static_php')

        with self.assertRaisesRegex(LookupError, 'Container static_php does not seem to exist'):
            docker_actions._container_in_network('static_php', 'bridge')

        exec_cmd(['stakkr', 'stop'])
        remove_network('static_stakkr')
        self.assertFalse(docker_actions.network_exists('static_stakkr'))
    def test_create_network(self):
        """
        Create a network then a container, attache one to the other
        And verify everything is OK

        """
        stop_remove_container('pytest')

        docker_actions.get_client().containers.run(
            'edyan/adminer:latest', detach=True, name='pytest')

        self.assertTrue(docker_actions.container_running('pytest'))
        self.assertFalse(docker_actions._container_in_network('pytest', 'pytest'))
        self.assertFalse(docker_actions.network_exists('nw_pytest'))

        network_created = docker_actions.create_network('nw_pytest')
        self.assertNotEqual(False, network_created)
        self.assertIs(str, type(network_created))

        self.assertFalse(docker_actions.create_network('nw_pytest'))
        self.assertTrue(docker_actions.add_container_to_network('pytest', 'nw_pytest'))
        self.assertFalse(docker_actions.add_container_to_network('pytest', 'nw_pytest'))
        self.assertTrue(docker_actions._container_in_network('pytest', 'nw_pytest'))

        stop_remove_container('pytest')
        remove_network('nw_pytest')
    def test_create_network(self):
        """
        Create a network then a container, attache one to the other
        And verify everything is OK

        """
        try:
            exec_cmd(['docker', 'stop', 'pytest'])
            exec_cmd(['docker', 'rm', 'pytest'])
            exec_cmd(['docker', 'network', 'prune', '-f'])
        except Exception:
            pass

        if docker_actions.network_exists('nw_pytest'):
            exec_cmd(['docker', 'network', 'rm', 'nw_pytest'])

        cmd = [
            'docker', 'run', '-d', '--rm', '--name', 'pytest',
            'nginx:stable-alpine'
        ]
        subprocess.call(cmd)

        self.assertTrue(docker_actions.container_running('pytest'))
        self.assertFalse(
            docker_actions._container_in_network('pytest', 'pytest'))
        self.assertFalse(docker_actions.network_exists('nw_pytest'))

        network_created = docker_actions.create_network('nw_pytest')
        self.assertNotEqual(False, network_created)
        self.assertIs(str, type(network_created))

        self.assertFalse(docker_actions.create_network('nw_pytest'))
        self.assertTrue(
            docker_actions.add_container_to_network('pytest', 'nw_pytest'))
        self.assertFalse(
            docker_actions.add_container_to_network('pytest', 'nw_pytest'))
        self.assertTrue(
            docker_actions._container_in_network('pytest', 'nw_pytest'))
        exec_cmd(['docker', 'stop', 'pytest'])

        if docker_actions.network_exists('nw_pytest'):
            exec_cmd(['docker', 'network', 'rm', 'nw_pytest'])