|
1 | 1 | import csv
|
2 | 2 | import json
|
| 3 | +import os |
| 4 | + |
| 5 | +from bp_controller.flows.bp_download_test_file_flow import BPDownloadTestFileFlow |
3 | 6 | from bp_controller.helpers.port_reservation_helper import PortReservationHelper
|
4 | 7 | import re
|
5 | 8 |
|
@@ -34,6 +37,7 @@ def __init__(self, context, logger, api):
|
34 | 37 | self.__test_statistics_flow = None
|
35 | 38 | self.__test_results_flow = None
|
36 | 39 | self.__test_configuration_file_flow = None
|
| 40 | + self.__download_test_file_flow = None |
37 | 41 | self.__reservation_details = None
|
38 | 42 | self.__port_reservation_helper = None
|
39 | 43 |
|
@@ -104,6 +108,13 @@ def _test_configuration_file_flow(self):
|
104 | 108 | self.logger)
|
105 | 109 | return self.__test_configuration_file_flow
|
106 | 110 |
|
| 111 | + @property |
| 112 | + def _download_test_file_flow(self): |
| 113 | + if not self.__download_test_file_flow: |
| 114 | + self.__download_test_file_flow = BPDownloadTestFileFlow(self._session_context_manager, |
| 115 | + self.logger) |
| 116 | + return self.__download_test_file_flow |
| 117 | + |
107 | 118 | @property
|
108 | 119 | def _cs_reservation_details(self):
|
109 | 120 | """
|
@@ -151,12 +162,29 @@ def _port_reservation_helper(self):
|
151 | 162 | self.logger)
|
152 | 163 | return self.__port_reservation_helper
|
153 | 164 |
|
| 165 | + def _get_existing_path(self, file_path): |
| 166 | + """ |
| 167 | + Looking for existing path |
| 168 | + :return: |
| 169 | + :rtype: basestring |
| 170 | + """ |
| 171 | + search_order = [os.path.join(self.context.resource.attributes.get('Test Files Location') or '', file_path), |
| 172 | + file_path] |
| 173 | + for path in search_order: |
| 174 | + if os.path.exists(path): |
| 175 | + return path |
| 176 | + raise BPRunnerException(self.__class__.__name__, |
| 177 | + 'File {} does not exists or "Test Files Location" attribute was not specified'.format( |
| 178 | + file_path)) |
| 179 | + |
154 | 180 | def load_configuration(self, file_path):
|
155 | 181 | """
|
156 | 182 | Upload configuration file and reserve ports
|
157 | 183 | :param file_path:
|
158 | 184 | :return:
|
159 | 185 | """
|
| 186 | + file_path = self._get_existing_path(file_path) |
| 187 | + |
160 | 188 | self._test_name = self._test_configuration_file_flow.load_configuration(file_path)
|
161 | 189 | test_model = ElementTree.parse(file_path).getroot().find('testmodel')
|
162 | 190 | network_name = test_model.get('network')
|
@@ -247,6 +275,23 @@ def get_results(self):
|
247 | 275 | file_stream=pdf_result)
|
248 | 276 | return "Please check attachments for results"
|
249 | 277 |
|
| 278 | + def get_test_file(self, test_name): |
| 279 | + test_files_location = self.context.resource.attributes.get('Test Files Location') |
| 280 | + if not test_files_location: |
| 281 | + raise BPRunnerException(self.__class__.__name__, "Test Files Location attribute is not defined") |
| 282 | + if not os.path.exists(test_files_location) or os.access(test_files_location, os.W_OK) is not True: |
| 283 | + raise BPRunnerException(self.__class__.__name__, |
| 284 | + 'The location of the test files "{}" does not exist or is not writable'.format( |
| 285 | + test_files_location)) |
| 286 | + reservation_files = os.path.join(test_files_location, self.context.reservation.reservation_id) |
| 287 | + if not os.path.exists(reservation_files): |
| 288 | + os.makedirs(reservation_files) |
| 289 | + test_file_path = os.path.join(reservation_files, test_name + '.bpt', ) |
| 290 | + test_file_content = self._download_test_file_flow.download_test_file(test_name) |
| 291 | + with open(test_file_path, 'w') as f: |
| 292 | + f.write(test_file_content) |
| 293 | + return test_file_path |
| 294 | + |
250 | 295 | def close(self):
|
251 | 296 | """
|
252 | 297 | Destroy
|
|
0 commit comments