def setUp(self):
     """Create a chef client"""
     super(ChefClientTestCase, self).setUp()
     self.client = ChefClientSSH("1.1.1.1")
     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')
     CONF.set_override('cmd_config', "cmdconfig {}", group='clients_chef')
示例#2
0
    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
class ChefClientTestCase(tb.ValidatorTestCase):
    """Chef Client unit tests"""

    def setUp(self):
        """Create a chef client"""
        super(ChefClientTestCase, self).setUp()
        self.client = ChefClientSSH("1.1.1.1")
        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')
        CONF.set_override('cmd_config', "cmdconfig {}", group='clients_chef')

    def test_connect_session(self):
        """Test client creation"""
        self.client.ssh = mock.MagicMock()
        self.client.ssh.connect.return_value = "OK"
        expected = None
        observed = self.client.connect_session()
        self.assertEqual(expected, observed)


    def test_disconnect_session(self):
        """Test stopping and removing a container"""
        self.client.ssh = mock.MagicMock()
        self.client.disconnect_session()
        self.client.ssh.disconnect.assert_called_once_with()

    def test_run_deploy(self):
        self.client.execute_command = self.m.CreateMockAnything()
        self.client.ssh = mock.MagicMock()
        self.client.container = "1234"
        self.client.execute_command(
            'cmdinject cmdconfig fakecookbook'
        ).AndReturn("Alls good")
        self.client.execute_command('cmdlaunch {}').AndReturn("Alls good")
        self.m.ReplayAll()
        obs = self.client.run_deploy("fakecookbook")
        expected = "{'response': u'Alls good', 'success': True}"
        self.assertEqual(expected, str(obs))
        self.m.VerifyAll()

    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.ssh = self.m.CreateMockAnything()
        stdin = mock.MagicMock()
        self.client.ssh.exec_command(
            u'mycommand'
        ).AndReturn((stdin, "OK", None))
        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()