From 29d77c4d5117fe3b5e177c917e1157de3b753672 Mon Sep 17 00:00:00 2001 From: Kennan Date: Fri, 27 Mar 2015 09:49:43 +0800 Subject: [PATCH] Add kube pod tests in kubeutils Add tests for pod update/create Closes-Bug: #1436633 Change-Id: I50fce54588df476bf961b1f4e2cd9bdb8873ce96 --- .../handlers/common/test_kube_utils.py | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/magnum/tests/conductor/handlers/common/test_kube_utils.py b/magnum/tests/conductor/handlers/common/test_kube_utils.py index 2e18b3b9bf..59aca15513 100644 --- a/magnum/tests/conductor/handlers/common/test_kube_utils.py +++ b/magnum/tests/conductor/handlers/common/test_kube_utils.py @@ -150,6 +150,94 @@ class KubeClientTestCase(base.TestCase): super(KubeClientTestCase, self).setUp() self.kube_client = kube_utils.KubeClient() + @patch('magnum.conductor.handlers.common.kube_utils._k8s_create') + def test_pod_create(self, mock_k8s_create): + expected_api_address = 'master-address' + expected_pod_content = mock.MagicMock(manifest='pod_content') + expected_command = [ + expected_api_address, + expected_pod_content + ] + mock_k8s_create.return_value = ("", "") + + result = self.kube_client.pod_create(expected_api_address, + expected_pod_content) + self.assertTrue(result) + mock_k8s_create.assert_called_once_with(*expected_command) + + @patch('magnum.conductor.handlers.common.kube_utils._k8s_create') + def test_pod_create_with_err(self, mock_k8s_create): + expected_api_address = 'master-address' + expected_pod_content = mock.MagicMock(manifest='pod_content') + expected_command = [ + expected_api_address, + expected_pod_content + ] + mock_k8s_create.return_value = ("", "create failed") + + result = self.kube_client.pod_create(expected_api_address, + expected_pod_content) + self.assertFalse(result) + mock_k8s_create.assert_called_once_with(*expected_command) + + @patch('magnum.conductor.handlers.common.kube_utils._k8s_create') + def test_pod_create_failure_exception(self, mock_k8s_create): + expected_api_address = 'master-address' + expected_pod_content = mock.MagicMock(manifest='pod_content') + expected_command = [ + expected_api_address, + expected_pod_content + ] + mock_k8s_create.side_effect = Exception() + result = self.kube_client.pod_create(expected_api_address, + expected_pod_content) + self.assertFalse(result) + mock_k8s_create.assert_called_once_with(*expected_command) + + @patch('magnum.conductor.handlers.common.kube_utils._k8s_update') + def test_pod_update(self, mock_k8s_update): + expected_api_address = 'master-address' + expected_pod_content = mock.MagicMock(manifest='pod_content') + expected_command = [ + expected_api_address, + expected_pod_content + ] + mock_k8s_update.return_value = ("", "") + + result = self.kube_client.pod_update(expected_api_address, + expected_pod_content) + self.assertTrue(result) + mock_k8s_update.assert_called_once_with(*expected_command) + + @patch('magnum.conductor.handlers.common.kube_utils._k8s_update') + def test_pod_update_with_err(self, mock_k8s_update): + expected_api_address = 'master-address' + expected_pod_content = mock.MagicMock(manifest='pod_content') + expected_command = [ + expected_api_address, + expected_pod_content + ] + mock_k8s_update.return_value = ("", "create failed") + + result = self.kube_client.pod_update(expected_api_address, + expected_pod_content) + self.assertFalse(result) + mock_k8s_update.assert_called_once_with(*expected_command) + + @patch('magnum.conductor.handlers.common.kube_utils._k8s_update') + def test_pod_update_failure_exception(self, mock_k8s_update): + expected_api_address = 'master-address' + expected_pod_content = mock.MagicMock(manifest='pod_content') + expected_command = [ + expected_api_address, + expected_pod_content + ] + mock_k8s_update.side_effect = Exception() + result = self.kube_client.pod_update(expected_api_address, + expected_pod_content) + self.assertFalse(result) + mock_k8s_update.assert_called_once_with(*expected_command) + @patch('magnum.openstack.common.utils.trycmd') def test_pod_delete(self, mock_trycmd): expected_api_address = 'master-address'