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
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# coding=utf-8
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2012 NTT DOCOMO, INC.
# All Rights Reserved.
#
# 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.
"""Test class for baremetal IPMI power manager."""
import os
import stat
import tempfile
from oslo.config import cfg
from ironic import test
from ironic.common import states
from ironic.common import utils
from ironic.tests.db import utils as db_utils
from ironic.drivers import ipmi
CONF = cfg.CONF
class BareMetalIPMITestCase(test.TestCase):
def setUp(self):
super(BareMetalIPMITestCase, self).setUp()
self.node = db_utils.get_test_node()
self.ipmi = ipmi.IPMIPowerDriver(self.node)
def test_construct(self):
self.assertEqual(self.ipmi.node_id, 123)
self.assertEqual(self.ipmi.address, 'fake-address')
self.assertEqual(self.ipmi.user, 'fake-user')
self.assertEqual(self.ipmi.password, 'fake-password')
def test_make_password_file(self):
fakepass = 'this is a fake password'
pw_file = ipmi._make_password_file(fakepass)
try:
self.assertTrue(os.path.isfile(pw_file))
self.assertEqual(os.stat(pw_file)[stat.ST_MODE] & 0777, 0600)
with open(pw_file, "r") as f:
password = f.read()
self.assertEqual(password, fakepass)
finally:
os.unlink(pw_file)
def test_exec_ipmitool(self):
pw_file = '/tmp/password_file'
self.mox.StubOutWithMock(ipmi, '_make_password_file')
self.mox.StubOutWithMock(utils, 'execute')
self.mox.StubOutWithMock(utils, 'delete_if_exists')
ipmi._make_password_file(self.ipmi.password).AndReturn(pw_file)
args = [
'ipmitool',
'-I', 'lanplus',
'-H', self.ipmi.address,
'-U', self.ipmi.user,
'-f', pw_file,
'A', 'B', 'C',
]
utils.execute(*args, attempts=3).AndReturn(('', ''))
utils.delete_if_exists(pw_file).AndReturn(None)
self.mox.ReplayAll()
self.ipmi._exec_ipmitool('A B C')
self.mox.VerifyAll()
def test_update_state_on(self):
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is on\n"])
self.mox.ReplayAll()
self.ipmi.state = states.NOSTATE
self.ipmi._update_state()
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.POWER_ON)
def test_update_state_off(self):
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.mox.ReplayAll()
self.ipmi.state = states.NOSTATE
self.ipmi._update_state()
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.POWER_OFF)
def test_update_state_error(self):
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is foobar\n"])
self.mox.ReplayAll()
self.ipmi.state = states.NOSTATE
self.ipmi._update_state()
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.ERROR)
def test_set_power_on_ok(self):
self.flags(ipmi_power_retry=0)
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("set bootdev pxe").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.ipmi._exec_ipmitool("power on").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is on\n"])
self.mox.ReplayAll()
self.ipmi.state = states.POWER_OFF
self.ipmi.set_power_state(states.POWER_ON)
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.POWER_ON)
def test_set_power_off_ok(self):
self.flags(ipmi_power_retry=0)
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is on\n"])
self.ipmi._exec_ipmitool("power off").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.mox.ReplayAll()
self.ipmi.state = states.POWER_ON
self.ipmi._power_off()
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.POWER_OFF)
def test_power_on_fail(self):
self.flags(ipmi_power_retry=0)
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.ipmi._exec_ipmitool("power on").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.mox.ReplayAll()
self.ipmi.state = states.POWER_OFF
self.ipmi._power_on()
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.ERROR)
def test_power_on_max_retries(self):
self.flags(ipmi_power_retry=2)
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.ipmi._exec_ipmitool("power on").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.ipmi._exec_ipmitool("power on").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.ipmi._exec_ipmitool("power on").AndReturn([])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.mox.ReplayAll()
self.ipmi.state = states.POWER_OFF
self.ipmi._power_on()
self.mox.VerifyAll()
self.assertEqual(self.ipmi.state, states.ERROR)
self.assertEqual(self.ipmi.retries, 3)
def test_get_power_state(self):
self.mox.StubOutWithMock(self.ipmi, '_exec_ipmitool')
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is off\n"])
self.ipmi._exec_ipmitool("power status").AndReturn(
["Chassis Power is on\n"])
self.mox.ReplayAll()
self.ipmi.state = states.POWER_OFF
pstate = self.ipmi.get_power_state()
self.assertEqual(pstate, states.POWER_OFF)
self.ipmi.state = states.POWER_ON
pstate = self.ipmi.get_power_state()
self.assertEqual(pstate, states.POWER_ON)
self.mox.VerifyAll()
def test_reboot(self):
# TODO
pass
def test_get_console_pid_path(self):
self.flags(terminal_pid_dir='/tmp')
path = ipmi._get_console_pid_path(self.ipmi.node_id)
self.assertEqual(path, '/tmp/%s.pid' % self.ipmi.node_id)
def test_console_pid(self):
fd, path = tempfile.mkstemp()
with os.fdopen(fd, 'w') as f:
f.write("12345\n")
self.mox.StubOutWithMock(ipmi, '_get_console_pid_path')
ipmi._get_console_pid_path(self.ipmi.node_id).AndReturn(path)
self.mox.ReplayAll()
pid = ipmi._get_console_pid(self.ipmi.node_id)
utils.delete_if_exists(path)
self.mox.VerifyAll()
self.assertEqual(pid, 12345)
def test_console_pid_nan(self):
fd, path = tempfile.mkstemp()
with os.fdopen(fd, 'w') as f:
f.write("hello world\n")
self.mox.StubOutWithMock(ipmi, '_get_console_pid_path')
ipmi._get_console_pid_path(self.ipmi.node_id).AndReturn(path)
self.mox.ReplayAll()
pid = ipmi._get_console_pid(self.ipmi.node_id)
utils.delete_if_exists(path)
self.mox.VerifyAll()
self.assertTrue(pid is None)
def test_console_pid_file_not_found(self):
pid_path = ipmi._get_console_pid_path(self.ipmi.node_id)
self.mox.StubOutWithMock(os.path, 'exists')
os.path.exists(pid_path).AndReturn(False)
self.mox.ReplayAll()
pid = ipmi._get_console_pid(self.ipmi.node_id)
self.mox.VerifyAll()
self.assertTrue(pid is None)
|