e3e5516ca6da4960f312badebfdc89528be52427
[yardstick.git] / yardstick / tests / unit / orchestrator / test_kubernetes.py
1 ##############################################################################
2 # Copyright (c) 2017 Intel Corporation
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
12 from yardstick.common import exceptions
13 from yardstick.common import kubernetes_utils
14 from yardstick.orchestrator import kubernetes
15 from yardstick.tests.unit import base
16
17
18 class GetTemplateTestCase(base.BaseUnitTestCase):
19
20     def test_get_template(self):
21         output_t = {
22             "apiVersion": "v1",
23             "kind": "ReplicationController",
24             "metadata": {
25                 "name": "host-k8s-86096c30"
26             },
27             "spec": {
28                 "replicas": 1,
29                 "template": {
30                     "metadata": {
31                         "labels": {
32                             "app": "host-k8s-86096c30"
33                         }
34                     },
35                     "spec": {
36                         "containers": [
37                             {
38                                 "args": [
39                                     "-c",
40                                     "chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
41 service ssh restart;while true ; do sleep 10000; done"
42                                 ],
43                                 "command": [
44                                     "/bin/bash"
45                                 ],
46                                 "image": "openretriever/yardstick",
47                                 "name": "host-k8s-86096c30-container",
48                                 "volumeMounts": [
49                                     {
50                                         "mountPath": "/tmp/.ssh/",
51                                         "name": "k8s-86096c30-key"
52                                     }
53                                 ]
54                             }
55                         ],
56                         "volumes": [
57                             {
58                                 "configMap": {
59                                     "name": "k8s-86096c30-key"
60                                 },
61                                 "name": "k8s-86096c30-key"
62                             }
63                         ],
64                         "nodeSelector": {
65                             "kubernetes.io/hostname": "node-01"
66                         }
67                     }
68                 }
69             }
70         }
71         input_s = {
72             'command': '/bin/bash',
73             'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
74 service ssh restart;while true ; do sleep 10000; done'],
75             'ssh_key': 'k8s-86096c30-key',
76             'nodeSelector': {'kubernetes.io/hostname': 'node-01'},
77             'volumes': []
78         }
79         name = 'host-k8s-86096c30'
80         output_r = kubernetes.KubernetesObject(name, **input_s).get_template()
81         self.assertEqual(output_r, output_t)
82
83
84 class GetRcPodsTestCase(base.BaseUnitTestCase):
85
86     @mock.patch('yardstick.orchestrator.kubernetes.k8s_utils.get_pod_list')
87     def test_get_rc_pods(self, mock_get_pod_list):
88         servers = {
89             'host': {
90                 'image': 'openretriever/yardstick',
91                 'command': '/bin/bash',
92                 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
93 service ssh restart;while true ; do sleep 10000; done']
94             },
95             'target': {
96                 'image': 'openretriever/yardstick',
97                 'command': '/bin/bash',
98                 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
99 service ssh restart;while true ; do sleep 10000; done']
100             }
101         }
102         k8s_template = kubernetes.KubernetesTemplate('k8s-86096c30', servers)
103         mock_get_pod_list.return_value.items = []
104         pods = k8s_template.get_rc_pods()
105         self.assertEqual(pods, [])
106
107
108 class KubernetesObjectTestCase(base.BaseUnitTestCase):
109
110     def test__add_volumes(self):
111         volume1 = {'name': 'fake_sshkey',
112                    'configMap': {'name': 'fake_sshkey'}}
113         volume2 = {'name': 'volume2',
114                    'configMap': 'data'}
115         k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey',
116                                               volumes=[volume2])
117         k8s_obj._add_volumes()
118         volumes = k8s_obj.template['spec']['template']['spec']['volumes']
119         self.assertEqual(sorted([volume1, volume2], key=lambda k: k['name']),
120                          sorted(volumes, key=lambda k: k['name']))
121
122     def test__add_volumes_no_volumes(self):
123         volume1 = {'name': 'fake_sshkey',
124                    'configMap': {'name': 'fake_sshkey'}}
125         k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey')
126         k8s_obj._add_volumes()
127         volumes = k8s_obj.template['spec']['template']['spec']['volumes']
128         self.assertEqual([volume1], volumes)
129
130     def test__create_ssh_key_volume(self):
131         expected = {'name': 'fake_sshkey',
132                     'configMap': {'name': 'fake_sshkey'}}
133         k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey')
134         self.assertEqual(expected, k8s_obj._create_ssh_key_volume())
135
136     def test__create_volume(self):
137         for vol_type in kubernetes_utils.get_volume_types():
138             volume = {'name': 'vol_name',
139                       vol_type: 'data'}
140             self.assertEqual(
141                 volume, kubernetes.KubernetesObject._create_volume(volume))
142
143     def test__create_volume_invalid_type(self):
144         volume = {'name': 'vol_name',
145                   'invalid_type': 'data'}
146         with self.assertRaises(exceptions.KubernetesTemplateInvalidVolumeType):
147             kubernetes.KubernetesObject._create_volume(volume)