Initial tests. Verifies instancing works As Expected.

This commit is contained in:
Aurynn Shaw 2013-08-08 11:38:41 +12:00
parent daa6923eeb
commit 153b8342e1
7 changed files with 164 additions and 12 deletions

View File

@ -33,9 +33,7 @@ class OpenERP(invoice.Invoice):
super(OpenERP, self).__init__(tenant)
# Conn is expected to be a dict with:
#
self.conn = connection(self.config)
self.id = self.conn.create()
def add_section(self, name):

View File

@ -1,5 +1,5 @@
# Interfaces to the Ceilometer API
import ceilometer
# import ceilometer
# Brings in HTTP support
import requests
@ -9,11 +9,13 @@ import json
import datetime
# Provides authentication against Openstack
from keystoneclient.v2_0 import client
from keystoneclient.v2_0 import client as keystone
#
# from .models import usage
from .models import session, usage
from .models import Session, usage
from sqlalchemy import create_engine
# Date format Ceilometer uses
# 2013-07-03T13:34:17
@ -37,11 +39,11 @@ class Artifice(object):
# This is the Keystone client connection, which provides our
# OpenStack authentication
self.auth = client.Client(
self.auth = keystone.Client(
username= config["openstack"]["username"],
password= config["openstack"]["password"],
tenant_name= config["openstack"]["default_tenant"],
auth_url= config["openstack"]["authenticator"]
auth_url= config["openstack"]["authentication_url"]
# auth_url="http://localhost:35357/v2.0"
)
conn_string = 'postgresql://%(username)s:%(password)s@%(host)s:%(port)s/%(database)s' % {
@ -63,12 +65,12 @@ class Artifice(object):
# Returns a Tenant object for the given name.
# Uses Keystone API to perform a direct name lookup,
# as this is expected to work via name.
self.config["authenticator"]
authenticator = config["openstack"]["authentication_url"]
url = "%(url)s/tenants?%(query)s" % {
"url": self.config["authenticator"],
"url": authenticator,
"query": urllib.urlencode({"name":name})
}
r = requests.get(url, headers={"X-Auth-Token": keystone.auth_token, "Content-Type": "application/json"})
r = requests.get(url, headers={"X-Auth-Token": self.auth.auth_token, "Content-Type": "application/json"})
if r.ok:
data = json.loads(r.text)
assert data
@ -88,7 +90,7 @@ class Artifice(object):
for tenant in self.auth.tenants.list():
t = Tenant(tenant, self)
dict([(t.name, Tenant(t, self)) for t in self.auth.tenants.list()))
dict( [(t.name, Tenant(t, self)) for t in self.auth.tenants.list() ] )
return self._tenancy
class Tenant(object):
@ -123,7 +125,7 @@ class Tenant(object):
"""
if self.invoice_type is None:
invoice_type = self.conn.config.get("main", "invoice:object")
invoice_type = self.conn.config["main"]["invoice:object"]
if ":" not in invoice_type:
raise AttributeError("Invoice configuration incorrect! %s" % invoice_type)
module, call = invoice_type.split(":")

0
tests/__init__.py Normal file
View File

View File

@ -0,0 +1,76 @@
# from artifice.models import usage, tenants, resources
import unittest
import datetime
import mock
class keystone(object):
def __new__(cls):
pass
class TestInstancing(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
@mock.patch("artifice.models.Session")
@mock.patch("keystoneclient.v2_0.client.Client")
@mock.patch("sqlalchemy.create_engine")
def test_instance_artifice(self, sqlmock, keystone, session):
"""Tests that artifice correctly instances."""
from artifice.interface import Artifice
# from artifice.models import usage
config = {
"main": {},
"database": {
"username": "foo",
"password": "bar",
"host": "1234",
"port": "1234",
"database": "artifice"
},
"openstack": {
"username": "foo",
"password": "bar",
"default_tenant":"asdf",
"authentication_url": "foo"
},
"invoice": {}
}
a = Artifice(config)
self.assertTrue( isinstance(a, Artifice) )
def test_instance_usage(self):
from artifice.models import usage, tenants, resources
start = datetime.datetime.now() - datetime.timedelta(days=30)
end = datetime.datetime.now()
u = usage.Usage(resources.Resource(), tenants.Tenant() , 1, start, end )
self.assertTrue ( isinstance( u, usage.Usage ) )
def test_instance_tenant(self):
from artifice.models import tenants
t = tenants.Tenant()
self.assertTrue( isinstance(t, tenants.Tenant) )
def test_instance_resource(self):
from artifice.models import resources
r = resources.Resource()
self.assertTrue( isinstance(r, resources.Resource) )

23
tests/test_invoice.py Normal file
View File

@ -0,0 +1,23 @@
import unittest
from artifice.interface import Artifice
class TestInvoicing(unittest.TestCase):
pass
def setUp(self):
pass
def tearDown(self):
pass
def test_replace_invoice_object(self):
pass
def test_add_usage_to_invoice(self):
pass
def test_save_invoice(self):
pass

53
tests/test_models.py Normal file
View File

@ -0,0 +1,53 @@
import unittest
from artifice.models import usage, tenants, resources, Session
class TestTenant(unittest.TestCase):
def test_create_tenant(self):
pass
def test_create_identical_tenant_fails(self):
pass
class TestResource(unittest.TestCase):
def test_create_resource(self):
pass
def test_create_resource_with_bad_tenant(self):
pass
def test_create_resource_without_tenant(self):
pass
class TestUsage(unittest.TestCase):
"""Tests various states of the Usage objects."""
def setUp(self):
pass
def tearDown(self):
pass
def test_save_to_database(self):
pass
def test_overlap_throws_exception(self):
pass
def test_non_overlap_does_not_throw_exception(self):
pass
def test_tenant_does_not_exist(self):
pass
def test_resource_does_not_exist(self):
pass

0
tests/test_tenant.py Normal file
View File