def test__add_volumes_no_volumes(self): volume1 = {'name': 'fake_sshkey', 'configMap': {'name': 'fake_sshkey'}} k8s_obj = kubernetes.ReplicationControllerObject( 'name', ssh_key='fake_sshkey') k8s_obj._add_volumes() volumes = k8s_obj.template['spec']['template']['spec']['volumes'] self.assertEqual([volume1], volumes)
def test__add_networks(self): k8s_obj = kubernetes.ReplicationControllerObject( 'name', networks=['network1', 'network2', 'network3']) k8s_obj._add_networks() networks = k8s_obj.\ template['spec']['template']['metadata']['annotations']['networks'] expected = ('[{"name": "network1"}, {"name": "network2"}, ' '{"name": "network3"}]') self.assertEqual(expected, networks)
def test__add_security_context(self): k8s_obj = kubernetes.ReplicationControllerObject('pod_name') self.assertNotIn('securityContext', k8s_obj.template['spec']['template']['spec']) k8s_obj._security_context = {'key_pod': 'value_pod'} k8s_obj._add_security_context() self.assertEqual( {'key_pod': 'value_pod'}, k8s_obj.template['spec']['template']['spec']['securityContext'])
def test__add_volumes(self): volume1 = {'name': 'fake_sshkey', 'configMap': {'name': 'fake_sshkey'}} volume2 = {'name': 'volume2', 'configMap': 'data'} k8s_obj = kubernetes.ReplicationControllerObject( 'name', ssh_key='fake_sshkey', volumes=[volume2]) k8s_obj._add_volumes() volumes = k8s_obj.template['spec']['template']['spec']['volumes'] self.assertEqual(sorted([volume1, volume2], key=lambda k: k['name']), sorted(volumes, key=lambda k: k['name']))
def test__init_one_container(self): pod_name = 'pod_name' _kwargs = {'args': ['arg1', 'arg2'], 'image': 'fake_image', 'command': 'fake_command'} k8s_obj = kubernetes.ReplicationControllerObject(pod_name, **_kwargs) self.assertEqual(1, len(k8s_obj._containers)) container = k8s_obj._containers[0] self.assertEqual(['arg1', 'arg2'], container._args) self.assertEqual('fake_image', container._image) self.assertEqual(['fake_command'], container._command) self.assertEqual([], container._volume_mounts)
def test__add_tolerations(self): _kwargs = {'tolerations': [{'key': 'key1', 'value': 'value2', 'effect': 'effect3', 'operator': 'operator4', 'wrong_key': 'error_key'}] } k8s_obj = kubernetes.ReplicationControllerObject('pod_name', **_kwargs) k8s_obj._add_tolerations() _tol = k8s_obj.template['spec']['template']['spec']['tolerations'] self.assertEqual(1, len(_tol)) self.assertEqual({'key': 'key1', 'value': 'value2', 'effect': 'effect3', 'operator': 'operator4'}, _tol[0])
def test__add_security_context_by_init(self): containers = [] for i in range(5): containers.append( {'securityContext': {'key%s' % i: 'value%s' % i}}) _kwargs = {'containers': containers, 'securityContext': {'key_pod': 'value_pod'}} k8s_obj = kubernetes.ReplicationControllerObject('pod_name', **_kwargs) self.assertEqual( {'key_pod': 'value_pod'}, k8s_obj.template['spec']['template']['spec']['securityContext']) for i in range(5): container = ( k8s_obj.template['spec']['template']['spec']['containers'][i]) self.assertEqual({'key%s' % i: 'value%s' % i}, container['securityContext'])
def test__init_multipe_containers(self): pod_name = 'pod_name' containers = [] for i in range(5): containers.append({'args': ['arg1', 'arg2'], 'image': 'fake_image_%s' % i, 'command': 'fake_command_%s' % i}) _kwargs = {'containers': containers} k8s_obj = kubernetes.ReplicationControllerObject(pod_name, **_kwargs) self.assertEqual(5, len(k8s_obj._containers)) for i in range(5): container = k8s_obj._containers[i] self.assertEqual(['arg1', 'arg2'], container._args) self.assertEqual('fake_image_%s' % i, container._image) self.assertEqual(['fake_command_%s' % i], container._command) self.assertEqual([], container._volume_mounts)
def test__get_interfaces(self, mock_get_rc): rc = orchestrator_kubernetes.ReplicationControllerObject('rc_name') rc._networks = ['net1', 'net2'] mock_get_rc.return_value = rc expected = { 'net1': { 'network_name': 'net1', 'local_mac': None, 'local_ip': None }, 'net2': { 'network_name': 'net2', 'local_mac': None, 'local_ip': None } } self.assertEqual(expected, self.k8s_context._get_interfaces('rc_name'))
def test_get_template_invalid_restart_policy(self): input_s = {'restartPolicy': 'invalid_option'} name = 'host-k8s-86096c30' with self.assertRaises(exceptions.KubernetesWrongRestartPolicy): kubernetes.ReplicationControllerObject( name, **input_s).get_template()
def test__add_tolerations_default(self): k8s_obj = kubernetes.ReplicationControllerObject('pod_name') k8s_obj._add_tolerations() _tol = k8s_obj.template['spec']['template']['spec']['tolerations'] self.assertEqual(1, len(_tol)) self.assertEqual({'operator': 'Exists'}, _tol[0])
def test_get_template(self): output_t = { "apiVersion": "v1", "kind": "ReplicationController", "metadata": { "name": "host-k8s-86096c30" }, "spec": { "replicas": 1, "template": { "metadata": { "labels": { "app": "host-k8s-86096c30" } }, "spec": { "containers": [ { "args": [ "-c", "chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ service ssh restart;while true ; do sleep 10000; done" ], "command": [ "/bin/bash" ], "image": "openretriever/yardstick", "name": "host-k8s-86096c30-container", "volumeMounts": [ { "mountPath": "/tmp/.ssh/", "name": "k8s-86096c30-key", "readOnly": False } ] } ], "volumes": [ { "configMap": { "name": "k8s-86096c30-key" }, "name": "k8s-86096c30-key" } ], "nodeSelector": { "kubernetes.io/hostname": "node-01" }, "restartPolicy": "Always", "tolerations": [ {"operator": "Exists"} ] } } } } input_s = { 'command': '/bin/bash', 'args': ['-c', 'chmod 700 ~/.ssh; chmod 600 ~/.ssh/*; \ service ssh restart;while true ; do sleep 10000; done'], 'ssh_key': 'k8s-86096c30-key', 'nodeSelector': {'kubernetes.io/hostname': 'node-01'}, 'volumes': [], 'restartPolicy': 'Always' } name = 'host-k8s-86096c30' output_r = kubernetes.ReplicationControllerObject( name, **input_s).get_template() self.assertEqual(output_r, output_t)
def test__create_ssh_key_volume(self): expected = {'name': 'fake_sshkey', 'configMap': {'name': 'fake_sshkey'}} k8s_obj = kubernetes.ReplicationControllerObject( 'name', ssh_key='fake_sshkey') self.assertEqual(expected, k8s_obj._create_ssh_key_volume())
def test__get_interfaces_no_networks(self, mock_get_rc): rc = orchestrator_kubernetes.ReplicationControllerObject('rc_name') mock_get_rc.return_value = rc self.assertEqual({}, self.k8s_context._get_interfaces('rc_name'))