def setUp(self):
     """ Create a docker client"""
     super(ChefClientTestCase, self).setUp()
     self.client = ChefClient()
     CONF.set_override('cmd_test', "cmdtest {}", group='clients_chef')
     CONF.set_override('cmd_install', "cmdinstall {}", group='clients_chef')
     CONF.set_override('cmd_inject', "cmdinject {}", group='clients_chef')
     CONF.set_override('cmd_launch', "cmdlaunch {}", group='clients_chef')
Пример #2
0
    def validate_chef_cookbook(cookbook, image, request):
        """
        Cookbook validation
        :param image: name of the image to deploy
        :param cookbook: name of the cookbook to validate
        :param request: request context
        :return:
        """
        res = None
        # process request based on configuration options
        if hasattr(CONF, 'clients_docker') \
                and hasattr(CONF.clients_docker, 'url') \
                and len(CONF.clients_docker.url) > 1:

            # use direct docker connection, fast and simple
            d = ChefClient()
            res = d.cookbook_deployment_test(cookbook, image)
        return res
class ChefClientTestCase(tb.ValidatorTestCase):
    """Docker Client unit tests"""

    def setUp(self):
        """ Create a docker client"""
        super(ChefClientTestCase, self).setUp()
        self.client = ChefClient()
        CONF.set_override('cmd_test', "cmdtest {}", group='clients_chef')
        CONF.set_override('cmd_install', "cmdinstall {}", group='clients_chef')
        CONF.set_override('cmd_inject', "cmdinject {}", group='clients_chef')
        CONF.set_override('cmd_launch', "cmdlaunch {}", group='clients_chef')

    def test_create_client(self):
        """ Test client creation"""
        self.assertRaises(DockerException, ChefClient, 'fakeurl')
        self.assertIsInstance(self.client.dc, docker.client.Client)

    def test_run_container(self):
        """ Test container deployment"""
        self.assertRaises(DockerContainerException, self.client.run_container, "fakeimage")
        self.client.dc = mock.MagicMock()
        self.client.run_container('validimage')
        self.client.dc.create_container.assert_called_once_with('validimage', name=u'validimage-validate', tty=True)
        self.client.dc.start.assert_called_once_with(container=self.client.container)

    def test_stop_container(self):
        """ Test stopping and removing a container"""
        self.client.dc = self.m.CreateMockAnything()
        self.client.dc.stop(self.client.container)
        self.client.dc.remove_container(self.client.container)
        self.m.ReplayAll()
        self.client.remove_container()
        self.m.VerifyAll()

    def test_run_deploy(self):
        self.client.execute_command = mock.MagicMock()
        self.client.execute_command.return_value = "Alls good"
        self.client.run_deploy("mycookbook")
        obs = self.client.run_test("fakecookbook")
        expected = "{'response': u'Alls good', 'success': True}"
        self.assertEqual(expected, str(obs))


    def test_run_install(self):
        self.client.execute_command = self.m.CreateMockAnything()
        self.client.container = "1234"
        self.client.execute_command('cmdinstall fakecookbook').AndReturn("Alls good")
        self.m.ReplayAll()
        obs = self.client.run_install("fakecookbook")
        expected = "{'response': u'Alls good', 'success': True}"
        self.assertEqual(expected, str(obs))
        self.m.VerifyAll()

    def test_run_test(self):
        self.client.execute_command = self.m.CreateMockAnything()
        self.client.container = "1234"
        self.client.execute_command('cmdtest fakecookbook').AndReturn("Alls good")
        self.m.ReplayAll()
        obs = self.client.run_test("fakecookbook")
        expected = "{'response': u'Alls good', 'success': True}"
        self.assertEqual(expected, str(obs))
        self.m.VerifyAll()

    def test_execute_command(self):
        """Test a command execution in container"""
        self.client.dc = self.m.CreateMockAnything()
        self.client.container = "1234"
        self.client.dc.exec_create(cmd='/bin/bash -c "mycommand"', container=u'1234').AndReturn("validcmd")
        self.client.dc.exec_start("validcmd").AndReturn("OK")
        self.m.ReplayAll()
        obs = self.client.execute_command("mycommand")
        self.assertEqual("OK",obs)
        self.m.VerifyAll()

    def tearDown(self):
        """ Cleanup environment"""
        super(ChefClientTestCase, self).tearDown()
        self.m.UnsetStubs()
        self.m.ResetAll()