8 import experimental_framework.common as common
9 import experimental_framework.constants.conf_file_sections as cf
14 common.CONF_FILE = None
15 common.DEPLOYMENT_UNIT = None
16 common.ITERATIONS = None
17 common.BASE_DIR = None
18 common.RESULT_DIR = None
19 common.TEMPLATE_DIR = None
20 common.TEMPLATE_NAME = None
21 common.TEMPLATE_FILE_EXTENSION = None
23 common.PKTGEN_DIR = None
24 common.PKTGEN_DPDK_DIRECTORY = None
25 common.PKTGEN_PROGRAM = None
26 common.PKTGEN_COREMASK = None
27 common.PKTGEN_MEMCHANNEL = None
28 common.PKTGEN_BUS_SLOT_NIC_1 = None
29 common.PKTGEN_BUS_SLOT_NIC_2 = None
30 common.INFLUXDB_IP = None
31 common.INFLUXDB_PORT = None
32 common.INFLUXDB_DB_NAME = None
35 class DummyConfigurationFile(common.ConfigurationFile):
36 def __init__(self, sections):
39 def get_variable(self, section, variable_name):
42 def get_variable_list(self, section):
43 return ['template_base_name']
46 class DummyConfigurationFile2(common.ConfigurationFile):
47 def __init__(self, sections):
48 self.pktgen_counter = 0
50 def get_variable(self, section, variable_name):
51 if variable_name == cf.CFSG_TEMPLATE_NAME:
53 if variable_name == cf.CFSG_ITERATIONS:
55 if variable_name == cf.CFSG_DEBUG:
57 if variable_name == cf.CFSP_PACKET_GENERATOR:
58 if self.pktgen_counter == 1:
59 return 'non_supported'
60 self.pktgen_counter += 1
62 if variable_name == cf.CFSP_DPDK_PKTGEN_DIRECTORY:
64 if variable_name == cf.CFSP_DPDK_PROGRAM_NAME:
66 if variable_name == cf.CFSP_DPDK_COREMASK:
68 if variable_name == cf.CFSP_DPDK_MEMORY_CHANNEL:
70 if variable_name == cf.CFSP_DPDK_BUS_SLOT_NIC_1:
71 return 'bus_slot_nic_1'
72 if variable_name == cf.CFSP_DPDK_BUS_SLOT_NIC_2:
73 return 'bus_slot_nic_2'
74 if variable_name == cf.CFSP_DPDK_DPDK_DIRECTORY:
77 def get_variable_list(self, section):
78 if section == cf.CFS_PKTGEN:
80 cf.CFSP_DPDK_NAME_IF_2,
81 cf.CFSP_DPDK_NAME_IF_1,
82 cf.CFSP_DPDK_BUS_SLOT_NIC_1,
83 cf.CFSP_DPDK_BUS_SLOT_NIC_2,
84 cf.CFSP_DPDK_COREMASK,
85 cf.CFSP_DPDK_DPDK_DIRECTORY,
86 cf.CFSP_DPDK_PKTGEN_DIRECTORY,
87 cf.CFSP_DPDK_MEMORY_CHANNEL,
88 cf.CFSP_DPDK_PROGRAM_NAME,
89 cf.CFSP_PACKET_GENERATOR
99 class TestCommonInit(unittest.TestCase):
102 common.CONF_FILE = DummyConfigurationFile('')
103 self.dir = '{}/{}'.format(os.getcwd(),
104 'experimental_framework/')
108 # common.CONF_FILE = None
110 @mock.patch('os.getcwd')
111 @mock.patch('experimental_framework.common.init_conf_file')
112 @mock.patch('experimental_framework.common.init_general_vars')
113 @mock.patch('experimental_framework.common.init_log')
114 @mock.patch('experimental_framework.common.init_pktgen')
115 @mock.patch('experimental_framework.common.CONF_FILE')
116 def test_init_for_success(self, mock_conf_file, init_pkgen, init_log,
117 init_general_vars, init_conf_file, mock_getcwd):
118 mock_getcwd.return_value = self.dir
120 init_pkgen.assert_called_once()
121 init_conf_file.assert_called_once()
122 init_general_vars.assert_called_once()
123 init_log.assert_called_once()
124 expected = self.dir.split('experimental_framework/')[0]
125 self.assertEqual(common.BASE_DIR, expected)
127 def test_init_general_vars_for_success(self):
128 common.BASE_DIR = "{}/".format(os.getcwd())
129 common.init_general_vars()
130 self.assertEqual(common.TEMPLATE_FILE_EXTENSION, '.yaml')
131 heat_dir = self.dir.split('experimental_framework/')[0]
132 self.assertEqual(common.TEMPLATE_DIR,
133 '{}{}'.format(heat_dir, 'heat_templates/'))
134 self.assertEqual(common.TEMPLATE_NAME, 'vTC.yaml')
135 self.assertEqual(common.RESULT_DIR,
136 '{}{}'.format(heat_dir, 'results/'))
137 self.assertEqual(common.ITERATIONS, 1)
140 class TestCommonInit2(unittest.TestCase):
143 common.CONF_FILE = DummyConfigurationFile2('')
144 self.dir = '{}/{}'.format(os.getcwd(), 'experimental_framework/')
148 common.CONF_FILE = None
150 def test_init_general_vars_2_for_success(self):
151 common.BASE_DIR = "{}/".format(os.getcwd())
152 common.init_general_vars()
153 self.assertEqual(common.TEMPLATE_FILE_EXTENSION, '.yaml')
154 heat_dir = self.dir.split('experimental_framework/')[0]
155 self.assertEqual(common.TEMPLATE_DIR,
156 '{}{}'.format(heat_dir, 'heat_templates/'))
157 self.assertEqual(common.TEMPLATE_NAME, 'vTC.yaml')
158 self.assertEqual(common.RESULT_DIR,
159 '{}{}'.format(heat_dir, 'results/'))
160 self.assertEqual(common.ITERATIONS, 2)
162 def test_init_log_2_for_success(self):
164 self.assertIsInstance(common.LOG, logging.RootLogger)
166 def test_init_pktgen_for_success(self):
168 self.assertEqual(common.PKTGEN, 'dpdk_pktgen')
169 directory = self.dir.split('experimental_framework/')[0]
170 self.assertEqual(common.PKTGEN_DIR, directory)
171 self.assertEqual(common.PKTGEN_PROGRAM, 'program')
172 self.assertEqual(common.PKTGEN_COREMASK, 'coremask')
173 self.assertEqual(common.PKTGEN_MEMCHANNEL, 'memchannel')
174 self.assertEqual(common.PKTGEN_BUS_SLOT_NIC_1, 'bus_slot_nic_1')
175 self.assertEqual(common.PKTGEN_BUS_SLOT_NIC_2, 'bus_slot_nic_2')
176 expected_dir = "{}/".format(os.getcwd())
177 self.assertEqual(common.PKTGEN_DPDK_DIRECTORY, expected_dir)
179 def test_init_pktgen_for_failure(self):
180 common.CONF_FILE.get_variable('', cf.CFSP_PACKET_GENERATOR)
181 self.assertRaises(ValueError, common.init_pktgen)
184 class TestConfFileInitialization(unittest.TestCase):
192 @mock.patch('experimental_framework.common.ConfigurationFile',
193 side_effect=DummyConfigurationFile)
194 def test_init_conf_file_for_success(self, conf_file):
195 common.CONF_FILE = None
196 common.init_conf_file(False)
197 self.assertIsInstance(common.CONF_FILE,
198 DummyConfigurationFile)
200 common.CONF_FILE = None
201 common.init_conf_file(True)
202 self.assertIsInstance(common.CONF_FILE,
203 DummyConfigurationFile)
205 @mock.patch('experimental_framework.common.CONF_FILE')
206 def test_init_log_for_success(self, mock_conf_file):
207 mock_conf_file.get_variable_list.return_value = 'value'
209 self.assertIsInstance(common.LOG, logging.RootLogger)
211 @mock.patch('experimental_framework.common.CONF_FILE')
212 def test_init_influxdb_for_success(self, mock_conf_file):
213 mock_conf_file.get_variable.return_value = 'value'
214 common.init_influxdb()
215 self.assertEqual(common.INFLUXDB_IP, 'value')
216 self.assertEqual(common.INFLUXDB_PORT, 'value')
217 self.assertEqual(common.INFLUXDB_DB_NAME, 'value')
220 class DummyConfigurationFile3(common.ConfigurationFile):
223 def __init__(self, sections, config_file='conf.cfg'):
224 common.ConfigurationFile.__init__(self, sections, config_file)
227 def _config_section_map(section, config_file, get_counter=None):
229 return DummyConfigurationFile3.counter
231 DummyConfigurationFile3.counter += 1
235 class TestConfigFileClass(unittest.TestCase):
243 'Deployment-parameters',
244 'Testcase-parameters'
246 c_file = '/tests/data/common/conf.cfg'
247 common.BASE_DIR = os.getcwd()
248 self.conf_file = common.ConfigurationFile(self.sections, c_file)
252 common.BASE_DIR = None
254 @mock.patch('experimental_framework.common.ConfigurationFile.'
255 '_config_section_map',
256 side_effect=DummyConfigurationFile3._config_section_map)
257 def test___init___for_success(self, mock_conf_map):
258 sections = ['General', 'OpenStack', 'Experiment-VNF', 'PacketGen',
259 'Deployment-parameters', 'Testcase-parameters']
260 c = DummyConfigurationFile3(
261 sections, config_file='/tests/data/common/conf.cfg')
263 DummyConfigurationFile3._config_section_map('', '', True),
265 for section in sections:
266 self.assertEqual(getattr(c, section), dict())
268 def test__config_section_map_for_success(self):
269 general_section = 'General'
270 # openstack_section = 'OpenStack'
271 config_file = 'tests/data/common/conf.cfg'
272 config = ConfigParser.ConfigParser()
273 config.read(config_file)
276 'benchmarks': 'b_marks',
278 'template_base_name': 't_name'
281 ConfigurationFile._config_section_map(general_section, config)
282 self.assertEqual(expected, output)
284 @mock.patch('experimental_framework.common.'
285 'ConfigurationFile.get_variable_list')
286 def test_get_variable_for_success(self, mock_get_var_list):
287 section = self.sections[0]
288 variable_name = 'template_base_name'
290 mock_get_var_list.return_value = [variable_name]
291 output = self.conf_file.get_variable(section, variable_name)
292 self.assertEqual(expected, output)
294 @mock.patch('experimental_framework.common.'
295 'ConfigurationFile.get_variable_list')
296 def test_get_variable_for_failure(self, mock_get_var_list):
297 section = self.sections[0]
298 variable_name = 'something_else'
301 self.conf_file.get_variable,
302 section, variable_name
305 def test_get_variable_list_for_success(self):
306 section = self.sections[0]
308 'benchmarks': 'b_marks',
310 'template_base_name': 't_name'
312 output = self.conf_file.get_variable_list(section)
313 self.assertEqual(expected, output)
315 def test_get_variable_list_for_failure(self):
316 section = 'something_else'
319 self.conf_file.get_variable_list,
323 class DummyConfigurationFile4(common.ConfigurationFile):
325 def get_variable(self, section, variable_name):
326 if variable_name == 'vnic2_type':
328 elif variable_name == cf.CFSG_BENCHMARKS:
329 return "BenchmarkClass1, BenchmarkClass2"
330 return '@string "value"'
332 # def get_variable_list(self, section):
336 class TestCommonMethods(unittest.TestCase):
344 'Deployment-parameters',
345 'Testcase-parameters'
347 config_file = '/tests/data/common/conf.cfg'
348 common.BASE_DIR = os.getcwd()
349 common.CONF_FILE = DummyConfigurationFile4(self.sections, config_file)
353 common.CONF_FILE = None
355 def test_get_credentials_for_success(self):
357 'ip_controller': '@string "value"',
358 'project': '@string "value"',
359 'auth_uri': '@string "value"',
360 'user': '@string "value"',
361 'heat_url': '@string "value"',
362 'password': '@string "value"'
364 output = common.get_credentials()
365 self.assertEqual(expected, output)
367 def test_get_heat_template_params_for_success(self):
369 'param_1': '@string "value"',
370 'param_2': '@string "value"',
371 'param_3': '@string "value"',
372 'param_4': '@string "value"'
374 output = common.get_heat_template_params()
375 self.assertEqual(expected, output)
377 def test_get_testcase_params_for_success(self):
378 expected = {'test_case_param': '@string "value"'}
379 output = common.get_testcase_params()
380 self.assertEqual(expected, output)
382 def test_get_file_first_line_for_success(self):
383 file = 'tests/data/common/conf.cfg'
384 expected = '[General]\n'
385 output = common.get_file_first_line(file)
386 self.assertEqual(expected, output)
388 def test_replace_in_file_for_success(self):
389 filename = 'tests/data/common/file_replacement.txt'
390 text_to_search = 'replacement of'
391 text_to_replace = '***'
392 common.replace_in_file(filename, text_to_search, text_to_replace)
393 after = open(filename, 'r').readline()
394 self.assertEqual(after, 'Test for the *** strings into a file\n')
395 text_to_search = '***'
396 text_to_replace = 'replacement of'
397 common.replace_in_file(filename, text_to_search, text_to_replace)
399 @mock.patch('os.system')
400 @mock.patch('experimental_framework.common.LOG')
401 def test_run_command_for_success(self, mock_log, mock_os_system):
402 command = 'command to be run'
403 common.run_command(command)
404 mock_os_system.assert_called_once_with(command)
406 @mock.patch('experimental_framework.common.run_command')
407 def test_push_data_influxdb_for_success(self, mock_run_cmd):
408 data = 'string that describes the data'
409 expected = "curl -i -XPOST 'http://None:None/write?db=None' " \
410 "--data-binary string that describes the data"
411 common.push_data_influxdb(data)
412 mock_run_cmd.assert_called_once_with(expected)
414 def test_get_base_dir_for_success(self):
415 base_dir = common.BASE_DIR
416 common.BASE_DIR = 'base_dir'
417 expected = 'base_dir'
418 output = common.get_base_dir()
419 self.assertEqual(expected, output)
420 common.BASE_DIR = base_dir
422 def test_get_template_dir_for_success(self):
423 template_dir = common.TEMPLATE_DIR
424 common.TEMPLATE_DIR = 'base_dir'
425 expected = 'base_dir'
426 output = common.get_template_dir()
427 self.assertEqual(expected, output)
428 common.TEMPLATE_DIR = template_dir
430 def test_get_dpdk_pktgen_vars_test(self):
432 common.PKTGEN = 'dpdk_pktgen'
433 common.PKTGEN_DIR = 'var'
434 common.PKTGEN_PROGRAM = 'var'
435 common.PKTGEN_COREMASK = 'var'
436 common.PKTGEN_MEMCHANNEL = 'var'
437 common.PKTGEN_BUS_SLOT_NIC_1 = 'var'
438 common.PKTGEN_BUS_SLOT_NIC_2 = 'var'
439 common.PKTGEN_DPDK_DIRECTORY = 'var'
441 'bus_slot_nic_1': 'var',
442 'bus_slot_nic_2': 'var',
444 'dpdk_directory': 'var',
445 'memory_channels': 'var',
446 'pktgen_directory': 'var',
447 'program_name': 'var'
449 output = common.get_dpdk_pktgen_vars()
450 self.assertEqual(expected, output)
453 common.PKTGEN = 'something_else'
454 common.PKTGEN_DIR = 'var'
455 common.PKTGEN_PROGRAM = 'var'
456 common.PKTGEN_COREMASK = 'var'
457 common.PKTGEN_MEMCHANNEL = 'var'
458 common.PKTGEN_BUS_SLOT_NIC_1 = 'var'
459 common.PKTGEN_BUS_SLOT_NIC_2 = 'var'
460 common.PKTGEN_DPDK_DIRECTORY = 'var'
462 output = common.get_dpdk_pktgen_vars()
463 self.assertEqual(expected, output)
465 @mock.patch('experimental_framework.common.LOG')
466 def test_get_deployment_configuration_variables_for_success(self,
470 'vnic1_type': ['value'],
472 'vnic2_type': ['value']
474 output = common.get_deployment_configuration_variables_from_conf_file()
475 self.assertEqual(expected, output)
477 def test_get_benchmarks_from_conf_file_for_success(self):
478 expected = ['BenchmarkClass1', 'BenchmarkClass2']
479 output = common.get_benchmarks_from_conf_file()
480 self.assertEqual(expected, output)
483 class TestinputValidation(unittest.TestCase):
491 def test_validate_string_for_success(self):
492 output = common.InputValidation.validate_string('string', '')
493 self.assertTrue(output)
495 def test_validate_string_for_failure(self):
498 common.InputValidation.validate_string,
502 def test_validate_int_for_success(self):
503 output = common.InputValidation.validate_integer(1111, '')
504 self.assertTrue(output)
506 def test_validate_int_for_failure(self):
509 common.InputValidation.validate_integer,
513 def test_validate_dict_for_success(self):
514 output = common.InputValidation.validate_dictionary(dict(), '')
515 self.assertTrue(output)
517 def test_validate_dict_for_failure(self):
520 common.InputValidation.validate_dictionary,
524 def test_validate_file_exist_for_success(self):
525 filename = 'tests/data/common/file_replacement.txt'
526 output = common.InputValidation.validate_file_exist(filename, '')
527 self.assertTrue(output)
529 def test_validate_file_exist_for_failure(self):
530 filename = 'tests/data/common/file_replacement'
533 common.InputValidation.validate_file_exist,
537 def test_validate_directory_exist_and_format_for_success(self):
538 directory = 'tests/data/common/'
539 output = common.InputValidation.\
540 validate_directory_exist_and_format(directory, '')
541 self.assertTrue(output)
543 def test_validate_directory_exist_and_format_for_failure(self):
544 directory = 'tests/data/com/'
547 common.InputValidation.validate_directory_exist_and_format,
551 @mock.patch('experimental_framework.common.CONF_FILE')
552 def test_validate_configuration_file_parameter_for_success(self,
554 mock_conf.get_variable_list.return_value = ['param']
558 output = common.InputValidation.\
559 validate_configuration_file_parameter(section, parameter, message)
560 self.assertTrue(output)
562 @mock.patch('experimental_framework.common.CONF_FILE')
563 def test_validate_configuration_file_parameter_for_failure(
564 self, mock_conf_file):
566 parameter = 'something_else'
568 mock_conf_file.get_variable_list.return_value(['parameter'])
571 common.InputValidation.
572 validate_configuration_file_parameter,
573 section, parameter, message
576 def test_validate_configuration_file_section_for_success(self):
579 output = common.InputValidation.\
580 validate_configuration_file_section(section, message)
581 self.assertTrue(output)
583 def test_validate_configuration_file_section_for_failure(self):
584 section = 'Something-Else'
588 common.InputValidation.validate_configuration_file_section,
592 def test_validate_boolean_for_success(self):
595 output = common.InputValidation.validate_boolean(boolean, message)
596 self.assertTrue(output)
599 output = common.InputValidation.validate_boolean(boolean, message)
600 self.assertTrue(output)
603 output = common.InputValidation.validate_boolean(boolean, message)
604 self.assertFalse(output)
606 def test_validate_boolean_for_failure(self):
611 common.InputValidation.validate_boolean,
615 def test_validate_os_credentials_for_failure(self):
618 self.assertRaises(ValueError,
619 common.InputValidation.validate_os_credentials,
624 credentials['ip_controller'] = ''
625 credentials['heat_url'] = ''
626 credentials['user'] = ''
627 credentials['password'] = ''
628 credentials['auth_uri'] = ''
629 # credentials['project'] = ''
630 self.assertRaises(ValueError,
631 common.InputValidation.validate_os_credentials,
634 def test_validate_os_credentials_for_success(self):
636 credentials['ip_controller'] = ''
637 credentials['heat_url'] = ''
638 credentials['user'] = ''
639 credentials['password'] = ''
640 credentials['auth_uri'] = ''
641 credentials['project'] = ''
643 common.InputValidation.validate_os_credentials(credentials))