Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.7 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
### IMPORTS ###
35
import sys
36
import os
37
import subprocess
38
import time
39
from socket import socket, AF_INET, SOCK_STREAM, IPPROTO_TCP, error as socket_error
40
from errno import ECONNREFUSED
41
from os.path import dirname
42

    
43
# The following import is copied from snf-tools/syneffo_tools/burnin.py
44
# Thank you John Giannelos <johngian@grnet.gr>
45
# Use backported unittest functionality if Python < 2.7
46
try:
47
    import unittest2 as unittest
48
except ImportError:
49
    if sys.version_info < (2, 7):
50
        raise Exception("The unittest2 package is required for Python < 2.7")
51
    import unittest
52

    
53
from kamaki.clients.quotaholder import QuotaholderClient
54
from synnefo.lib.quotaholder.api import (InvalidKeyError, NoEntityError,
55
                                         NoQuantityError, NoCapacityError,
56
                                         ExportLimitError, ImportLimitError)
57

    
58
import random 
59

    
60
def printf(fmt, *args):
61
    print(fmt.format(*args))
62

    
63
def environ_get(key, default_value = ''):
64
    if os.environ.has_key(key):
65
        return os.environ.get(key)
66
    else:
67
        return default_value
68

    
69
# Use environ vars [TEST_]QH_{HOST, PORT}
70
QH_HOST = environ_get("TEST_QH_HOST", environ_get("QH_HOST", "127.0.0.1"))
71
QH_PORT = environ_get("TEST_QH_PORT", environ_get("QH_PORT", "8888"))
72

    
73
assert QH_HOST != None
74
assert QH_PORT != None
75

    
76
printf("Will connect to QH_HOST = {0}", QH_HOST)
77
printf("            and QH_PORT = {0}", QH_PORT)
78

    
79
QH_SERVER = '{0}:{1}'.format(QH_HOST, QH_PORT)
80
QH_URL = "http://{0}/quotaholder/v".format(QH_SERVER)
81

    
82
### DEFS ###
83
def new_quota_holder_client():
84
    """
85
    Create a new quota holder api client
86
    """
87
    return QuotaholderClient(QH_URL)
88

    
89
def run_test_case(test_case):
90
    """
91
    Runs the test_case and returns the result
92
    """
93
    # Again from snf-tools/synnefo_tools/burnin.py
94
    # Thank you John Giannelos <johngian@grnet.gr>
95
    printf("Running {0}", test_case)
96
    import sys
97
    suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
98
    runner = unittest.TextTestRunner(stream=sys.stderr, verbosity=2,
99
                                     failfast=True, buffer=False)
100
    return runner.run(suite)
101

    
102
def run_test_cases(test_cases):
103
    for test_case in test_cases:
104
        run_test_case(test_case)
105

    
106
def rand_string():
107
   alphabet = 'abcdefghijklmnopqrstuvwxyz'
108
   min = 5
109
   max = 15
110
   string=''
111
   for x in random.sample(alphabet,random.randint(min,max)):
112
    string += x
113
   return string
114

    
115
HERE = dirname(__file__)
116

    
117
def init_server():
118
    p = subprocess.Popen(['setsid', HERE+'/qh_init', QH_SERVER])
119
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
120
    while True:
121
        try:
122
            s.connect((QH_HOST, int(QH_PORT)))
123
            break
124
        except socket_error, e:
125
            if e.errno != ECONNREFUSED:
126
                raise
127
            time.sleep(0.1)
128
    return p.pid
129

    
130
### CLASSES ###
131
class QHTestCase(unittest.TestCase):
132
    @classmethod
133
    def setUpClass(self):
134
        self.server = init_server()
135
        self.qh = new_quota_holder_client()
136

    
137
    def setUp(self):
138
        print
139

    
140
    @classmethod
141
    def tearDownClass(self):
142
        from signal import SIGTERM
143
        os.kill(-self.server, SIGTERM)
144
        os.remove('/tmp/qh_testdb')
145

    
146

    
147
### VARS ###
148
DefaultOrCustom = {
149
    True: "default",
150
    False: "custom"
151
}
152