Extracted all global parameters into functest_constants.py
[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:
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:
106
107     def __init__(self, name, dependency, criteria, blocking, description=""):
108         self.name = name
109         self.dependency = dependency
110         self.description = description
111         self.criteria = criteria
112         self.blocking = blocking
113
114     @staticmethod
115     def is_none(item):
116         return item is None or item is ""
117
118     def is_compatible(self, ci_installer, ci_scenario):
119         try:
120             if not self.is_none(ci_installer):
121                 if re.search(self.dependency.get_installer(),
122                              ci_installer) is None:
123                     return False
124             if not self.is_none(ci_scenario):
125                 if re.search(self.dependency.get_scenario(),
126                              ci_scenario) is None:
127                     return False
128             return True
129         except TypeError:
130             return False
131
132     def get_name(self):
133         return self.name
134
135     def get_criteria(self):
136         return self.criteria
137
138     def is_blocking(self):
139         return self.blocking
140
141     def __str__(self):
142         lines = split_text(self.description, LINE_LENGTH - 6)
143
144         out = ""
145         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
146         out += ("| Testcase:  " + self.name.ljust(LINE_LENGTH - 14) + "|\n")
147         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
148         out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n")
149         for line in lines:
150             out += ("|    " + line.ljust(LINE_LENGTH - 7) + " |\n")
151         out += ("| Criteria:  " +
152                 self.criteria.ljust(LINE_LENGTH - 14) + "|\n")
153         out += ("| Dependencies:".ljust(LINE_LENGTH - 1) + "|\n")
154         installer = self.dependency.get_installer()
155         scenario = self.dependency.get_scenario()
156         out += ("|   - Installer:" + installer.ljust(LINE_LENGTH - 17) + "|\n")
157         out += ("|   - Scenario :" + scenario.ljust(LINE_LENGTH - 17) + "|\n")
158         out += ("|".ljust(LINE_LENGTH - 1) + "|\n")
159         out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2)))
160         return out
161
162
163 class Dependency:
164
165     def __init__(self, installer, scenario):
166         self.installer = installer
167         self.scenario = scenario
168
169     def get_installer(self):
170         return self.installer
171
172     def get_scenario(self):
173         return self.scenario
174
175     def __str__(self):
176         return ("Dependency info:\n"
177                 "        installer: " + self.installer + "\n"
178                 "        scenario:  " + self.scenario + "\n")