Skip the selected testcase too
[functest-xtesting.git] / xtesting / 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 + self.skipped_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 + self.skipped_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):  # pylint: disable=too-many-instance-attributes
104
105     def __init__(self, name, enabled, skipped, 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.skipped = skipped
112         self.criteria = criteria
113         self.blocking = blocking
114         self.description = description
115         self.project = project
116
117     def is_compatible(self, ci_installer, ci_scenario):
118         try:
119             if not re.search(self.dependency.get_installer(), ci_installer):
120                 return False
121             if not re.search(self.dependency.get_scenario(), ci_scenario):
122                 return False
123             return True
124         except TypeError:
125             return False
126
127     def get_name(self):
128         return self.name
129
130     def is_enabled(self):
131         return self.enabled
132
133     def is_skipped(self):
134         return self.skipped
135
136     def get_criteria(self):
137         return self.criteria
138
139     def is_blocking(self):
140         return self.blocking
141
142     def get_project(self):
143         return self.project
144
145     def __str__(self):
146         msg = prettytable.PrettyTable(
147             header_style='upper', padding_width=5,
148             field_names=['test case', 'description', 'criteria', 'dependency'])
149         msg.add_row([self.name, textwrap.fill(self.description, width=40),
150                      self.criteria, self.dependency])
151         return msg.get_string()
152
153
154 class Dependency(object):
155
156     def __init__(self, installer='.*', scenario='.*'):
157         self.installer = installer
158         self.scenario = scenario
159
160     def get_installer(self):
161         return self.installer
162
163     def get_scenario(self):
164         return self.scenario
165
166     def __str__(self):
167         delimitator = "\n" if self.get_installer(
168             ) and self.get_scenario() else ""
169         return "{}{}{}".format(self.get_installer(), delimitator,
170                                self.get_scenario())