def test_mountPoints(self):
     cd = ContainerDefinition(yml=self.yml['services'][1]['containers'][0])
     self.assertEqual(len(cd.render()['mountPoints']), 2)
     compare(cd.render()['mountPoints'], [
         {'sourceVolume': '_host_path', 'containerPath': '/container/path', 'readOnly': False},
         {'sourceVolume': '_host_path-ro', 'containerPath': '/container/path-ro', 'readOnly': True},
     ])
class TestContainerDefinition_render(unittest.TestCase):

    def setUp(self):
        current_dir = os.path.dirname(os.path.abspath(__file__))
        filename = os.path.join(current_dir, 'simple.yml')
        with open(filename) as f:
            self.yml = yaml.load(f, Loader=yaml.FullLoader)
        self.cd = ContainerDefinition(yml=self.yml['services'][0]['containers'][0])

    def test_name(self):
        self.assertEqual(self.cd.render()['name'], 'example')

    def test_cpu(self):
        self.assertEqual(self.cd.render()['cpu'], 1024)

    def test_cap_add(self):
        compare(self.cd.render()['linuxParameters']['capabilities']['add'], ["SYS_ADMIN"])

    def test_cap_drop(self):
        compare(self.cd.render()['linuxParameters']['capabilities']['drop'], ["SYS_RAWIO"])

    def test_tmpfs(self):
        compare(self.cd.render()['linuxParameters']['tmpfs'], [
            {'containerPath': '/tmpfs', 'size': 256, 'mountOptions': ['defaults', 'noatime']},
            {'containerPath': '/tmpfs_another', 'size': 128}
        ])

    def test_memory(self):
        self.assertEqual(self.cd.render()['memory'], 4000)

    def test_memoryReservation(self):
        self.assertEqual(self.cd.render()['memoryReservation'], 2000)

    def test_image(self):
        self.assertEqual(self.cd.render()['image'], 'example:1.2.3')

    def test_command(self):
        self.assertEqual(self.cd.render()['command'], ['/usr/bin/supervisord'])

    def test_entryPoint(self):
        self.assertEqual(self.cd.render()['entryPoint'], ['/entrypoint.sh'])

    def test_portMappings(self):
        self.assertEqual(len(self.cd.render()['portMappings']), 4)
        compare(self.cd.render()['portMappings'], [
            {'containerPort': 22, 'protocol': 'tcp'},
            {'hostPort': 80, 'containerPort': 80, 'protocol': 'tcp'},
            {'hostPort': 443, 'containerPort': 443, 'protocol': 'tcp'},
            {'hostPort': 8021, 'containerPort': 8021, 'protocol': 'udp'},
        ]
        )

    def test_ulimits(self):
        self.assertEqual(len(self.cd.ulimits), 2)
        render = self.cd.render()['ulimits']
        self.assertTrue({'name': 'nproc', 'softLimit': 65535, 'hardLimit': 65535} in render)
        self.assertTrue({'name': 'nofile', 'softLimit': 65535, 'hardLimit': 65535} in render)

    def test_environment(self):
        render = self.cd.render()['environment']
        self.assertTrue({'name': 'LDAPTLS_REQCERT', 'value': 'never'} in render)
        self.assertTrue({'name': 'ENVIRONMENT', 'value': 'prod'} in render)
        self.assertTrue({'name': 'SECRETS_BUCKET_NAME', 'value': 'config-store'} in render)

    def test_dockerLabels(self):
        compare(self.cd.render()['dockerLabels'], {'edu.caltech.imss-ads': 'foobar'})

    def test_links(self):
        cd = ContainerDefinition(yml=self.yml['services'][1]['containers'][0])
        compare(cd.render()['links'], ['redis', 'db:database'])

    def test_mountPoints(self):
        cd = ContainerDefinition(yml=self.yml['services'][1]['containers'][0])
        self.assertEqual(len(cd.render()['mountPoints']), 2)
        compare(cd.render()['mountPoints'], [
            {'sourceVolume': '_host_path', 'containerPath': '/container/path', 'readOnly': False},
            {'sourceVolume': '_host_path-ro', 'containerPath': '/container/path-ro', 'readOnly': True},
        ])

    def test_extraHosts(self):
        self.assertEqual(len(self.cd.render()['extraHosts']), 2)
        compare(self.cd.render()['extraHosts'], [
            {'hostname': 'foobar', 'ipAddress': '127.0.0.1'},
            {'hostname': 'foobaz', 'ipAddress': '127.0.0.2'},
        ])

    def test_logging(self):
        render = self.cd.render()
        log_config = render['logConfiguration']
        self.assertIn('logDriver', log_config)
        self.assertEqual(log_config['logDriver'], 'fluentd')
        self.assertIn('options', log_config)
        options = log_config['options']
        self.assertIn('fluentd-address', options)
        self.assertIn('tag', options)
        self.assertEqual(options['fluentd-address'], '127.0.0.1:24224')
        self.assertEqual(options['tag'], 'foobar')
 def test_links(self):
     cd = ContainerDefinition(yml=self.yml['services'][1]['containers'][0])
     compare(cd.render()['links'], ['redis', 'db:database'])
class TestContainerDefinition_render(unittest.TestCase):
    def setUp(self):
        current_dir = os.path.dirname(os.path.abspath(__file__))
        fname = os.path.join(current_dir, 'simple.yml')
        with open(fname) as f:
            self.yml = yaml.load(f)
        self.cd = ContainerDefinition(
            yml=self.yml['services'][0]['containers'][0])

    def test_name(self):
        self.assertEqual(self.cd.render()['name'], 'cit_auth')

    def test_cpu(self):
        self.assertEqual(self.cd.render()['cpu'], 1024)

    def test_memory(self):
        self.assertEqual(self.cd.render()['memory'], 4000)

    def test_image(self):
        self.assertEqual(
            self.cd.render()['image'],
            '467892444047.dkr.ecr.us-west-2.amazonaws.com/caltech-imss-ads/cit_auth:1.2.3'
        )

    def test_command(self):
        self.assertEqual(self.cd.render()['command'], ['/usr/bin/supervisord'])

    def test_entryPoint(self):
        self.assertEqual(self.cd.render()['entryPoint'], ['/entrypoint.sh'])

    def test_portMappings(self):
        self.assertEqual(len(self.cd.render()['portMappings']), 4)
        compare(self.cd.render()['portMappings'], [
            {
                'containerPort': 22,
                'protocol': 'tcp'
            },
            {
                'hostPort': 80,
                'containerPort': 80,
                'protocol': 'tcp'
            },
            {
                'hostPort': 443,
                'containerPort': 443,
                'protocol': 'tcp'
            },
            {
                'hostPort': 8021,
                'containerPort': 8021,
                'protocol': 'udp'
            },
        ])

    def test_ulimits(self):
        self.assertEqual(len(self.cd.ulimits), 2)
        render = self.cd.render()['ulimits']
        self.assertTrue({
            'name': 'nproc',
            'softLimit': 65535,
            'hardLimit': 65535
        } in render)
        self.assertTrue({
            'name': 'nofile',
            'softLimit': 65535,
            'hardLimit': 65535
        } in render)

    def test_environment(self):
        render = self.cd.render()['environment']
        self.assertTrue({
            'name': 'LDAPTLS_REQCERT',
            'value': 'never'
        } in render)
        self.assertTrue({'name': 'ENVIRONMENT', 'value': 'prod'} in render)
        self.assertTrue({
            'name': 'SECRETS_BUCKET_NAME',
            'value': 'ac-config-store'
        } in render)

    def test_dockerLabels(self):
        compare(self.cd.render()['dockerLabels'],
                {'edu.caltech.imss-ads': 'foobar'})

    def test_links(self):
        cd = ContainerDefinition(yml=self.yml['services'][1]['containers'][0])
        compare(cd.render()['links'], ['redis', 'db:database'])

    def test_mountPoints(self):
        cd = ContainerDefinition(yml=self.yml['services'][1]['containers'][0])
        self.assertEqual(len(cd.render()['mountPoints']), 2)
        compare(cd.render()['mountPoints'], [
            {
                'sourceVolume': '_host_path',
                'containerPath': '/container/path',
                'readOnly': False
            },
            {
                'sourceVolume': '_host_path-ro',
                'containerPath': '/container/path-ro',
                'readOnly': True
            },
        ])

    def test_extraHosts(self):
        self.assertEqual(len(self.cd.render()['extraHosts']), 2)
        compare(self.cd.render()['extraHosts'], [
            {
                'hostname': 'foobar',
                'ipAddress': '127.0.0.1'
            },
            {
                'hostname': 'foobaz',
                'ipAddress': '127.0.0.2'
            },
        ])

    def test_logging(self):
        render = self.cd.render()
        logconfig = render['logConfiguration']
        self.assertIn('logDriver', logconfig)
        self.assertEqual(logconfig['logDriver'], 'fluentd')
        self.assertIn('options', logconfig)
        options = logconfig['options']
        self.assertIn('fluentd-address', options)
        self.assertIn('tag', options)
        self.assertEqual(options['fluentd-address'], '127.0.0.1:24224')
        self.assertEqual(options['tag'], 'cit_auth')