summaryrefslogtreecommitdiff
path: root/nova/tests/unit/virt/zvm/test_hypervisor.py
blob: c816ca57f62402c6f5719d48283b3a542ee64d5d (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright 2017,2018 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from unittest import mock

from nova import context
from nova import exception
from nova import test
from nova.tests.unit import fake_instance
from nova.virt.zvm import driver as zvmdriver


class TestZVMHypervisor(test.NoDBTestCase):

    def setUp(self):
        super(TestZVMHypervisor, self).setUp()
        self.flags(instance_name_template='abc%5d')
        self.flags(cloud_connector_url='https://1.1.1.1:1111', group='zvm')
        with mock.patch('nova.virt.zvm.utils.'
                        'ConnectorClient.call') as mcall:
            mcall.return_value = {'hypervisor_hostname': 'TESTHOST',
                                  'ipl_time': 'IPL at 11/14/17 10:47:44 EST'}
            driver = zvmdriver.ZVMDriver('virtapi')
            self._hypervisor = driver._hypervisor

        self._context = context.RequestContext('fake_user', 'fake_project')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_get_available_resource(self, call):
        host_info = {'disk_available': 1144,
                     'ipl_time': 'IPL at 11/14/17 10:47:44 EST',
                     'vcpus_used': 4,
                     'hypervisor_type': 'zvm',
                     'disk_total': 2000,
                     'zvm_host': 'TESTHOST',
                     'memory_mb': 78192,
                     'cpu_info': {'cec_model': '2827',
                                  'architecture': 's390x'},
                     'vcpus': 84,
                     'hypervisor_hostname': 'TESTHOST',
                     'hypervisor_version': 640,
                     'disk_used': 856,
                     'memory_mb_used': 8192}
        call.return_value = host_info
        results = self._hypervisor.get_available_resource()
        self.assertEqual(host_info, results)

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_get_available_resource_err_case(self, call):
        res = {'overallRC': 1, 'errmsg': 'err', 'rc': 0, 'rs': 0}
        call.side_effect = exception.ZVMConnectorError(results=res)
        results = self._hypervisor.get_available_resource()
        # Should return an empty dict
        self.assertFalse(results)

    def test_get_available_nodes(self):
        nodes = self._hypervisor.get_available_nodes()
        self.assertEqual(['TESTHOST'], nodes)

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_list_names(self, call):
        call.return_value = ['vm1', 'vm2']
        inst_list = self._hypervisor.list_names()
        self.assertEqual(['vm1', 'vm2'], inst_list)

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_get_host_uptime(self, call):
        host_info = {'disk_available': 1144,
                     'ipl_time': 'IPL at 11/14/17 10:47:44 EST',
                     'memory_mb_used': 8192.0}
        call.return_value = host_info

        time = self._hypervisor.get_host_uptime()
        self.assertEqual('IPL at 11/14/17 10:47:44 EST', time)

    @mock.patch('nova.virt.zvm.hypervisor.Hypervisor.list_names')
    def test_private_guest_exists_true(self, list_names):
        instance = fake_instance.fake_instance_obj(self._context)
        list_names.return_value = [instance.name.upper(), 'TEST0002']
        res = self._hypervisor.guest_exists(instance)
        self.assertTrue(res)

    @mock.patch('nova.virt.zvm.hypervisor.Hypervisor.list_names')
    def test_private_guest_exists_false(self, list_names):
        list_names.return_value = ['dummy1', 'dummy2']
        instance = fake_instance.fake_instance_obj(self._context)
        res = self._hypervisor.guest_exists(instance)
        self.assertFalse(res)

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_capture(self, mcall):
        self._hypervisor.guest_capture('n1', 'image-id')
        mcall.assert_called_once_with('guest_capture', 'n1', 'image-id')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_image_export(self, mcall):
        self._hypervisor.image_export('image-id', 'path')
        mcall.assert_called_once_with('image_export', 'image-id', 'path',
                                      remote_host=self._hypervisor._rhost)

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_image_delete(self, mcall):
        self._hypervisor.image_delete('image-id')
        mcall.assert_called_once_with('image_delete', 'image-id')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_softstop(self, mcall):
        self._hypervisor.guest_softstop('guest')
        mcall.assert_called_once_with('guest_softstop', 'guest',
                                      poll_interval=0, timeout=0)

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_pause(self, mcall):
        self._hypervisor.guest_pause('guest')
        mcall.assert_called_once_with('guest_pause', 'guest')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_unpause(self, mcall):
        self._hypervisor.guest_unpause('guest')
        mcall.assert_called_once_with('guest_unpause', 'guest')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_reboot(self, mcall):
        self._hypervisor.guest_reboot('guest')
        mcall.assert_called_once_with('guest_reboot', 'guest')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_reset(self, mcall):
        self._hypervisor.guest_reset('guest')
        mcall.assert_called_once_with('guest_reset', 'guest')

    @mock.patch('nova.virt.zvm.utils.ConnectorClient.call')
    def test_guest_get_console_output(self, mcall):
        self._hypervisor.guest_get_console_output('guest')
        mcall.assert_called_once_with('guest_get_console_output', 'guest')