def setUp(self):
     """Create a docker client"""
     super(DockerClientTestCase, self).setUp()
     self.client = DockerClient()
     CONF.set_override("cmd_test", "knife cookbook test {cookbook_name}", group="clients_chef")
     CONF.set_override("cmd_install", " knife cookbook site install {cookbook_name}", group="clients_chef")
     CONF.set_override("cmd_inject", "cmdinject {}", group="clients_chef")
     CONF.set_override("cmd_launch", "cmdlaunch {}", group="clients_chef")
Ejemplo n.º 2
0
class ValidateEngine(object):
    """Engine for validations"""
    def __init__(self):
        self.d = DockerClient()

    def validate_cookbook(self, cookbook, recipe, image, request):
        """
        Cookbook validation
        :param recipe:
        :param image: name of the image to deploy
        :param cookbook: name of the cookbook to validate
        :param request: request context
        :return:
        """
        # 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
            res = self.d.cookbook_deployment_test(cookbook, recipe, image)
        else:
            # nova client connection
            n = NovaClient(request.context)

            # find the image id
            image = n.get_image_by_name(image)
            if not image:
                raise exception.ImageNotFound

            machine = "%s-validate" % cookbook

            # if the machine already exists, destroy it
            if n.get_machine(machine):
                LOG.info(_LI("Server %s already exists, deleting") % machine)
                n.delete_machine(machine)

            # deploy machine
            n.deploy_machine(machine, image=image['name'])
            ip = n.get_ip()

            # generic ssh connection
            c = ChefClientSSH(ip)
            res = c.cookbook_deploy_test(cookbook)
        return res
Ejemplo n.º 3
0
 def __init__(self):
     self.d = DockerClient()
class DockerClientTestCase(tb.ValidatorTestCase):
    """Docker Client unit tests"""

    def setUp(self):
        """Create a docker client"""
        super(DockerClientTestCase, self).setUp()
        self.client = DockerClient()
        CONF.set_override("cmd_test", "knife cookbook test {cookbook_name}", group="clients_chef")
        CONF.set_override("cmd_install", " knife cookbook site install {cookbook_name}", 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, DockerClient, "fakeurl")
        self.assertIsInstance(self.client.dc, docker.client.Client)

    def test_run_container(self):
        """Test container deployment"""
        self.client.dc = mock.MagicMock()
        self.client.run_container("validimage")
        self.client.dc.create_container.assert_called_once_with("validimage", name="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.remove_container = mock.MagicMock()
        self.client.execute_command = mock.MagicMock()
        self.client.execute_command.return_value = "Alls good"
        obs = self.client.run_deploy("mycookbook", "myrecipe")
        expected = "{'response': u'Alls good', 'success': True}"
        self.assertEqual(expected, str(obs))

    def test_run_install(self):
        self.client.container = "1234"
        self.client.remove_container = mock.MagicMock()
        self.client.execute_command = mock.MagicMock()
        self.client.execute_command.return_value = "Alls good"
        cookbook = "testing"
        obs = self.client.run_install(cookbook)
        expected = "{'response': u'Alls good', 'success': True}"
        self.assertEqual(expected, str(obs))

    def test_run_test(self):
        self.client.remove_container = mock.MagicMock()
        self.client.execute_command = mock.MagicMock()
        self.client.execute_command.return_value = "Alls good"
        test_input = "testing"
        cookbook = test_input
        obs = self.client.run_test(cookbook)
        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="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(DockerClientTestCase, self).tearDown()
        self.m.UnsetStubs()
        self.m.ResetAll()