Support both Python 2.x and Python 3.x

Change-Id: If2b02553859345a4322e0a2fd56ce1f7436c08ca
This commit is contained in:
Mark Hamzy 2016-10-26 21:48:10 +00:00
parent d9afc4be46
commit 0b641d436e
13 changed files with 186 additions and 138 deletions

View File

@ -21,6 +21,8 @@ Create the MoltenIron user in mysql and grant it access.
# pylint: disable-msg=C0103
from __future__ import print_function
import os
import sys
import yaml
@ -28,7 +30,7 @@ import yaml
def SQL(query):
"""Perform a mysql command"""
print os.popen("mysql -u root -p --execute=\"" + query + "\"").read()
print(os.popen("mysql -u root -p --execute=\"" + query + "\"").read())
def main():

View File

@ -23,12 +23,17 @@ a MoltenIron server.
# pylint: disable-msg=C0103
# pylint: disable=redefined-outer-name
from __future__ import print_function
import argparse
import httplib
import json
import sys
import os
import sys
import yaml
if (sys.version_info >= (3, 0)):
import http.client # noqa
else:
import httplib # noqa
DEBUG = False
@ -88,8 +93,12 @@ class MoltenIron(object):
def send(self, request):
"""Send the generated request """
connection = httplib.HTTPConnection(str(self.conf['serverIP']),
int(self.conf['mi_port']))
ip = str(self.conf['serverIP'])
port = int(self.conf['mi_port'])
if (sys.version_info > (3, 0)):
connection = http.client.HTTPConnection(ip, port) # noqa
else:
connection = httplib.HTTPConnection(ip, port) # noqa
connection.request('POST', '/', json.dumps(request))
response = connection.getresponse()
@ -316,7 +325,7 @@ if __name__ == "__main__":
# Register all decorated class functions by telling them argparse
# is running
for (cmd_name, cmd_func) in command.all.items():
for (cmd_name, cmd_func) in list(command.all.items()):
func = getattr(mi, cmd_name)
func(subparsers) # Tell the function to setup for argparse
@ -325,7 +334,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -339,12 +348,12 @@ if __name__ == "__main__":
mi.setup_argv(args)
mi.setup_parser(parser)
print mi.get_response()
print(mi.get_response())
try:
rc = mi.get_response_map()['status']
except KeyError:
print "Error: Server returned: %s" % (mi.get_response_map(),)
print("Error: Server returned: %s" % (mi.get_response_map(),))
rc = 444
if rc == 200:

View File

@ -22,6 +22,8 @@ This is a helper program for the MoltenIron server.
# pylint: disable-msg=C0103
# pylint: disable=redefined-outer-name
from __future__ import print_function
import argparse
import sys
import os
@ -83,7 +85,7 @@ def log_error(s):
with open(ERROR_LOGFILE, "a+") as fobj:
fobj.writelines(s + "\n")
print >> sys.stderr, s
print(s, file=sys.stderr)
if __name__ == "__main__":
@ -114,7 +116,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
YAML_CONF = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -122,14 +124,14 @@ if __name__ == "__main__":
if args.pid_dir:
if not os.path.isdir(args.pid_dir):
msg = "Error: %s is not a valid directory" % (args.pid_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
PID = os.path.realpath("%s/moltenirond.pid" % (args.pid_dir, ))
if args.verbose:
print "YAML_CONF = %s" % (YAML_CONF, )
print "PID = %s" % (PID, )
print("YAML_CONF = %s" % (YAML_CONF, ))
print("PID = %s" % (PID, ))
if len(args.command) != 1:
msg = "Error: Expecting one command? Received: %s" % (args.command, )

View File

@ -27,7 +27,8 @@ This is the MoltenIron server.
# pylint: disable-msg=C0103
# pylint: disable=redefined-outer-name
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from __future__ import print_function
import calendar
from datetime import datetime
import json
@ -52,6 +53,14 @@ from sqlalchemy.schema import MetaData, Table
import sqlalchemy_utils
from sqlalchemy.exc import OperationalError
import collections # noqa
if (sys.version_info >= (3, 0)):
from http.server import HTTPServer, BaseHTTPRequestHandler # noqa
else:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler # noqa
DEBUG = False
metadata = MetaData()
@ -102,7 +111,7 @@ def MakeMoltenIronHandlerWithConf(conf):
def send_reply(self, response):
"""Sends the HTTP reply"""
if DEBUG:
print "send_reply: response = %s" % (response,)
print("send_reply: response = %s" % (response,))
# get the status code off the response json and send it
status_code = response['status']
self.send_response(status_code)
@ -142,7 +151,7 @@ def MakeMoltenIronHandlerWithConf(conf):
response = {'status': 400, 'message': str(e)}
if DEBUG:
print "parse: response = %s" % (response,)
print("parse: response = %s" % (response,))
return response
@ -206,8 +215,9 @@ class Nodes(declarative_base()):
def map(self):
"""Returns a map of the database row contents"""
return {key: value for key, value
in self.__dict__.items()
if not key.startswith('_') and not callable(key)}
in list(self.__dict__.items())
if not key.startswith('_') and
not isinstance(key, collections.Callable)}
def __repr__(self):
fmt = """<Node(name='%s',
@ -360,10 +370,10 @@ class DataBase():
def close(self):
"""Close the sqlalchemy database engine"""
if DEBUG:
print "close: Calling engine.dispose()"
print("close: Calling engine.dispose()")
self.engine.dispose()
if DEBUG:
print "close: Finished"
print("close: Finished")
def get_session(self):
"""Get a SQL academy session from the pool """
@ -402,7 +412,7 @@ class DataBase():
yield conn
except Exception as e:
if DEBUG:
print "Exception caught in connection_scope: %s" % (e,)
print("Exception caught in connection_scope: %s" % (e,))
raise
finally:
conn.close()
@ -422,10 +432,10 @@ class DataBase():
# Nodes.__table__.create(self.engine, checkfirst=True)
# IPs.__table__.create(self.engine, checkfirst=True)
if DEBUG:
print "create_metadata: Calling metadata.create_all"
print("create_metadata: Calling metadata.create_all")
metadata.create_all(self.engine, checkfirst=True)
if DEBUG:
print "create_metadata: Finished"
print("create_metadata: Finished")
def to_timestamp(self, ts):
"""Convert from a database time stamp to a Python time stamp"""
@ -511,7 +521,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in deallocateBM: %s" % (e,)
print("Exception caught in deallocateBM: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -530,9 +540,16 @@ class DataBase():
query = session.query(Nodes.id, Nodes.ipmi_ip, Nodes.name)
if (isinstance(node_id, str) or
isinstance(node_id, unicode)) \
and ("." in node_id):
# WAS:
# if (isinstance(node_id, str) or
# isinstance(node_id, unicode)) \
# and ("." in node_id):
check = isinstance(node_id, str)
if (sys.version_info < (3, 0)):
check = check or isinstance(node_id, unicode) # noqa
if (check and ("." in node_id)):
# If an ipmi_ip was passed
query = query.filter_by(ipmi_ip=node_id)
else:
@ -554,7 +571,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in deallocateBM: %s" % (e,)
print("Exception caught in deallocateBM: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -578,7 +595,7 @@ class DataBase():
self.deallocateBM(node.id)
except Exception as e:
if DEBUG:
print "Exception caught in deallocateOwner: %s" % (e,)
print("Exception caught in deallocateOwner: %s" % (e,))
message = "Failed to deallocate node with ID %d" % (node.id,)
return {'status': 400, 'message': message}
@ -602,7 +619,7 @@ class DataBase():
try:
if DEBUG:
print "addBMNode: node = %s" % (node, )
print("addBMNode: node = %s" % (node, ))
with self.session_scope() as session, \
self.connection_scope() as conn:
@ -638,15 +655,15 @@ class DataBase():
if 'timestamp' in node:
timestamp_str = node['timestamp']
if DEBUG:
print "timestamp_str = %s" % (timestamp_str, )
print("timestamp_str = %s" % (timestamp_str, ))
if len(timestamp_str) != 0 and timestamp_str != "-1":
ts = time.gmtime(float(timestamp_str))
timestamp = self.to_timestamp(ts)
if DEBUG:
print "timestamp = %s" % (timestamp, )
print("timestamp = %s" % (timestamp, ))
stmt = stmt.values(timestamp=timestamp)
if DEBUG:
print stmt.compile().params
print(stmt.compile().params)
conn.execute(stmt)
@ -667,14 +684,14 @@ class DataBase():
stmt = stmt.values(node_id=new_node.id, ip=ip)
if DEBUG:
print stmt.compile().params
print(stmt.compile().params)
conn.execute(stmt)
except Exception as e:
if DEBUG:
print "Exception caught in addBMNode: %s" % (e,)
print("Exception caught in addBMNode: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -719,7 +736,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in removeBMNode: %s" % (e,)
print("Exception caught in removeBMNode: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -735,7 +752,7 @@ class DataBase():
"""
if DEBUG:
print "cull: maxSeconds = %s" % (maxSeconds, )
print("cull: maxSeconds = %s" % (maxSeconds, ))
nodes_culled = {}
@ -745,12 +762,12 @@ class DataBase():
nodes = session.query(Nodes)
if DEBUG:
print "There are %d nodes" % (nodes.count(), )
print("There are %d nodes" % (nodes.count(), ))
for node in nodes:
if DEBUG:
print node
print(node)
if node.timestamp in ('', '-1', None):
continue
@ -775,7 +792,7 @@ class DataBase():
log(self.conf, logstring)
if DEBUG:
print logstring
print(logstring)
self.deallocateBM(node.id)
@ -785,7 +802,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in cull: %s" % (e,)
print("Exception caught in cull: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -821,7 +838,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in doClean: %s" % (e,)
print("Exception caught in doClean: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -849,7 +866,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in get_ips: %s" % (e,)
print("Exception caught in get_ips: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -889,7 +906,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in get_field: %s" % (e,)
print("Exception caught in get_field: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -928,7 +945,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in set_field: %s" % (e,)
print("Exception caught in set_field: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -1025,7 +1042,7 @@ class DataBase():
except Exception as e:
if DEBUG:
print "Exception caught in status: %s" % (e,)
print("Exception caught in status: %s" % (e,))
# Don't send the exception object as it is not json serializable!
return {'status': 400, 'message': str(e)}
@ -1038,7 +1055,7 @@ def listener(conf):
mi_addr = str(conf['serverIP'])
mi_port = int(conf['mi_port'])
handler_class = MakeMoltenIronHandlerWithConf(conf)
print 'Listening... to %s:%d' % (mi_addr, mi_port,)
print('Listening... to %s:%d' % (mi_addr, mi_port,))
moltenirond = HTTPServer((mi_addr, mi_port), handler_class)
moltenirond.serve_forever()
@ -1130,7 +1147,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))

View File

@ -21,6 +21,8 @@ Tests the addBMNode MoltenIron command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -41,7 +43,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -115,24 +117,24 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret['status'] == 400
assert ret['message'] == "Node already exists"
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret['status'] == 400
assert ret['message'] == "Node already exists"
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret['status'] == 400
assert ret['message'] == "Node already exists"

View File

@ -21,6 +21,8 @@ Tests the MoltenIron allocateBM command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -55,7 +57,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -129,20 +131,20 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.allocateBM("hamzy", 1)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret["nodes"]) == 1
compare_provisioned_nodes(ret["nodes"]["node_1"], node1)
@ -158,20 +160,20 @@ if __name__ == "__main__":
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.allocateBM("hamzy", 2)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret["nodes"]) == 2
compare_provisioned_nodes(ret["nodes"]["node_1"], node1)
@ -188,20 +190,20 @@ if __name__ == "__main__":
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.allocateBM("hamzy", 3)
print ret
print(ret)
assert ret == {'status': 404,
'message': ('Not enough available nodes found. '
'Found 2, requested 3')}

View File

@ -21,6 +21,8 @@ Tests the MoltenIron cull command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -54,7 +56,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -129,20 +131,20 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.cull(1000)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['nodes']) == 2
@ -160,20 +162,20 @@ if __name__ == "__main__":
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.cull(2000)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['nodes']) == 1
@ -190,20 +192,20 @@ if __name__ == "__main__":
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.cull(3000)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['nodes']) == 0

View File

@ -21,6 +21,8 @@ Tests the MoltenIron deallocateBM command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -54,7 +56,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -128,20 +130,20 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.allocateBM("hamzy", 1)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret["nodes"]) == 1
compare_provisioned_nodes(ret["nodes"]["node_1"], node1)
@ -150,7 +152,7 @@ if __name__ == "__main__":
n1 = session.query(moltenirond.Nodes).filter_by(name=node1["name"]).one()
session.close()
ret = database.deallocateBM(n1.id)
print ret
print(ret)
assert ret['status'] == 200
database.close()
@ -164,20 +166,20 @@ if __name__ == "__main__":
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.allocateBM("hamzy", 1)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret["nodes"]) == 1
compare_provisioned_nodes(ret["nodes"]["node_1"], node1)
@ -186,7 +188,7 @@ if __name__ == "__main__":
n1 = session.query(moltenirond.Nodes).filter_by(name=node1["name"]).one()
session.close()
ret = database.deallocateBM(n1.ipmi_ip)
print ret
print(ret)
assert ret['status'] == 200
database.close()

View File

@ -21,6 +21,8 @@ Tests the MoltenIron deallocateOwner command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -54,7 +56,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -128,26 +130,26 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.allocateBM("hamzy", 1)
print ret
print(ret)
assert ret['status'] == 200
assert len(ret["nodes"]) == 1
compare_provisioned_nodes(ret["nodes"]["node_1"], node1)
ret = database.deallocateOwner("hamzy")
print ret
print(ret)
assert ret['status'] == 200
database.close()

View File

@ -21,6 +21,8 @@ Tests the MoltenIron doClean command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -41,7 +43,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -115,30 +117,30 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.doClean(1)
print ret
print(ret)
assert ret == {'status': 400, 'message': 'The node at 1 has status ready'}
ret = database.doClean(2)
print ret
print(ret)
assert ret == {'status': 400, 'message': 'The node at 2 has status ready'}
ret = database.doClean(3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.doClean(4)
print ret
print(ret)
assert ret == {'status': 200}

View File

@ -21,6 +21,8 @@ Tests the MoltenIron get_field command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -41,7 +43,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -115,33 +117,33 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.get_field("hamzy", "cpus")
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['result']) == 1
assert ret['result'][0]['field'] == node1["cpus"]
ret = database.get_field("mjturek", "port_hwaddr")
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['result']) == 2
assert ret['result'][0]['field'] == node2["port_hwaddr"]
assert ret['result'][1]['field'] == node4["port_hwaddr"]
ret = database.get_field("mmedvede", "candy")
print ret
print(ret)
assert ret == {'status': 400, 'message': 'field candy does not exist'}
database.close()

View File

@ -21,6 +21,8 @@ Tests the MoltenIron get_ips command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -41,7 +43,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -115,26 +117,26 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.get_ips("hamzy")
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['ips']) == 1
assert ret['ips'] == [node1["ipmi_ip"]]
ret = database.get_ips("mjturek")
print ret
print(ret)
assert ret['status'] == 200
assert len(ret['ips']) == 2
assert ret['ips'] == [node2["ipmi_ip"], node4["ipmi_ip"]]

View File

@ -21,6 +21,8 @@ Tests the MoltenIron removeBMNode command.
# pylint: disable-msg=C0103
from __future__ import print_function
import sys
import os
import yaml
@ -41,7 +43,7 @@ if __name__ == "__main__":
if args.conf_dir:
if not os.path.isdir(args.conf_dir):
msg = "Error: %s is not a valid directory" % (args.conf_dir, )
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)
yaml_file = os.path.realpath("%s/conf.yaml" % (args.conf_dir, ))
@ -115,23 +117,23 @@ if __name__ == "__main__":
# 8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
database = moltenirond.DataBase(conf, moltenirond.TYPE_SQLITE_MEMORY)
ret = database.addBMNode(node1)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node2)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node3)
print ret
print(ret)
assert ret == {'status': 200}
ret = database.addBMNode(node4)
print ret
print(ret)
assert ret == {'status': 200}
session = database.get_session()
n1 = session.query(moltenirond.Nodes).filter_by(name=node1["name"]).one()
session.close()
ret = database.removeBMNode(n1.id, False)
print ret
print(ret)
assert ret['status'] == 200
database.close()