Skip to content
Browse files

changing the db layout adding tests

  • Loading branch information...
1 parent 60c951a commit 436b35532e30da1de1904d2ad84498b96014e943 BuzzTroll committed Oct 1, 2010
View
49 cloudminer/cloudminer.py
@@ -11,6 +11,7 @@
from sqlalchemy import String, MetaData, Sequence
from sqlalchemy import Column
import cloudyvents.cyvents as cyvents
+from sqlalchemy import and_
class _CYventExtra(object):
def __init__(self, key, value):
@@ -21,25 +22,39 @@ class _CYvent(object):
"""Convenience class for a parsed event.
"""
- def __init__(self, runname, iaasid, source, name, key, timestamp, extra):
+ def __init__(self, source, name, key, timestamp, extra):
- self.runname = runname
- self.iaasid = iaasid
self.source = source
self.name = name
self.key = key
self.timestamp = timestamp
self.extra = extra
+class _CYVM(object):
+
+ def __init__(self, runname, iaasid, events=[]):
+ self.runname = runname
+ self.iaasid = iaasid
+ self.events = events
+
+ def add_event(self, e):
+ self.events.append(e)
+
metadata = MetaData()
+
+vm_table = Table('vms', metadata,
+ Column('id', Integer, Sequence('event_id_seq'), primary_key=True),
+ Column('runname', String(50)),
+ Column('iaasid', String(50), unique=True)
+ )
+
event_table = Table('events', metadata,
Column('id', Integer, Sequence('event_id_seq'), primary_key=True),
Column('source', String(50)),
Column('name', String(50)),
Column('key', String(50)),
Column('timestamp', sqlalchemy.types.Time),
- Column('runname', String(50)),
- Column('iaasid', String(50)),
+ Column('vm_id', Integer, ForeignKey('vms.id'))
)
xtra_table = Table('extras', metadata,
@@ -52,6 +67,8 @@ def __init__(self, runname, iaasid, source, name, key, timestamp, extra):
mapper(_CYventExtra, xtra_table)
mapper(_CYvent, event_table, properties={
'extra': relation(_CYventExtra)})
+mapper(_CYVM, vm_table, properties={
+ 'events': relation(_CYvent)})
class CloudMiner(object):
@@ -66,17 +83,33 @@ def __init__(self, dburl, module=None):
self.session = self.Session()
def add_cloudyvent(self, runname, iaasid, cyv):
+
+ # first see if we already have this iaasid. if not create it
+ cyvm = self.get_by_iaasid(iaasid)
+ if cyvm == None:
+ cyvm = _CYVM(runname, iaasid)
+ self.session.add(cyvm)
+
xtras_list = []
if cyv.extra != None:
for k in cyv.extra.keys():
e = _CYventExtra(k, cyv.extra[k])
xtras_list.append(e)
- _cyv = _CYvent(runname, iaasid, cyv.source, cyv.name, cyv.key, cyv.timestamp, xtras_list)
- self.session.add(_cyv)
+ _cyv = _CYvent(cyv.source, cyv.name, cyv.key, cyv.timestamp, xtras_list)
+ cyvm.add_event(_cyv)
def get_events_by_runname(self, runname):
- return self.session.query(_CYvent).filter(_CYvent.runname == runname).all()
+ cyvm_a = self.session.query(_CYVM).filter(_CYVM.runname == runname).all()
+ e_a = []
+ for cyvm in cyvm_a:
+ e_a = e_a + cyvm.events
+
+ return e_a
+
+ def get_by_iaasid(self, iaasid):
+ cyvm = self.session.query(_CYVM).filter(_CYVM.iaasid == iaasid).first()
+ return cyvm
def commit(self):
self.session.commit()
View
3 cloudminer/tests/__init__.py
@@ -0,0 +1,3 @@
+from cloudminer.tests.test_simple_cm import *
+from cloudminer.tests.test_filedb_cm import *
+from cloudminer.tests.test_iaasid_cm import *
View
64 cloudminer/tests/test_iaasid_cm.py
@@ -0,0 +1,64 @@
+import unittest
+import datetime
+import logging
+import os
+import shutil
+import uuid
+import tempfile
+import time
+import cloudyvents.cyvents as cyvents
+from cloudminer import CloudMiner
+from cloudyvents.cyvents import CYvent
+import tempfile
+
+# Set this to False to look at generated log files afterwards. There will be
+# many directories like /tmp/cytestlog*
+DESTROY_LOGDIR = True
+
+logger = logging.getLogger(__name__)
+
+class CloudIaaSTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.cdb = CloudMiner('sqlite:///:memory:')
+ self.runname = "run1"
+
+ def tearDown(self):
+ pass
+
+ def test_iaas_query(self):
+ extras = {}
+ extras['hi'] = 'there'
+ runname = str(uuid.uuid1())
+ iaasid = str(uuid.uuid1())
+ source = "src"
+ key = "key"
+ event_count = 10
+
+ # create and add events
+ for i in range(0, event_count):
+ name = str(uuid.uuid1())
+ cye = CYvent(source, name, key, datetime.datetime.now(), extras)
+ self.cdb.add_cloudyvent(runname, iaasid, cye)
+ self.cdb.commit()
+
+ # now get an IaaS object
+ cyvm = self.cdb.get_by_iaasid(iaasid)
+ self.assertEqual(len(cyvm.events), event_count)
+
+ def test_many_iaas_1_runname(self):
+ runname = str(uuid.uuid1())
+ source = "src"
+ key = "key"
+ name = "name"
+ vm_count = 10
+
+ for i in range(0, vm_count):
+ iaasid = str(uuid.uuid1())
+ cye = CYvent(source, name, key, datetime.datetime.now(), None)
+ self.cdb.add_cloudyvent(runname, iaasid, cye)
+ self.cdb.commit()
+
+ cyvm_a = self.cdb.get_events_by_runname(runname)
+ self.assertEqual(len(cyvm_a), vm_count)
+
View
3 cloudminer/tests/test_simple_cm.py
@@ -71,11 +71,12 @@ def test_man_run_query(self):
key2 = "key2"
source2 = "src2"
name2 = "name2"
+ iaas2 = "iaas2"
extras['hi'] = 'there'
cye = CYvent(source, name, key, datetime.datetime.now(), extras)
self.cdb.add_cloudyvent(self.runname, self.iaasid, cye)
cye = CYvent(source2, name2, key2, datetime.datetime.now(), extras)
- self.cdb.add_cloudyvent(run2, self.iaasid, cye)
+ self.cdb.add_cloudyvent(run2, iaas2, cye)
self.cdb.commit()
rc = self.cdb.get_events_by_runname(self.runname)

0 comments on commit 436b355

Please sign in to comment.
Something went wrong with that request. Please try again.