1 ##############################################################################
2 # Copyright (c) 2017 Intel Corporation
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 ##############################################################################
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
18 class GetTemplateTestCase(base.BaseUnitTestCase):
20 def test_get_template(self):
23 "kind": "ReplicationController",
25 "name": "host-k8s-86096c30"
32 "app": "host-k8s-86096c30"
40 "chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \
41 service ssh restart;while true ; do sleep 10000; done"
46 "image": "openretriever/yardstick",
47 "name": "host-k8s-86096c30-container",
50 "mountPath": "/tmp/.ssh/",
51 "name": "k8s-86096c30-key"
59 "name": "k8s-86096c30-key"
61 "name": "k8s-86096c30-key"
65 "kubernetes.io/hostname": "node-01"
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'},
79 name = 'host-k8s-86096c30'
80 output_r = kubernetes.KubernetesObject(name, **input_s).get_template()
81 self.assertEqual(output_r, output_t)
84 class GetRcPodsTestCase(base.BaseUnitTestCase):
86 @mock.patch('yardstick.orchestrator.kubernetes.k8s_utils.get_pod_list')
87 def test_get_rc_pods(self, mock_get_pod_list):
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']
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']
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, [])
108 class KubernetesObjectTestCase(base.BaseUnitTestCase):
110 def test__add_volumes(self):
111 volume1 = {'name': 'fake_sshkey',
112 'configMap': {'name': 'fake_sshkey'}}
113 volume2 = {'name': 'volume2',
115 k8s_obj = kubernetes.KubernetesObject('name', ssh_key='fake_sshkey',
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']))
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)
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())
136 def test__create_volume(self):
137 for vol_type in kubernetes_utils.get_volume_types():
138 volume = {'name': 'vol_name',
141 volume, kubernetes.KubernetesObject._create_volume(volume))
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)