Skip to content

Commit 1942203

Browse files
committed
Merge remote-tracking branch 'pridhiviraj/openbmc_fixes'
2 parents e989656 + 7194cf2 commit 1942203

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

common/Exceptions.py

+18
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ def __init__(self, command, output, exitcode):
3737
def __str__(self):
3838
return "Command '%s' exited with %d.\nOutput:\n%s" % (self.command, self.exitcode, self.output)
3939

40+
class SSHSessionDisconnected(Exception):
41+
'''
42+
SSH session/console was disconnected unexpectedly. e.g. it may have crashed
43+
'''
44+
def __init__(self, notice):
45+
self.notice = notice
46+
def __str__(self):
47+
return "SSH session/console disconnected due to '%s'" % self.notice
48+
49+
class LoginFailure(Exception):
50+
'''
51+
SSH/IPMI Login failure - username/password may be wrong
52+
'''
53+
def __init__(self, notice):
54+
self.notice = notice
55+
def __str__(self):
56+
return "Login failure due to '%s'" % self.notice
57+
4058
class BMCDisconnected(Exception):
4159
'''
4260
BMC Cosnole was disconnected unexpectedly. e.g. it may have crashed

common/OpTestBMC.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
import subprocess
3838
from OpTestIPMI import OpTestIPMI
3939
from OpTestSSH import OpTestSSH
40+
from OpTestUtil import OpTestUtil
4041
from OpTestConstants import OpTestConstants as BMC_CONST
4142
from OpTestError import OpTestError
4243
from OpTestWeb import OpTestWeb
43-
from Exceptions import CommandFailed
44+
from Exceptions import CommandFailed, SSHSessionDisconnected
4445

4546
class OpTestBMC():
4647
def __init__(self, ip=None, username=None, password=None,
@@ -57,6 +58,7 @@ def __init__(self, ip=None, username=None, password=None,
5758
self.known_hosts_file = known_hosts_file
5859
self.ssh = OpTestSSH(ip, username, password, logfile, prompt='\[PEXPECT\]#',
5960
check_ssh_keys=check_ssh_keys, known_hosts_file=known_hosts_file)
61+
self.util = OpTestUtil()
6062

6163
def bmc_host(self):
6264
return self.cv_bmcIP
@@ -87,10 +89,13 @@ def reboot(self):
8789
retries = 0
8890
try:
8991
self.ssh.run_command('reboot')
90-
except pexpect.EOF:
92+
except SSHSessionDisconnected:
9193
pass
9294
print 'Sent reboot command now waiting for reboot to complete...'
93-
time.sleep(10)
95+
# Wait for BMC to go down.
96+
self.util.ping_fail_check(self.cv_bmcIP)
97+
# Wait for BMC to ping back.
98+
self.util.PingFunc(self.cv_bmcIP, BMC_CONST.PING_RETRY_FOR_STABILITY)
9499
''' Ping the system until it reboots '''
95100
while True:
96101
try:

common/OpTestOpenBMC.py

+39-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from OpTestIPMI import OpTestIPMI
2929
from OpTestUtil import OpTestUtil
3030
from OpTestBMC import OpTestBMC
31-
from Exceptions import CommandFailed
31+
from Exceptions import CommandFailed, LoginFailure
3232
from common.OpTestError import OpTestError
3333
from OpTestConstants import OpTestConstants as BMC_CONST
3434
from common import OPexpect
@@ -51,6 +51,8 @@ def __init__(self, binary="curl",
5151
self.password = password
5252
self.binary = binary
5353
self.logresult = True
54+
self.cmd_bkup = ""
55+
self.login_retry = 0
5456

5557
def feed_data(self, dbus_object=None, action=None,
5658
operation=None, command=None,
@@ -137,8 +139,13 @@ def arguments(self):
137139
args += self.dbus_interface()
138140
return args
139141

142+
def bkup_cmd(self, cmd):
143+
self.cmd_bkup = cmd
144+
140145
def run(self, background=False, cmdprefix=None):
141-
if cmdprefix:
146+
if self.cmd_bkup:
147+
cmd = self.cmd_bkup
148+
elif cmdprefix:
142149
cmd = cmdprefix + self.binary + self.arguments() + cmd
143150
else:
144151
cmd = self.binary + self.arguments()
@@ -155,16 +162,26 @@ def run(self, background=False, cmdprefix=None):
155162
# TODO - need python 2.7
156163
# output = check_output(cmd, stderr=subprocess.STDOUT, shell=True)
157164
try:
158-
cmd = subprocess.Popen(cmd, stderr=subprocess.STDOUT,
165+
obj = subprocess.Popen(cmd, stderr=subprocess.STDOUT,
159166
stdout=subprocess.PIPE, shell=True)
160167
except Exception as e:
161168
l_msg = "Curl Command Failed"
162169
print l_msg
163170
print str(e)
164171
raise OpTestError(l_msg)
165-
output = cmd.communicate()[0]
172+
output = obj.communicate()[0]
166173
if self.logresult:
167174
print output
175+
if '"description": "Login required"' in output:
176+
if self.login_retry > 5:
177+
raise LoginFailure("Rest Login retry exceeded")
178+
output = ""
179+
cmd_bkup = cmd
180+
self.login()
181+
self.login_retry += 1
182+
self.bkup_cmd(cmd_bkup)
183+
output = self.run()
184+
self.cmd_bkup = ""
168185
if '"status": "error"' in output:
169186
print output
170187
raise FailedCurlInvocation(cmd, output)
@@ -173,6 +190,16 @@ def run(self, background=False, cmdprefix=None):
173190
def log_result(self):
174191
self.logresult = True
175192

193+
def login(self):
194+
data = '\'{"data": [ "%s", "%s" ] }\'' % (self.username, self.password)
195+
self.feed_data(dbus_object="/login", operation='w', command="POST", data=data)
196+
try:
197+
output = self.run()
198+
except FailedCurlInvocation as fci:
199+
output = fci.output
200+
if '"description": "Invalid username or password"' in output:
201+
raise LoginFailure("Rest login invalid username or password")
202+
176203
class HostManagement():
177204
def __init__(self, ip=None, username=None, password=None):
178205
self.hostname = ip
@@ -182,7 +209,9 @@ def __init__(self, ip=None, username=None, password=None):
182209
username=username,
183210
password=password)
184211
self.util = OpTestUtil()
212+
self.util.PingFunc(self.hostname, BMC_CONST.PING_RETRY_FOR_STABILITY)
185213
self.login()
214+
self.wait_for_bmc_runtime()
186215

187216
'''
188217
curl -c cjar -k -X POST -H "Content-Type: application/json" \
@@ -191,7 +220,12 @@ def __init__(self, ip=None, username=None, password=None):
191220
def login(self):
192221
data = '\'{"data": [ "%s", "%s" ] }\'' % (self.username, self.password)
193222
self.curl.feed_data(dbus_object="/login", operation='w', command="POST", data=data)
194-
self.curl.run()
223+
try:
224+
output = self.curl.run()
225+
except FailedCurlInvocation as fci:
226+
output = fci.output
227+
if '"description": "Invalid username or password"' in output:
228+
raise LoginFailure("Rest login invalid username or password")
195229

196230
'''
197231
Logout:

common/OpTestSSH.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import time
2323
import pexpect
2424

25-
from Exceptions import CommandFailed
25+
from Exceptions import CommandFailed, SSHSessionDisconnected
2626
import OpTestSystem
2727
try:
2828
from common import OPexpect
@@ -131,9 +131,9 @@ def set_unique_prompt(self, console):
131131
except pexpect.EOF as cf:
132132
print cf
133133
if self.check_ssh_keys:
134-
raise Exception("SSH session exited early - bad host key?")
134+
raise SSHSessionDisconnected("SSH session exited early - bad host key?")
135135
else:
136-
raise Exception("SSH session exited early!")
136+
raise SSHSessionDisconnected("SSH session exited early!")
137137

138138
def build_prompt(self):
139139
if self.prompt:
@@ -212,7 +212,7 @@ def retry_password(self, console, command):
212212
raise CommandFailed(command, 'Retry Password TIMEOUT ' + ''.join(retry_list_output), -1)
213213
elif (rc == 3):
214214
self.state = ConsoleState.DISCONNECTED
215-
raise Exception("SSH session exited early!")
215+
raise SSHSessionDisconnected("SSH session exited early!")
216216

217217
return retry_list_output, echo_rc
218218

@@ -249,7 +249,7 @@ def handle_password(self, console, command):
249249
failure_list_output += handle_list_output
250250
if (rc == 3):
251251
self.state = ConsoleState.DISCONNECTED
252-
raise Exception("SSH session exited early!")
252+
raise SSHSessionDisconnected("SSH session exited early!")
253253
else:
254254
raise CommandFailed(command, ''.join(failure_list_output), -1)
255255
return list_output, echo_rc
@@ -292,7 +292,7 @@ def run_command(self, command, timeout=60):
292292
output_list, echo_rc = self.try_sendcontrol(console, command)
293293
else:
294294
self.state = ConsoleState.DISCONNECTED
295-
raise Exception("SSH session exited early!")
295+
raise SSHSessionDisconnected("SSH session exited early!")
296296
res = output_list
297297
if echo_rc != 0:
298298
raise CommandFailed(command, res, echo_rc)

0 commit comments

Comments
 (0)