Statistics
| Branch: | Tag: | Revision:

root / snf-quotaholder-app / quotaholder_django / test / config.py @ 763b4da1

History | View | Annotate | Download (4.7 kB)

1 ddf4eaad Giorgos Korfiatis
# Copyright 2012 GRNET S.A. All rights reserved.
2 ddf4eaad Giorgos Korfiatis
#
3 ddf4eaad Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 ddf4eaad Giorgos Korfiatis
# without modification, are permitted provided that the following
5 ddf4eaad Giorgos Korfiatis
# conditions are met:
6 ddf4eaad Giorgos Korfiatis
#
7 ddf4eaad Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 ddf4eaad Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 ddf4eaad Giorgos Korfiatis
#      disclaimer.
10 ddf4eaad Giorgos Korfiatis
#
11 ddf4eaad Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 ddf4eaad Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 ddf4eaad Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 ddf4eaad Giorgos Korfiatis
#      provided with the distribution.
15 ddf4eaad Giorgos Korfiatis
#
16 ddf4eaad Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 ddf4eaad Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ddf4eaad Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 ddf4eaad Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 ddf4eaad Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ddf4eaad Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 ddf4eaad Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 ddf4eaad Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 ddf4eaad Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 ddf4eaad Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 ddf4eaad Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 ddf4eaad Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 ddf4eaad Giorgos Korfiatis
#
29 ddf4eaad Giorgos Korfiatis
# The views and conclusions contained in the software and
30 ddf4eaad Giorgos Korfiatis
# documentation are those of the authors and should not be
31 ddf4eaad Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 ddf4eaad Giorgos Korfiatis
# or implied, of GRNET S.A.
33 ddf4eaad Giorgos Korfiatis
34 239b068c Christos KK Loverdos
### IMPORTS ###
35 a812c719 Christos KK Loverdos
import sys
36 a812c719 Christos KK Loverdos
import os
37 c645b39c Giorgos Korfiatis
import subprocess
38 c645b39c Giorgos Korfiatis
import time
39 c645b39c Giorgos Korfiatis
from socket import socket, AF_INET, SOCK_STREAM, IPPROTO_TCP, error as socket_error
40 c645b39c Giorgos Korfiatis
from errno import ECONNREFUSED
41 c645b39c Giorgos Korfiatis
from os.path import dirname
42 c645b39c Giorgos Korfiatis
43 239b068c Christos KK Loverdos
# The following import is copied from snf-tools/syneffo_tools/burnin.py
44 239b068c Christos KK Loverdos
# Thank you John Giannelos <johngian@grnet.gr>
45 239b068c Christos KK Loverdos
# Use backported unittest functionality if Python < 2.7
46 239b068c Christos KK Loverdos
try:
47 239b068c Christos KK Loverdos
    import unittest2 as unittest
48 239b068c Christos KK Loverdos
except ImportError:
49 239b068c Christos KK Loverdos
    if sys.version_info < (2, 7):
50 239b068c Christos KK Loverdos
        raise Exception("The unittest2 package is required for Python < 2.7")
51 239b068c Christos KK Loverdos
    import unittest
52 239b068c Christos KK Loverdos
53 c803f606 Giorgos Korfiatis
from kamaki.clients.quotaholder import QuotaholderClient
54 c803f606 Giorgos Korfiatis
from synnefo.lib.quotaholder.api import (InvalidKeyError, NoEntityError,
55 c803f606 Giorgos Korfiatis
                                         NoQuantityError, NoCapacityError,
56 c803f606 Giorgos Korfiatis
                                         ExportLimitError, ImportLimitError)
57 c645b39c Giorgos Korfiatis
58 f611ef3d Prodromos Gerakios
import random 
59 239b068c Christos KK Loverdos
60 c645b39c Giorgos Korfiatis
def printf(fmt, *args):
61 c645b39c Giorgos Korfiatis
    print(fmt.format(*args))
62 c645b39c Giorgos Korfiatis
63 c645b39c Giorgos Korfiatis
def environ_get(key, default_value = ''):
64 c645b39c Giorgos Korfiatis
    if os.environ.has_key(key):
65 c645b39c Giorgos Korfiatis
        return os.environ.get(key)
66 c645b39c Giorgos Korfiatis
    else:
67 c645b39c Giorgos Korfiatis
        return default_value
68 c645b39c Giorgos Korfiatis
69 c645b39c Giorgos Korfiatis
# Use environ vars [TEST_]QH_{HOST, PORT}
70 c645b39c Giorgos Korfiatis
QH_HOST = environ_get("TEST_QH_HOST", environ_get("QH_HOST", "127.0.0.1"))
71 e893482e Giorgos Korfiatis
QH_PORT = environ_get("TEST_QH_PORT", environ_get("QH_PORT", "8888"))
72 c645b39c Giorgos Korfiatis
73 c645b39c Giorgos Korfiatis
assert QH_HOST != None
74 c645b39c Giorgos Korfiatis
assert QH_PORT != None
75 c645b39c Giorgos Korfiatis
76 c645b39c Giorgos Korfiatis
printf("Will connect to QH_HOST = {0}", QH_HOST)
77 c645b39c Giorgos Korfiatis
printf("            and QH_PORT = {0}", QH_PORT)
78 c645b39c Giorgos Korfiatis
79 c645b39c Giorgos Korfiatis
QH_SERVER = '{0}:{1}'.format(QH_HOST, QH_PORT)
80 c9f1cf17 Giorgos Korfiatis
QH_URL = "http://{0}/quotaholder/v".format(QH_SERVER)
81 c645b39c Giorgos Korfiatis
82 239b068c Christos KK Loverdos
### DEFS ###
83 239b068c Christos KK Loverdos
def new_quota_holder_client():
84 239b068c Christos KK Loverdos
    """
85 239b068c Christos KK Loverdos
    Create a new quota holder api client
86 239b068c Christos KK Loverdos
    """
87 c803f606 Giorgos Korfiatis
    return QuotaholderClient(QH_URL)
88 239b068c Christos KK Loverdos
89 f6d15c9b Christos KK Loverdos
def run_test_case(test_case):
90 f6d15c9b Christos KK Loverdos
    """
91 f6d15c9b Christos KK Loverdos
    Runs the test_case and returns the result
92 f6d15c9b Christos KK Loverdos
    """
93 f6d15c9b Christos KK Loverdos
    # Again from snf-tools/synnefo_tools/burnin.py
94 f6d15c9b Christos KK Loverdos
    # Thank you John Giannelos <johngian@grnet.gr>
95 39a725a6 Christos KK Loverdos
    printf("Running {0}", test_case)
96 f6d15c9b Christos KK Loverdos
    import sys
97 f6d15c9b Christos KK Loverdos
    suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
98 c645b39c Giorgos Korfiatis
    runner = unittest.TextTestRunner(stream=sys.stderr, verbosity=2,
99 c645b39c Giorgos Korfiatis
                                     failfast=True, buffer=False)
100 f6d15c9b Christos KK Loverdos
    return runner.run(suite)
101 f6d15c9b Christos KK Loverdos
102 f6d15c9b Christos KK Loverdos
def run_test_cases(test_cases):
103 f6d15c9b Christos KK Loverdos
    for test_case in test_cases:
104 f6d15c9b Christos KK Loverdos
        run_test_case(test_case)
105 f6d15c9b Christos KK Loverdos
106 f611ef3d Prodromos Gerakios
def rand_string():
107 f611ef3d Prodromos Gerakios
   alphabet = 'abcdefghijklmnopqrstuvwxyz'
108 f611ef3d Prodromos Gerakios
   min = 5
109 f611ef3d Prodromos Gerakios
   max = 15
110 f611ef3d Prodromos Gerakios
   string=''
111 f611ef3d Prodromos Gerakios
   for x in random.sample(alphabet,random.randint(min,max)):
112 f611ef3d Prodromos Gerakios
    string += x
113 f611ef3d Prodromos Gerakios
   return string
114 f611ef3d Prodromos Gerakios
115 c645b39c Giorgos Korfiatis
HERE = dirname(__file__)
116 c645b39c Giorgos Korfiatis
117 c645b39c Giorgos Korfiatis
def init_server():
118 c645b39c Giorgos Korfiatis
    p = subprocess.Popen(['setsid', HERE+'/qh_init', QH_SERVER])
119 c645b39c Giorgos Korfiatis
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
120 c645b39c Giorgos Korfiatis
    while True:
121 c645b39c Giorgos Korfiatis
        try:
122 c645b39c Giorgos Korfiatis
            s.connect((QH_HOST, int(QH_PORT)))
123 c645b39c Giorgos Korfiatis
            break
124 c645b39c Giorgos Korfiatis
        except socket_error, e:
125 c645b39c Giorgos Korfiatis
            if e.errno != ECONNREFUSED:
126 c645b39c Giorgos Korfiatis
                raise
127 c645b39c Giorgos Korfiatis
            time.sleep(0.1)
128 c645b39c Giorgos Korfiatis
    return p.pid
129 a812c719 Christos KK Loverdos
130 b9ec4de7 Christos KK Loverdos
### CLASSES ###
131 b9ec4de7 Christos KK Loverdos
class QHTestCase(unittest.TestCase):
132 c645b39c Giorgos Korfiatis
    @classmethod
133 c645b39c Giorgos Korfiatis
    def setUpClass(self):
134 c645b39c Giorgos Korfiatis
        self.server = init_server()
135 b9ec4de7 Christos KK Loverdos
        self.qh = new_quota_holder_client()
136 c645b39c Giorgos Korfiatis
137 c645b39c Giorgos Korfiatis
    def setUp(self):
138 c645b39c Giorgos Korfiatis
        print
139 b9ec4de7 Christos KK Loverdos
140 c645b39c Giorgos Korfiatis
    @classmethod
141 c645b39c Giorgos Korfiatis
    def tearDownClass(self):
142 c645b39c Giorgos Korfiatis
        from signal import SIGTERM
143 c645b39c Giorgos Korfiatis
        os.kill(-self.server, SIGTERM)
144 85f307d7 Giorgos Korfiatis
        os.remove('/tmp/qh_testdb')
145 b9ec4de7 Christos KK Loverdos
146 b9ec4de7 Christos KK Loverdos
147 239b068c Christos KK Loverdos
### VARS ###
148 a812c719 Christos KK Loverdos
DefaultOrCustom = {
149 a812c719 Christos KK Loverdos
    True: "default",
150 a812c719 Christos KK Loverdos
    False: "custom"
151 a812c719 Christos KK Loverdos
}