Additional Unit Tests for core modules
[functest.git] / functest / ci / tier_handler.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9
10
11 import re
12
13 LINE_LENGTH = 72
14
15
16 def split_text(text, max_len):
17     words = text.split()
18     lines = []
19     line = ""
20     for word in words:
21         if len(line) + len(word) < max_len - 1:
22             line += word + " "
23         else:
24             lines.append(line)
25             line = word + " "
26     if line != "":
27         lines.append(line)
28     return lines
29
30
31 class Tier(object):
32
33     def __init__(self, name, order, ci_loop, description=""):
34         self.tests_array = []
35         self.name = name
36         self.order = order
37         self.ci_loop = ci_loop
38         self.description = description
39
40     def add_test(self, testcase):
41         self.tests_array.append(testcase)
42
43     def get_tests(self):
44         array_tests = []
45         for test in self.tests_array:
46             array_tests.append(test)
47         return array_tests
48
49     def get_test_names(self):
50         array_tests = []
51         for test in self.tests_array:
52             array_tests.append(test.get_name())
53         return array_tests
54
55     def get_test(self, test_name):
56         if self.is_test(test_name):
57             for test in self.tests_array:
58                 if test.get_name() == test_name:
59                     return test
60         return None
61
62     def is_test(self, test_name):
63         for test in self.tests_array:
64             if test.get_name() == test_name:
65                 return True
66         return False
67
68     def get_name(self):
69         return self.name
70
71     def get_order(self):
72         return self.order
73
74     def get_ci_loop(self):
75         return self.ci_loop
76
77     def __str__(self):
78         lines = split_text(self.description, LINE_LENGTH - 6)
79
80         out = ""
81         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
82         out += ("| Tier:  " + self.name.ljust(LINE_LENGTH - 10) + "|\n")
83         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
84         out += ("| Order: " + str(self.order).ljust(LINE_LENGTH - 10) + "|\n")
85         out += ("| CI Loop: " + str(self.ci_loop).ljust(LINE_LENGTH - 12) +
86                 "|\n")
87         out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n")
88         for line in lines:
89             out += ("|    " + line.ljust(LINE_LENGTH - 7) + " |\n")
90         out += ("| Test cases:".ljust(LINE_LENGTH - 1) + "|\n")
91         tests = self.get_test_names()
92         if len(tests) > 0:
93             for i in range(len(tests)):
94                 out += ("|    - %s |\n" % tests[i].ljust(LINE_LENGTH - 9))
95         else:
96             out += ("|    (There are no supported test cases "
97                     .ljust(LINE_LENGTH - 1) + "|\n")
98             out += ("|    in this tier for the given scenario) "
99                     .ljust(LINE_LENGTH - 1) + "|\n")
100         out += ("|".ljust(LINE_LENGTH - 1) + "|\n")
101         out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2)))
102         return out
103
104
105 class TestCase(object):
106
107     def __init__(self, name,
108                  dependency,
109                  criteria,
110                  blocking,
111                  clean_flag,
112                  description=""):
113         self.name = name
114         self.dependency = dependency
115         self.criteria = criteria
116         self.blocking = blocking
117         self.clean_flag = clean_flag
118         self.description = description
119
120     @staticmethod
121     def is_none(item):
122         return item is None or item is ""
123
124     def is_compatible(self, ci_installer, ci_scenario):
125         try:
126             if not self.is_none(ci_installer):
127                 if re.search(self.dependency.get_installer(),
128                              ci_installer) is None:
129                     return False
130             if not self.is_none(ci_scenario):
131                 if re.search(self.dependency.get_scenario(),
132                              ci_scenario) is None:
133                     return False
134             return True
135         except TypeError:
136             return False
137
138     def get_name(self):
139         return self.name
140
141     def get_criteria(self):
142         return self.criteria
143
144     def is_blocking(self):
145         return self.blocking
146
147     def needs_clean(self):
148         return self.clean_flag
149
150     def __str__(self):
151         lines = split_text(self.description, LINE_LENGTH - 6)
152
153         out = ""
154         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
155         out += ("| Testcase:  " + self.name.ljust(LINE_LENGTH - 14) + "|\n")
156         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
157         out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n")
158         for line in lines:
159             out += ("|    " + line.ljust(LINE_LENGTH - 7) + " |\n")
160         out += ("| Criteria:  " +
161                 str(self.criteria).ljust(LINE_LENGTH - 14) + "|\n")
162         out += ("| Dependencies:".ljust(LINE_LENGTH - 1) + "|\n")
163         installer = self.dependency.get_installer()
164         scenario = self.dependency.get_scenario()
165         out += ("|   - Installer:" + installer.ljust(LINE_LENGTH - 17) + "|\n")
166         out += ("|   - Scenario :" + scenario.ljust(LINE_LENGTH - 17) + "|\n")
167         out += ("|".ljust(LINE_LENGTH - 1) + "|\n")
168         out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2)))
169         return out
170
171
172 class Dependency(object):
173
174     def __init__(self, installer, scenario):
175         self.installer = installer
176         self.scenario = scenario
177
178     def get_installer(self):
179         return self.installer
180
181     def get_scenario(self):
182         return self.scenario
183
184     def __str__(self):
185         return ("Dependency info:\n"
186                 "        installer: " + self.installer + "\n"
187                 "        scenario:  " + self.scenario + "\n")