/usr/lib/python3/dist-packages/provisioningserver/drivers/power/tests/test_mscm.py is in python3-maas-provisioningserver 2.0.0~beta3+bzr4941-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | # Copyright 2015-2016 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for `provisioningserver.drivers.power.mscm`."""
__all__ = []
from io import BytesIO
from random import randint
import re
from socket import error as SOCKETError
from textwrap import dedent
from hypothesis import given
from hypothesis.strategies import sampled_from
from maastesting.factory import factory
from maastesting.matchers import (
MockCalledOnceWith,
MockCallsMatch,
)
from maastesting.testcase import (
MAASTestCase,
MAASTwistedRunTest,
)
from mock import (
call,
Mock,
)
from paramiko import SSHException
from provisioningserver.drivers.power import (
mscm as mscm_module,
PowerActionError,
PowerConnError,
PowerFatalError,
)
from provisioningserver.drivers.power.mscm import (
cartridge_mapping,
MSCMPowerDriver,
probe_and_enlist_mscm,
)
from provisioningserver.utils.twisted import asynchronous
from testtools.matchers import Equals
from testtools.testcase import ExpectedException
from twisted.internet.defer import inlineCallbacks
from twisted.internet.threads import deferToThread
def make_node_id():
"""Make a node_id."""
return 'c%sn%s' % (randint(1, 45), randint(1, 8))
NODE_LIST = dedent("""\
Slot ID Proc Manufacturer Architecture Memory Power Status
---- ----- ---------------------- -------------------- ------ ----- ------
1 %s Intel Corporation x86 Architecture 32 GB Off OK
...
""")
NODE_INFO = dedent("""\
c1: #Cartridge %s
Type: Compute
Manufacturer: HP
Product Name: %s
...
""")
NODE_MACADDR = dedent("""\
Slot ID NIC 1 (Switch A) NIC 2 (Switch B) NIC 3 (Switch A) NIC 4 (Switch B)
---- -- ---------------- ---------------- ---------------- ----------------
1 %s %s %s N/A N/A
...
""")
def make_context():
"""Make and return a power parameters context."""
return {
'node_id': factory.make_name('node_id'),
'power_address': factory.make_name('power_address'),
'power_user': factory.make_name('power_user'),
'power_pass': factory.make_name('power_pass'),
}
class TestMSCMPowerDriver(MAASTestCase):
def test_missing_packages(self):
# there's nothing to check for, just confirm it returns []
driver = mscm_module.MSCMPowerDriver()
missing = driver.detect_missing_packages()
self.assertItemsEqual([], missing)
def test_run_mscm_command_returns_command_output(self):
driver = MSCMPowerDriver()
command = factory.make_name('command')
context = make_context()
SSHClient = self.patch(mscm_module, "SSHClient")
AutoAddPolicy = self.patch(mscm_module, "AutoAddPolicy")
ssh_client = SSHClient.return_value
expected = factory.make_name('output').encode('utf-8')
stdout = BytesIO(expected)
streams = factory.make_streams(stdout=stdout)
ssh_client.exec_command = Mock(return_value=streams)
output = driver.run_mscm_command(command, **context)
self.expectThat(expected.decode('utf-8'), Equals(output))
self.expectThat(SSHClient, MockCalledOnceWith())
self.expectThat(
ssh_client.set_missing_host_key_policy, MockCalledOnceWith(
AutoAddPolicy.return_value))
self.expectThat(
ssh_client.connect, MockCalledOnceWith(
context['power_address'], username=context['power_user'],
password=context['power_pass']))
self.expectThat(ssh_client.exec_command, MockCalledOnceWith(command))
@given(sampled_from([SSHException, EOFError, SOCKETError]))
def test_run_mscm_command_crashes_for_ssh_connection_error(self, error):
driver = MSCMPowerDriver()
command = factory.make_name('command')
context = make_context()
self.patch(mscm_module, "AutoAddPolicy")
SSHClient = self.patch(mscm_module, "SSHClient")
ssh_client = SSHClient.return_value
ssh_client.connect.side_effect = error
self.assertRaises(
PowerConnError, driver.run_mscm_command, command, **context)
def test_power_on_calls_run_mscm_command(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
power_query = self.patch(driver, "power_query")
power_query.return_value = 'on'
self.patch(driver, "power_off")
self.patch(driver, "configure_node_bootonce_pxe")
run_mscm_command = self.patch(driver, "run_mscm_command")
driver.power_on(system_id, context)
self.assertThat(
run_mscm_command, MockCallsMatch(
call(
"set node bootonce pxe %s"
% context['node_id'], **context),
call("set node power on %s" % context['node_id'], **context)))
def test_power_on_crashes_for_connection_error(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
power_query = self.patch(driver, "power_query")
power_query.return_value = 'off'
self.patch(driver, "configure_node_bootonce_pxe")
run_mscm_command = self.patch(driver, "run_mscm_command")
run_mscm_command.side_effect = PowerConnError("Connection Error")
self.assertRaises(
PowerActionError, driver.power_on, system_id, context)
def test_power_off_calls_run_mscm_command(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
run_mscm_command = self.patch(driver, "run_mscm_command")
driver.power_off(system_id, context)
self.assertThat(
run_mscm_command, MockCalledOnceWith(
"set node power off force %s" % context['node_id'], **context))
def test_power_off_crashes_for_connection_error(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
run_mscm_command = self.patch(driver, "run_mscm_command")
run_mscm_command.side_effect = PowerConnError("Connection Error")
self.assertRaises(
PowerActionError, driver.power_off, system_id, context)
def test_power_query_returns_power_state_on(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
run_mscm_command = self.patch(driver, "run_mscm_command")
run_mscm_command.return_value = (
"show node power c1n1\r\r\n\r\nCartridge #1\r\n Node #1\r\n"
" Power State: On\r\n")
output = driver.power_query(system_id, context)
self.assertThat(output, Equals('on'))
@given(sampled_from(['Off', 'Unavailable', 'On']))
def test_power_query_returns_power_state(self, power_state):
states = {
'Off': 'off',
'Unavailable': 'off',
'On': 'on',
}
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
run_mscm_command = self.patch(driver, "run_mscm_command")
run_mscm_command.return_value = (
"show node power c1n1\r\r\n\r\nCartridge #1\r\n Node #1\r\n"
" Power State: %s\r\n" % power_state)
output = driver.power_query(system_id, context)
self.assertThat(output, Equals(states[power_state]))
def test_power_query_crashes_for_connection_error(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
run_mscm_command = self.patch(driver, "run_mscm_command")
run_mscm_command.side_effect = PowerConnError("Connection Error")
self.assertRaises(
PowerActionError, driver.power_query, system_id, context)
def test_power_query_crashes_when_unable_to_find_match(self):
driver = MSCMPowerDriver()
system_id = factory.make_name('system_id')
context = make_context()
run_mscm_command = self.patch(driver, "run_mscm_command")
run_mscm_command.return_value = "Rubbish"
self.assertRaises(
PowerFatalError, driver.power_query, system_id, context)
class TestMSCMProbeAndEnlist(MAASTestCase):
run_tests_with = MAASTwistedRunTest.make_factory(timeout=5)
scenarios = [
(key, dict(product_name=key, arch=value))
for key, value in cartridge_mapping.items()
] + [('Fake', {'arch': 'fake', 'product_name': 'fake'})]
@inlineCallbacks
def test_probe_and_enlist(self):
node_id = make_node_id()
node_list = NODE_LIST % node_id
node_info = NODE_INFO % (node_id, self.product_name)
node_macaddr = NODE_MACADDR % (
node_id, factory.make_mac_address(), factory.make_mac_address())
macs = re.findall(r':'.join(['[0-9a-f]{2}'] * 6), node_macaddr)
user = factory.make_name('user')
host = factory.make_hostname('mscm')
username = factory.make_name('user')
password = factory.make_name('password')
domain = factory.make_name('domain')
system_id = factory.make_name('system_id')
Driver = self.patch(mscm_module, "MSCMPowerDriver")
mscm_driver = Driver.return_value
mscm_driver.run_mscm_command.side_effect = (
node_list, None, node_info, node_macaddr)
create_node = self.patch(mscm_module, 'create_node')
create_node.side_effect = asynchronous(lambda *args: system_id)
commission_node = self.patch(mscm_module, 'commission_node')
params = {
'power_address': host,
'power_user': username,
'power_pass': password,
'node_id': node_id,
}
yield deferToThread(
probe_and_enlist_mscm,
user, host, username, password, True, domain)
self.expectThat(
create_node,
MockCalledOnceWith(macs, self.arch, 'mscm', params, domain))
self.expectThat(
commission_node,
MockCalledOnceWith(system_id, user))
class TestMSCMProbeAndEnlistCrashesNoMatch(MAASTestCase):
run_tests_with = MAASTwistedRunTest.make_factory(timeout=5)
@inlineCallbacks
def test_probe_and_enlist_mscm_crashes_for_no_match(self):
node_id = make_node_id()
node_list = NODE_LIST % node_id
user = factory.make_name('user')
host = factory.make_hostname('mscm')
username = factory.make_name('user')
password = factory.make_name('password')
Driver = self.patch(mscm_module, "MSCMPowerDriver")
mscm_driver = Driver.return_value
mscm_driver.run_mscm_command.side_effect = (node_list, None, 'Error')
with ExpectedException(PowerFatalError):
yield deferToThread(
probe_and_enlist_mscm, user, host, username, password)
|