a8067252261a2cd62751442993d0d159d70d7c73
[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):
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     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)
138
139
140 class TestCommonInit2(unittest.TestCase):
141
142     def setUp(self):
143         common.CONF_FILE = DummyConfigurationFile2('')
144         self.dir = '{}/{}'.format(os.getcwd(), 'experimental_framework/')
145
146     def tearDown(self):
147         reset_common()
148         common.CONF_FILE = None
149
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)
161
162     def test_init_log_2_for_success(self):
163         common.init_log()
164         self.assertIsInstance(common.LOG, logging.RootLogger)
165
166     def test_init_pktgen_for_success(self):
167         common.init_pktgen()
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)
178
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)
182
183
184 class TestConfFileInitialization(unittest.TestCase):
185
186     def setUp(self):
187         pass
188
189     def tearDown(self):
190         reset_common()
191
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)
199
200         common.CONF_FILE = None
201         common.init_conf_file(True)
202         self.assertIsInstance(common.CONF_FILE,
203                               DummyConfigurationFile)
204
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'
208         common.init_log()
209         self.assertIsInstance(common.LOG, logging.RootLogger)
210
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')
218
219
220 class DummyConfigurationFile3(common.ConfigurationFile):
221     counter = 0
222
223     def __init__(self, sections, config_file='conf.cfg'):
224         common.ConfigurationFile.__init__(self, sections, config_file)
225
226     @staticmethod
227     def _config_section_map(section, config_file, get_counter=None):
228         if get_counter:
229             return DummyConfigurationFile3.counter
230         else:
231             DummyConfigurationFile3.counter += 1
232             return dict()
233
234
235 class TestConfigFileClass(unittest.TestCase):
236
237     def setUp(self):
238         self.sections = [
239             'General',
240             'OpenStack',
241             'Experiment-VNF',
242             'PacketGen',
243             'Deployment-parameters',
244             'Testcase-parameters'
245         ]
246         c_file = '/tests/data/common/conf.cfg'
247         common.BASE_DIR = os.getcwd()
248         self.conf_file = common.ConfigurationFile(self.sections, c_file)
249
250     def tearDown(self):
251         reset_common()
252         common.BASE_DIR = None
253
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')
262         self.assertEqual(
263             DummyConfigurationFile3._config_section_map('', '', True),
264             6)
265         for section in sections:
266             self.assertEqual(getattr(c, section), dict())
267
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)
274
275         expected = {
276             'benchmarks': 'b_marks',
277             'iterations': '1',
278             'template_base_name': 't_name'
279         }
280         output = common.\
281             ConfigurationFile._config_section_map(general_section, config)
282         self.assertEqual(expected, output)
283
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'
289         expected = 't_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)
293
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'
299         self.assertRaises(
300             ValueError,
301             self.conf_file.get_variable,
302             section, variable_name
303         )
304
305     def test_get_variable_list_for_success(self):
306         section = self.sections[0]
307         expected = {
308             'benchmarks': 'b_marks',
309             'iterations': '1',
310             'template_base_name': 't_name'
311         }
312         output = self.conf_file.get_variable_list(section)
313         self.assertEqual(expected, output)
314
315     def test_get_variable_list_for_failure(self):
316         section = 'something_else'
317         self.assertRaises(
318             ValueError,
319             self.conf_file.get_variable_list,
320             section)
321
322
323 class DummyConfigurationFile4(common.ConfigurationFile):
324
325     def get_variable(self, section, variable_name):
326         if variable_name == 'vnic2_type':
327             return '"value"'
328         elif variable_name == cf.CFSG_BENCHMARKS:
329             return "BenchmarkClass1, BenchmarkClass2"
330         return '@string "value"'
331
332     # def get_variable_list(self, section):
333     #     return list()
334
335
336 class TestCommonMethods(unittest.TestCase):
337
338     def setUp(self):
339         self.sections = [
340             'General',
341             'OpenStack',
342             'Experiment-VNF',
343             'PacketGen',
344             'Deployment-parameters',
345             'Testcase-parameters'
346         ]
347         config_file = '/tests/data/common/conf.cfg'
348         common.BASE_DIR = os.getcwd()
349         common.CONF_FILE = DummyConfigurationFile4(self.sections, config_file)
350
351     def tearDown(self):
352         reset_common()
353         common.CONF_FILE = None
354
355     def test_get_credentials_for_success(self):
356         expected = {
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"'
363         }
364         output = common.get_credentials()
365         self.assertEqual(expected, output)
366
367     def test_get_heat_template_params_for_success(self):
368         expected = {
369             'param_1': '@string "value"',
370             'param_2': '@string "value"',
371             'param_3': '@string "value"',
372             'param_4': '@string "value"'
373         }
374         output = common.get_heat_template_params()
375         self.assertEqual(expected, output)
376
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)
381
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)
387
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)
398
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)
405
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)
413
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
421
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
429
430     def test_get_dpdk_pktgen_vars_test(self):
431         # Test 1
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'
440         expected = {
441             'bus_slot_nic_1': 'var',
442             'bus_slot_nic_2': 'var',
443             'coremask': 'var',
444             'dpdk_directory': 'var',
445             'memory_channels': 'var',
446             'pktgen_directory': 'var',
447             'program_name': 'var'
448         }
449         output = common.get_dpdk_pktgen_vars()
450         self.assertEqual(expected, output)
451
452         # Test 2
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'
461         expected = {}
462         output = common.get_dpdk_pktgen_vars()
463         self.assertEqual(expected, output)
464
465     @mock.patch('experimental_framework.common.LOG')
466     def test_get_deployment_configuration_variables_for_success(self,
467                                                                 mock_log):
468         expected = {
469             'vcpu': ['value'],
470             'vnic1_type': ['value'],
471             'ram': ['value'],
472             'vnic2_type': ['value']
473         }
474         output = common.get_deployment_configuration_variables_from_conf_file()
475         self.assertEqual(expected, output)
476
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)
481
482
483 class TestinputValidation(unittest.TestCase):
484
485     def setUp(self):
486         pass
487
488     def tearDown(self):
489         reset_common()
490
491     def test_validate_string_for_success(self):
492         output = common.InputValidation.validate_string('string', '')
493         self.assertTrue(output)
494
495     def test_validate_string_for_failure(self):
496         self.assertRaises(
497             ValueError,
498             common.InputValidation.validate_string,
499             list(), ''
500         )
501
502     def test_validate_int_for_success(self):
503         output = common.InputValidation.validate_integer(1111, '')
504         self.assertTrue(output)
505
506     def test_validate_int_for_failure(self):
507         self.assertRaises(
508             ValueError,
509             common.InputValidation.validate_integer,
510             list(), ''
511         )
512
513     def test_validate_dict_for_success(self):
514         output = common.InputValidation.validate_dictionary(dict(), '')
515         self.assertTrue(output)
516
517     def test_validate_dict_for_failure(self):
518         self.assertRaises(
519             ValueError,
520             common.InputValidation.validate_dictionary,
521             list(), ''
522         )
523
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)
528
529     def test_validate_file_exist_for_failure(self):
530         filename = 'tests/data/common/file_replacement'
531         self.assertRaises(
532             ValueError,
533             common.InputValidation.validate_file_exist,
534             filename, ''
535         )
536
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)
542
543     def test_validate_directory_exist_and_format_for_failure(self):
544         directory = 'tests/data/com/'
545         self.assertRaises(
546             ValueError,
547             common.InputValidation.validate_directory_exist_and_format,
548             directory, ''
549         )
550
551     @mock.patch('experimental_framework.common.CONF_FILE')
552     def test_validate_configuration_file_parameter_for_success(self,
553                                                                mock_conf):
554         mock_conf.get_variable_list.return_value = ['param']
555         section = ''
556         parameter = 'param'
557         message = ''
558         output = common.InputValidation.\
559             validate_configuration_file_parameter(section, parameter, message)
560         self.assertTrue(output)
561
562     @mock.patch('experimental_framework.common.CONF_FILE')
563     def test_validate_configuration_file_parameter_for_failure(
564             self, mock_conf_file):
565         section = ''
566         parameter = 'something_else'
567         message = ''
568         mock_conf_file.get_variable_list.return_value(['parameter'])
569         self.assertRaises(
570             ValueError,
571             common.InputValidation.
572             validate_configuration_file_parameter,
573             section, parameter, message
574         )
575
576     def test_validate_configuration_file_section_for_success(self):
577         section = 'General'
578         message = ''
579         output = common.InputValidation.\
580             validate_configuration_file_section(section, message)
581         self.assertTrue(output)
582
583     def test_validate_configuration_file_section_for_failure(self):
584         section = 'Something-Else'
585         message = ''
586         self.assertRaises(
587             ValueError,
588             common.InputValidation.validate_configuration_file_section,
589             section, message
590         )
591
592     def test_validate_boolean_for_success(self):
593         message = ''
594         boolean = True
595         output = common.InputValidation.validate_boolean(boolean, message)
596         self.assertTrue(output)
597
598         boolean = 'True'
599         output = common.InputValidation.validate_boolean(boolean, message)
600         self.assertTrue(output)
601
602         boolean = 'False'
603         output = common.InputValidation.validate_boolean(boolean, message)
604         self.assertFalse(output)
605
606     def test_validate_boolean_for_failure(self):
607         message = ''
608         boolean = 'string'
609         self.assertRaises(
610             ValueError,
611             common.InputValidation.validate_boolean,
612             boolean, message
613         )
614
615     def test_validate_os_credentials_for_failure(self):
616         # Test 1
617         credentials = list()
618         self.assertRaises(ValueError,
619                           common.InputValidation.validate_os_credentials,
620                           credentials)
621
622         # Test 2
623         credentials = dict()
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,
632                           credentials)
633
634     def test_validate_os_credentials_for_success(self):
635         credentials = dict()
636         credentials['ip_controller'] = ''
637         credentials['heat_url'] = ''
638         credentials['user'] = ''
639         credentials['password'] = ''
640         credentials['auth_uri'] = ''
641         credentials['project'] = ''
642         self.assertTrue(
643             common.InputValidation.validate_os_credentials(credentials))