Example #1
0
class TestGunicornDockerRunner(unittest.TestCase):
    """
    Test the docker runner
    """

    @mock.patch('mc.builders.Client')
    def setUp(self, mocked):
        instance = mocked.return_value
        instance.create_container.return_value = {'Id': 'mocked'}
        instance.port.return_value = [{'HostIp': '127.0.0.1', 'HostPort': 1234}]
        instance.containers.return_value = [
            {
                u'Command': u'/entrypoint.sh redis-server',
                u'Created': 1443632967,
                u'Id': u'mocked',
                u'Image': u'redis',
                u'Labels': {},
                u'Names': [u'/livetest-redis-tLJpZ'],
                u'Ports': [{u'PrivatePort': 6379, u'Type': u'tcp'}],
                u'Status': u'Up About a minute'
            }
        ]
        instance.create_host_config.return_value = {'PortBindings': {
                '80/tcp': [{'HostPort': '', 'HostIp': ''}]
            }, 'NetworkMode': 'host'}

        self.instance = instance
        self.environment = dict(consul_host='localhost', consul_port=8500)
        self.builder = GunicornDockerRunner(network_mode="host",
                                            environment=self.environment)

    def test_can_set_consul(self):
        """
        Tests that consul properties get passed correctly
        """
        expected_call = mock.call(
            command=None,
            image=None,
            name=None,
            environment={'consul_host': 'localhost', 'consul_port': 8500},
            host_config={'PortBindings': {
                '80/tcp': [{'HostPort': '', 'HostIp': ''}]
            }, 'NetworkMode': 'host'}
        )

        self.instance.create_container.assert_has_calls(
            [expected_call]
        )

    def test_ready(self):
        """
        Tests the health check of the service
        """

        with mock.patch.object(mc.builders.requests,
                               'get',
                               side_effect=requests.ConnectionError):
            self.assertFalse(self.builder.ready)

        with HTTMock(response_500):
            self.assertFalse(self.builder.ready)

        with HTTMock(response_200):
            self.assertTrue(self.builder.ready)

    def test_can_provision(self):
        """
        Tests that there is a method that allows provisioning
        """
        try:
            self.builder.provision(services=['adsaws'])
        except Exception as e:
            self.fail('Provisioning failed: {}'.format(e))
Example #2
0
class TestGunicornDockerRunner(unittest.TestCase):
    """
    Test the docker runner
    """
    @mock.patch('mc.builders.Client')
    def setUp(self, mocked):
        instance = mocked.return_value
        instance.create_container.return_value = {'Id': 'mocked'}
        instance.port.return_value = [{
            'HostIp': '127.0.0.1',
            'HostPort': 1234
        }]
        instance.containers.return_value = [{
            u'Command':
            u'/entrypoint.sh redis-server',
            u'Created':
            1443632967,
            u'Id':
            u'mocked',
            u'Image':
            u'redis',
            u'Labels': {},
            u'Names': [u'/livetest-redis-tLJpZ'],
            u'Ports': [{
                u'PrivatePort': 6379,
                u'Type': u'tcp'
            }],
            u'Status':
            u'Up About a minute'
        }]
        instance.create_host_config.return_value = {
            'PortBindings': {
                '80/tcp': [{
                    'HostPort': '',
                    'HostIp': ''
                }]
            },
            'NetworkMode': 'host'
        }

        self.instance = instance
        self.environment = dict(consul_host='localhost', consul_port=8500)
        self.builder = GunicornDockerRunner(network_mode="host",
                                            environment=self.environment)

    def test_can_set_consul(self):
        """
        Tests that consul properties get passed correctly
        """
        expected_call = mock.call(command=None,
                                  image=None,
                                  name=None,
                                  environment={
                                      'consul_host': 'localhost',
                                      'consul_port': 8500
                                  },
                                  host_config={
                                      'PortBindings': {
                                          '80/tcp': [{
                                              'HostPort': '',
                                              'HostIp': ''
                                          }]
                                      },
                                      'NetworkMode': 'host'
                                  })

        self.instance.create_container.assert_has_calls([expected_call])

    def test_ready(self):
        """
        Tests the health check of the service
        """

        with mock.patch.object(mc.builders.requests,
                               'get',
                               side_effect=requests.ConnectionError):
            self.assertFalse(self.builder.ready)

        with HTTMock(response_500):
            self.assertFalse(self.builder.ready)

        with HTTMock(response_200):
            self.assertTrue(self.builder.ready)

    def test_can_provision(self):
        """
        Tests that there is a method that allows provisioning
        """
        try:
            self.builder.provision(services=['adsaws'])
        except Exception as e:
            self.fail('Provisioning failed: {}'.format(e))