~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/tests/baremetal/test_virtual_power_driver.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandelman
  • Date: 2013-02-22 09:27:29 UTC
  • mfrom: (1.1.68)
  • Revision ID: package-import@ubuntu.com-20130222092729-nn3gt8rf97uvts77
Tags: 2013.1.g3-0ubuntu1
[ Chuck Short ]
* New usptream release. 
* debian/patches/debian/patches/fix-ubuntu-tests.patch: Refreshed.
* debian/nova-baremetal.logrotate: Fix logfile path.
* debian/control, debian/nova-spiceproxy.{install, logrotate, upstart}:
  Add spice html5 proxy support.
* debian/nova-novncproxy.upstart: Start on runlevel [2345]
* debian/rules: Call testr directly since run_tests.sh -N gives weird return
  value when tests pass.
* debian/pyddist-overrides: Add websockify.
* debian/nova-common.postinst: Removed config file conversion, since
  the option is no longer available. (LP: #1110567)
* debian/control: Add python-pyasn1 as a dependency.
* debian/control: Add python-oslo-config as a dependency.
* debian/control: Suggest sysfsutils, sg3-utils, multipath-tools for fibre
  channel support.

[ Adam Gandelman ]
* debian/control: Fix typo (websocikfy -> websockify).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# coding=utf-8
 
3
 
 
4
# Copyright 2012 Hewlett-Packard Development Company, L.P.
 
5
# All Rights Reserved.
 
6
#
 
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
#    not use this file except in compliance with the License. You may obtain
 
9
#    a copy of the License at
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
#    Unless required by applicable law or agreed to in writing, software
 
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
#    License for the specific language governing permissions and limitations
 
17
#    under the License.
 
18
 
 
19
"""Tests for baremetal virtual power driver."""
 
20
 
 
21
import mox
 
22
from oslo.config import cfg
 
23
 
 
24
from nova import exception
 
25
from nova.tests.baremetal.db import base as bm_db_base
 
26
from nova.tests.baremetal.db import utils as bm_db_utils
 
27
from nova.tests.image import fake as fake_image
 
28
from nova.tests import utils
 
29
from nova import utils as nutils
 
30
from nova.virt.baremetal import db
 
31
from nova.virt.baremetal import virtual_power_driver
 
32
import nova.virt.powervm.common as connection
 
33
 
 
34
CONF = cfg.CONF
 
35
 
 
36
COMMON_FLAGS = dict(
 
37
    firewall_driver='nova.virt.baremetal.fake.FakeFirewallDriver',
 
38
    host='test_host',
 
39
)
 
40
 
 
41
BAREMETAL_FLAGS = dict(
 
42
    driver='nova.virt.baremetal.pxe.PXE',
 
43
    instance_type_extra_specs=['cpu_arch:test', 'test_spec:test_value'],
 
44
    power_manager=
 
45
        'nova.virt.baremetal.virtual_power_driver.VirtualPowerManager',
 
46
    vif_driver='nova.virt.baremetal.fake.FakeVifDriver',
 
47
    volume_driver='nova.virt.baremetal.fake.FakeVolumeDriver',
 
48
    virtual_power_ssh_host=None,
 
49
    virtual_power_type='vbox',
 
50
    virtual_power_host_user=None,
 
51
    virtual_power_host_pass=None,
 
52
    group='baremetal',
 
53
)
 
54
 
 
55
 
 
56
class BareMetalVPDTestCase(bm_db_base.BMDBTestCase):
 
57
 
 
58
    def setUp(self):
 
59
        super(BareMetalVPDTestCase, self).setUp()
 
60
        self.flags(**COMMON_FLAGS)
 
61
        self.flags(**BAREMETAL_FLAGS)
 
62
 
 
63
        fake_image.stub_out_image_service(self.stubs)
 
64
        self.context = utils.get_test_admin_context()
 
65
        self.test_block_device_info = None,
 
66
        self.instance = utils.get_test_instance()
 
67
        self.test_network_info = utils.get_test_network_info(),
 
68
        self.node_info = bm_db_utils.new_bm_node(
 
69
                id=123,
 
70
                service_host='test_host',
 
71
                cpus=2,
 
72
                memory_mb=2048,
 
73
                prov_mac_address='11:11:11:11:11:11',
 
74
            )
 
75
        self.nic_info = [
 
76
                {'address': '22:22:22:22:22:22', 'datapath_id': '0x1',
 
77
                    'port_no': 1},
 
78
                {'address': '33:33:33:33:33:33', 'datapath_id': '0x2',
 
79
                    'port_no': 2},
 
80
            ]
 
81
        self.addCleanup(fake_image.FakeImageService_reset)
 
82
 
 
83
    def _create_node(self):
 
84
        self.node = db.bm_node_create(self.context, self.node_info)
 
85
        for nic in self.nic_info:
 
86
            db.bm_interface_create(
 
87
                                    self.context,
 
88
                                    self.node['id'],
 
89
                                    nic['address'],
 
90
                                    nic['datapath_id'],
 
91
                                    nic['port_no'],
 
92
                )
 
93
        self.instance['node'] = self.node['id']
 
94
 
 
95
    def _create_pm(self):
 
96
        self.pm = virtual_power_driver.VirtualPowerManager(
 
97
                        node=self.node,
 
98
                        instance=self.instance)
 
99
        return self.pm
 
100
 
 
101
 
 
102
class VPDMissingOptionsTestCase(BareMetalVPDTestCase):
 
103
 
 
104
    def test_get_conn_missing_options(self):
 
105
        self.flags(virtual_power_ssh_host=None, group="baremetal")
 
106
        self.flags(virtual_power_host_user=None, group="baremetal")
 
107
        self.flags(virtual_power_host_pass=None, group="baremetal")
 
108
        self._create_node()
 
109
        self._create_pm()
 
110
        self._conn = None
 
111
        self.assertRaises(exception.NovaException,
 
112
                self.pm._get_conn)
 
113
        self._conn = None
 
114
        self.flags(virtual_power_ssh_host='127.0.0.1', group="baremetal")
 
115
        self.assertRaises(exception.NovaException,
 
116
                self.pm._get_conn)
 
117
        self._conn = None
 
118
        self.flags(virtual_power_host_user='user', group="baremetal")
 
119
        self.assertRaises(exception.NovaException,
 
120
                self.pm._get_conn)
 
121
 
 
122
 
 
123
class VPDClassMethodsTestCase(BareMetalVPDTestCase):
 
124
 
 
125
    def setUp(self):
 
126
        super(VPDClassMethodsTestCase, self).setUp()
 
127
        self.flags(virtual_power_ssh_host='127.0.0.1', group="baremetal")
 
128
        self.flags(virtual_power_host_user='user', group="baremetal")
 
129
        self.flags(virtual_power_host_pass='password', group="baremetal")
 
130
 
 
131
    def test_get_conn_success(self):
 
132
        self._create_node()
 
133
        self._create_pm()
 
134
        self._conn = self.pm._get_conn()
 
135
        self.mox.StubOutWithMock(connection, 'ssh_connect')
 
136
        connection.ssh_connect(mox.IsA(self._conn)).AndReturn(True)
 
137
        self.mox.ReplayAll()
 
138
        self.pm._set_connection()
 
139
        self.assertEqual(self.pm.connection_data.host, '127.0.0.1')
 
140
        self.assertEqual(self.pm.connection_data.username, 'user')
 
141
        self.assertEqual(self.pm.connection_data.password, 'password')
 
142
        self.mox.VerifyAll()
 
143
 
 
144
    def test_get_full_node_list(self):
 
145
        self._create_node()
 
146
        self._create_pm()
 
147
 
 
148
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
149
        cmd = self.pm._vp_cmd.list_cmd
 
150
        self.pm._run_command(cmd).AndReturn("testNode")
 
151
 
 
152
        self.mox.ReplayAll()
 
153
        name = self.pm._get_full_node_list()
 
154
        self.assertEqual(name, 'testNode')
 
155
        self.mox.VerifyAll()
 
156
 
 
157
    def test_check_for_node(self):
 
158
        self._create_node()
 
159
        self._create_pm()
 
160
 
 
161
        self.mox.StubOutWithMock(self.pm, '_get_full_node_list')
 
162
        self.pm._get_full_node_list().\
 
163
                AndReturn(["testNode"])
 
164
 
 
165
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
166
        cmd = self.pm._vp_cmd.get_node_macs.replace('{_NodeName_}', 'testNode')
 
167
        self.pm._run_command(cmd).\
 
168
                AndReturn(["111111111111", "ffeeddccbbaa"])
 
169
 
 
170
        self.mox.ReplayAll()
 
171
        name = self.pm._check_for_node()
 
172
        self.assertEqual(name, 'testNode')
 
173
        self.mox.VerifyAll()
 
174
 
 
175
    def test_check_for_node_not_found(self):
 
176
        self._create_node()
 
177
        self._create_pm()
 
178
 
 
179
        self.mox.StubOutWithMock(self.pm, '_get_full_node_list')
 
180
        self.pm._get_full_node_list().AndReturn(["testNode"])
 
181
 
 
182
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
183
        cmd = self.pm._vp_cmd.get_node_macs.replace('{_NodeName_}', 'testNode')
 
184
        self.pm._run_command(cmd).AndReturn(["aabbccddeeff", "ffeeddccbbaa"])
 
185
 
 
186
        self.mox.ReplayAll()
 
187
        name = self.pm._check_for_node()
 
188
        self.assertEqual(name, '')
 
189
        self.mox.VerifyAll()
 
190
 
 
191
    def test_activate_node(self):
 
192
        self._create_node()
 
193
        self._create_pm()
 
194
 
 
195
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
196
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
197
        self.mox.StubOutWithMock(self.pm, 'is_power_on')
 
198
        self.pm._check_for_node().AndReturn("testNode")
 
199
        self.pm._run_command(self.pm._vp_cmd.start_cmd).AndReturn("Started")
 
200
        self.pm.is_power_on().AndReturn(True)
 
201
        self.mox.ReplayAll()
 
202
        state = self.pm.activate_node()
 
203
        self.assertEqual(state, 'active')
 
204
        self.mox.VerifyAll()
 
205
 
 
206
    def test_activate_node_fail(self):
 
207
        self._create_node()
 
208
        self._create_pm()
 
209
 
 
210
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
211
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
212
        self.mox.StubOutWithMock(self.pm, 'is_power_on')
 
213
        self.pm._check_for_node().AndReturn("testNode")
 
214
        self.pm._run_command(self.pm._vp_cmd.start_cmd).AndReturn("Started")
 
215
        self.pm.is_power_on().AndReturn(False)
 
216
        self.mox.ReplayAll()
 
217
        state = self.pm.activate_node()
 
218
        self.assertEqual(state, 'error')
 
219
        self.mox.VerifyAll()
 
220
 
 
221
    def test_deactivate_node(self):
 
222
        self._create_node()
 
223
        self._create_pm()
 
224
 
 
225
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
226
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
227
        self.mox.StubOutWithMock(self.pm, 'is_power_on')
 
228
        self.pm._check_for_node().AndReturn("testNode")
 
229
        self.pm.is_power_on().AndReturn(True)
 
230
        self.pm._run_command(self.pm._vp_cmd.stop_cmd).AndReturn("Stopped")
 
231
        self.pm.is_power_on().AndReturn(False)
 
232
        self.mox.ReplayAll()
 
233
        state = self.pm.deactivate_node()
 
234
        self.assertEqual(state, 'deleted')
 
235
        self.mox.VerifyAll()
 
236
 
 
237
    def test_deactivate_node_fail(self):
 
238
        self._create_node()
 
239
        self._create_pm()
 
240
 
 
241
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
242
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
243
        self.mox.StubOutWithMock(self.pm, 'is_power_on')
 
244
        self.pm._check_for_node().AndReturn("testNode")
 
245
        self.pm.is_power_on().AndReturn(True)
 
246
        self.pm._run_command(self.pm._vp_cmd.stop_cmd).AndReturn("Stopped")
 
247
        self.pm.is_power_on().AndReturn(True)
 
248
        self.mox.ReplayAll()
 
249
        state = self.pm.deactivate_node()
 
250
        self.assertEqual(state, 'error')
 
251
        self.mox.VerifyAll()
 
252
 
 
253
    def test_reboot_node(self):
 
254
        self._create_node()
 
255
        self._create_pm()
 
256
 
 
257
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
258
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
259
        self.mox.StubOutWithMock(self.pm, 'is_power_on')
 
260
        self.pm._check_for_node().AndReturn(["testNode"])
 
261
        self.pm._run_command(self.pm._vp_cmd.reboot_cmd).AndReturn("Restarted")
 
262
        self.pm.is_power_on().AndReturn(True)
 
263
        self.mox.ReplayAll()
 
264
        state = self.pm.reboot_node()
 
265
        self.assertEqual(state, 'active')
 
266
        self.mox.VerifyAll()
 
267
 
 
268
    def test_reboot_node_fail(self):
 
269
        self._create_node()
 
270
        self._create_pm()
 
271
 
 
272
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
273
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
274
        self.mox.StubOutWithMock(self.pm, 'is_power_on')
 
275
        self.pm._check_for_node().AndReturn(["testNode"])
 
276
        self.pm._run_command(self.pm._vp_cmd.reboot_cmd).AndReturn("Restarted")
 
277
        self.pm.is_power_on().AndReturn(False)
 
278
        self.mox.ReplayAll()
 
279
        state = self.pm.reboot_node()
 
280
        self.assertEqual(state, 'error')
 
281
        self.mox.VerifyAll()
 
282
 
 
283
    def test_is_power_on(self):
 
284
        self._create_node()
 
285
        self._create_pm()
 
286
 
 
287
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
288
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
289
        self.pm._check_for_node().AndReturn(["testNode"])
 
290
        self.pm._run_command(self.pm._vp_cmd.list_running_cmd).\
 
291
                AndReturn(["testNode"])
 
292
        self.pm._matched_name = 'testNode'
 
293
        self.mox.ReplayAll()
 
294
        state = self.pm.is_power_on()
 
295
        self.assertEqual(state, True)
 
296
        self.mox.VerifyAll()
 
297
 
 
298
    def test_is_power_on_fail(self):
 
299
        self._create_node()
 
300
        self._create_pm()
 
301
 
 
302
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
303
        self.mox.StubOutWithMock(self.pm, '_run_command')
 
304
        self.pm._check_for_node().AndReturn(["NotFoundNode"])
 
305
        self.pm._run_command(self.pm._vp_cmd.list_running_cmd).\
 
306
                AndReturn(["NotFoundNode"])
 
307
        self.pm._matched_name = 'testNode'
 
308
        self.mox.ReplayAll()
 
309
        state = self.pm.is_power_on()
 
310
        self.assertEqual(state, False)
 
311
        self.mox.VerifyAll()
 
312
 
 
313
    def test_run_command(self):
 
314
        self._create_node()
 
315
        self._create_pm()
 
316
 
 
317
        self.mox.StubOutWithMock(self.pm, '_set_connection')
 
318
        self.mox.StubOutWithMock(nutils, 'ssh_execute')
 
319
        self.pm._set_connection().AndReturn(True)
 
320
        nutils.ssh_execute(None, '/usr/bin/VBoxManage test return',
 
321
                check_exit_code=True).AndReturn(("test\nreturn", ""))
 
322
        self.pm._matched_name = 'testNode'
 
323
        self.mox.ReplayAll()
 
324
        result = self.pm._run_command("test return")
 
325
        self.assertEqual(result, ['test', 'return'])
 
326
        self.mox.VerifyAll()
 
327
 
 
328
    def test_run_command_raises_exception(self):
 
329
        self._create_node()
 
330
        self._create_pm()
 
331
 
 
332
        self.mox.StubOutWithMock(self.pm, '_set_connection')
 
333
        self.mox.StubOutWithMock(nutils, 'ssh_execute')
 
334
 
 
335
        self.pm._set_connection().AndReturn(True)
 
336
        nutils.ssh_execute(None, '/usr/bin/VBoxManage test return',
 
337
                check_exit_code=True).\
 
338
                AndRaise(exception.ProcessExecutionError)
 
339
        self.mox.ReplayAll()
 
340
 
 
341
        result = self.pm._run_command("test return")
 
342
        self.assertEqual(result, [])
 
343
        self.mox.VerifyAll()
 
344
 
 
345
    def test_activate_node_with_exception(self):
 
346
        self._create_node()
 
347
        self._create_pm()
 
348
 
 
349
        self.mox.StubOutWithMock(self.pm, '_check_for_node')
 
350
        self.mox.StubOutWithMock(nutils, 'ssh_execute')
 
351
 
 
352
        self.pm._check_for_node().AndReturn(["testNode"])
 
353
        self.pm._check_for_node().AndReturn(["testNode"])
 
354
        nutils.ssh_execute('test', '/usr/bin/VBoxManage startvm ',
 
355
                check_exit_code=True).\
 
356
                AndRaise(exception.ProcessExecutionError)
 
357
        nutils.ssh_execute('test', '/usr/bin/VBoxManage list runningvms',
 
358
                check_exit_code=True).\
 
359
                AndRaise(exception.ProcessExecutionError)
 
360
 
 
361
        self.mox.ReplayAll()
 
362
        self.pm._connection = 'test'
 
363
        state = self.pm.activate_node()
 
364
        self.assertEqual(state, 'error')
 
365
        self.mox.VerifyAll()