File: test_loader.py

package info (click to toggle)
tuned 2.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,280 kB
  • sloc: python: 14,045; sh: 836; makefile: 216; ansic: 178
file content (117 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import unittest
import tempfile
import shutil
import os

import tuned.profiles as profiles
from tuned.profiles.exceptions import InvalidProfileException

class LoaderTestCase(unittest.TestCase):
	@classmethod
	def setUpClass(cls):
		cls._test_dir = tempfile.mkdtemp()
		cls._profiles_dir = cls._test_dir + '/test_profiles'
		cls._dummy_profile_dir = cls._profiles_dir + '/dummy'
		cls._dummy_profile_dir2 = cls._profiles_dir + '/dummy2'
		cls._dummy_profile_dir3 = cls._profiles_dir + '/dummy3'
		cls._dummy_profile_dir4 = cls._profiles_dir + '/dummy4'
		try:
			os.mkdir(cls._profiles_dir)
			os.mkdir(cls._dummy_profile_dir)
			os.mkdir(cls._dummy_profile_dir2)
			os.mkdir(cls._dummy_profile_dir3)
			os.mkdir(cls._dummy_profile_dir4)
		except OSError:
			pass

		with open(cls._dummy_profile_dir + '/tuned.conf','w') as f:
			f.write('[main]\nsummary=dummy profile\n')
			f.write('[test_unit]\ntest_option=hello\n')
			f.write('random_option=random\n')

		with open(cls._dummy_profile_dir2 + '/tuned.conf','w') as f:
			f.write(\
			'[main]\nsummary=second dummy profile\n')
			f.write('[test_unit]\ntest_option=hello world\n')
			f.write('secondary_option=whatever\n')

		with open(cls._dummy_profile_dir3 + '/tuned.conf','w') as f:
			f.write('[main]\nsummary=another profile\ninclude=dummy\n')
			f.write('[test_unit]\ntest_option=bye bye\n')
			f.write('new_option=add this\n')

		with open(cls._dummy_profile_dir4 + '/tuned.conf','w') as f:
			f.write(\
				'[main]\nsummary=dummy profile for configuration read test\n')
			f.write('file_path=${i:PROFILE_DIR}/whatever\n')
			f.write('script=random_name.sh\n')
			f.write('[test_unit]\ntest_option=hello world\n')
			f.write('devices=/dev/${variable1},/dev/${variable2}\n')
			f.write('[variables]\nvariable1=net\nvariable2=cpu')

	def setUp(self):
		locator = profiles.Locator([self._profiles_dir])
		factory = profiles.Factory()
		merger = profiles.Merger()
		self._loader = profiles.Loader(locator,factory,merger,None,\
			profiles.variables.Variables())

	def test_safe_name(self):
		self.assertFalse(self._loader.safe_name('*'))
		self.assertFalse(self._loader.safe_name('$'))
		self.assertTrue(self._loader.safe_name('Allowed_ch4rs.-'))

	def test_load_without_include(self):
		merged_profile = self._loader.load(['dummy','dummy2'])

		self.assertEqual(merged_profile.name, 'dummy dummy2')
		self.assertEqual(merged_profile.options['summary'],\
			'second dummy profile')
		self.assertEqual(merged_profile.units['test_unit'].\
			options['test_option'],'hello world')
		self.assertEqual(merged_profile.units['test_unit'].\
			options['secondary_option'],'whatever')

		with self.assertRaises(InvalidProfileException):
			self._loader.load([])

		with self.assertRaises(InvalidProfileException):
			self._loader.load(['invalid'])

	def test_load_with_include(self):
		merged_profile = self._loader.load(['dummy3'])

		self.assertEqual(merged_profile.name,'dummy3')
		self.assertEqual(merged_profile.options['summary'],'another profile')
		self.assertEqual(merged_profile.units['test_unit'].\
			options['test_option'],'bye bye')
		self.assertEqual(merged_profile.units['test_unit'].\
			options['new_option'],'add this')
		self.assertEqual(merged_profile.units['test_unit'].\
			options['random_option'],'random')

	def test_expand_profile_dir(self):
		self.assertEqual(self._loader._expand_profile_dir(\
			'/hello/world','${i:PROFILE_DIR}/file'),'/hello/world/file')

	def test_load_config_data(self):
		config = self._loader._load_config_data(\
			self._dummy_profile_dir4 + '/tuned.conf')

		self.assertEqual(config['main']['script'][0],\
			self._dummy_profile_dir4 + '/random_name.sh')

		self.assertEqual(config['main']['file_path'],\
			self._dummy_profile_dir4 + '/whatever')

		self.assertEqual(config['test_unit']['test_option'],\
			'hello world')

	def test_variables(self):
		config = self._loader.load(['dummy4'])
		self.assertEqual(config.units['test_unit'].devices,\
			'/dev/net,/dev/cpu')

	@classmethod
	def tearDownClass(cls):
		shutil.rmtree(cls._test_dir)