Switch to py3.6 in containers
[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 # pylint: disable=missing-docstring,too-many-instance-attributes
11
12 """Tier and TestCase classes to wrap the testcases config file"""
13
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():
38
39     def __init__(self, name, order, description=""):
40         self.tests_array = []
41         self.skipped_tests_array = []
42         self.name = name
43         self.order = order
44         self.description = description
45
46     def add_test(self, testcase):
47         self.tests_array.append(testcase)
48
49     def skip_test(self, testcase):
50         self.skipped_tests_array.append(testcase)
51
52     def get_tests(self):
53         array_tests = []
54         for test in self.tests_array:
55             array_tests.append(test)
56         return array_tests
57
58     def get_skipped_test(self):
59         return self.skipped_tests_array
60
61     def get_test_names(self):
62         array_tests = []
63         for test in self.tests_array:
64             array_tests.append(test.get_name())
65         return array_tests
66
67     def get_test(self, test_name):
68         if self.is_test(test_name):
69             for test in self.tests_array + self.skipped_tests_array:
70                 if test.get_name() == test_name:
71                     return test
72         return None
73
74     def is_test(self, test_name):
75         for test in self.tests_array + self.skipped_tests_array:
76             if test.get_name() == test_name:
77                 return True
78         return False
79
80     def get_name(self):
81         return self.name
82
83     def get_order(self):
84         return self.order
85
86     def __str__(self):
87         msg = prettytable.PrettyTable(
88             header_style='upper', padding_width=5,
89             field_names=['tiers', 'order', 'description',
90                          'testcases'])
91         msg.add_row(
92             [self.name, self.order,
93              textwrap.fill(self.description, width=40),
94              textwrap.fill(' '.join([str(x.get_name(
95                  )) for x in self.get_tests()]), width=40)])
96         return msg.get_string()
97
98
99 class TestCase():
100
101     def __init__(self, name, enabled, skipped, criteria, blocking,
102                  description="", project=""):
103         # pylint: disable=too-many-arguments
104         self.name = name
105         self.enabled = enabled
106         self.skipped = skipped
107         self.criteria = criteria
108         self.blocking = blocking
109         self.description = description
110         self.project = project
111
112     def get_name(self):
113         return self.name
114
115     def is_enabled(self):
116         return self.enabled
117
118     def is_skipped(self):
119         return self.skipped
120
121     def get_criteria(self):
122         return self.criteria
123
124     def is_blocking(self):
125         return self.blocking
126
127     def get_project(self):
128         return self.project
129
130     def __str__(self):
131         msg = prettytable.PrettyTable(
132             header_style='upper', padding_width=5,
133             field_names=['test case', 'description', 'criteria'])
134         msg.add_row([self.name, textwrap.fill(self.description, width=40),
135                      self.criteria])
136         return msg.get_string()