Statistics
| Branch: | Tag: | Revision:

root / snf-tools / synnefo_tools / burnin / common.py @ 4c52d5bf

History | View | Annotate | Download (6 kB)

1
# Copyright 2013 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
"""
35
Common utils for burnin tests
36

37
"""
38

    
39
import sys
40
import traceback
41
# Use backported unittest functionality if Python < 2.7
42
try:
43
    import unittest2 as unittest
44
except ImportError:
45
    if sys.version_info < (2, 7):
46
        raise Exception("The unittest2 package is required for Python < 2.7")
47
    import unittest
48

    
49
from kamaki.clients.astakos import AstakosClient
50
from kamaki.clients.compute import ComputeClient
51

    
52
from logger import Log
53

    
54

    
55
# --------------------------------------------------------------------
56
# Global variables
57
logger = None  # Invalid constant name. pylint: disable-msg=C0103
58
AUTH_URL = "https://accounts.okeanos.grnet.gr/identity/v2.0/"
59
TOKEN = ""
60
CONNECTION_RETRY_LIMIT = 2
61

    
62

    
63
# --------------------------------------------------------------------
64
# BurninTestResult class
65
class BurninTestResult(unittest.TestResult):
66
    """Modify the TextTestResult class"""
67
    def __init__(self):
68
        super(BurninTestResult, self).__init__()
69

    
70
        # Test parameters
71
        self.failfast = True
72

    
73
    def startTest(self, test):  # noqa
74
        """Called when the test case test is about to be run"""
75
        super(BurninTestResult, self).startTest(test)
76
        # Access to a protected member. pylint: disable-msg=W0212
77
        logger.log(test.__class__.__name__, test._testMethodDoc)
78

    
79
    # Method could be a function. pylint: disable-msg=R0201
80
    def _test_failed(self, test, err):
81
        """Test failed"""
82
        # Access to a protected member. pylint: disable-msg=W0212
83
        err_msg = test._testMethodDoc + "... failed."
84
        logger.error(test.__class__.__name__, err_msg)
85
        (err_type, err_value, err_trace) = err
86
        trcback = traceback.format_exception(err_type, err_value, err_trace)
87
        logger.info(test.__class__.__name__, trcback)
88

    
89
    def addError(self, test, err):  # noqa
90
        """Called when the test case test raises an unexpected exception"""
91
        super(BurninTestResult, self).addError(test, err)
92
        self._test_failed(test, err)
93

    
94
    def addFailure(self, test, err):  # noqa
95
        """Called when the test case test signals a failure"""
96
        super(BurninTestResult, self).addFailure(test, err)
97
        self._test_failed(test, err)
98

    
99

    
100
# --------------------------------------------------------------------
101
# BurninTests class
102
# Too many public methods (45/20). pylint: disable-msg=R0904
103
class BurninTests(unittest.TestCase):
104
    """Common class that all burnin tests should implement"""
105
    @classmethod
106
    def setUpClass(cls):  # noqa
107
        """Initialize BurninTests"""
108
        cls.suite_name = cls.__name__
109
        cls.connection_retry_limit = CONNECTION_RETRY_LIMIT
110
        logger.testsuite_start(cls.suite_name)
111

    
112
        # Set test parameters
113
        cls.longMessage = True
114

    
115
    def test_clients_setup(self):
116
        """Initializing astakos/cyclades/pithos clients"""
117
        # Update class attributes
118
        cls = type(self)
119
        self.info("Astakos auth url is %s", AUTH_URL)
120
        cls.astakos = AstakosClient(AUTH_URL, TOKEN)
121
        cls.astakos.CONNECTION_RETRY_LIMIT = CONNECTION_RETRY_LIMIT
122

    
123
        cls.compute_url = \
124
            cls.astakos.get_service_endpoints('compute')['publicURL']
125
        self.info("Cyclades url is %s", cls.compute_url)
126
        cls.compute = ComputeClient(cls.compute_url, TOKEN)
127
        cls.compute.CONNECTION_RETRY_LIMIT = CONNECTION_RETRY_LIMIT
128

    
129
    def log(self, msg, *args):
130
        """Pass the section value to logger"""
131
        logger.log(self.suite_name, msg, *args)
132

    
133
    def info(self, msg, *args):
134
        """Pass the section value to logger"""
135
        logger.info(self.suite_name, msg, *args)
136

    
137
    def debug(self, msg, *args):
138
        """Pass the section value to logger"""
139
        logger.debug(self.suite_name, msg, *args)
140

    
141
    def warning(self, msg, *args):
142
        """Pass the section value to logger"""
143
        logger.warning(self.suite_name, msg, *args)
144

    
145
    def error(self, msg, *args):
146
        """Pass the section value to logger"""
147
        logger.error(self.suite_name, msg, *args)
148

    
149

    
150
# --------------------------------------------------------------------
151
# Helper functions
152
def was_succesful(tsuite, success):
153
    """Handle whether a testsuite was succesful or not"""
154
    if success:
155
        logger.testsuite_success(tsuite)
156
    else:
157
        logger.testsuite_failure(tsuite)
158

    
159

    
160
def setup_logger(output_dir, verbose=1, use_colors=True, in_parallel=False):
161
    """Setup our logger"""
162
    global logger  # Using global statement. pylint: disable-msg=C0103,W0603
163

    
164
    logger = Log(output_dir, verbose=verbose,
165
                 use_colors=use_colors, in_parallel=in_parallel)