1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from __future__ import absolute_import
20 import six.moves.configparser
21 import experimental_framework.common as common
22 import experimental_framework.constants.conf_file_sections as cf
23 from experimental_framework import APEX_LAKE_ROOT
25 __author__ = 'vmricco'
30 common.CONF_FILE = None
31 common.DEPLOYMENT_UNIT = None
32 common.ITERATIONS = None
33 common.BASE_DIR = None
34 common.RESULT_DIR = None
35 common.TEMPLATE_DIR = None
36 common.TEMPLATE_NAME = None
37 common.TEMPLATE_FILE_EXTENSION = None
39 common.PKTGEN_DIR = None
40 common.PKTGEN_DPDK_DIRECTORY = None
41 common.PKTGEN_PROGRAM = None
42 common.PKTGEN_COREMASK = None
43 common.PKTGEN_MEMCHANNEL = None
44 common.PKTGEN_BUS_SLOT_NIC_1 = None
45 common.PKTGEN_BUS_SLOT_NIC_2 = None
46 common.INFLUXDB_IP = None
47 common.INFLUXDB_PORT = None
48 common.INFLUXDB_DB_NAME = None
51 class DummyConfigurationFile(common.ConfigurationFile):
53 def __init__(self, sections, conf_file=''):
56 def get_variable(self, section, variable_name):
59 def get_variable_list(self, section):
60 return ['template_base_name']
63 class DummyConfigurationFile2(common.ConfigurationFile):
65 def __init__(self, sections):
66 self.pktgen_counter = 0
68 def get_variable(self, section, variable_name):
69 if variable_name == cf.CFSG_TEMPLATE_NAME:
71 if variable_name == cf.CFSG_ITERATIONS:
73 if variable_name == cf.CFSG_DEBUG:
75 if variable_name == cf.CFSP_PACKET_GENERATOR:
76 if self.pktgen_counter == 1:
77 return 'non_supported'
78 self.pktgen_counter += 1
80 if variable_name == cf.CFSP_DPDK_PKTGEN_DIRECTORY:
82 if variable_name == cf.CFSP_DPDK_PROGRAM_NAME:
84 if variable_name == cf.CFSP_DPDK_COREMASK:
86 if variable_name == cf.CFSP_DPDK_MEMORY_CHANNEL:
88 if variable_name == cf.CFSP_DPDK_BUS_SLOT_NIC_1:
89 return 'bus_slot_nic_1'
90 if variable_name == cf.CFSP_DPDK_BUS_SLOT_NIC_2:
91 return 'bus_slot_nic_2'
92 if variable_name == cf.CFSP_DPDK_DPDK_DIRECTORY:
95 def get_variable_list(self, section):
96 if section == cf.CFS_PKTGEN:
98 cf.CFSP_DPDK_NAME_IF_2,
99 cf.CFSP_DPDK_NAME_IF_1,
100 cf.CFSP_DPDK_BUS_SLOT_NIC_1,
101 cf.CFSP_DPDK_BUS_SLOT_NIC_2,
102 cf.CFSP_DPDK_COREMASK,
103 cf.CFSP_DPDK_DPDK_DIRECTORY,
104 cf.CFSP_DPDK_PKTGEN_DIRECTORY,
105 cf.CFSP_DPDK_MEMORY_CHANNEL,
106 cf.CFSP_DPDK_PROGRAM_NAME,
107 cf.CFSP_PACKET_GENERATOR
111 'template_base_name',
117 class TestCommonInit(unittest.TestCase):
120 common.CONF_FILE = DummyConfigurationFile('')
121 self.dir = os.path.join(APEX_LAKE_ROOT, 'experimental_framework/')
125 # common.CONF_FILE = None
127 @mock.patch('os.getcwd')
128 @mock.patch('experimental_framework.common.init_conf_file')
129 @mock.patch('experimental_framework.common.init_general_vars')
130 @mock.patch('experimental_framework.common.init_log')
131 @mock.patch('experimental_framework.common.init_pktgen')
132 @mock.patch('experimental_framework.common.CONF_FILE')
133 def test_init_for_success(self, mock_conf_file, init_pkgen, init_log,
134 init_general_vars, init_conf_file, mock_getcwd):
135 mock_getcwd.return_value = self.dir
137 if common.CONF_FILE.get_variable_list(cf.CFS_PKTGEN):
138 init_pkgen.assert_called_once()
139 init_conf_file.assert_called_once()
140 init_general_vars.assert_called_once()
141 init_log.assert_called_once()
142 expected = self.dir.split('experimental_framework/')[0]
143 self.assertEqual(common.BASE_DIR, expected)
145 @mock.patch('experimental_framework.common.InputValidation')
146 @mock.patch('os.path.exists')
147 @mock.patch('os.makedirs')
148 @mock.patch('experimental_framework.common.LOG')
149 def test_init_general_vars_for_success(self, mock_log, mock_makedirs,
150 mock_path_exists, mock_val_file):
151 common.BASE_DIR = APEX_LAKE_ROOT
152 mock_path_exists.return_value = False
153 mock_val_file.return_value = True
154 common.init_general_vars()
155 self.assertEqual(common.TEMPLATE_FILE_EXTENSION, '.yaml')
156 self.assertEqual(common.TEMPLATE_DIR, '/tmp/apexlake/heat_templates/')
157 self.assertEqual(common.TEMPLATE_NAME, 'vTC.yaml')
158 self.assertEqual(common.RESULT_DIR, '/tmp/apexlake/results/')
159 self.assertEqual(common.ITERATIONS, 1)
160 # mock_makedirs.assert_called_once_with('/tmp/apexlake/heat_templates/')
163 class TestCommonInit2(unittest.TestCase):
166 common.CONF_FILE = DummyConfigurationFile2('')
167 self.dir = os.path.join(APEX_LAKE_ROOT, 'experimental_framework')
171 common.CONF_FILE = None
173 @mock.patch('experimental_framework.common.InputValidation')
174 @mock.patch('os.path.exists')
175 @mock.patch('os.makedirs')
176 @mock.patch('experimental_framework.common.LOG')
177 def test_init_general_vars_2_for_success(self, mock_log, mock_makedirs,
178 mock_path_exists, mock_val_file):
179 common.BASE_DIR = APEX_LAKE_ROOT
180 common.init_general_vars()
181 self.assertEqual(common.TEMPLATE_FILE_EXTENSION, '.yaml')
182 self.assertEqual(common.TEMPLATE_DIR, '/tmp/apexlake/heat_templates/')
183 self.assertEqual(common.TEMPLATE_NAME, 'vTC.yaml')
184 self.assertEqual(common.RESULT_DIR, '/tmp/apexlake/results/')
185 self.assertEqual(common.ITERATIONS, 2)
187 def test_init_log_2_for_success(self):
189 self.assertIsInstance(common.LOG, logging.RootLogger)
191 def test_init_pktgen_for_success(self):
193 self.assertEqual(common.PKTGEN, 'dpdk_pktgen')
194 directory = self.dir.split('experimental_framework')[0]
195 self.assertEqual(common.PKTGEN_DIR, directory)
196 self.assertEqual(common.PKTGEN_PROGRAM, 'program')
197 self.assertEqual(common.PKTGEN_COREMASK, 'coremask')
198 self.assertEqual(common.PKTGEN_MEMCHANNEL, 'memchannel')
199 self.assertEqual(common.PKTGEN_BUS_SLOT_NIC_1, 'bus_slot_nic_1')
200 self.assertEqual(common.PKTGEN_BUS_SLOT_NIC_2, 'bus_slot_nic_2')
201 # we always add '/' to end of dirs for some reason
202 # probably because we aren't using os.path.join everywhere
203 expected_dir = APEX_LAKE_ROOT + '/'
204 self.assertEqual(common.PKTGEN_DPDK_DIRECTORY, expected_dir)
206 def test_init_pktgen_for_failure(self):
207 common.CONF_FILE.get_variable('', cf.CFSP_PACKET_GENERATOR)
208 self.assertRaises(ValueError, common.init_pktgen)
211 class TestConfFileInitialization(unittest.TestCase):
219 @mock.patch('experimental_framework.common.ConfigurationFile',
220 side_effect=DummyConfigurationFile)
221 def test_init_conf_file_for_success(self, conf_file):
222 common.CONF_FILE = None
223 common.init_conf_file(False)
224 self.assertIsInstance(common.CONF_FILE,
225 DummyConfigurationFile)
227 common.CONF_FILE = None
228 common.init_conf_file(True)
229 self.assertIsInstance(common.CONF_FILE,
230 DummyConfigurationFile)
232 @mock.patch('experimental_framework.common.CONF_FILE')
233 def test_init_log_for_success(self, mock_conf_file):
234 mock_conf_file.get_variable_list.return_value = 'value'
236 self.assertIsInstance(common.LOG, logging.RootLogger)
238 @mock.patch('experimental_framework.common.CONF_FILE')
239 def test_init_influxdb_for_success(self, mock_conf_file):
240 mock_conf_file.get_variable.return_value = 'value'
241 common.init_influxdb()
242 self.assertEqual(common.INFLUXDB_IP, 'value')
243 self.assertEqual(common.INFLUXDB_PORT, 'value')
244 self.assertEqual(common.INFLUXDB_DB_NAME, 'value')
247 class DummyConfigurationFile3(common.ConfigurationFile):
250 def __init__(self, sections, config_file='conf.cfg'):
251 common.ConfigurationFile.__init__(self, sections, config_file)
254 def _config_section_map(section, config_file, get_counter=None):
256 return DummyConfigurationFile3.counter
258 DummyConfigurationFile3.counter += 1
262 class TestConfigFileClass(unittest.TestCase):
270 'Deployment-parameters',
271 'Testcase-parameters'
273 c_file = os.path.join(APEX_LAKE_ROOT, 'tests/data/common/conf.cfg')
274 common.BASE_DIR = APEX_LAKE_ROOT
275 self.conf_file = common.ConfigurationFile(self.sections, c_file)
279 common.BASE_DIR = None
281 @mock.patch('experimental_framework.common.ConfigurationFile.'
282 '_config_section_map',
283 side_effect=DummyConfigurationFile3._config_section_map)
284 def test___init___for_success(self, mock_conf_map):
285 sections = ['General', 'OpenStack', 'Experiment-VNF', 'PacketGen',
286 'Deployment-parameters', 'Testcase-parameters']
287 c = DummyConfigurationFile3(
288 sections, config_file=os.path.join(APEX_LAKE_ROOT,
289 'tests/data/common/conf.cfg'))
291 DummyConfigurationFile3._config_section_map('', '', True),
293 for section in sections:
294 self.assertEqual(getattr(c, section), dict())
296 def test__config_section_map_for_success(self):
297 general_section = 'General'
298 # openstack_section = 'OpenStack'
299 config_file = os.path.join(APEX_LAKE_ROOT,
300 'tests/data/common/conf.cfg')
301 config = six.moves.configparser.ConfigParser()
302 config.read(config_file)
305 'benchmarks': 'b_marks',
307 'template_base_name': 't_name'
310 ConfigurationFile._config_section_map(general_section, config)
311 self.assertEqual(expected, output)
313 @mock.patch('experimental_framework.common.'
314 'ConfigurationFile.get_variable_list')
315 def test_get_variable_for_success(self, mock_get_var_list):
316 section = self.sections[0]
317 variable_name = 'template_base_name'
319 mock_get_var_list.return_value = [variable_name]
320 output = self.conf_file.get_variable(section, variable_name)
321 self.assertEqual(expected, output)
323 @mock.patch('experimental_framework.common.'
324 'ConfigurationFile.get_variable_list')
325 def test_get_variable_for_failure(self, mock_get_var_list):
326 section = self.sections[0]
327 variable_name = 'something_else'
330 self.conf_file.get_variable,
331 section, variable_name
334 def test_get_variable_list_for_success(self):
335 section = self.sections[0]
337 'benchmarks': 'b_marks',
339 'template_base_name': 't_name'
341 output = self.conf_file.get_variable_list(section)
342 self.assertEqual(expected, output)
344 def test_get_variable_list_for_failure(self):
345 section = 'something_else'
348 self.conf_file.get_variable_list,
352 class DummyConfigurationFile4(common.ConfigurationFile):
354 def get_variable(self, section, variable_name):
355 if variable_name == 'vnic2_type':
357 elif variable_name == cf.CFSG_BENCHMARKS:
358 return "BenchmarkClass1, BenchmarkClass2"
359 return '@string "value"'
361 # def get_variable_list(self, section):
365 class TestCommonMethods(unittest.TestCase):
373 'Deployment-parameters',
374 'Testcase-parameters'
376 config_file = os.path.join(APEX_LAKE_ROOT,
377 'tests/data/common/conf.cfg')
378 common.BASE_DIR = APEX_LAKE_ROOT
379 common.CONF_FILE = DummyConfigurationFile4(self.sections, config_file)
383 common.CONF_FILE = None
385 def test_get_credentials_for_success(self):
387 'ip_controller': '@string "value"',
388 'project': '@string "value"',
389 'auth_uri': '@string "value"',
390 'user': '@string "value"',
391 'heat_url': '@string "value"',
392 'password': '@string "value"'
394 output = common.get_credentials()
395 self.assertEqual(expected, output)
397 def test_get_heat_template_params_for_success(self):
399 'param_1': '@string "value"',
400 'param_2': '@string "value"',
401 'param_3': '@string "value"',
402 'param_4': '@string "value"'
404 output = common.get_heat_template_params()
405 self.assertEqual(expected, output)
407 def test_get_testcase_params_for_success(self):
408 expected = {'test_case_param': '@string "value"'}
409 output = common.get_testcase_params()
410 self.assertEqual(expected, output)
412 def test_get_file_first_line_for_success(self):
413 file = os.path.join(APEX_LAKE_ROOT, 'tests/data/common/conf.cfg')
414 expected = '[General]\n'
415 output = common.get_file_first_line(file)
416 self.assertEqual(expected, output)
418 def test_replace_in_file_for_success(self):
419 filename = os.path.join(APEX_LAKE_ROOT,
420 'tests/data/common/file_replacement.txt')
421 text_to_search = 'replacement of'
422 text_to_replace = '***'
423 common.replace_in_file(filename, text_to_search, text_to_replace)
424 after = open(filename, 'r').readline()
425 self.assertEqual(after, 'Test for the *** strings into a file\n')
426 text_to_search = '***'
427 text_to_replace = 'replacement of'
428 common.replace_in_file(filename, text_to_search, text_to_replace)
430 @mock.patch('os.system')
431 @mock.patch('experimental_framework.common.LOG')
432 def test_run_command_for_success(self, mock_log, mock_os_system):
433 command = 'command to be run'
434 common.run_command(command)
435 mock_os_system.assert_called_once_with(command)
437 @mock.patch('experimental_framework.common.run_command')
438 def test_push_data_influxdb_for_success(self, mock_run_cmd):
439 data = 'string that describes the data'
440 expected = "curl -i -XPOST 'http://None:None/write?db=None' " \
441 "--data-binary string that describes the data"
442 common.push_data_influxdb(data)
443 mock_run_cmd.assert_called_once_with(expected)
445 def test_get_base_dir_for_success(self):
446 base_dir = common.BASE_DIR
447 common.BASE_DIR = 'base_dir'
448 expected = 'base_dir'
449 output = common.get_base_dir()
450 self.assertEqual(expected, output)
451 common.BASE_DIR = base_dir
453 def test_get_template_dir_for_success(self):
454 template_dir = common.TEMPLATE_DIR
455 common.TEMPLATE_DIR = 'base_dir'
456 expected = 'base_dir'
457 output = common.get_template_dir()
458 self.assertEqual(expected, output)
459 common.TEMPLATE_DIR = template_dir
461 def test_get_dpdk_pktgen_vars_test(self):
463 common.PKTGEN = 'dpdk_pktgen'
464 common.PKTGEN_DIR = 'var'
465 common.PKTGEN_PROGRAM = 'var'
466 common.PKTGEN_COREMASK = 'var'
467 common.PKTGEN_MEMCHANNEL = 'var'
468 common.PKTGEN_BUS_SLOT_NIC_1 = 'var'
469 common.PKTGEN_BUS_SLOT_NIC_2 = 'var'
470 common.PKTGEN_NAME_NIC_1 = 'var'
471 common.PKTGEN_NAME_NIC_2 = 'var'
472 common.PKTGEN_DPDK_DIRECTORY = 'var'
474 'bus_slot_nic_1': 'var',
475 'bus_slot_nic_2': 'var',
479 'dpdk_directory': 'var',
480 'memory_channels': 'var',
481 'pktgen_directory': 'var',
482 'program_name': 'var'
484 output = common.get_dpdk_pktgen_vars()
485 self.assertEqual(expected, output)
488 common.PKTGEN = 'something_else'
489 common.PKTGEN_DIR = 'var'
490 common.PKTGEN_PROGRAM = 'var'
491 common.PKTGEN_COREMASK = 'var'
492 common.PKTGEN_MEMCHANNEL = 'var'
493 common.PKTGEN_BUS_SLOT_NIC_1 = 'var'
494 common.PKTGEN_BUS_SLOT_NIC_2 = 'var'
495 common.PKTGEN_DPDK_DIRECTORY = 'var'
497 output = common.get_dpdk_pktgen_vars()
498 self.assertEqual(expected, output)
500 @mock.patch('experimental_framework.common.LOG')
501 def test_get_deployment_configuration_variables_for_success(self,
505 'vnic1_type': ['value'],
507 'vnic2_type': ['value']
509 output = common.get_deployment_configuration_variables_from_conf_file()
510 self.assertEqual(expected, output)
512 def test_get_benchmarks_from_conf_file_for_success(self):
513 expected = ['BenchmarkClass1', 'BenchmarkClass2']
514 output = common.get_benchmarks_from_conf_file()
515 self.assertEqual(expected, output)
518 class TestinputValidation(unittest.TestCase):
526 def test_validate_string_for_success(self):
527 output = common.InputValidation.validate_string('string', '')
528 self.assertTrue(output)
530 def test_validate_string_for_failure(self):
533 common.InputValidation.validate_string,
537 def test_validate_int_for_success(self):
538 output = common.InputValidation.validate_integer(1111, '')
539 self.assertTrue(output)
541 def test_validate_int_for_failure(self):
544 common.InputValidation.validate_integer,
548 def test_validate_dict_for_success(self):
549 output = common.InputValidation.validate_dictionary(dict(), '')
550 self.assertTrue(output)
552 def test_validate_dict_for_failure(self):
555 common.InputValidation.validate_dictionary,
559 def test_validate_directory_exist_and_format_for_success(self):
560 directory = os.path.join(APEX_LAKE_ROOT, 'tests/data/common/')
561 output = common.InputValidation.\
562 validate_directory_exist_and_format(directory, '')
563 self.assertTrue(output)
565 def test_validate_directory_exist_and_format_for_failure(self):
566 directory = os.path.join(APEX_LAKE_ROOT, 'tests/data/com/')
569 common.InputValidation.validate_directory_exist_and_format,
573 @mock.patch('experimental_framework.common.CONF_FILE')
574 def test_validate_configuration_file_parameter_for_success(self,
576 mock_conf.get_variable_list.return_value = ['param']
580 output = common.InputValidation.\
581 validate_configuration_file_parameter(section, parameter, message)
582 self.assertTrue(output)
584 @mock.patch('experimental_framework.common.CONF_FILE')
585 def test_validate_configuration_file_parameter_for_failure(
586 self, mock_conf_file):
588 parameter = 'something_else'
590 mock_conf_file.get_variable_list.return_value(['parameter'])
593 common.InputValidation.
594 validate_configuration_file_parameter,
595 section, parameter, message
598 def test_validate_configuration_file_section_for_success(self):
601 output = common.InputValidation.\
602 validate_configuration_file_section(section, message)
603 self.assertTrue(output)
605 def test_validate_configuration_file_section_for_failure(self):
606 section = 'Something-Else'
610 common.InputValidation.validate_configuration_file_section,
614 def test_validate_boolean_for_success(self):
617 output = common.InputValidation.validate_boolean(boolean, message)
618 self.assertTrue(output)
621 output = common.InputValidation.validate_boolean(boolean, message)
622 self.assertTrue(output)
625 output = common.InputValidation.validate_boolean(boolean, message)
626 self.assertFalse(output)
628 def test_validate_boolean_for_failure(self):
633 common.InputValidation.validate_boolean,
637 def test_validate_os_credentials_for_failure(self):
640 self.assertRaises(ValueError,
641 common.InputValidation.validate_os_credentials,
646 credentials['ip_controller'] = ''
647 credentials['heat_url'] = ''
648 credentials['user'] = ''
649 credentials['password'] = ''
650 credentials['auth_uri'] = ''
651 # credentials['project'] = ''
652 self.assertRaises(ValueError,
653 common.InputValidation.validate_os_credentials,
656 def test_validate_os_credentials_for_success(self):
658 credentials['ip_controller'] = ''
659 credentials['heat_url'] = ''
660 credentials['user'] = ''
661 credentials['password'] = ''
662 credentials['auth_uri'] = ''
663 credentials['project'] = ''
665 common.InputValidation.validate_os_credentials(credentials))