2828from OpTestIPMI import OpTestIPMI
2929from OpTestUtil import OpTestUtil
3030from OpTestBMC import OpTestBMC
31- from Exceptions import CommandFailed
31+ from Exceptions import CommandFailed , LoginFailure
3232from common .OpTestError import OpTestError
3333from OpTestConstants import OpTestConstants as BMC_CONST
3434from 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+
176203class 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:
0 commit comments