Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix column_range type error/refact api_server use class

  • Loading branch information...
commit 127ddced31d6eafd78a1a4ebe3dfe6087642dda4 1 parent b475cc6
@hokerffb hokerffb authored
View
11 bin/api-server
@@ -33,9 +33,10 @@ if __name__ == '__main__':
config = ConfigParser.ConfigParser()
config.read("kanyun.conf")
api_cfg = dict(config.items('api'))
+
+ api = api_server.ApiServer(db_host=api_cfg['db_host'])
context = zmq.Context()
-
api_socket = context.socket(zmq.REP)
api_socket.bind("tcp://%(api_host)s:%(api_port)s" % api_cfg)
print "listen tcp://%(api_host)s:%(api_port)s" % api_cfg
@@ -55,7 +56,7 @@ if __name__ == '__main__':
period = msg[5]
time_from = msg[6]
time_to = msg[7]
- rs, count, _ = api_server.api_statistic(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
+ rs, count, _ = api.statistic(row_id, cf_str, scf_str, statistic, period=period, time_from=time_from, time_to=time_to)
api_socket.send(json.dumps(rs))
elif msg[0] == 'G':
print '*' * 60
@@ -63,18 +64,18 @@ if __name__ == '__main__':
row_id = msg[1]
cf_str = msg[2]
scf_str = msg[3]
- rs, count, _ = api_server.api_getbykey(row_id, cf_str, scf_str)
+ rs, count, _ = api.get_by_key(row_id, cf_str, scf_str)
api_socket.send(json.dumps(rs))
elif msg[0] == 'K':
print '*' * 60
print "recv:", msg
row_id = msg[1]
cf_str = msg[2]
- rs, count, _ = api_server.api_get_by_instance_id(row_id, cf_str)
+ rs, count, _ = api.get_by_instance_id(row_id, cf_str)
api_socket.send(json.dumps(rs))
elif msg[0] == 'L':
cf_str = msg[1]
- rs = api_server.api_get_instances_list(cf_str)
+ rs = api.get_instances_list(cf_str)
api_socket.send(json.dumps(rs))
else:
api_socket.send(json.dumps([]))
View
1  bin/kanyun.conf
@@ -28,5 +28,6 @@ feedback_port: 5551
log: /tmp/kanyun-worker.log
[client]
+#api_host: 10.210.228.23
api_host: 123.126.53.174
api_port: 5556
View
1  kanyun/database/cassadb.py
@@ -59,6 +59,7 @@ def get(self, cf_str, key, super_column,
"""get(key[, columns][, column_start][, column_finish][, column_count]
[, column_reversed][, include_timestamp][, super_column][, read_consistency_level])
"""
+ # column_start, column_finish must be IntType
rs = None
cf = self.get_cf(cf_str)
if cf is None or key is None or super_column is None or column_start is None or column_finish is None:
View
498 kanyun/server/api_server.py
@@ -36,10 +36,10 @@
[u'S', u'instance-000001@pyw.novalocal', u'cpu', u'total', 0, 5, 1332897600, 0]
"""
-logger = logging.getLogger()
-handler = logging.FileHandler("/tmp/api-server.log")
-logger.addHandler(handler)
-logger.setLevel(logging.NOTSET)
+#logger = logging.getLogger()
+#handler = logging.FileHandler("/tmp/api-server.log")
+#logger.addHandler(handler)
+#logger.setLevel(logging.NOTSET)
class Statistics():
@@ -110,175 +110,343 @@ def get_samples(self):
return 0;
-"""cassandra database object"""
-db = None
-"""
-# ColumnFamilys object collection
-# data format: {key: ColumnFamily Object}
-# example: {'cpu', ColumnFamily()}
-"""
+class ApiServer():
-def get_db():
- global db
- if db is None:
- config = ConfigParser.ConfigParser()
- config.read("kanyun.conf")
- api_cfg = dict(config.items('api'))
- db = CassaDb('data', api_cfg['db_host'])
- return db
-
-
-def api_getdata(row_id, cf_str, scf_str, time_from=0, time_to=0):
- """
- param type: UnicodeType and IntType
- return: recordset, count, bool(count > limit?)
- """
- if not isinstance(row_id, unicode) \
- or not isinstance(cf_str, unicode) \
- or not isinstance(scf_str, unicode) \
- or not isinstance(time_from, int) \
- or not isinstance(time_to, int):
- return None, 0, True
+ def __init__(self,
+ db_host='127.0.0.1',
+ log_file="/tmp/api-server.log",
+ log_level=logging.NOTSET):
+ # cassandra database object
+ self.db = None
+ self.db_host = db_host
+ self.logger = logging.getLogger()
+ handler = logging.FileHandler(log_file)
+ self.logger.addHandler(handler)
+ self.logger.setLevel(log_level)
- db = get_db()
+ def get_db(self):
+ if self.db is None:
+ self.db = CassaDb('data', self.db_host)
+ return self.db
- if time_to == 0:
- time_to = time.time()
-
- rs = db.get(cf_str, row_id, super_column=scf_str,
- column_start=time_from, column_finish=time_to, column_count=20000)
- count = 0 if rs is None else len(rs)
-
- return rs, count, False if (count == 20000) else True
-
-
-def analyize_data(rs, period, statistic):
- """[private func]analyize the data
- period: minutes
- """
- if rs is None \
- or not isinstance(period, int) \
- or not isinstance(statistic, int):
- return None
- t = 0
- key_time = 0
- st = Statistics()
- this_period = dict()
-
- for timestmp, value in rs.iteritems():
- rt = time.gmtime(timestmp)
- key = rt.tm_min + rt.tm_hour*100 + rt.tm_mday*10000 + \
- rt.tm_mon*1000000 + rt.tm_year*100000000
- if t == 0:
- print '\tget first value'
- st.clean()
- t = timestmp
- key_time = time.gmtime(timestmp)
- if timestmp >= t + period*60:
- print '\tnext', key, ">=", t, "+", period
- st.clean()
- t = timestmp
- key_time = time.gmtime(timestmp)
- st.update(float(value))
- key2 = time.mktime(
- (key_time.tm_year, key_time.tm_mon, key_time.tm_mday,
- key_time.tm_hour, key_time.tm_min,0,0,0,0))
- this_period[key2] = st.get_value(statistic)
- print '\tcompute time=%d, value=%s(%f) "update(%s)=%d"' % \
- (key, value, float(value), key2, this_period[key2])
+ def get_data(self, row_id, cf_str, scf_str, time_from=0, time_to=0):
+ """
+ param type: UnicodeType and IntType
+ return: recordset, count, bool(count > limit?)
+ """
+ if not isinstance(row_id, unicode) \
+ or not isinstance(cf_str, unicode) \
+ or not isinstance(scf_str, unicode) \
+ or not isinstance(time_from, int) \
+ or not isinstance(time_to, int):
+ return None, 0, True
- this_period = OrderedDict(sorted(this_period.items(), key=lambda t: t[0]))
- print statistic, ":each period(", period, "):"
- for m, val in this_period.iteritems():
- rt = time.gmtime(m)
- key = rt.tm_min + rt.tm_hour*100 + rt.tm_mday*10000 + \
- rt.tm_mon*1000000 + rt.tm_year*100000000
- print '\t', key, m, val
+ db = self.get_db()
+
+ if time_to == 0:
+ time_to = int(time.time())
+
+ rs = db.get(cf_str, row_id, super_column=scf_str,
+ column_start=time_from, column_finish=time_to, column_count=20000)
+ count = 0 if rs is None else len(rs)
- return this_period
+ return rs, count, False if (count == 20000) else True
+
+ def analyize_data(self, rs, period, statistic):
+ """[private func]analyize the data
+ period: minutes
+ """
+ if rs is None \
+ or not isinstance(period, int) \
+ or not isinstance(statistic, int):
+ return None
+ t = 0
+ key_time = 0
+ st = Statistics()
+ this_period = dict()
+
+ for timestmp, value in rs.iteritems():
+ rt = time.gmtime(timestmp)
+ key = rt.tm_min + rt.tm_hour*100 + rt.tm_mday*10000 + \
+ rt.tm_mon*1000000 + rt.tm_year*100000000
+ if t == 0:
+ print '\tget first value'
+ st.clean()
+ t = timestmp
+ key_time = time.gmtime(timestmp)
+ if timestmp >= t + period*60:
+ print '\tnext', key, ">=", t, "+", period
+ st.clean()
+ t = timestmp
+ key_time = time.gmtime(timestmp)
+ st.update(float(value))
+ key2 = time.mktime(
+ (key_time.tm_year, key_time.tm_mon, key_time.tm_mday,
+ key_time.tm_hour, key_time.tm_min,0,0,0,0))
+ this_period[key2] = st.get_value(statistic)
+ print '\tcompute time=%d, value=%s(%f) "update(%s)=%d"' % \
+ (key, value, float(value), key2, this_period[key2])
+
+ this_period = OrderedDict(sorted(this_period.items(), key=lambda t: t[0]))
+ print statistic, ":each period(", period, "):"
+ for m, val in this_period.iteritems():
+ rt = time.gmtime(m)
+ key = rt.tm_min + rt.tm_hour*100 + rt.tm_mday*10000 + \
+ rt.tm_mon*1000000 + rt.tm_year*100000000
+ print '\t', key, m, val
+
+ return this_period
+ ######################### public API interface ########################
+ def get_instances_list(self, cf_str):
+ if not isinstance(cf_str, unicode):
+ print 'param types error'
+ return None
+ ret = list()
+ limit = 20000
+ time_to = int(time.time())
+ time_from = time_to - 24 * 60 * 60
+ db = self.get_db()
+
+ rs = db.get_range(cf_str)
+ if not rs is None:
+ for i in rs:
+ ret.append(i[0])
+
+ return ret
+
+ def get_by_instance_id(self, row_id, cf_str):
+ if not isinstance(row_id, unicode) \
+ or not isinstance(cf_str, unicode):
+ print 'param types error'
+ return None, 0, True
+ db = self.get_db()
+ rs = db.getbykey(cf_str, row_id)
+ count = 0 if rs is None else len(rs)
+
+ return rs, count, False if (count == 20000) else True
+
+ def get_by_key(self, row_id, cf_str, scf_str, limit=20000):
+ """
+ example:cf=u'vmnetwork',scf=u'10.0.0.1',key=u'instance-0000002'
+ return: recordset, count, bool(count > limit?)
+ """
+ if not isinstance(row_id, unicode) \
+ or not isinstance(cf_str, unicode) \
+ or not isinstance(scf_str, unicode) \
+ or not isinstance(limit, int):
+ print 'param types error'
+ return None, 0, True
+ db = self.get_db()
+ rs = db.getbykey2(cf_str, key=row_id, super_column=scf_str, column_count=limit)
+ count = 0 if rs is None else len(rs)
+
+ return rs, count, False if (count == 20000) else True
-############################# public API interface ############################
-def api_get_instances_list(cf_str):
- if not isinstance(cf_str, unicode):
- print 'param types error'
- return None
- ret = list()
- limit = 20000
- time_to = time.time()
- time_from = time_to - 24 * 60 * 60
- db = get_db()
-
- rs = db.get_range(cf_str)
- if not rs is None:
- for i in rs:
- ret.append(i[0])
-
- return ret
-
-
-def api_get_by_instance_id(row_id, cf_str):
- if not isinstance(row_id, unicode) \
- or not isinstance(cf_str, unicode):
- print 'param types error'
- return None, 0, True
- db = get_db()
- rs = db.getbykey(cf_str, row_id)
- count = 0 if rs is None else len(rs)
-
- return rs, count, False if (count == 20000) else True
-
-
-def api_getbykey(row_id, cf_str, scf_str, limit=20000):
- """
- example:cf=u'vmnetwork',scf=u'10.0.0.1',key=u'instance-0000002'
- return: recordset, count, bool(count > limit?)
- """
- if not isinstance(row_id, unicode) \
- or not isinstance(cf_str, unicode) \
- or not isinstance(scf_str, unicode) \
- or not isinstance(limit, int):
- print 'param types error'
- return None, 0, True
- db = get_db()
- rs = db.getbykey2(cf_str, key=row_id, super_column=scf_str, column_count=limit)
- count = 0 if rs is None else len(rs)
-
- return rs, count, False if (count == 20000) else True
+ def statistic(self, row_id, cf_str, scf_str,
+ statistic, period=5, time_from=0, time_to=0):
+ """statistic is STATISTIC enum
+ period default=5 minutes
+ time_to default=0(now)"""
+ if (not isinstance(row_id, unicode) \
+ or not isinstance(cf_str, unicode) \
+ or not isinstance(scf_str, unicode) \
+ or not isinstance(statistic, int) \
+ or not isinstance(period, int) \
+ or not isinstance(time_from, int) \
+ or not isinstance(time_to, int)):
+ print 'param types error'
+ return None, 0, True
+
+ ret_len = 0
+ rs, count, all_data = self.get_data(row_id, cf_str, scf_str,
+ time_from, time_to)
+ if not rs is None and count > 0:
+ buf = self.analyize_data(rs, 1, statistic)
+ ret = self.analyize_data(buf, period, statistic)
+ if ret is None:
+ ret_len = 0
+ else:
+ ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0]))
+ ret_len = len(ret)
+ print ret_len, "result."
+ else:
+ print "no result."
+ ret = None
+ ret_len = 0
+ return ret, ret_len, all_data
+ ##################### end public API interface ########################
-def api_statistic(row_id, cf_str, scf_str,
- statistic, period=5, time_from=0, time_to=0):
- """statistic is STATISTIC enum
- period default=5 minutes
- time_to default=0(now)"""
- if (not isinstance(row_id, unicode) \
- or not isinstance(cf_str, unicode) \
- or not isinstance(scf_str, unicode) \
- or not isinstance(statistic, int) \
- or not isinstance(period, int) \
- or not isinstance(time_from, int) \
- or not isinstance(time_to, int)):
- print 'param types error'
- return None, 0, True
-
- ret_len = 0
- rs, count, all_data = api_getdata(row_id, cf_str, scf_str,
- time_from, time_to)
- if not rs is None and count > 0:
- buf = analyize_data(rs, 1, statistic)
- ret = analyize_data(buf, period, statistic)
- if ret is None:
- ret_len = 0
- else:
- ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0]))
- ret_len = len(ret)
- print ret_len, "result."
- else:
- print "no result."
- ret = None
- ret_len = 0
- return ret, ret_len, all_data
-######################### end public API interface ############################
+#db = None
+#"""
+## ColumnFamilys object collection
+## data format: {key: ColumnFamily Object}
+## example: {'cpu', ColumnFamily()}
+#"""
+
+#def get_db():
+# global db
+# if db is None:
+# config = ConfigParser.ConfigParser()
+# config.read("kanyun.conf")
+# api_cfg = dict(config.items('api'))
+# db = CassaDb('data', api_cfg['db_host'])
+# return db
+#
+#
+#def api_getdata(row_id, cf_str, scf_str, time_from=0, time_to=0):
+# """
+# param type: UnicodeType and IntType
+# return: recordset, count, bool(count > limit?)
+# """
+# if not isinstance(row_id, unicode) \
+# or not isinstance(cf_str, unicode) \
+# or not isinstance(scf_str, unicode) \
+# or not isinstance(time_from, int) \
+# or not isinstance(time_to, int):
+# return None, 0, True
+#
+# db = get_db()
+#
+# if time_to == 0:
+# time_to = int(time.time())
+#
+# rs = db.get(cf_str, row_id, super_column=scf_str,
+# column_start=time_from, column_finish=time_to, column_count=20000)
+# count = 0 if rs is None else len(rs)
+#
+# return rs, count, False if (count == 20000) else True
+#
+#
+#def analyize_data(rs, period, statistic):
+# """[private func]analyize the data
+# period: minutes
+# """
+# if rs is None \
+# or not isinstance(period, int) \
+# or not isinstance(statistic, int):
+# return None
+# t = 0
+# key_time = 0
+# st = Statistics()
+# this_period = dict()
+#
+# for timestmp, value in rs.iteritems():
+# rt = time.gmtime(timestmp)
+# key = rt.tm_min + rt.tm_hour*100 + rt.tm_mday*10000 + \
+# rt.tm_mon*1000000 + rt.tm_year*100000000
+# if t == 0:
+# print '\tget first value'
+# st.clean()
+# t = timestmp
+# key_time = time.gmtime(timestmp)
+# if timestmp >= t + period*60:
+# print '\tnext', key, ">=", t, "+", period
+# st.clean()
+# t = timestmp
+# key_time = time.gmtime(timestmp)
+# st.update(float(value))
+# key2 = time.mktime(
+# (key_time.tm_year, key_time.tm_mon, key_time.tm_mday,
+# key_time.tm_hour, key_time.tm_min,0,0,0,0))
+# this_period[key2] = st.get_value(statistic)
+# print '\tcompute time=%d, value=%s(%f) "update(%s)=%d"' % \
+# (key, value, float(value), key2, this_period[key2])
+#
+# this_period = OrderedDict(sorted(this_period.items(), key=lambda t: t[0]))
+# print statistic, ":each period(", period, "):"
+# for m, val in this_period.iteritems():
+# rt = time.gmtime(m)
+# key = rt.tm_min + rt.tm_hour*100 + rt.tm_mday*10000 + \
+# rt.tm_mon*1000000 + rt.tm_year*100000000
+# print '\t', key, m, val
+#
+# return this_period
+
+
+############################## public API interface ############################
+#def api_get_instances_list(cf_str):
+# if not isinstance(cf_str, unicode):
+# print 'param types error'
+# return None
+# ret = list()
+# limit = 20000
+# time_to = int(time.time())
+# time_from = time_to - 24 * 60 * 60
+# db = get_db()
+#
+# rs = db.get_range(cf_str)
+# if not rs is None:
+# for i in rs:
+# ret.append(i[0])
+#
+# return ret
+#
+#
+#def api_get_by_instance_id(row_id, cf_str):
+# if not isinstance(row_id, unicode) \
+# or not isinstance(cf_str, unicode):
+# print 'param types error'
+# return None, 0, True
+# db = get_db()
+# rs = db.getbykey(cf_str, row_id)
+# count = 0 if rs is None else len(rs)
+#
+# return rs, count, False if (count == 20000) else True
+#
+#
+#def api_getbykey(row_id, cf_str, scf_str, limit=20000):
+# """
+# example:cf=u'vmnetwork',scf=u'10.0.0.1',key=u'instance-0000002'
+# return: recordset, count, bool(count > limit?)
+# """
+# if not isinstance(row_id, unicode) \
+# or not isinstance(cf_str, unicode) \
+# or not isinstance(scf_str, unicode) \
+# or not isinstance(limit, int):
+# print 'param types error'
+# return None, 0, True
+# db = get_db()
+# rs = db.getbykey2(cf_str, key=row_id, super_column=scf_str, column_count=limit)
+# count = 0 if rs is None else len(rs)
+#
+# return rs, count, False if (count == 20000) else True
+
+
+#def api_statistic(row_id, cf_str, scf_str,
+# statistic, period=5, time_from=0, time_to=0):
+# """statistic is STATISTIC enum
+# period default=5 minutes
+# time_to default=0(now)"""
+# if (not isinstance(row_id, unicode) \
+# or not isinstance(cf_str, unicode) \
+# or not isinstance(scf_str, unicode) \
+# or not isinstance(statistic, int) \
+# or not isinstance(period, int) \
+# or not isinstance(time_from, int) \
+# or not isinstance(time_to, int)):
+# print 'param types error'
+# return None, 0, True
+#
+# ret_len = 0
+# rs, count, all_data = api_getdata(row_id, cf_str, scf_str,
+# time_from, time_to)
+# if not rs is None and count > 0:
+# buf = analyize_data(rs, 1, statistic)
+# ret = analyize_data(buf, period, statistic)
+# if ret is None:
+# ret_len = 0
+# else:
+# ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0]))
+# ret_len = len(ret)
+# print ret_len, "result."
+# else:
+# print "no result."
+# ret = None
+# ret_len = 0
+# return ret, ret_len, all_data
+
+########################## end public API interface ############################
View
24 tests/iptables_rule.sh
@@ -0,0 +1,24 @@
+#76 instance-0000004c 10.210.228.32
+virsh list
+if [ -z $instance_id ]
+then
+ echo -e "instance_id:\c"
+ read instance_id
+fi
+if [ -z $instance_ip ]
+then
+ echo -e "instance_ip:\c"
+ read instance_ip
+fi
+if [ -z $public_interface ]
+then
+ echo -e "public_interface:\c"
+ read public_interface
+fi
+sudo iptables -t filter -N nova-compute-f-inst-$instance_id
+sudo iptables -I FORWARD -s $instance_ip -j nova-compute-f-inst-$instance_id
+sudo iptables -A nova-compute-f-inst-$instance_id -o $public_interface \
+ -m comment \
+ --comment " $instance_id $instance_ip accounting rule "
+
+sudo iptables-save -t filter -c|grep comment
View
166 tests/test_api_server.py
@@ -81,6 +81,164 @@ def __init__(self, a='', b=''):
pass
+class TestApiServerClass(unittest.TestCase):
+
+ def setUp(self):
+ self.mox = mox.Mox()
+
+ def tearDown(self):
+ self.mox.UnsetStubs()
+
+ def testParamTypeCheck(self):
+ row_id = None
+ cf_str = None
+ scf_str = None
+ period = None
+ statistic = None
+ apiServer = api_server.ApiServer()
+ rs1, count, _ = apiServer.statistic(None, None, None, None, period=5, time_from=0, time_to=0)
+ rs2, count, _ = apiServer.get_by_key(None, cf_str, scf_str, limit=20000)
+ rs3, count, _ = apiServer.get_by_instance_id(row_id, cf_str)
+ rs4, count, _ = apiServer.get_data(row_id, cf_str, scf_str, time_from=0, time_to=0)
+ rs5 = apiServer.get_instances_list(None)
+ rs6 = apiServer.analyize_data(None, period, statistic)
+ assert(rs1 is None)
+ assert(rs2 is None)
+ assert(rs3 is None)
+ assert(rs4 is None)
+ assert(rs5 is None)
+ assert(rs6 is None)
+
+ print "Param type test \t\t\t[\033[1;33mOK\033[0m]"
+
+ def testApiGetdata(self):
+ time.clock()
+ row_id = 'instance-00000001@pyw.novalocal'
+ time_from = 1332815400.0
+ time_to = 0
+ scf_str = 'total'
+ cf_str = 'cpu'
+
+ apiServer = api_server.ApiServer()
+ self.mox.StubOutWithMock(apiServer, 'get_data')
+ apiServer.get_data(row_id, cf_str, scf_str, \
+ time_from, time_to).AndReturn(ApiGetDataMox())
+ self.mox.ReplayAll()
+
+ rs, count, _ = apiServer.get_data(row_id, cf_str, scf_str, time_from, time_to)
+ self.mox.VerifyAll()
+ print "%d results, spend %f seconds" % (count, time.clock())
+ print "ApiGetdata test \t\t\t[\033[1;33mOK\033[0m]"
+ print '-' * 60
+
+ def testApiStatisticSUM(self):
+ time.clock()
+ row_id = u'instance-00000001@pyw.novalocal'
+ period = 5
+ time_from = 1332833464
+ time_to = 0
+ cf_str = u'cpu'
+ scf_str = u'total'
+ statistic = api_server.STATISTIC.SUM
+
+ apiServer = api_server.ApiServer()
+ self.mox.StubOutWithMock(apiServer, 'get_data')
+ apiServer.get_data(row_id, cf_str, scf_str, \
+ time_from, time_to).AndReturn(ApiGetDataMox())
+ self.mox.ReplayAll()
+ statistic = api_server.STATISTIC.SUM
+ rs, count, _ = apiServer.statistic(row_id, cf_str, scf_str, \
+ statistic, period, \
+ time_from, time_to)
+ self.assertEqual(count, 1)
+ self.assertEqual(rs.values()[0], 44)
+ self.mox.VerifyAll()
+
+ print "%d SUM results, spend %f seconds" % (count, time.clock())
+ print "api_statistics sum test \t\t\t[\033[1;33mOK\033[0m]"
+ print '-' * 60
+
+ def testApiStatisticMAX(self):
+ time.clock()
+ row_id = u'instance-00000001@pyw.novalocal'
+ period = 5
+ time_from = 1332833464
+ time_to = 0
+ cf_str = u'cpu'
+ scf_str = u'total'
+ statistic = api_server.STATISTIC.MAXIMUM
+
+ apiServer = api_server.ApiServer()
+ self.mox.StubOutWithMock(apiServer, 'get_data')
+ apiServer.get_data(row_id, cf_str, scf_str, \
+ time_from, time_to).AndReturn(ApiGetDataMox())
+ self.mox.ReplayAll()
+ statistic = api_server.STATISTIC.MAXIMUM
+ rs, count, _ = apiServer.statistic(row_id, cf_str, scf_str, \
+ statistic, period, \
+ time_from, time_to)
+ self.assertEqual(count, 1)
+ self.assertEqual(rs.values()[0], 20)
+ self.mox.VerifyAll()
+ print "%d MAX results, spend %f seconds" % (count, time.clock())
+ print "api_statistics max test \t\t\t[\033[1;33mOK\033[0m]"
+ print '-' * 60
+
+
+ def testApiStatisticMIN(self):
+ time.clock()
+ row_id = u'instance-00000001@pyw.novalocal'
+ period = 5
+ time_from = 1332833464
+ time_to = 0
+ cf_str = u'cpu'
+ scf_str = u'total'
+ statistic = api_server.STATISTIC.MINIMUM
+
+ apiServer = api_server.ApiServer()
+ self.mox.StubOutWithMock(apiServer, 'get_data')
+ apiServer.get_data(row_id, cf_str, scf_str, \
+ time_from, time_to).AndReturn(ApiGetDataMox())
+ self.mox.ReplayAll()
+ statistic = api_server.STATISTIC.MINIMUM
+ rs, count, _ = apiServer.statistic(row_id, cf_str, scf_str, \
+ statistic, period, \
+ time_from, time_to)
+ self.assertEqual(count, 1)
+ self.assertEqual(rs.values()[0], 1)
+ self.mox.VerifyAll()
+
+ print "%d MIN results, spend %f seconds" % (count, time.clock())
+ print "api_statistics min test \t\t\t[\033[1;33mOK\033[0m]"
+ print '-' * 60
+
+ def testApiStatisticAVG(self):
+ time.clock()
+ row_id = u'instance-00000001@pyw.novalocal'
+ period = 5
+ time_from = 1332833464
+ time_to = 0
+ cf_str = u'cpu'
+ scf_str = u'total'
+ statistic = api_server.STATISTIC.AVERAGE
+
+ apiServer = api_server.ApiServer()
+ self.mox.StubOutWithMock(apiServer, 'get_data')
+ apiServer.get_data(row_id, cf_str, scf_str, \
+ time_from, time_to).AndReturn(ApiGetDataMox())
+ self.mox.ReplayAll()
+ statistic = api_server.STATISTIC.AVERAGE
+ rs, count, _ = apiServer.statistic(row_id, cf_str, scf_str, \
+ statistic, period, \
+ time_from, time_to)
+ self.assertEqual(count, 1)
+ self.mox.VerifyAll()
+
+ print "%d AVERAGE results, spend %f seconds" % (count, time.clock())
+ print "api_statistics AVERAGE test \t\t\t[\033[1;33mOK\033[0m]"
+ print '-' * 60
+
+
class TestApiServer(unittest.TestCase):
def setUp(self):
@@ -241,6 +399,14 @@ def testApiStatisticAVG(self):
time.clock()
ApiTestSuite = unittest.TestSuite()
ApiTestSuite.addTest(StatisticsTest("testStatistics"))
+
+ ApiTestSuite.addTest(TestApiServerClass("testParamTypeCheck"))
+ ApiTestSuite.addTest(TestApiServerClass("testApiGetdata"))
+ ApiTestSuite.addTest(TestApiServerClass("testApiStatisticSUM"))
+ ApiTestSuite.addTest(TestApiServerClass("testApiStatisticMAX"))
+ ApiTestSuite.addTest(TestApiServerClass("testApiStatisticMIN"))
+ ApiTestSuite.addTest(TestApiServerClass("testApiStatisticAVG"))
+
ApiTestSuite.addTest(TestApiServer("testParamTypeCheck"))
ApiTestSuite.addTest(TestApiServer("testApiGetdata"))
ApiTestSuite.addTest(TestApiServer("testApiStatisticSUM"))
Please sign in to comment.
Something went wrong with that request. Please try again.