Merge "testcase start service script fail on centos env"
[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 # Unittest for yardstick.benchmark.orchestrator.heat
11 import unittest
12 import mock
13
14 from yardstick.orchestrator.kubernetes import KubernetesObject
15 from yardstick.orchestrator.kubernetes import KubernetesTemplate
16
17
18 class GetTemplateTestCase(unittest.TestCase):
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": "/root/.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         }
78         name = 'host-k8s-86096c30'
79         output_r = KubernetesObject(name, **input_s).get_template()
80         self.assertEqual(output_r, output_t)
81
82
83 class GetRcPodsTestCase(unittest.TestCase):
84
85     @mock.patch('yardstick.orchestrator.kubernetes.k8s_utils.get_pod_list')
86     def test_get_rc_pods(self, mock_get_pod_list):
87         servers = {
88             'host': {
89                 'image': 'openretriever/yardstick',
90                 'command': '/bin/bash',
91                 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
92 service ssh restart;while true ; do sleep 10000; done']
93             },
94             'target': {
95                 'image': 'openretriever/yardstick',
96                 'command': '/bin/bash',
97                 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
98 service ssh restart;while true ; do sleep 10000; done']
99             }
100         }
101         k8s_template = KubernetesTemplate('k8s-86096c30', servers)
102         mock_get_pod_list.return_value.items = []
103         pods = k8s_template.get_rc_pods()
104         self.assertEqual(pods, [])