Fix small things for integration of ApexLake with Yardstick
[yardstick.git] / yardstick / vTC / apexlake / tests / common_test.py
1 __author__ = 'vmricco'
2
3 import unittest
4 import mock
5 import os
6 import logging
7 import ConfigParser
8 import experimental_framework.common as common
9 import experimental_framework.constants.conf_file_sections as cf
10
11
12 def reset_common():
13     common.LOG = None
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
22     common.PKTGEN = 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
33
34
35 class DummyConfigurationFile(common.ConfigurationFile):
36     def __init__(self, sections, conf_file=''):
37         pass
38
39     def get_variable(self, section, variable_name):
40         return 'vTC.yaml'
41
42     def get_variable_list(self, section):
43         return ['template_base_name']
44
45
46 class DummyConfigurationFile2(common.ConfigurationFile):
47     def __init__(self, sections):
48         self.pktgen_counter = 0
49
50     def get_variable(self, section, variable_name):
51         if variable_name == cf.CFSG_TEMPLATE_NAME:
52             return 'vTC.yaml'
53         if variable_name == cf.CFSG_ITERATIONS:
54             return '2'
55         if variable_name == cf.CFSG_DEBUG:
56             return 'True'
57         if variable_name == cf.CFSP_PACKET_GENERATOR:
58             if self.pktgen_counter == 1:
59                 return 'non_supported'
60             self.pktgen_counter += 1
61             return 'dpdk_pktgen'
62         if variable_name == cf.CFSP_DPDK_PKTGEN_DIRECTORY:
63             return os.getcwd()
64         if variable_name == cf.CFSP_DPDK_PROGRAM_NAME:
65             return 'program'
66         if variable_name == cf.CFSP_DPDK_COREMASK:
67             return 'coremask'
68         if variable_name == cf.CFSP_DPDK_MEMORY_CHANNEL:
69             return 'memchannel'
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:
75             return os.getcwd()
76
77     def get_variable_list(self, section):
78         if section == cf.CFS_PKTGEN:
79             return [
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
90             ]
91         else:
92             return [
93                 'template_base_name',
94                 'iterations',
95                 cf.CFSG_DEBUG
96             ]
97
98
99 class TestCommonInit(unittest.TestCase):
100
101     def setUp(self):
102         common.CONF_FILE = DummyConfigurationFile('')
103         self.dir = '{}/{}'.format(os.getcwd(),
104                                   'experimental_framework/')
105
106     def tearDown(self):
107         reset_common()
108         # common.CONF_FILE = None
109
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
119         common.init(True)
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)
126
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,
131                                            mock_path_exists):
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/')
141
142
143 class TestCommonInit2(unittest.TestCase):
144
145     def setUp(self):
146         common.CONF_FILE = DummyConfigurationFile2('')
147         self.dir = '{}/{}'.format(os.getcwd(), 'experimental_framework/')
148
149     def tearDown(self):
150         reset_common()
151         common.CONF_FILE = None
152
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)
162
163     def test_init_log_2_for_success(self):
164         common.init_log()
165         self.assertIsInstance(common.LOG, logging.RootLogger)
166
167     def test_init_pktgen_for_success(self):
168         common.init_pktgen()
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)
179
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)
183
184
185 class TestConfFileInitialization(unittest.TestCase):
186
187     def setUp(self):
188         pass
189
190     def tearDown(self):
191         reset_common()
192
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)
200
201         common.CONF_FILE = None
202         common.init_conf_file(True)
203         self.assertIsInstance(common.CONF_FILE,
204                               DummyConfigurationFile)
205
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'
209         common.init_log()
210         self.assertIsInstance(common.LOG, logging.RootLogger)
211
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')
219
220
221 class DummyConfigurationFile3(common.ConfigurationFile):
222     counter = 0
223
224     def __init__(self, sections, config_file='conf.cfg'):
225         common.ConfigurationFile.__init__(self, sections, config_file)
226
227     @staticmethod
228     def _config_section_map(section, config_file, get_counter=None):
229         if get_counter:
230             return DummyConfigurationFile3.counter
231         else:
232             DummyConfigurationFile3.counter += 1
233             return dict()
234
235
236 class TestConfigFileClass(unittest.TestCase):
237
238     def setUp(self):
239         self.sections = [
240             'General',
241             'OpenStack',
242             'Experiment-VNF',
243             'PacketGen',
244             'Deployment-parameters',
245             'Testcase-parameters'
246         ]
247         c_file = './tests/data/common/conf.cfg'
248         common.BASE_DIR = os.getcwd()
249         self.conf_file = common.ConfigurationFile(self.sections, c_file)
250
251     def tearDown(self):
252         reset_common()
253         common.BASE_DIR = None
254
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')
263         self.assertEqual(
264             DummyConfigurationFile3._config_section_map('', '', True),
265             6)
266         for section in sections:
267             self.assertEqual(getattr(c, section), dict())
268
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)
275
276         expected = {
277             'benchmarks': 'b_marks',
278             'iterations': '1',
279             'template_base_name': 't_name'
280         }
281         output = common.\
282             ConfigurationFile._config_section_map(general_section, config)
283         self.assertEqual(expected, output)
284
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'
290         expected = 't_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)
294
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'
300         self.assertRaises(
301             ValueError,
302             self.conf_file.get_variable,
303             section, variable_name
304         )
305
306     def test_get_variable_list_for_success(self):
307         section = self.sections[0]
308         expected = {
309             'benchmarks': 'b_marks',
310             'iterations': '1',
311             'template_base_name': 't_name'
312         }
313         output = self.conf_file.get_variable_list(section)
314         self.assertEqual(expected, output)
315
316     def test_get_variable_list_for_failure(self):
317         section = 'something_else'
318         self.assertRaises(
319             ValueError,
320             self.conf_file.get_variable_list,
321             section)
322
323
324 class DummyConfigurationFile4(common.ConfigurationFile):
325
326     def get_variable(self, section, variable_name):
327         if variable_name == 'vnic2_type':
328             return '"value"'
329         elif variable_name == cf.CFSG_BENCHMARKS:
330             return "BenchmarkClass1, BenchmarkClass2"
331         return '@string "value"'
332
333     # def get_variable_list(self, section):
334     #     return list()
335
336
337 class TestCommonMethods(unittest.TestCase):
338
339     def setUp(self):
340         self.sections = [
341             'General',
342             'OpenStack',
343             'Experiment-VNF',
344             'PacketGen',
345             'Deployment-parameters',
346             'Testcase-parameters'
347         ]
348         config_file = './tests/data/common/conf.cfg'
349         common.BASE_DIR = os.getcwd()
350         common.CONF_FILE = DummyConfigurationFile4(self.sections, config_file)
351
352     def tearDown(self):
353         reset_common()
354         common.CONF_FILE = None
355
356     def test_get_credentials_for_success(self):
357         expected = {
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"'
364         }
365         output = common.get_credentials()
366         self.assertEqual(expected, output)
367
368     def test_get_heat_template_params_for_success(self):
369         expected = {
370             'param_1': '@string "value"',
371             'param_2': '@string "value"',
372             'param_3': '@string "value"',
373             'param_4': '@string "value"'
374         }
375         output = common.get_heat_template_params()
376         self.assertEqual(expected, output)
377
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)
382
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)
388
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)
399
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)
406
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)
414
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
422
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
430
431     def test_get_dpdk_pktgen_vars_test(self):
432         # Test 1
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'
443         expected = {
444             'bus_slot_nic_1': 'var',
445             'bus_slot_nic_2': 'var',
446             'name_if_1': 'var',
447             'name_if_2': 'var',
448             'coremask': 'var',
449             'dpdk_directory': 'var',
450             'memory_channels': 'var',
451             'pktgen_directory': 'var',
452             'program_name': 'var'
453         }
454         output = common.get_dpdk_pktgen_vars()
455         self.assertEqual(expected, output)
456
457         # Test 2
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'
466         expected = {}
467         output = common.get_dpdk_pktgen_vars()
468         self.assertEqual(expected, output)
469
470     @mock.patch('experimental_framework.common.LOG')
471     def test_get_deployment_configuration_variables_for_success(self,
472                                                                 mock_log):
473         expected = {
474             'vcpu': ['value'],
475             'vnic1_type': ['value'],
476             'ram': ['value'],
477             'vnic2_type': ['value']
478         }
479         output = common.get_deployment_configuration_variables_from_conf_file()
480         self.assertEqual(expected, output)
481
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)
486
487
488 class TestinputValidation(unittest.TestCase):
489
490     def setUp(self):
491         pass
492
493     def tearDown(self):
494         reset_common()
495
496     def test_validate_string_for_success(self):
497         output = common.InputValidation.validate_string('string', '')
498         self.assertTrue(output)
499
500     def test_validate_string_for_failure(self):
501         self.assertRaises(
502             ValueError,
503             common.InputValidation.validate_string,
504             list(), ''
505         )
506
507     def test_validate_int_for_success(self):
508         output = common.InputValidation.validate_integer(1111, '')
509         self.assertTrue(output)
510
511     def test_validate_int_for_failure(self):
512         self.assertRaises(
513             ValueError,
514             common.InputValidation.validate_integer,
515             list(), ''
516         )
517
518     def test_validate_dict_for_success(self):
519         output = common.InputValidation.validate_dictionary(dict(), '')
520         self.assertTrue(output)
521
522     def test_validate_dict_for_failure(self):
523         self.assertRaises(
524             ValueError,
525             common.InputValidation.validate_dictionary,
526             list(), ''
527         )
528
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)
533
534     def test_validate_file_exist_for_failure(self):
535         filename = 'tests/data/common/file_replacement'
536         self.assertRaises(
537             ValueError,
538             common.InputValidation.validate_file_exist,
539             filename, ''
540         )
541
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)
547
548     def test_validate_directory_exist_and_format_for_failure(self):
549         directory = 'tests/data/com/'
550         self.assertRaises(
551             ValueError,
552             common.InputValidation.validate_directory_exist_and_format,
553             directory, ''
554         )
555
556     @mock.patch('experimental_framework.common.CONF_FILE')
557     def test_validate_configuration_file_parameter_for_success(self,
558                                                                mock_conf):
559         mock_conf.get_variable_list.return_value = ['param']
560         section = ''
561         parameter = 'param'
562         message = ''
563         output = common.InputValidation.\
564             validate_configuration_file_parameter(section, parameter, message)
565         self.assertTrue(output)
566
567     @mock.patch('experimental_framework.common.CONF_FILE')
568     def test_validate_configuration_file_parameter_for_failure(
569             self, mock_conf_file):
570         section = ''
571         parameter = 'something_else'
572         message = ''
573         mock_conf_file.get_variable_list.return_value(['parameter'])
574         self.assertRaises(
575             ValueError,
576             common.InputValidation.
577             validate_configuration_file_parameter,
578             section, parameter, message
579         )
580
581     def test_validate_configuration_file_section_for_success(self):
582         section = 'General'
583         message = ''
584         output = common.InputValidation.\
585             validate_configuration_file_section(section, message)
586         self.assertTrue(output)
587
588     def test_validate_configuration_file_section_for_failure(self):
589         section = 'Something-Else'
590         message = ''
591         self.assertRaises(
592             ValueError,
593             common.InputValidation.validate_configuration_file_section,
594             section, message
595         )
596
597     def test_validate_boolean_for_success(self):
598         message = ''
599         boolean = True
600         output = common.InputValidation.validate_boolean(boolean, message)
601         self.assertTrue(output)
602
603         boolean = 'True'
604         output = common.InputValidation.validate_boolean(boolean, message)
605         self.assertTrue(output)
606
607         boolean = 'False'
608         output = common.InputValidation.validate_boolean(boolean, message)
609         self.assertFalse(output)
610
611     def test_validate_boolean_for_failure(self):
612         message = ''
613         boolean = 'string'
614         self.assertRaises(
615             ValueError,
616             common.InputValidation.validate_boolean,
617             boolean, message
618         )
619
620     def test_validate_os_credentials_for_failure(self):
621         # Test 1
622         credentials = list()
623         self.assertRaises(ValueError,
624                           common.InputValidation.validate_os_credentials,
625                           credentials)
626
627         # Test 2
628         credentials = dict()
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,
637                           credentials)
638
639     def test_validate_os_credentials_for_success(self):
640         credentials = dict()
641         credentials['ip_controller'] = ''
642         credentials['heat_url'] = ''
643         credentials['user'] = ''
644         credentials['password'] = ''
645         credentials['auth_uri'] = ''
646         credentials['project'] = ''
647         self.assertTrue(
648             common.InputValidation.validate_os_credentials(credentials))