Merge "Change methods to call openstack utils"
[functest.git] / 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     def __init__(self, name, order, ci_loop, description=""):
33         self.tests_array = []
34         self.name = name
35         self.order = order
36         self.ci_loop = ci_loop
37         self.description = description
38
39     def add_test(self, testcase):
40         self.tests_array.append(testcase)
41
42     def get_tests(self):
43         array_tests = []
44         for test in self.tests_array:
45             array_tests.append(test)
46         return array_tests
47
48     def get_test_names(self):
49         array_tests = []
50         for test in self.tests_array:
51             array_tests.append(test.get_name())
52         return array_tests
53
54     def get_test(self, test_name):
55         if self.is_test(test_name):
56             for test in self.tests_array:
57                 if test.get_name() == test_name:
58                     return test
59         return None
60
61     def is_test(self, test_name):
62         for test in self.tests_array:
63             if test.get_name() == test_name:
64                 return True
65         return False
66
67     def get_name(self):
68         return self.name
69
70     def get_order(self):
71         return self.order
72
73     def get_ci_loop(self):
74         return self.ci_loop
75
76     def __str__(self):
77         lines = split_text(self.description, LINE_LENGTH - 6)
78
79         out = ""
80         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
81         out += ("| Tier:  " + self.name.ljust(LINE_LENGTH - 10) + "|\n")
82         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
83         out += ("| Order: " + str(self.order).ljust(LINE_LENGTH - 10) + "|\n")
84         out += ("| CI Loop: " + str(self.ci_loop).ljust(LINE_LENGTH - 12) +
85                 "|\n")
86         out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n")
87         for line in lines:
88             out += ("|    " + line.ljust(LINE_LENGTH - 7) + " |\n")
89         out += ("| Test cases:".ljust(LINE_LENGTH - 1) + "|\n")
90         tests = self.get_test_names()
91         if len(tests) > 0:
92             for i in range(len(tests)):
93                 out += ("|    - %s |\n" % tests[i].ljust(LINE_LENGTH - 9))
94         else:
95             out += ("|    (There are no supported test cases "
96                     .ljust(LINE_LENGTH - 1) + "|\n")
97             out += ("|    in this tier for the given scenario) "
98                     .ljust(LINE_LENGTH - 1) + "|\n")
99         out += ("|".ljust(LINE_LENGTH - 1) + "|\n")
100         out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2)))
101         return out
102
103
104 class TestCase:
105     def __init__(self, name, dependency, criteria, description=""):
106         self.name = name
107         self.dependency = dependency
108         self.description = description
109         self.criteria = criteria
110
111     def is_compatible(self, ci_installer, ci_scenario):
112         try:
113             if ci_installer is not None:
114                 if re.search(self.dependency.get_installer(),
115                              ci_installer) is None:
116                     return False
117             if ci_scenario is not None:
118                 if re.search(self.dependency.get_scenario(),
119                              ci_scenario) is None:
120                     return False
121             return not (ci_scenario is None and ci_installer is None)
122         except TypeError:
123             return False
124
125     def get_name(self):
126         return self.name
127
128     def get_criteria(self):
129         return self.criteria
130
131     def __str__(self):
132         lines = split_text(self.description, LINE_LENGTH - 6)
133
134         out = ""
135         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
136         out += ("| Testcase:  " + self.name.ljust(LINE_LENGTH - 14) + "|\n")
137         out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2)))
138         out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n")
139         for line in lines:
140             out += ("|    " + line.ljust(LINE_LENGTH - 7) + " |\n")
141         out += ("| Criteria:  " +
142                 self.criteria.ljust(LINE_LENGTH - 14) + "|\n")
143         out += ("| Dependencies:".ljust(LINE_LENGTH - 1) + "|\n")
144         installer = self.dependency.get_installer()
145         scenario = self.dependency.get_scenario()
146         out += ("|   - Installer:" + installer.ljust(LINE_LENGTH - 17) + "|\n")
147         out += ("|   - Scenario :" + scenario.ljust(LINE_LENGTH - 17) + "|\n")
148         out += ("|".ljust(LINE_LENGTH - 1) + "|\n")
149         out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2)))
150         return out
151
152
153 class Dependency:
154     def __init__(self, installer, scenario):
155         self.installer = installer
156         self.scenario = scenario
157
158     def get_installer(self):
159         return self.installer
160
161     def get_scenario(self):
162         return self.scenario
163
164     def __str__(self):
165         return ("Dependency info:\n"
166                 "        installer: " + self.installer + "\n"
167                 "        scenario:  " + self.scenario + "\n")