firmware_tools/autobuild/Common_scripts/test/powercontrollerutils.groovy
2023-01-08 14:01:08 +00:00

166 lines
No EOL
5.4 KiB
Groovy

//To Be called by the ethernet test jobs to perform REST actions against the relay controller
//Note, HTTP Calls only seem to be executed by the master node and not the slave. Therefore consider the networking environment.
import groovy.json.JsonSlurper
def RawGet(url,timeout=5000){
println(" Making Get to: " + url);
def returnObject = {};
try{
def get = new URL(url).openConnection();
get.setReadTimeout(timeout);
get.setConnectTimeout(timeout);
def returnCode = get.getResponseCode();
if (returnCode.equals(200)) {
r_returnString= get.getInputStream().getText();
}
returnObject = [
"code":returnCode,
"response":r_returnString];
}
catch (SocketTimeoutException | ConnectException e){
println("SocketTimeout or ConnectionException attempting to GET " + url);
returnObject = ["code":"0",
"response":"SocketTimeoutException"];
}
catch( Exception e){
println("Exception attempting to GET " + url);
returnObject = ["code":"0",
"response":"clientException"];
throw (e)
}
return returnObject
}
def HttpGet(url,timeout=5000){
println(" Making Get to: " + url);
def returnObject = {};
try{
def get = new URL(url).openConnection();
get.setReadTimeout(timeout);
get.setConnectTimeout(timeout);
def returnCode = get.getResponseCode();
if (returnCode.equals(200)) {
r_returnString= get.getInputStream().getText();
}
def parser = new JsonSlurper()
returnObject = [
"code":returnCode,
"response":parser.parseText(r_returnString)];
}
catch (SocketTimeoutException | ConnectException e){
println("SocketTimeout or ConnectionException attempting to GET " + url);
returnObject = ["code":"0",
"response":"SocketTimeoutException"];
}
catch( Exception e){
println("Exception attempting to GET " + url);
returnObject = ["code":"0",
"response":"clientException"];
throw (e)
}
return returnObject
}
def NullPost(url,timeout=5000){
println(" Making Post to: " + url);
def returnObject = {};
try{
def slurper = new JsonSlurper()
def post = new URL(url).openConnection();
def message = '""'
post.setReadTimeout(timeout);
post.setConnectTimeout(timeout);
post.setRequestMethod("POST");
post.setDoOutput(true);
post.setRequestProperty("Content-Type", "application/json");
post.getOutputStream().write(message.getBytes("UTF-8"));
def returnCode = post.getResponseCode();
if (returnCode.equals(200)) {
r_returnString= post.getInputStream().getText();
}
def parser = new JsonSlurper()
returnObject = [
"code":returnCode,
"response":parser.parseText(r_returnString)];
return returnObject
}
catch (SocketTimeoutException | ConnectException e){
println("SocketTimeout or ConnectionException attempting to POST " + url);
returnObject = ["code":"0",
"response":"SocketTimeoutException"];
}
catch( Exception e){
println("Exception attempting to POST " + url);
returnObject = ["code":"0",
"response":"clientException"];
throw (e)
}
return returnObject
}
def PowerDownHMB(Host,Device,Channel){
// Send a power up request to the relay controller and device addressed in the profile.
def path = "http://" + Host + ":9001/api/powercontrollers/" + Device + "/switchOff/" + Channel
def response = NullPost(path);
println("PowerdownPost code is: " + response["code"])
println("PowerdownPost reponse is: " + response["response"])
//Only Flag as working is the correct response it received.
if (response["response"]["name"] != "poweredDown"){
return false;
}
else {
return true
}
}
def PowerUpHMB(Host,Device,Channel){
// Send a power up request to the relay controller and device addressed in the profile.
def path = "http://" + Host + ":9001/api/powercontrollers/" + Device + "/switchOn/" + Channel
def response = NullPost(path);
println("PowerupPost code is: " + response["code"])
println("PowerupPost reponse is: " + response["response"])
//Only Flag as working is the correct response it received.
if (response["response"]["name"] != "poweredUp"){
return false;
}
else {
return true
}
}
def PollForLife(url, isJson = true,timeout = 24){
//Hardcode to 2 minute timeout. Need to work out default values for arguments.
//simple polling loop to establish if an endpoint is available.
def returnVal = false;
for (int i = 0; i<timeout; i++){
if (isJson){
if (HttpGet(url)["code"] == 200){
returnVal = true;
break;
}
}
else {
if (RawGet(url)["code"] == 200){
returnVal = true;
break;
}
}
}
return returnVal
}
def InspectHMBPowerState(HMBAddress){
def url = "http://"+ HMBAddress + ":8080/api/headmanagerboards/0/productinfo";
return PollForLife(url)
}
def InspectControlServiceState(HMBAddress){
def url = "http://"+ HMBAddress + ":8081";
return PollForLife(url,false)
}
powercontrollerutils = this;
return this;