Improve "get_server" function in Kubernetes context
[yardstick.git] / yardstick / tests / unit / benchmark / contexts / test_kubernetes.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 import mock
11 import unittest
12
13 from yardstick.benchmark.contexts import base
14 from yardstick.benchmark.contexts import kubernetes
15 from yardstick.common import constants
16 from yardstick.orchestrator import kubernetes as orchestrator_kubernetes
17
18
19 CONTEXT_CFG = {
20     'type': 'Kubernetes',
21     'name': 'k8s',
22     'task_id': '1234567890',
23     'servers': {
24         'host': {
25             'image': 'openretriever/yardstick',
26             'command': '/bin/bash',
27             'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; '
28                      'service ssh restart;while true ; do sleep 10000; done']
29         },
30         'target': {
31             'image': 'openretriever/yardstick',
32             'command': '/bin/bash',
33             'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; '
34                      'service ssh restart;while true ; do sleep 10000; done']
35         }
36     }
37 }
38
39 prefix = 'yardstick.benchmark.contexts.kubernetes'
40
41
42 class KubernetesTestCase(unittest.TestCase):
43
44     def setUp(self):
45         self.k8s_context = kubernetes.KubernetesContext()
46         self.addCleanup(self._remove_contexts)
47         self.k8s_context.init(CONTEXT_CFG)
48
49     @staticmethod
50     def _remove_contexts():
51         for context in base.Context.list:
52             context._delete_context()
53         base.Context.list = []
54
55     @mock.patch.object(kubernetes.KubernetesContext, '_delete_services')
56     @mock.patch.object(kubernetes.KubernetesContext, '_delete_ssh_key')
57     @mock.patch.object(kubernetes.KubernetesContext, '_delete_rcs')
58     @mock.patch.object(kubernetes.KubernetesContext, '_delete_pods')
59     def test_undeploy(self,
60                       mock_delete_pods,
61                       mock_delete_rcs,
62                       mock_delete_ssh,
63                       mock_delete_services):
64
65         self.k8s_context.undeploy()
66         mock_delete_ssh.assert_called_once()
67         mock_delete_rcs.assert_called_once()
68         mock_delete_pods.assert_called_once()
69         mock_delete_services.assert_called_once()
70
71     @mock.patch.object(kubernetes.KubernetesContext, '_create_services')
72     @mock.patch.object(kubernetes.KubernetesContext, '_wait_until_running')
73     @mock.patch.object(orchestrator_kubernetes.KubernetesTemplate,
74                        'get_rc_pods')
75     @mock.patch.object(kubernetes.KubernetesContext, '_create_rcs')
76     @mock.patch.object(kubernetes.KubernetesContext, '_set_ssh_key')
77     def test_deploy(self,
78                     mock_set_ssh_key,
79                     mock_create_rcs,
80                     mock_get_rc_pods,
81                     mock_wait_until_running,
82                     mock_create_services):
83
84         with mock.patch("yardstick.benchmark.contexts.kubernetes.time"):
85             self.k8s_context.deploy()
86         mock_set_ssh_key.assert_called_once()
87         mock_create_rcs.assert_called_once()
88         mock_create_services.assert_called_once()
89         mock_get_rc_pods.assert_called_once()
90         mock_wait_until_running.assert_called_once()
91
92     @mock.patch.object(kubernetes, 'paramiko', **{"resource_filename.return_value": ""})
93     @mock.patch.object(kubernetes, 'pkg_resources', **{"resource_filename.return_value": ""})
94     @mock.patch.object(kubernetes, 'utils')
95     @mock.patch.object(kubernetes, 'open', create=True)
96     @mock.patch.object(kubernetes.k8s_utils, 'delete_config_map')
97     @mock.patch.object(kubernetes.k8s_utils, 'create_config_map')
98     def test_ssh_key(self, mock_create, mock_delete, *args):
99         self.k8s_context._set_ssh_key()
100         self.k8s_context._delete_ssh_key()
101
102         mock_create.assert_called_once()
103         mock_delete.assert_called_once()
104
105     @mock.patch.object(kubernetes.k8s_utils, 'read_pod_status')
106     def test_wait_until_running(self, mock_read_pod_status):
107
108         self.k8s_context.template.pods = ['server']
109         mock_read_pod_status.return_value = 'Running'
110         self.k8s_context._wait_until_running()
111
112     @mock.patch.object(kubernetes.k8s_utils, 'get_pod_by_name')
113     @mock.patch.object(kubernetes.KubernetesContext, '_get_node_ip')
114     @mock.patch.object(kubernetes.k8s_utils, 'get_service_by_name')
115     def test_get_server(self,
116                         mock_get_service_by_name,
117                         mock_get_node_ip,
118                         mock_get_pod_by_name):
119         class Service(object):
120             def __init__(self):
121                 self.node_port = 30000
122                 self.port = constants.SSH_PORT
123
124         class Services(object):
125             def __init__(self):
126                 self.ports = [Service()]
127
128         class Status(object):
129             def __init__(self):
130                 self.pod_ip = '172.16.10.131'
131
132         class Pod(object):
133             def __init__(self):
134                 self.status = Status()
135
136         mock_get_service_by_name.return_value = Services()
137         mock_get_pod_by_name.return_value = Pod()
138         mock_get_node_ip.return_value = '172.16.10.131'
139         server = self.k8s_context._get_server('server_name')
140         self.assertEqual('server_name', server['name'])
141         self.assertEqual(30000, server['ssh_port'])
142
143     @mock.patch.object(kubernetes.KubernetesContext, '_create_rc')
144     def test_create_rcs(self, mock_create_rc):
145         self.k8s_context._create_rcs()
146         mock_create_rc.assert_called()
147
148     @mock.patch.object(kubernetes.k8s_utils, 'create_replication_controller')
149     def test_create_rc(self, mock_create_replication_controller):
150         self.k8s_context._create_rc({})
151         mock_create_replication_controller.assert_called_once()
152
153     @mock.patch.object(kubernetes.KubernetesContext, '_delete_rc')
154     def test_delete_rcs(self, mock_delete_rc):
155         self.k8s_context._delete_rcs()
156         mock_delete_rc.assert_called()
157
158     @mock.patch.object(kubernetes.k8s_utils, 'delete_replication_controller')
159     def test_delete_rc(self, mock_delete_replication_controller):
160         self.k8s_context._delete_rc({})
161         mock_delete_replication_controller.assert_called_once()
162
163     @mock.patch.object(kubernetes.k8s_utils, 'get_node_list')
164     def test_get_node_ip(self, mock_get_node_list):
165         self.k8s_context._get_node_ip()
166         mock_get_node_list.assert_called_once()
167
168     @mock.patch.object(orchestrator_kubernetes.ServiceNodePortObject, 'create')
169     def test_create_services(self, mock_create):
170         self.k8s_context._create_services()
171         mock_create.assert_called()
172
173     @mock.patch.object(orchestrator_kubernetes.ServiceNodePortObject, 'delete')
174     def test_delete_services(self, mock_delete):
175         self.k8s_context._delete_services()
176         mock_delete.assert_called()
177
178     def test_init(self):
179         self.k8s_context._delete_context()
180         with mock.patch.object(orchestrator_kubernetes, 'KubernetesTemplate',
181                 return_value='fake_template') as mock_k8stemplate:
182             self.k8s_context = kubernetes.KubernetesContext()
183             self.k8s_context.init(CONTEXT_CFG)
184         mock_k8stemplate.assert_called_once_with(self.k8s_context.name,
185                                                  CONTEXT_CFG)
186         self.assertEqual('fake_template', self.k8s_context.template)
187
188     def test__get_physical_nodes(self):
189         result = self.k8s_context._get_physical_nodes()
190         self.assertIsNone(result)
191
192     def test__get_physical_node_for_server(self):
193         result = self.k8s_context._get_physical_node_for_server("fake")
194         self.assertIsNone(result)