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, conf_file=''):
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 @mock.patch('os.path.exists')
128 @mock.patch('os.makedirs')
129 @mock.patch('experimental_framework.common.LOG')
130 def test_init_general_vars_for_success(self, mock_log, mock_makedirs,
132 common.BASE_DIR = "{}/".format(os.getcwd())
133 mock_path_exists.return_value = False
134 common.init_general_vars()
135 self.assertEqual(common.TEMPLATE_FILE_EXTENSION, '.yaml')
136 self.assertEqual(common.TEMPLATE_DIR, '/tmp/apexlake/heat_templates/')
137 self.assertEqual(common.TEMPLATE_NAME, 'vTC.yaml')
138 self.assertEqual(common.RESULT_DIR, '/tmp/apexlake/results/')
139 self.assertEqual(common.ITERATIONS, 1)
140 mock_makedirs.assert_called_once_with('/tmp/apexlake/heat_templates/')
143 class TestCommonInit2(unittest.TestCase):
146 common.CONF_FILE = DummyConfigurationFile2('')
147 self.dir = '{}/{}'.format(os.getcwd(), 'experimental_framework/')
151 common.CONF_FILE = None
153 @mock.patch('experimental_framework.common.LOG')
154 def test_init_general_vars_2_for_success(self, mock_log):
155 common.BASE_DIR = "{}/".format(os.getcwd())
156 common.init_general_vars()
157 self.assertEqual(common.TEMPLATE_FILE_EXTENSION, '.yaml')
158 self.assertEqual(common.TEMPLATE_DIR, '/tmp/apexlake/heat_templates/')
159 self.assertEqual(common.TEMPLATE_NAME, 'vTC.yaml')
160 self.assertEqual(common.RESULT_DIR, '/tmp/apexlake/results/')
161 self.assertEqual(common.ITERATIONS, 2)
163 def test_init_log_2_for_success(self):
165 self.assertIsInstance(common.LOG, logging.RootLogger)
167 def test_init_pktgen_for_success(self):
169 self.assertEqual(common.PKTGEN, 'dpdk_pktgen')
170 directory = self.dir.split('experimental_framework/')[0]
171 self.assertEqual(common.PKTGEN_DIR, directory)
172 self.assertEqual(common.PKTGEN_PROGRAM, 'program')
173 self.assertEqual(common.PKTGEN_COREMASK, 'coremask')
174 self.assertEqual(common.PKTGEN_MEMCHANNEL, 'memchannel')
175 self.assertEqual(common.PKTGEN_BUS_SLOT_NIC_1, 'bus_slot_nic_1')
176 self.assertEqual(common.PKTGEN_BUS_SLOT_NIC_2, 'bus_slot_nic_2')
177 expected_dir = "{}/".format(os.getcwd())
178 self.assertEqual(common.PKTGEN_DPDK_DIRECTORY, expected_dir)
180 def test_init_pktgen_for_failure(self):
181 common.CONF_FILE.get_variable('', cf.CFSP_PACKET_GENERATOR)
182 self.assertRaises(ValueError, common.init_pktgen)
185 class TestConfFileInitialization(unittest.TestCase):
193 @mock.patch('experimental_framework.common.ConfigurationFile',
194 side_effect=DummyConfigurationFile)
195 def test_init_conf_file_for_success(self, conf_file):
196 common.CONF_FILE = None
197 common.init_conf_file(False)
198 self.assertIsInstance(common.CONF_FILE,
199 DummyConfigurationFile)
201 common.CONF_FILE = None
202 common.init_conf_file(True)
203 self.assertIsInstance(common.CONF_FILE,
204 DummyConfigurationFile)
206 @mock.patch('experimental_framework.common.CONF_FILE')
207 def test_init_log_for_success(self, mock_conf_file):
208 mock_conf_file.get_variable_list.return_value = 'value'
210 self.assertIsInstance(common.LOG, logging.RootLogger)
212 @mock.patch('experimental_framework.common.CONF_FILE')
213 def test_init_influxdb_for_success(self, mock_conf_file):
214 mock_conf_file.get_variable.return_value = 'value'
215 common.init_influxdb()
216 self.assertEqual(common.INFLUXDB_IP, 'value')
217 self.assertEqual(common.INFLUXDB_PORT, 'value')
218 self.assertEqual(common.INFLUXDB_DB_NAME, 'value')
221 class DummyConfigurationFile3(common.ConfigurationFile):
224 def __init__(self, sections, config_file='conf.cfg'):
225 common.ConfigurationFile.__init__(self, sections, config_file)
228 def _config_section_map(section, config_file, get_counter=None):
230 return DummyConfigurationFile3.counter
232 DummyConfigurationFile3.counter += 1
236 class TestConfigFileClass(unittest.TestCase):
244 'Deployment-parameters',
245 'Testcase-parameters'
247 c_file = './tests/data/common/conf.cfg'
248 common.BASE_DIR = os.getcwd()
249 self.conf_file = common.ConfigurationFile(self.sections, c_file)
253 common.BASE_DIR = None
255 @mock.patch('experimental_framework.common.ConfigurationFile.'
256 '_config_section_map',
257 side_effect=DummyConfigurationFile3._config_section_map)
258 def test___init___for_success(self, mock_conf_map):
259 sections = ['General', 'OpenStack', 'Experiment-VNF', 'PacketGen',
260 'Deployment-parameters', 'Testcase-parameters']
261 c = DummyConfigurationFile3(
262 sections, config_file='./tests/data/common/conf.cfg')
264 DummyConfigurationFile3._config_section_map('', '', True),
266 for section in sections:
267 self.assertEqual(getattr(c, section), dict())
269 def test__config_section_map_for_success(self):
270 general_section = 'General'
271 # openstack_section = 'OpenStack'
272 config_file = 'tests/data/common/conf.cfg'
273 config = ConfigParser.ConfigParser()
274 config.read(config_file)
277 'benchmarks': 'b_marks',
279 'template_base_name': 't_name'
282 ConfigurationFile._config_section_map(general_section, config)
283 self.assertEqual(expected, output)
285 @mock.patch('experimental_framework.common.'
286 'ConfigurationFile.get_variable_list')
287 def test_get_variable_for_success(self, mock_get_var_list):
288 section = self.sections[0]
289 variable_name = 'template_base_name'
291 mock_get_var_list.return_value = [variable_name]
292 output = self.conf_file.get_variable(section, variable_name)
293 self.assertEqual(expected, output)
295 @mock.patch('experimental_framework.common.'
296 'ConfigurationFile.get_variable_list')
297 def test_get_variable_for_failure(self, mock_get_var_list):
298 section = self.sections[0]
299 variable_name = 'something_else'
302 self.conf_file.get_variable,
303 section, variable_name
306 def test_get_variable_list_for_success(self):
307 section = self.sections[0]
309 'benchmarks': 'b_marks',
311 'template_base_name': 't_name'
313 output = self.conf_file.get_variable_list(section)
314 self.assertEqual(expected, output)
316 def test_get_variable_list_for_failure(self):
317 section = 'something_else'
320 self.conf_file.get_variable_list,
324 class DummyConfigurationFile4(common.ConfigurationFile):
326 def get_variable(self, section, variable_name):
327 if variable_name == 'vnic2_type':
329 elif variable_name == cf.CFSG_BENCHMARKS:
330 return "BenchmarkClass1, BenchmarkClass2"
331 return '@string "value"'
333 # def get_variable_list(self, section):
337 class TestCommonMethods(unittest.TestCase):
345 'Deployment-parameters',
346 'Testcase-parameters'
348 config_file = './tests/data/common/conf.cfg'
349 common.BASE_DIR = os.getcwd()
350 common.CONF_FILE = DummyConfigurationFile4(self.sections, config_file)
354 common.CONF_FILE = None
356 def test_get_credentials_for_success(self):
358 'ip_controller': '@string "value"',
359 'project': '@string "value"',
360 'auth_uri': '@string "value"',
361 'user': '@string "value"',
362 'heat_url': '@string "value"',
363 'password': '@string "value"'
365 output = common.get_credentials()
366 self.assertEqual(expected, output)
368 def test_get_heat_template_params_for_success(self):
370 'param_1': '@string "value"',
371 'param_2': '@string "value"',
372 'param_3': '@string "value"',
373 'param_4': '@string "value"'
375 output = common.get_heat_template_params()
376 self.assertEqual(expected, output)
378 def test_get_testcase_params_for_success(self):
379 expected = {'test_case_param': '@string "value"'}
380 output = common.get_testcase_params()
381 self.assertEqual(expected, output)
383 def test_get_file_first_line_for_success(self):
384 file = 'tests/data/common/conf.cfg'
385 expected = '[General]\n'
386 output = common.get_file_first_line(file)
387 self.assertEqual(expected, output)
389 def test_replace_in_file_for_success(self):
390 filename = 'tests/data/common/file_replacement.txt'
391 text_to_search = 'replacement of'
392 text_to_replace = '***'
393 common.replace_in_file(filename, text_to_search, text_to_replace)
394 after = open(filename, 'r').readline()
395 self.assertEqual(after, 'Test for the *** strings into a file\n')
396 text_to_search = '***'
397 text_to_replace = 'replacement of'
398 common.replace_in_file(filename, text_to_search, text_to_replace)
400 @mock.patch('os.system')
401 @mock.patch('experimental_framework.common.LOG')
402 def test_run_command_for_success(self, mock_log, mock_os_system):
403 command = 'command to be run'
404 common.run_command(command)
405 mock_os_system.assert_called_once_with(command)
407 @mock.patch('experimental_framework.common.run_command')
408 def test_push_data_influxdb_for_success(self, mock_run_cmd):
409 data = 'string that describes the data'
410 expected = "curl -i -XPOST 'http://None:None/write?db=None' " \
411 "--data-binary string that describes the data"
412 common.push_data_influxdb(data)
413 mock_run_cmd.assert_called_once_with(expected)
415 def test_get_base_dir_for_success(self):
416 base_dir = common.BASE_DIR
417 common.BASE_DIR = 'base_dir'
418 expected = 'base_dir'
419 output = common.get_base_dir()
420 self.assertEqual(expected, output)
421 common.BASE_DIR = base_dir
423 def test_get_template_dir_for_success(self):
424 template_dir = common.TEMPLATE_DIR
425 common.TEMPLATE_DIR = 'base_dir'
426 expected = 'base_dir'
427 output = common.get_template_dir()
428 self.assertEqual(expected, output)
429 common.TEMPLATE_DIR = template_dir
431 def test_get_dpdk_pktgen_vars_test(self):
433 common.PKTGEN = 'dpdk_pktgen'
434 common.PKTGEN_DIR = 'var'
435 common.PKTGEN_PROGRAM = 'var'
436 common.PKTGEN_COREMASK = 'var'
437 common.PKTGEN_MEMCHANNEL = 'var'
438 common.PKTGEN_BUS_SLOT_NIC_1 = 'var'
439 common.PKTGEN_BUS_SLOT_NIC_2 = 'var'
440 common.PKTGEN_NAME_NIC_1 = 'var'
441 common.PKTGEN_NAME_NIC_2 = 'var'
442 common.PKTGEN_DPDK_DIRECTORY = 'var'
444 'bus_slot_nic_1': 'var',
445 'bus_slot_nic_2': 'var',
449 'dpdk_directory': 'var',
450 'memory_channels': 'var',
451 'pktgen_directory': 'var',
452 'program_name': 'var'
454 output = common.get_dpdk_pktgen_vars()
455 self.assertEqual(expected, output)
458 common.PKTGEN = 'something_else'
459 common.PKTGEN_DIR = 'var'
460 common.PKTGEN_PROGRAM = 'var'
461 common.PKTGEN_COREMASK = 'var'
462 common.PKTGEN_MEMCHANNEL = 'var'
463 common.PKTGEN_BUS_SLOT_NIC_1 = 'var'
464 common.PKTGEN_BUS_SLOT_NIC_2 = 'var'
465 common.PKTGEN_DPDK_DIRECTORY = 'var'
467 output = common.get_dpdk_pktgen_vars()
468 self.assertEqual(expected, output)
470 @mock.patch('experimental_framework.common.LOG')
471 def test_get_deployment_configuration_variables_for_success(self,
475 'vnic1_type': ['value'],
477 'vnic2_type': ['value']
479 output = common.get_deployment_configuration_variables_from_conf_file()
480 self.assertEqual(expected, output)
482 def test_get_benchmarks_from_conf_file_for_success(self):
483 expected = ['BenchmarkClass1', 'BenchmarkClass2']
484 output = common.get_benchmarks_from_conf_file()
485 self.assertEqual(expected, output)
488 class TestinputValidation(unittest.TestCase):
496 def test_validate_string_for_success(self):
497 output = common.InputValidation.validate_string('string', '')
498 self.assertTrue(output)
500 def test_validate_string_for_failure(self):
503 common.InputValidation.validate_string,
507 def test_validate_int_for_success(self):
508 output = common.InputValidation.validate_integer(1111, '')
509 self.assertTrue(output)
511 def test_validate_int_for_failure(self):
514 common.InputValidation.validate_integer,
518 def test_validate_dict_for_success(self):
519 output = common.InputValidation.validate_dictionary(dict(), '')
520 self.assertTrue(output)
522 def test_validate_dict_for_failure(self):
525 common.InputValidation.validate_dictionary,
529 def test_validate_file_exist_for_success(self):
530 filename = 'tests/data/common/file_replacement.txt'
531 output = common.InputValidation.validate_file_exist(filename, '')
532 self.assertTrue(output)
534 def test_validate_file_exist_for_failure(self):
535 filename = 'tests/data/common/file_replacement'
538 common.InputValidation.validate_file_exist,
542 def test_validate_directory_exist_and_format_for_success(self):
543 directory = 'tests/data/common/'
544 output = common.InputValidation.\
545 validate_directory_exist_and_format(directory, '')
546 self.assertTrue(output)
548 def test_validate_directory_exist_and_format_for_failure(self):
549 directory = 'tests/data/com/'
552 common.InputValidation.validate_directory_exist_and_format,
556 @mock.patch('experimental_framework.common.CONF_FILE')
557 def test_validate_configuration_file_parameter_for_success(self,
559 mock_conf.get_variable_list.return_value = ['param']
563 output = common.InputValidation.\
564 validate_configuration_file_parameter(section, parameter, message)
565 self.assertTrue(output)
567 @mock.patch('experimental_framework.common.CONF_FILE')
568 def test_validate_configuration_file_parameter_for_failure(
569 self, mock_conf_file):
571 parameter = 'something_else'
573 mock_conf_file.get_variable_list.return_value(['parameter'])
576 common.InputValidation.
577 validate_configuration_file_parameter,
578 section, parameter, message
581 def test_validate_configuration_file_section_for_success(self):
584 output = common.InputValidation.\
585 validate_configuration_file_section(section, message)
586 self.assertTrue(output)
588 def test_validate_configuration_file_section_for_failure(self):
589 section = 'Something-Else'
593 common.InputValidation.validate_configuration_file_section,
597 def test_validate_boolean_for_success(self):
600 output = common.InputValidation.validate_boolean(boolean, message)
601 self.assertTrue(output)
604 output = common.InputValidation.validate_boolean(boolean, message)
605 self.assertTrue(output)
608 output = common.InputValidation.validate_boolean(boolean, message)
609 self.assertFalse(output)
611 def test_validate_boolean_for_failure(self):
616 common.InputValidation.validate_boolean,
620 def test_validate_os_credentials_for_failure(self):
623 self.assertRaises(ValueError,
624 common.InputValidation.validate_os_credentials,
629 credentials['ip_controller'] = ''
630 credentials['heat_url'] = ''
631 credentials['user'] = ''
632 credentials['password'] = ''
633 credentials['auth_uri'] = ''
634 # credentials['project'] = ''
635 self.assertRaises(ValueError,
636 common.InputValidation.validate_os_credentials,
639 def test_validate_os_credentials_for_success(self):
641 credentials['ip_controller'] = ''
642 credentials['heat_url'] = ''
643 credentials['user'] = ''
644 credentials['password'] = ''
645 credentials['auth_uri'] = ''
646 credentials['project'] = ''
648 common.InputValidation.validate_os_credentials(credentials))