blob: 67746f79674d585f9042bb3b1173ba208ce11f93 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# osmo_gsm_tester: trial: directory of binaries to be tested
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import time
22import shutil
23import tarfile
24
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020025from . import log, util, suite, report
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026
27FILE_MARK_TAKEN = 'taken'
28FILE_CHECKSUMS = 'checksums.md5'
29TIMESTAMP_FMT = '%Y-%m-%d_%H-%M-%S'
30FILE_LAST_RUN = 'last_run'
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020031FILE_LOG = 'log'
Neels Hofmeyr4f33dcc2017-05-07 00:01:09 +020032FILE_LOG_BRIEF = 'log_brief'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020033
34class Trial(log.Origin):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020035 UNKNOWN = 'UNKNOWN'
36 PASS = 'PASS'
37 FAIL = 'FAIL'
38
Neels Hofmeyr3531a192017-03-28 14:30:28 +020039 path = None
40 dir = None
41 _run_dir = None
42 bin_tars = None
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020043 log_targets = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020044
45 @staticmethod
46 def next(trials_dir):
47
48 with trials_dir.lock('Trial.next'):
49 trials = [e for e in trials_dir.children()
50 if trials_dir.isdir(e) and not trials_dir.exists(e, FILE_MARK_TAKEN)]
51 if not trials:
52 return None
53 # sort by time to get the one that waited longest
54 trials.sort(key=lambda e: os.path.getmtime(trials_dir.child(e)))
55 next_trial = trials[0]
56 return Trial(trials_dir.child(next_trial)).take()
57
58 def __init__(self, trial_dir):
Neels Hofmeyre44a0cb2017-05-14 03:25:45 +020059 self.path = os.path.abspath(trial_dir)
Your Name3c6673a2017-04-08 18:52:39 +020060 self.set_name(os.path.basename(self.path))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020061 self.set_log_category(log.C_TST)
62 self.dir = util.Dir(self.path)
63 self.inst_dir = util.Dir(self.dir.child('inst'))
64 self.bin_tars = []
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020065 self.suites = []
66 self.junit_path = self.get_run_dir().new_file(self.name()+'.xml')
67 self.status = Trial.UNKNOWN
Neels Hofmeyr3531a192017-03-28 14:30:28 +020068
69 def __repr__(self):
70 return self.name()
71
72 def __enter__(self):
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020073 # add a log target to log to the run dir
74 run_dir = self.get_run_dir()
Neels Hofmeyr39b0b892017-05-14 16:16:31 +020075 detailed_log = run_dir.new_child(FILE_LOG)
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020076 self.log_targets = [
Neels Hofmeyr39b0b892017-05-14 16:16:31 +020077 log.FileLogTarget(detailed_log)
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020078 .set_all_levels(log.L_DBG)
79 .style_change(trace=True),
Neels Hofmeyr4f33dcc2017-05-07 00:01:09 +020080 log.FileLogTarget(run_dir.new_child(FILE_LOG_BRIEF))
81 .style_change(src=False, all_origins=False)
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020082 ]
Neels Hofmeyr3531a192017-03-28 14:30:28 +020083 self.log('Trial start')
Neels Hofmeyr39b0b892017-05-14 16:16:31 +020084 self.log('Detailed log at', detailed_log)
Neels Hofmeyr506edbc2017-05-06 21:56:27 +020085 self.take()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020086 super().__enter__()
87
88 def __exit__(self, *exc_info):
89 super().__exit__(*exc_info)
90 self.log('Trial end')
91
Neels Hofmeyrfd7b9d02017-05-05 19:51:40 +020092 for lt in self.log_targets:
93 lt.remove()
94 self.log_targets = None
95
Neels Hofmeyr3531a192017-03-28 14:30:28 +020096 def take(self):
97 self.dir.touch(FILE_MARK_TAKEN)
98 return self
99
100 def get_run_dir(self):
101 if self._run_dir is not None:
102 return self._run_dir
103 self._run_dir = util.Dir(self.dir.new_child('run.%s' % time.strftime(TIMESTAMP_FMT)))
104 self._run_dir.mkdir()
105
106 last_run = self.dir.child(FILE_LAST_RUN)
107 if os.path.islink(last_run):
108 os.remove(last_run)
109 if not os.path.exists(last_run):
110 os.symlink(self.dir.rel_path(self._run_dir.path), last_run)
111 return self._run_dir
112
113 def verify(self):
114 "verify checksums"
115
116 if not self.dir.exists():
117 raise RuntimeError('Trial dir does not exist: %r' % self.dir)
118 if not self.dir.isdir():
119 raise RuntimeError('Trial dir is not a dir: %r' % self.dir)
120
121 checksums = self.dir.child(FILE_CHECKSUMS)
122 if not self.dir.isfile(FILE_CHECKSUMS):
123 raise RuntimeError('No checksums file in trial dir: %r', checksums)
124
125 with open(checksums, 'r') as f:
126 line_nr = 0
127 for line in [l.strip() for l in f.readlines()]:
128 line_nr += 1
129 if not line:
130 continue
131 md5, filename = line.split(' ')
132 file_path = self.dir.child(filename)
133
134 if not self.dir.isfile(filename):
135 raise RuntimeError('File listed in checksums file but missing in trials dir:'
136 ' %r vs. %r line %d' % (file_path, checksums, line_nr))
137
138 if md5 != util.md5_of_file(file_path):
139 raise RuntimeError('Checksum mismatch for %r vs. %r line %d'
140 % (file_path, checksums, line_nr))
141
142 if filename.endswith('.tgz'):
143 self.bin_tars.append(filename)
144
145 def has_bin_tar(self, bin_name):
146 bin_tar_start = '%s.' % bin_name
147 matches = [t for t in self.bin_tars if t.startswith(bin_tar_start)]
148 self.dbg(bin_name=bin_name, matches=matches)
149 if not matches:
150 return None
151 if len(matches) > 1:
152 raise RuntimeError('More than one match for bin name %r: %r' % (bin_name, matches))
153 bin_tar = matches[0]
154 bin_tar_path = self.dir.child(bin_tar)
155 if not os.path.isfile(bin_tar_path):
156 raise RuntimeError('Not a file or missing: %r' % bin_tar_path)
157 return bin_tar_path
158
159 def get_inst(self, bin_name):
160 bin_tar = self.has_bin_tar(bin_name)
161 if not bin_tar:
Your Name3c6673a2017-04-08 18:52:39 +0200162 raise RuntimeError('No such binary available: %r' % bin_name)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200163 inst_dir = self.inst_dir.child(bin_name)
164
165 if os.path.isdir(inst_dir):
166 # already unpacked
167 return inst_dir
168
169 t = None
170 try:
171 os.makedirs(inst_dir)
172 t = tarfile.open(bin_tar)
173 t.extractall(inst_dir)
174 return inst_dir
175
176 except:
177 shutil.rmtree(inst_dir)
178 raise
179 finally:
180 if t:
181 try:
182 t.close()
183 except:
184 pass
185
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200186 def add_suite(self, suite_run):
187 self.suites.append(suite_run)
188
189 def run_suites(self, names=None):
190 self.status = Trial.UNKNOWN
191 for suite_run in self.suites:
192 st = suite_run.run_tests(names)
193 if st == suite.SuiteRun.FAIL:
194 self.status = Trial.FAIL
195 elif self.status == Trial.UNKNOWN:
196 self.status = Trial.PASS
197 self.log(self.status)
198 junit_path = self.get_run_dir().new_file(self.name()+'.xml')
199 self.log('Storing JUnit report in', junit_path)
200 report.trial_to_junit_write(self, junit_path)
201 return self.status
202
203 def log_report(self):
204 self.log(report.trial_to_text(self))
205
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200206# vim: expandtab tabstop=4 shiftwidth=4