28
28
from OpTestIPMI import OpTestIPMI
29
29
from OpTestUtil import OpTestUtil
30
30
from OpTestBMC import OpTestBMC
31
- from Exceptions import CommandFailed
31
+ from Exceptions import CommandFailed , LoginFailure
32
32
from common .OpTestError import OpTestError
33
33
from OpTestConstants import OpTestConstants as BMC_CONST
34
34
from common import OPexpect
@@ -51,6 +51,8 @@ def __init__(self, binary="curl",
51
51
self .password = password
52
52
self .binary = binary
53
53
self .logresult = True
54
+ self .cmd_bkup = ""
55
+ self .login_retry = 0
54
56
55
57
def feed_data (self , dbus_object = None , action = None ,
56
58
operation = None , command = None ,
@@ -137,8 +139,13 @@ def arguments(self):
137
139
args += self .dbus_interface ()
138
140
return args
139
141
142
+ def bkup_cmd (self , cmd ):
143
+ self .cmd_bkup = cmd
144
+
140
145
def run (self , background = False , cmdprefix = None ):
141
- if cmdprefix :
146
+ if self .cmd_bkup :
147
+ cmd = self .cmd_bkup
148
+ elif cmdprefix :
142
149
cmd = cmdprefix + self .binary + self .arguments () + cmd
143
150
else :
144
151
cmd = self .binary + self .arguments ()
@@ -155,16 +162,26 @@ def run(self, background=False, cmdprefix=None):
155
162
# TODO - need python 2.7
156
163
# output = check_output(cmd, stderr=subprocess.STDOUT, shell=True)
157
164
try :
158
- cmd = subprocess .Popen (cmd , stderr = subprocess .STDOUT ,
165
+ obj = subprocess .Popen (cmd , stderr = subprocess .STDOUT ,
159
166
stdout = subprocess .PIPE , shell = True )
160
167
except Exception as e :
161
168
l_msg = "Curl Command Failed"
162
169
print l_msg
163
170
print str (e )
164
171
raise OpTestError (l_msg )
165
- output = cmd .communicate ()[0 ]
172
+ output = obj .communicate ()[0 ]
166
173
if self .logresult :
167
174
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 = ""
168
185
if '"status": "error"' in output :
169
186
print output
170
187
raise FailedCurlInvocation (cmd , output )
@@ -173,6 +190,16 @@ def run(self, background=False, cmdprefix=None):
173
190
def log_result (self ):
174
191
self .logresult = True
175
192
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
+
176
203
class HostManagement ():
177
204
def __init__ (self , ip = None , username = None , password = None ):
178
205
self .hostname = ip
@@ -182,7 +209,9 @@ def __init__(self, ip=None, username=None, password=None):
182
209
username = username ,
183
210
password = password )
184
211
self .util = OpTestUtil ()
212
+ self .util .PingFunc (self .hostname , BMC_CONST .PING_RETRY_FOR_STABILITY )
185
213
self .login ()
214
+ self .wait_for_bmc_runtime ()
186
215
187
216
'''
188
217
curl -c cjar -k -X POST -H "Content-Type: application/json" \
@@ -191,7 +220,12 @@ def __init__(self, ip=None, username=None, password=None):
191
220
def login (self ):
192
221
data = '\' {"data": [ "%s", "%s" ] }\' ' % (self .username , self .password )
193
222
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" )
195
229
196
230
'''
197
231
Logout:
0 commit comments