Move source_credentials() into run_tests.py
[functest.git] / functest / ci / tier_handler.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Ericsson AB and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Tier and TestCase classes to wrap the testcases config file"""
11 # pylint: disable=missing-docstring
12
13 import re
14 import textwrap
15
16 import prettytable
17
18
19 LINE_LENGTH = 72
20
21
22 def split_text(text, max_len):
23     words = text.split()
24     lines = []
25     line = ""
26     for word in words:
27         if len(line) + len(word) < max_len - 1:
28             line += word + " "
29         else:
30             lines.append(line)
31             line = word + " "
32     if line != "":
33         lines.append(line)
34     return lines
35
36
37 class Tier(object):
38
39     def __init__(self, name, order, ci_loop, description=""):
40         self.tests_array = []
41         self.skipped_tests_array = []
42         self.name = name
43         self.order = order
44         self.ci_loop = ci_loop
45         self.description = description
46
47     def add_test(self, testcase):
48         self.tests_array.append(testcase)
49
50     def skip_test(self, testcase):
51         self.skipped_tests_array.append(testcase)
52
53     def get_tests(self):
54         array_tests = []
55         for test in self.tests_array:
56             array_tests.append(test)
57         return array_tests
58
59     def get_skipped_test(self):
60         return self.skipped_tests_array
61
62     def get_test_names(self):
63         array_tests = []
64         for test in self.tests_array:
65             array_tests.append(test.get_name())
66         return array_tests
67
68     def get_test(self, test_name):
69         if self.is_test(test_name):
70             for test in self.tests_array:
71                 if test.get_name() == test_name:
72                     return test
73         return None
74
75     def is_test(self, test_name):
76         for test in self.tests_array:
77             if test.get_name() == test_name:
78                 return True
79         return False
80
81     def get_name(self):
82         return self.name
83
84     def get_order(self):
85         return self.order
86
87     def get_ci_loop(self):
88         return self.ci_loop
89
90     def __str__(self):
91         msg = prettytable.PrettyTable(
92             header_style='upper', padding_width=5,
93             field_names=['tiers', 'order', 'CI Loop', 'description',
94                          'testcases'])
95         msg.add_row(
96             [self.name, self.order, self.ci_loop,
97              textwrap.fill(self.description, width=40),
98              textwrap.fill(' '.join([str(x.get_name(
99                  )) for x in self.get_tests()]), width=40)])
100         return msg.get_string()
101
102
103 class TestCase(object):
104
105     def __init__(self, name, enabled, dependency, criteria, blocking,
106                  description="", project=""):
107         # pylint: disable=too-many-arguments
108         self.name = name
109         self.enabled = enabled
110         self.dependency = dependency
111         self.criteria = criteria
112         self.blocking = blocking
113         self.description = description
114         self.project = project
115
116     @staticmethod
117     def is_none(item):
118         return item is None or item == ""
119
120     def is_compatible(self, ci_installer, ci_scenario):
121         try:
122             if not self.is_none(ci_installer):
123                 if re.search(self.dependency.get_installer(),
124                              ci_installer) is None:
125                     return False
126             if not self.is_none(ci_scenario):
127                 if re.search(self.dependency.get_scenario(),
128                              ci_scenario) is None:
129                     return False
130             return True
131         except TypeError:
132             return False
133
134     def get_name(self):
135         return self.name
136
137     def is_enabled(self):
138         return self.enabled
139
140     def get_criteria(self):
141         return self.criteria
142
143     def is_blocking(self):
144         return self.blocking
145
146     def get_project(self):
147         return self.project
148
149     def __str__(self):
150         msg = prettytable.PrettyTable(
151             header_style='upper', padding_width=5,
152             field_names=['test case', 'description', 'criteria', 'dependency'])
153         msg.add_row([self.name, textwrap.fill(self.description, width=40),
154                      self.criteria, self.dependency])
155         return msg.get_string()
156
157
158 class Dependency(object):
159
160     def __init__(self, installer, scenario):
161         self.installer = installer
162         self.scenario = scenario
163
164     def get_installer(self):
165         return self.installer
166
167     def get_scenario(self):
168         return self.scenario
169
170     def __str__(self):
171         delimitator = "\n" if self.get_installer(
172             ) and self.get_scenario() else ""
173         return "{}{}{}".format(self.get_installer(), delimitator,
174                                self.get_scenario())