class ComposeTransformerTests(TestCase):

    def setUp(self):
        self.file_name = './container_transform/tests/docker-compose.yml'
        self.transformer = ComposeTransformer(self.file_name)

    @patch.object(uuid, 'uuid4', return_value='2e9c3538-b9d3-4f47-8a23-2a19315b370b')
    def test_emit_containers_no_name(self, mock_uuid):
        """
        Test .emit_containers() for a container without a name
        """
        containers = [{
            'image': 'postgres:9.3',
            'cpu': 200
        }]

        output = self.transformer.emit_containers(containers)

        self.assertEqual(
            (
                'services:\n'
                '  {mock_uuid}:\n'
                '    cpu: 200\n'
                '    image: postgres:9.3\n'
                'version: \'2\'\n'
            ).format(mock_uuid=mock_uuid.return_value),
            output
        )

    def test_emit_mapping(self):
        """
        Test ._emit_mapping()
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'host_port': 8000,
            'container_ip': '127.0.0.1',
            'container_port': 80,
        }

        self.assertEqual(
            self.transformer._emit_mapping(mapping),
            '192.168.59.103:8000:127.0.0.1:80'
        )

    def test_emit_mapping_udp(self):
        """
        Test ._emit_mapping() with udp port
        """
        mapping = {
            'host_port': 53,
            'container_port': 53,
            'protocol': 'udp'
        }
        self.assertEqual(
            self.transformer._emit_mapping(mapping),
            '53:53/udp'
        )

    def test_emit_mapping_missing_ports(self):
        """
        Test ._emit_mapping() missing ports
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'container_ip': '127.0.0.1',
        }

        self.assertEqual(
            self.transformer._emit_mapping(mapping),
            '192.168.59.103:127.0.0.1'
        )

    def test_parse_port_mapping_fails(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '192.168.59.103:8000:127.0.0.1:80:'

        self.assertEqual(
            self.transformer._parse_port_mapping(mapping),
            None
        )

    def test_parse_port_mapping_udp(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '53:53/udp'

        self.assertEqual(
            self.transformer._parse_port_mapping(mapping),
            {
                'host_port': 53,
                'container_port': 53,
                'protocol': 'udp'
            }
        )

    def test_ingest_cpu(self):
        cpu = 100
        self.assertEqual(
            self.transformer.ingest_cpu(cpu),
            cpu
        )

    def test_emit_cpu(self):
        cpu = 100
        self.assertEqual(
            self.transformer.emit_cpu(cpu),
            cpu
        )
Exemple #2
0
class ComposeTransformerTests(TestCase):
    def setUp(self):
        self.file_name = './container_transform/tests/docker-compose.yml'
        self.transformer = ComposeTransformer(self.file_name)

    @patch.object(uuid,
                  'uuid4',
                  return_value='2e9c3538-b9d3-4f47-8a23-2a19315b370b')
    def test_emit_containers_no_name(self, mock_uuid):
        """
        Test .emit_containers() for a container without a name
        """
        containers = [{'image': 'postgres:9.3', 'cpu': 200}]

        output = self.transformer.emit_containers(containers)

        self.assertEqual(
            ('services:\n'
             '  {mock_uuid}:\n'
             '    cpu: 200\n'
             '    image: postgres:9.3\n'
             'version: \'2\'\n').format(mock_uuid=mock_uuid.return_value),
            output)

    def test_emit_mapping(self):
        """
        Test ._emit_mapping()
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'host_port': 8000,
            'container_ip': '127.0.0.1',
            'container_port': 80,
        }

        self.assertEqual(self.transformer._emit_mapping(mapping),
                         '192.168.59.103:8000:127.0.0.1:80')

    def test_emit_mapping_udp(self):
        """
        Test ._emit_mapping() with udp port
        """
        mapping = {'host_port': 53, 'container_port': 53, 'protocol': 'udp'}
        self.assertEqual(self.transformer._emit_mapping(mapping), '53:53/udp')

    def test_emit_mapping_missing_ports(self):
        """
        Test ._emit_mapping() missing ports
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'container_ip': '127.0.0.1',
        }

        self.assertEqual(self.transformer._emit_mapping(mapping),
                         '192.168.59.103:127.0.0.1')

    def test_parse_port_mapping_fails(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '192.168.59.103:8000:127.0.0.1:80:'

        self.assertEqual(self.transformer._parse_port_mapping(mapping), None)

    def test_parse_port_mapping_udp(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '53:53/udp'

        self.assertEqual(self.transformer._parse_port_mapping(mapping), {
            'host_port': 53,
            'container_port': 53,
            'protocol': 'udp'
        })

    def test_ingest_cpu(self):
        cpu = 100
        self.assertEqual(self.transformer.ingest_cpu(cpu), cpu)

    def test_emit_cpu(self):
        cpu = 100
        self.assertEqual(self.transformer.emit_cpu(cpu), cpu)
Exemple #3
0
class ComposeTransformerTests(TestCase):
    def setUp(self):
        self.file_name = './container_transform/tests/docker-compose.yml'
        self.transformer = ComposeTransformer(self.file_name)

    @patch.object(uuid,
                  'uuid4',
                  return_value='2e9c3538-b9d3-4f47-8a23-2a19315b370b')
    def test_emit_containers_no_name(self, mock_uuid):
        """
        Test .emit_containers() for a container without a name
        """
        containers = [{'image': 'postgres:9.3', 'cpu': 200}]

        output = self.transformer.emit_containers(containers)

        self.assertEqual(
            ('services:\n'
             '  {mock_uuid}:\n'
             '    cpu: 200\n'
             '    image: postgres:9.3\n'
             'version: \'2\'\n').format(mock_uuid=mock_uuid.return_value),
            output)

    def test_emit_mapping(self):
        """
        Test ._emit_mapping()
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'host_port': 8000,
            'container_ip': '127.0.0.1',
            'container_port': 80,
        }

        self.assertEqual(self.transformer._emit_mapping(mapping),
                         '192.168.59.103:8000:127.0.0.1:80')

    def test_emit_mapping_udp(self):
        """
        Test ._emit_mapping() with udp port
        """
        mapping = {'host_port': 53, 'container_port': 53, 'protocol': 'udp'}
        self.assertEqual(self.transformer._emit_mapping(mapping), '53:53/udp')

    def test_emit_mapping_missing_ports(self):
        """
        Test ._emit_mapping() missing ports
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'container_ip': '127.0.0.1',
        }

        self.assertEqual(self.transformer._emit_mapping(mapping),
                         '192.168.59.103:127.0.0.1')

    def test_parse_port_mapping_fails(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '192.168.59.103:8000:127.0.0.1:80:'

        self.assertEqual(self.transformer._parse_port_mapping(mapping), None)

    def test_parse_port_mapping_udp(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '53:53/udp'

        self.assertEqual(self.transformer._parse_port_mapping(mapping), {
            'host_port': 53,
            'container_port': 53,
            'protocol': 'udp'
        })

    def test_ingest_cpu(self):
        cpu = 100
        self.assertEqual(self.transformer.ingest_cpu(cpu), cpu)

    def test_emit_cpu(self):
        cpu = 100
        self.assertEqual(self.transformer.emit_cpu(cpu), cpu)

    def test_ingest_environment(self):
        environment = {'DB_PAS': '******', 'DB_USER': '******'}
        environment_exp = {'DB_PAS': '******', 'DB_USER': '******'}
        self.assertEqual(self.transformer.ingest_environment(environment),
                         environment_exp)

    def test_emit_environment(self):
        environment = {'DB_PAS': '******', 'DB_USER': '******'}
        environment_exp = {'DB_PAS': '******', 'DB_USER': '******'}
        self.assertEqual(self.transformer.emit_environment(environment),
                         environment_exp)

    def test_ingest_command_list(self):
        """
        Test .ingest_command() should respect that list items are single command args
        """
        command = ["/bin/echo", "Hello world"]

        self.assertEqual(self.transformer.ingest_command(command),
                         "/bin/echo 'Hello world'")

    def test_ingest_essential_dict(self):
        """
        If the 'essential' param is a dict, check the Name field.

        If the Name field is 'false' or 'no' or isn't present, return False.
        Otherwise return True.
        """
        dictionary = {'Name': 'test dictionary'}
        self.assertEqual(self.transformer.ingest_essential(dictionary), True)

        dictionary = {'Name': 'false'}
        self.assertEqual(self.transformer.ingest_essential(dictionary), False)

        dictionary = {'NoName': 'false'}
        self.assertEqual(self.transformer.ingest_essential(dictionary), False)

    def test_ingest_essential(self):
        """
        If the 'essential' field is 'no' or 'false' or any other string
        """
        self.assertEqual(self.transformer.ingest_essential('no'), False)

        self.assertEqual(self.transformer.ingest_essential('false'), False)

        self.assertEqual(self.transformer.emit_essential('Container_Name'), {
            'MaximumRetryCount': 0,
            'Name': 'container_name'
        })

    def test_emit_essential_false(self):
        self.assertEqual(self.transformer.emit_essential(False), None)
Exemple #4
0
class ComposeTransformerTests(TestCase):
    def setUp(self):
        self.file_name = './container_transform/tests/docker-compose.yml'
        self.transformer = ComposeTransformer(self.file_name)

    @patch.object(uuid,
                  'uuid4',
                  return_value='2e9c3538-b9d3-4f47-8a23-2a19315b370b')
    def test_emit_containers_no_name(self, mock_uuid):
        """
        Test .emit_containers() for a container without a name
        """
        containers = [{'image': 'postgres:9.3', 'cpu': 200}]

        output = self.transformer.emit_containers(containers)

        self.assertEqual(
            ('services:\n'
             '  {mock_uuid}:\n'
             '    cpu: 200\n'
             '    image: postgres:9.3\n'
             'version: \'2\'\n').format(mock_uuid=mock_uuid.return_value),
            output)

    def test_emit_mapping(self):
        """
        Test ._emit_mapping()
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'host_port': 8000,
            'container_ip': '127.0.0.1',
            'container_port': 80,
        }

        self.assertEqual(self.transformer._emit_mapping(mapping),
                         '192.168.59.103:8000:127.0.0.1:80')

    def test_emit_mapping_udp(self):
        """
        Test ._emit_mapping() with udp port
        """
        mapping = {'host_port': 53, 'container_port': 53, 'protocol': 'udp'}
        self.assertEqual(self.transformer._emit_mapping(mapping), '53:53/udp')

    def test_emit_mapping_missing_ports(self):
        """
        Test ._emit_mapping() missing ports
        """
        mapping = {
            'host_ip': '192.168.59.103',
            'container_ip': '127.0.0.1',
        }

        self.assertEqual(self.transformer._emit_mapping(mapping),
                         '192.168.59.103:127.0.0.1')

    def test_parse_port_mapping_fails(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '192.168.59.103:8000:127.0.0.1:80:'

        self.assertEqual(self.transformer._parse_port_mapping(mapping), None)

    def test_parse_port_mapping_udp(self):
        """
        Test ._parse_port_mapping() fails on > 4 parts
        """
        mapping = '53:53/udp'

        self.assertEqual(self.transformer._parse_port_mapping(mapping), {
            'host_port': 53,
            'container_port': 53,
            'protocol': 'udp'
        })

    def test_ingest_cpu(self):
        cpu = 100
        self.assertEqual(self.transformer.ingest_cpu(cpu), cpu)

    def test_emit_cpu(self):
        cpu = 100
        self.assertEqual(self.transformer.emit_cpu(cpu), cpu)

    def test_ingest_environment(self):
        environment = {'DB_PAS': '******', 'DB_USER': '******'}
        environment_exp = {'DB_PAS': '******', 'DB_USER': '******'}
        self.assertEqual(self.transformer.ingest_environment(environment),
                         environment_exp)

    def test_ingest_secrets(self):
        secrets = ['DB_PAS', 'DB_USER', 'EXTERNAL_SECRET']
        secrets_exp = {'DB_PAS': '******', 'DB_USER': '******'}
        self.assertEqual(self.transformer.ingest_secrets(secrets), secrets_exp)

    def test_ingest_secrets_error_file_not_found(self):
        secrets = {'DOESNOTEXIST': 'abc'}
        self.assertEqual(self.transformer.ingest_secrets(secrets), {})

    def test_ingest_secrets_error_key_not_exist(self):
        secrets = {'UNKNOWN': 'abc'}
        self.assertEqual(self.transformer.ingest_secrets(secrets), {})

    def test_emit_environment(self):
        environment = {'DB_PAS': '******', 'DB_USER': '******'}
        environment_exp = {'DB_PAS': '******', 'DB_USER': '******'}
        self.assertEqual(self.transformer.emit_environment(environment),
                         environment_exp)

    def test_emit_secrets(self):
        secrets = {'DB_PAS': '******', 'DB_USER': '******'}
        secrets_exp = {'DB_PAS': '******', 'DB_USER': '******'}
        self.assertEqual(self.transformer.emit_secrets(secrets), secrets_exp)

    def test_ingest_command_list(self):
        """
        Test .ingest_command() should respect that list items are single command args
        """
        command = ["/bin/echo", "Hello world"]

        self.assertEqual(self.transformer.ingest_command(command),
                         "/bin/echo 'Hello world'")