blob: 843ca6e9fd83182beca45ff0f416d2ee3d5a410d [file] [log] [blame]
James E. Blair6f27fca2017-11-21 17:05:43 -08001# Copyright (C) 2017 Red Hat, Inc.
2#
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
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
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
12# implied.
13#
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import os
18import shutil
19import tempfile
20import unittest
21
22from devstack_local_conf import LocalConf
23from collections import OrderedDict
24
25class TestDevstackLocalConf(unittest.TestCase):
26 def setUp(self):
27 self.tmpdir = tempfile.mkdtemp()
28
29 def tearDown(self):
30 shutil.rmtree(self.tmpdir)
31
32 def test_plugins(self):
33 "Test that plugins without dependencies work"
34 localrc = {'test_localrc': '1'}
35 local_conf = {'install':
36 {'nova.conf':
37 {'main':
38 {'test_conf': '2'}}}}
39 services = {'cinder': True}
40 # We use ordereddict here to make sure the plugins are in the
41 # *wrong* order for testing.
42 plugins = OrderedDict([
43 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
44 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
45 ('baz', 'git://git.openstack.org/openstack/baz-plugin'),
46 ])
47 p = dict(localrc=localrc,
48 local_conf=local_conf,
49 base_services=[],
50 services=services,
51 plugins=plugins,
52 base_dir='./test',
53 path=os.path.join(self.tmpdir, 'test.local.conf'))
54 lc = LocalConf(p.get('localrc'),
55 p.get('local_conf'),
56 p.get('base_services'),
57 p.get('services'),
58 p.get('plugins'),
59 p.get('base_dir'))
60 lc.write(p['path'])
61
62 plugins = []
63 with open(p['path']) as f:
64 for line in f:
65 if line.startswith('enable_plugin'):
66 plugins.append(line.split()[1])
67 self.assertEqual(['bar', 'baz', 'foo'], plugins)
68
69 def test_plugin_deps(self):
70 "Test that plugins with dependencies work"
71 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
72 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
73 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
74 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
75 with open(os.path.join(
76 self.tmpdir,
77 'foo-plugin', 'devstack', 'settings'), 'w') as f:
78 f.write('define_plugin foo\n')
79 with open(os.path.join(
80 self.tmpdir,
81 'bar-plugin', 'devstack', 'settings'), 'w') as f:
82 f.write('define_plugin bar\n')
83 f.write('plugin_requires bar foo\n')
84
85 localrc = {'test_localrc': '1'}
86 local_conf = {'install':
87 {'nova.conf':
88 {'main':
89 {'test_conf': '2'}}}}
90 services = {'cinder': True}
91 # We use ordereddict here to make sure the plugins are in the
92 # *wrong* order for testing.
93 plugins = OrderedDict([
94 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
95 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
96 ])
97 p = dict(localrc=localrc,
98 local_conf=local_conf,
99 base_services=[],
100 services=services,
101 plugins=plugins,
102 base_dir=self.tmpdir,
103 path=os.path.join(self.tmpdir, 'test.local.conf'))
104 lc = LocalConf(p.get('localrc'),
105 p.get('local_conf'),
106 p.get('base_services'),
107 p.get('services'),
108 p.get('plugins'),
109 p.get('base_dir'))
110 lc.write(p['path'])
111
112 plugins = []
113 with open(p['path']) as f:
114 for line in f:
115 if line.startswith('enable_plugin'):
116 plugins.append(line.split()[1])
117 self.assertEqual(['foo', 'bar'], plugins)
118
119 def test_plugin_circular_deps(self):
120 "Test that plugins with circular dependencies fail"
121 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
122 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
123 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
124 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
125 with open(os.path.join(
126 self.tmpdir,
127 'foo-plugin', 'devstack', 'settings'), 'w') as f:
128 f.write('define_plugin foo\n')
129 f.write('plugin_requires foo bar\n')
130 with open(os.path.join(
131 self.tmpdir,
132 'bar-plugin', 'devstack', 'settings'), 'w') as f:
133 f.write('define_plugin bar\n')
134 f.write('plugin_requires bar foo\n')
135
136 localrc = {'test_localrc': '1'}
137 local_conf = {'install':
138 {'nova.conf':
139 {'main':
140 {'test_conf': '2'}}}}
141 services = {'cinder': True}
142 # We use ordereddict here to make sure the plugins are in the
143 # *wrong* order for testing.
144 plugins = OrderedDict([
145 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
146 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
147 ])
148 p = dict(localrc=localrc,
149 local_conf=local_conf,
150 base_services=[],
151 services=services,
152 plugins=plugins,
153 base_dir=self.tmpdir,
154 path=os.path.join(self.tmpdir, 'test.local.conf'))
155 with self.assertRaises(Exception):
156 lc = LocalConf(p.get('localrc'),
157 p.get('local_conf'),
158 p.get('base_services'),
159 p.get('services'),
160 p.get('plugins'),
161 p.get('base_dir'))
162 lc.write(p['path'])
163
164
165if __name__ == '__main__':
166 unittest.main()