summaryrefslogtreecommitdiffstats
path: root/deploy/environments/virtual_fuel.py
blob: cb3bc6c5156155215659342ebf35b1839dcc83c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
###############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# szilard.cserey@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
###############################################################################


from lxml import etree
from execution_environment import ExecutionEnvironment
import tempfile

from common import (
    exec_cmd,
    check_file_exists,
    check_if_root,
    delete,
)


class VirtualFuel(ExecutionEnvironment):

    def __init__(self, storage_dir, pxe_bridge, dha_file, root_dir):
        super(VirtualFuel, self).__init__(storage_dir, dha_file, root_dir)
        self.pxe_bridge = pxe_bridge

    def set_vm_nic(self, temp_vm_file):
        with open(temp_vm_file) as f:
            vm_xml = etree.parse(f)
        interfaces = vm_xml.xpath('/domain/devices/interface')
        for interface in interfaces:
            interface.getparent().remove(interface)
        interface = etree.Element('interface')
        interface.set('type', 'bridge')
        source = etree.SubElement(interface, 'source')
        source.set('bridge', self.pxe_bridge)
        model = etree.SubElement(interface, 'model')
        model.set('type', 'virtio')
        devices = vm_xml.xpath('/domain/devices')
        if devices:
            device = devices[0]
            device.append(interface)
        with open(temp_vm_file, 'w') as f:
            vm_xml.write(f, pretty_print=True, xml_declaration=True)

    def create_vm(self):
        temp_dir = tempfile.mkdtemp()
        vm_name = self.dha.get_node_property(self.fuel_node_id, 'libvirtName')
        vm_template = '%s/%s' % (self.root_dir,
                                 self.dha.get_node_property(
                                     self.fuel_node_id, 'libvirtTemplate'))
        check_file_exists(vm_template)
        disk_path = '%s/%s.raw' % (self.storage_dir, vm_name)
        disk_sizes = self.dha.get_disks()
        disk_size = disk_sizes['fuel']
        exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))
        temp_vm_file = '%s/%s' % (temp_dir, vm_name)
        exec_cmd('cp %s %s' % (vm_template, temp_vm_file))
        self.set_vm_nic(temp_vm_file)
        vm_definition_overwrite = self.dha.get_vm_definition('fuel')
        self.define_vm(vm_name, temp_vm_file, disk_path,
                       vm_definition_overwrite)
        delete(temp_dir)

    def setup_environment(self):
        check_if_root()
        self.cleanup_environment()
        self.create_vm()

    def cleanup_environment(self):
        self.delete_vm(self.fuel_node_id)
sh_key, mock_create_rcs, mock_get_rc_pods, mock_wait_until_running, mock_create_services): k8s_context = KubernetesContext() k8s_context.init(context_cfg) with mock.patch("yardstick.benchmark.contexts.kubernetes.time"): k8s_context.deploy() self.assertTrue(mock_set_ssh_key.called) self.assertTrue(mock_create_rcs.called) self.assertTrue(mock_create_services.called) self.assertTrue(mock_get_rc_pods.called) self.assertTrue(mock_wait_until_running.called) @mock.patch('{}.paramiko'.format(prefix), **{"resource_filename.return_value": ""}) @mock.patch('{}.pkg_resources'.format(prefix), **{"resource_filename.return_value": ""}) @mock.patch('{}.utils'.format(prefix)) @mock.patch('{}.open'.format(prefix), create=True) @mock.patch('{}.k8s_utils.delete_config_map'.format(prefix)) @mock.patch('{}.k8s_utils.create_config_map'.format(prefix)) def test_ssh_key(self, mock_create, mock_delete, mock_open, mock_utils, mock_resources, mock_paramiko): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._set_ssh_key() k8s_context._delete_ssh_key() self.assertTrue(mock_create.called) self.assertTrue(mock_delete.called) @mock.patch('{}.k8s_utils.read_pod_status'.format(prefix)) def test_wait_until_running(self, mock_read_pod_status): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context.template.pods = ['server'] mock_read_pod_status.return_value = 'Running' k8s_context._wait_until_running() @mock.patch('{}.k8s_utils.get_pod_by_name'.format(prefix)) @mock.patch('{}.KubernetesContext._get_node_ip'.format(prefix)) @mock.patch('{}.k8s_utils.get_service_by_name'.format(prefix)) def test_get_server(self, mock_get_service_by_name, mock_get_node_ip, mock_get_pod_by_name): class Service(object): def __init__(self): self.name = 'yardstick' self.node_port = 30000 class Services(object): def __init__(self): self.ports = [Service()] class Status(object): def __init__(self): self.pod_ip = '172.16.10.131' class Pod(object): def __init__(self): self.status = Status() k8s_context = KubernetesContext() k8s_context.init(context_cfg) mock_get_service_by_name.return_value = Services() mock_get_pod_by_name.return_value = Pod() mock_get_node_ip.return_value = '172.16.10.131' server = k8s_context._get_server('server') self.assertIsNotNone(server) @mock.patch('{}.KubernetesContext._create_rc'.format(prefix)) def test_create_rcs(self, mock_create_rc): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._create_rcs() self.assertTrue(mock_create_rc.called) @mock.patch('{}.k8s_utils.create_replication_controller'.format(prefix)) def test_create_rc(self, mock_create_replication_controller): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._create_rc({}) self.assertTrue(mock_create_replication_controller.called) @mock.patch('{}.KubernetesContext._delete_rc'.format(prefix)) def test_delete_rcs(self, mock_delete_rc): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._delete_rcs() self.assertTrue(mock_delete_rc.called) @mock.patch('{}.k8s_utils.delete_replication_controller'.format(prefix)) def test_delete_rc(self, mock_delete_replication_controller): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._delete_rc({}) self.assertTrue(mock_delete_replication_controller.called) @mock.patch('{}.k8s_utils.get_node_list'.format(prefix)) def test_get_node_ip(self, mock_get_node_list): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._get_node_ip() self.assertTrue(mock_get_node_list.called) @mock.patch('yardstick.orchestrator.kubernetes.ServiceObject.create') def test_create_services(self, mock_create): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._create_services() self.assertTrue(mock_create.called) @mock.patch('yardstick.orchestrator.kubernetes.ServiceObject.delete') def test_delete_services(self, mock_delete): k8s_context = KubernetesContext() k8s_context.init(context_cfg) k8s_context._delete_services() self.assertTrue(mock_delete.called) def main(): unittest.main() if __name__ == '__main__': main()