1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from yardstick.benchmark import contexts
14 from yardstick.benchmark.contexts import base
15 from yardstick.benchmark.contexts import kubernetes
16 from yardstick.common import constants
17 from yardstick.orchestrator import kubernetes as orchestrator_kubernetes
21 'type': contexts.CONTEXT_KUBERNETES,
23 'task_id': '1234567890',
26 'image': 'openretriever/yardstick',
27 'command': '/bin/bash',
28 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; '
29 'service ssh restart;while true ; do sleep 10000; done']
32 'image': 'openretriever/yardstick',
33 'command': '/bin/bash',
34 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; '
35 'service ssh restart;while true ; do sleep 10000; done']
40 prefix = 'yardstick.benchmark.contexts.kubernetes'
43 class KubernetesTestCase(unittest.TestCase):
46 self.k8s_context = kubernetes.KubernetesContext()
47 self.addCleanup(self._remove_contexts)
48 self.k8s_context.init(CONTEXT_CFG)
51 def _remove_contexts():
52 for context in base.Context.list:
53 context._delete_context()
54 base.Context.list = []
56 @mock.patch.object(kubernetes.KubernetesContext, '_delete_services')
57 @mock.patch.object(kubernetes.KubernetesContext, '_delete_ssh_key')
58 @mock.patch.object(kubernetes.KubernetesContext, '_delete_rcs')
59 @mock.patch.object(kubernetes.KubernetesContext, '_delete_pods')
60 def test_undeploy(self,
64 mock_delete_services):
66 self.k8s_context.undeploy()
67 mock_delete_ssh.assert_called_once()
68 mock_delete_rcs.assert_called_once()
69 mock_delete_pods.assert_called_once()
70 mock_delete_services.assert_called_once()
72 @mock.patch.object(kubernetes.KubernetesContext, '_create_services')
73 @mock.patch.object(kubernetes.KubernetesContext, '_wait_until_running')
74 @mock.patch.object(orchestrator_kubernetes.KubernetesTemplate,
76 @mock.patch.object(kubernetes.KubernetesContext, '_create_rcs')
77 @mock.patch.object(kubernetes.KubernetesContext, '_set_ssh_key')
82 mock_wait_until_running,
83 mock_create_services):
85 with mock.patch("yardstick.benchmark.contexts.kubernetes.time"):
86 self.k8s_context.deploy()
87 mock_set_ssh_key.assert_called_once()
88 mock_create_rcs.assert_called_once()
89 mock_create_services.assert_called_once()
90 mock_get_rc_pods.assert_called_once()
91 mock_wait_until_running.assert_called_once()
93 @mock.patch.object(kubernetes, 'paramiko', **{"resource_filename.return_value": ""})
94 @mock.patch.object(kubernetes, 'pkg_resources', **{"resource_filename.return_value": ""})
95 @mock.patch.object(kubernetes, 'utils')
96 @mock.patch.object(kubernetes, 'open', create=True)
97 @mock.patch.object(kubernetes.k8s_utils, 'delete_config_map')
98 @mock.patch.object(kubernetes.k8s_utils, 'create_config_map')
99 def test_ssh_key(self, mock_create, mock_delete, *args):
100 self.k8s_context._set_ssh_key()
101 self.k8s_context._delete_ssh_key()
103 mock_create.assert_called_once()
104 mock_delete.assert_called_once()
106 @mock.patch.object(kubernetes.k8s_utils, 'read_pod_status')
107 def test_wait_until_running(self, mock_read_pod_status):
109 self.k8s_context.template.pods = ['server']
110 mock_read_pod_status.return_value = 'Running'
111 self.k8s_context._wait_until_running()
113 @mock.patch.object(kubernetes.k8s_utils, 'get_pod_by_name')
114 @mock.patch.object(kubernetes.KubernetesContext, '_get_node_ip')
115 @mock.patch.object(kubernetes.k8s_utils, 'get_service_by_name')
116 def test_get_server(self,
117 mock_get_service_by_name,
119 mock_get_pod_by_name):
120 class Service(object):
122 self.node_port = 30000
123 self.port = constants.SSH_PORT
125 class Services(object):
127 self.ports = [Service()]
129 class Status(object):
131 self.pod_ip = '172.16.10.131'
135 self.status = Status()
137 mock_get_service_by_name.return_value = Services()
138 mock_get_pod_by_name.return_value = Pod()
139 mock_get_node_ip.return_value = '172.16.10.131'
140 server = self.k8s_context._get_server('server_name')
141 self.assertEqual('server_name', server['name'])
142 self.assertEqual(30000, server['ssh_port'])
144 @mock.patch.object(kubernetes.KubernetesContext, '_create_rc')
145 def test_create_rcs(self, mock_create_rc):
146 self.k8s_context._create_rcs()
147 mock_create_rc.assert_called()
149 @mock.patch.object(kubernetes.k8s_utils, 'create_replication_controller')
150 def test_create_rc(self, mock_create_replication_controller):
151 self.k8s_context._create_rc({})
152 mock_create_replication_controller.assert_called_once()
154 @mock.patch.object(kubernetes.KubernetesContext, '_delete_rc')
155 def test_delete_rcs(self, mock_delete_rc):
156 self.k8s_context._delete_rcs()
157 mock_delete_rc.assert_called()
159 @mock.patch.object(kubernetes.k8s_utils, 'delete_replication_controller')
160 def test_delete_rc(self, mock_delete_replication_controller):
161 self.k8s_context._delete_rc({})
162 mock_delete_replication_controller.assert_called_once()
164 @mock.patch.object(kubernetes.k8s_utils, 'get_node_list')
165 def test_get_node_ip(self, mock_get_node_list):
166 self.k8s_context._get_node_ip()
167 mock_get_node_list.assert_called_once()
169 @mock.patch.object(orchestrator_kubernetes.ServiceNodePortObject, 'create')
170 def test_create_services(self, mock_create):
171 self.k8s_context._create_services()
172 mock_create.assert_called()
174 @mock.patch.object(orchestrator_kubernetes.ServiceNodePortObject, 'delete')
175 def test_delete_services(self, mock_delete):
176 self.k8s_context._delete_services()
177 mock_delete.assert_called()
180 self.k8s_context._delete_context()
181 with mock.patch.object(orchestrator_kubernetes, 'KubernetesTemplate',
182 return_value='fake_template') as mock_k8stemplate:
183 self.k8s_context = kubernetes.KubernetesContext()
184 self.k8s_context.init(CONTEXT_CFG)
185 mock_k8stemplate.assert_called_once_with(self.k8s_context.name,
187 self.assertEqual('fake_template', self.k8s_context.template)
189 def test__get_physical_nodes(self):
190 result = self.k8s_context._get_physical_nodes()
191 self.assertIsNone(result)
193 def test__get_physical_node_for_server(self):
194 result = self.k8s_context._get_physical_node_for_server("fake")
195 self.assertIsNone(result)