Commit 0189a7fe authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Update quotaholder unit tests

parent a9183704
# Copyright 2012 GRNET S.A. All rights reserved.
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -36,10 +36,13 @@ import sys
import os
import subprocess
import time
from socket import socket, AF_INET, SOCK_STREAM, IPPROTO_TCP, error as socket_error
from errno import ECONNREFUSED
from os.path import dirname
path = os.path.dirname(os.path.realpath(__file__))
os.environ['SYNNEFO_SETTINGS_DIR'] = path + '/settings'
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings'
# The following import is copied from snf-tools/syneffo_tools/burnin.py
# Thank you John Giannelos <johngian@grnet.gr>
# Use backported unittest functionality if Python < 2.7
......@@ -50,41 +53,20 @@ except ImportError:
raise Exception("The unittest2 package is required for Python < 2.7")
import unittest
from synnefo.lib.quotaholder import QuotaholderClient
from synnefo.lib.quotaholder.api import (InvalidKeyError, NoEntityError,
NoQuantityError, NoCapacityError,
ExportLimitError, ImportLimitError)
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint
import random
import random
def printf(fmt, *args):
print(fmt.format(*args))
def environ_get(key, default_value = ''):
if os.environ.has_key(key):
return os.environ.get(key)
else:
return default_value
# Use environ vars [TEST_]QH_{HOST, PORT}
QH_HOST = environ_get("TEST_QH_HOST", environ_get("QH_HOST", "127.0.0.1"))
QH_PORT = environ_get("TEST_QH_PORT", environ_get("QH_PORT", "8888"))
assert QH_HOST != None
assert QH_PORT != None
printf("Will connect to QH_HOST = {0}", QH_HOST)
printf(" and QH_PORT = {0}", QH_PORT)
QH_SERVER = '{0}:{1}'.format(QH_HOST, QH_PORT)
QH_URL = "http://{0}/quotaholder/v".format(QH_SERVER)
### DEFS ###
def new_quota_holder_client():
"""
Create a new quota holder api client
"""
return QuotaholderClient(QH_URL, 'test')
return QuotaholderDjangoDBCallpoint()
def run_test_case(test_case):
"""
......@@ -114,24 +96,11 @@ def rand_string():
HERE = dirname(__file__)
def init_server():
p = subprocess.Popen(['setsid', HERE+'/qh_init', QH_SERVER])
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
while True:
try:
s.connect((QH_HOST, int(QH_PORT)))
break
except socket_error, e:
if e.errno != ECONNREFUSED:
raise
time.sleep(0.1)
return p.pid
### CLASSES ###
class QHTestCase(unittest.TestCase):
@classmethod
def setUpClass(self):
self.server = init_server()
subprocess.call([HERE+'/qh_init'])
self.qh = new_quota_holder_client()
def setUp(self):
......@@ -139,13 +108,4 @@ class QHTestCase(unittest.TestCase):
@classmethod
def tearDownClass(self):
from signal import SIGTERM
os.kill(-self.server, SIGTERM)
### VARS ###
DefaultOrCustom = {
True: "default",
False: "custom"
}
pass
......@@ -3,4 +3,3 @@ d=`dirname $0`
export SYNNEFO_SETTINGS_DIR=$d/settings
snf-manage syncdb --all --noinput
snf-manage flush --noinput
snf-manage runserver "$@" 2> $d/server_stderr &
# -*- coding: utf-8 -*-
#
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from random import random, choice, randint
def random_int(minimum=None, maximum=None):
minimum = minimum if minimum is not None else -4294967296L
maximum = maximum if maximum is not None else 4294967295L
r = random()
if r < 0.1:
return minimum
if r < 0.2:
return maximum
if minimum <= 0 and maximum >= 0 and r < 0.3:
return 0L
return long(minimum + r * (maximum - minimum))
def random_nat(maximum=None):
return random_int(minimum=0, maximum=maximum)
def random_name():
alphabet = u'abcdef_1233490.:@/-αβγδεζ'
length = randint(1, 48)
return ''.join(choice(alphabet) for _ in xrange(length))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment