Skip to content

Commit

Permalink
testing: adds system for regression testing
Browse files Browse the repository at this point in the history
Also added test for agents:
 * fence_apc
 * fence_docker
 * fence_ipmilan
  • Loading branch information
ondrejmular committed Jun 17, 2016
1 parent 9c8bcc0 commit 0914b30
Show file tree
Hide file tree
Showing 45 changed files with 2,349 additions and 5 deletions.
3 changes: 2 additions & 1 deletion fence/agents/cisco_ucs/fence_cisco_ucs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
sys.path.append("@FENCEAGENTSLIBDIR@")
from fencing import *
from fencing import fail, EC_STATUS, EC_LOGIN_DENIED, run_delay
import fencing_pycurl

#BEGIN_VERSION_GENERATION
RELEASE_VERSION="New Cisco UCS Agent - test release on steroids"
Expand Down Expand Up @@ -103,7 +104,7 @@ def send_command(opt, command, timeout):
url += "//" + opt["--ip"] + ":" + str(opt["--ipport"]) + "/nuova"

## send command through pycurl
conn = pycurl.Curl()
conn = fencing_pycurl.FencingPyCurl()
web_buffer = io.StringIO()
conn.setopt(pycurl.URL, url)
conn.setopt(pycurl.HTTPHEADER, ["Content-type: text/xml"])
Expand Down
3 changes: 2 additions & 1 deletion fence/agents/docker/fence_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

sys.path.append("@FENCEAGENTSLIBDIR@")
from fencing import fail_usage, all_opt, fence_action, atexit_handler, check_input, process_input, show_docs, run_delay
import fencing_pycurl

#BEGIN_VERSION_GENERATION
RELEASE_VERSION = ""
Expand Down Expand Up @@ -50,7 +51,7 @@ def get_list(conn, options):

def send_cmd(options, cmd, post = False):
url = "http%s://%s:%s/v%s/%s" % ("s" if "--ssl" in options else "", options["--ip"], options["--ipport"], options["--api-version"], cmd)
conn = pycurl.Curl()
conn = fencing_pycurl.FencingPyCurl()
output_buffer = io.StringIO()
if logging.getLogger().getEffectiveLevel() < logging.WARNING:
conn.setopt(pycurl.VERBOSE, True)
Expand Down
4 changes: 2 additions & 2 deletions fence/agents/lib/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
MAINTAINERCLEANFILES = Makefile.in

TARGET = fencing.py fencing_snmp.py
TARGET = fencing.py fencing_snmp.py fencing_pycurl.py

if BUILD_XENAPILIB
TARGET += XenAPI.py
endif

SRC = fencing.py.py fencing_snmp.py.py XenAPI.py.py check_used_options.py
SRC = fencing.py.py fencing_snmp.py.py XenAPI.py.py check_used_options.py fencing_pycurl.py.py

XSL = fence2man.xsl fence2rng.xsl fence2wiki.xsl

Expand Down
156 changes: 156 additions & 0 deletions fence/agents/lib/fencing_pycurl.py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
__author__ = 'Ondrej Mular <[email protected]>'
__all__ = ["FencingPyCurl"]

import pycurl
import sys
import atexit
import json
import StringIO
import time
import logging
import pprint

## do not add code here.
#BEGIN_VERSION_GENERATION
RELEASE_VERSION = ""
REDHAT_COPYRIGHT = ""
BUILD_DATE = ""
#END_VERSION_GENERATION

class FencingPyCurl():
active = False
input_file = None
output_file = None
options = None
actions = None
request_index = 0
output_buffer = None
write_function = None
pycurl_obj = None

def __init__(self):
if not FencingPyCurl.active and (FencingPyCurl.input_file or FencingPyCurl.output_file):
FencingPyCurl.active = True
logging.debug("FencingPyCurl is active now")
if FencingPyCurl.active:
self.options = {
"request": {},
"response": {},
"time": {}
}

self.output_buffer = StringIO.StringIO()
if FencingPyCurl.actions is None:
if FencingPyCurl.input_file:
logging.debug("Reading input file.")

try:
with open(FencingPyCurl.input_file, "r") as f:
FencingPyCurl.actions = json.load(f)
except Exception as e:
logging.debug("Reading input file (%s) failed: %s" % (FencingPyCurl.input_file, e.message))

if FencingPyCurl.output_file:
logging.debug("output file detected")
if not FencingPyCurl.actions:
FencingPyCurl.actions = []
FencingPyCurl.request_index = 0

self.pycurl_obj = pycurl.Curl()

def setopt(self, opt, value):
if FencingPyCurl.active:
if opt == pycurl.WRITEFUNCTION:
self.write_function = value
value = self.output_buffer.write
else:
self.options["request"][str(opt)] = value
return self.pycurl_obj.setopt(opt, value)

def perform(self):
if FencingPyCurl.active:
if FencingPyCurl.input_file:
perform_start = time.time()
if self.options["request"] == FencingPyCurl.actions[FencingPyCurl.request_index]["request"]:
self.options["response"] = FencingPyCurl.actions[FencingPyCurl.request_index]["response"]
if self.write_function:
self.write_function(self.options["response"]["output"])
diff = FencingPyCurl.actions[FencingPyCurl.request_index]["time"]["perform_duration"] - (time.time() - perform_start)
if diff > 0:
logging.debug("sleeping for: %s" % str(diff))
time.sleep(diff)
FencingPyCurl.last_request_time = time.time()
else:
print "Request:"
pprint.pprint(self.options["request"])
print "Expected:"
pprint.pprint(FencingPyCurl.actions[FencingPyCurl.request_index]["request"])
raise Exception("Invalid request")
else:
start_time = time.time()
self.pycurl_obj.perform()
self.options["time"]["perform_duration"] = FencingPyCurl.last_request_time - start_time
self.options["response"]["output"] = self.output_buffer.getvalue()
if self.write_function:
self.write_function(self.options["response"]["output"])
if FencingPyCurl.output_file:
FencingPyCurl.actions.append(self.options)
FencingPyCurl.request_index += 1
else:
return self.pycurl_obj.perform()

def unsetopt(self, opt):
if FencingPyCurl.active:
del self.options["request"][str(opt)]
return self.pycurl_obj.unsetopt(opt)

def getinfo(self, opt):
value = self.pycurl_obj.getinfo(opt)
if FencingPyCurl.active:
if FencingPyCurl.input_file:
value = self.options["response"][str(opt)]
else:
self.options["response"][opt] = value
return value

def reset(self):
if FencingPyCurl.active:
self.options.clear()
return self.pycurl_obj.reset()

def close(self):
return self.pycurl_obj.close()

@staticmethod
def save_log_to_file():
if FencingPyCurl.output_file and FencingPyCurl.actions:
logging.debug("Writing log to file: %s" % FencingPyCurl.output_file)
try:
with open(FencingPyCurl.output_file, "w") as f:
json.dump(FencingPyCurl.actions, f, sort_keys=True, indent=4, separators=(',', ': '))
except Exception as e:
logging.debug("Writing log to file (%s) failed: %s" % (FencingPyCurl.output_file, e.message))


def get_and_remove_arg(arg, has_value=True):
logging.debug("Getting arg: %s (has_value: %s)" % (arg, str(has_value)))
if not has_value:
if arg in sys.argv:
sys.argv.remove(arg)
logging.debug("%s: True" % arg)
return True
if arg in sys.argv:
index = sys.argv.index(arg)
sys.argv.remove(arg)
if len(sys.argv) > index:
value = sys.argv[index]
sys.argv.remove(value)
logging.debug("%s: %s" % (arg, value))
return value
return None

FencingPyCurl.input_file = get_and_remove_arg("--fencing_pycurl-log-in")
FencingPyCurl.output_file = get_and_remove_arg("--fencing_pycurl-log-out")

if FencingPyCurl.output_file:
atexit.register(FencingPyCurl.save_log_to_file)
3 changes: 2 additions & 1 deletion fence/agents/pve/fence_pve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
sys.path.append("@FENCEAGENTSLIBDIR@")
from fencing import fail, EC_LOGIN_DENIED, atexit_handler, all_opt, check_input, process_input, show_docs, fence_action, run_delay
import fencing_pycurl

if sys.version_info.major > 2: import urllib.parse as urllib
else: import urllib
Expand Down Expand Up @@ -95,7 +96,7 @@ def get_ticket(options):

def send_cmd(options, cmd, post=None):
url = options["url"] + cmd
conn = pycurl.Curl()
conn = fencing_pycurl.FencingPyCurl()
output_buffer = io.StringIO()
if logging.getLogger().getEffectiveLevel() < logging.WARNING:
conn.setopt(pycurl.VERBOSE, True)
Expand Down
2 changes: 2 additions & 0 deletions tests/actions.d/already-off.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Already OFF"
actions = [ { "command" : "off", "return_code" : "^0$" } ]
2 changes: 2 additions & 0 deletions tests/actions.d/already-on.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Already ON"
actions = [ { "command" : "on", "return_code" : "^0$" } ]
2 changes: 2 additions & 0 deletions tests/actions.d/off.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Off"
actions = [ { "command" : "off", "return_code" : "^0$" } ]
2 changes: 2 additions & 0 deletions tests/actions.d/on.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "On"
actions = [ { "command" : "on", "return_code" : "^0$" } ]
2 changes: 2 additions & 0 deletions tests/actions.d/reboot.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Reboot"
actions = [ { "command" : "reboot", "return_code" : "^0$" } ]
2 changes: 2 additions & 0 deletions tests/actions.d/status-off.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Status OFF"
actions = [ { "command" : "status", "return_code" : "^2$" } ]
2 changes: 2 additions & 0 deletions tests/actions.d/status-on.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Status ON"
actions = [ { "command" : "status", "return_code" : "^0$" } ]
Loading

0 comments on commit 0914b30

Please sign in to comment.