blob: 7ccb68f08d69131f24f928ce0ce4e4fd9a2fe7ac [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'),
James E. Blaire1edde32018-03-02 15:05:14 +000059 p.get('base_dir'),
60 p.get('projects'))
James E. Blair6f27fca2017-11-21 17:05:43 -080061 lc.write(p['path'])
62
63 plugins = []
64 with open(p['path']) as f:
65 for line in f:
66 if line.startswith('enable_plugin'):
67 plugins.append(line.split()[1])
68 self.assertEqual(['bar', 'baz', 'foo'], plugins)
69
James E. Blaire1edde32018-03-02 15:05:14 +000070
James E. Blair6f27fca2017-11-21 17:05:43 -080071 def test_plugin_deps(self):
72 "Test that plugins with dependencies work"
73 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
74 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
75 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
76 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
77 with open(os.path.join(
78 self.tmpdir,
79 'foo-plugin', 'devstack', 'settings'), 'w') as f:
80 f.write('define_plugin foo\n')
81 with open(os.path.join(
82 self.tmpdir,
83 'bar-plugin', 'devstack', 'settings'), 'w') as f:
84 f.write('define_plugin bar\n')
85 f.write('plugin_requires bar foo\n')
86
87 localrc = {'test_localrc': '1'}
88 local_conf = {'install':
89 {'nova.conf':
90 {'main':
91 {'test_conf': '2'}}}}
92 services = {'cinder': True}
93 # We use ordereddict here to make sure the plugins are in the
94 # *wrong* order for testing.
95 plugins = OrderedDict([
96 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
97 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
98 ])
99 p = dict(localrc=localrc,
100 local_conf=local_conf,
101 base_services=[],
102 services=services,
103 plugins=plugins,
104 base_dir=self.tmpdir,
105 path=os.path.join(self.tmpdir, 'test.local.conf'))
James E. Blaire1edde32018-03-02 15:05:14 +0000106
107 def test_libs_from_git(self):
108 "Test that LIBS_FROM_GIT is auto-generated"
109 projects = {
110 'git.openstack.org/openstack/nova': {
111 'required': True,
112 'short_name': 'nova',
113 },
114 'git.openstack.org/openstack/oslo.messaging': {
115 'required': True,
116 'short_name': 'oslo.messaging',
117 },
118 'git.openstack.org/openstack/devstack-plugin': {
119 'required': False,
120 'short_name': 'devstack-plugin',
121 },
122 }
123 p = dict(base_services=[],
124 base_dir='./test',
125 path=os.path.join(self.tmpdir, 'test.local.conf'),
126 projects=projects)
James E. Blair6f27fca2017-11-21 17:05:43 -0800127 lc = LocalConf(p.get('localrc'),
128 p.get('local_conf'),
129 p.get('base_services'),
130 p.get('services'),
131 p.get('plugins'),
James E. Blaire1edde32018-03-02 15:05:14 +0000132 p.get('base_dir'),
133 p.get('projects'))
James E. Blair6f27fca2017-11-21 17:05:43 -0800134 lc.write(p['path'])
135
James E. Blaire1edde32018-03-02 15:05:14 +0000136 lfg = None
James E. Blair6f27fca2017-11-21 17:05:43 -0800137 with open(p['path']) as f:
138 for line in f:
James E. Blaire1edde32018-03-02 15:05:14 +0000139 if line.startswith('LIBS_FROM_GIT'):
140 lfg = line.strip().split('=')[1]
141 self.assertEqual('nova,oslo.messaging', lfg)
142
143 def test_overridelibs_from_git(self):
144 "Test that LIBS_FROM_GIT can be overridden"
145 localrc = {'LIBS_FROM_GIT': 'oslo.db'}
146 projects = {
147 'git.openstack.org/openstack/nova': {
148 'required': True,
149 'short_name': 'nova',
150 },
151 'git.openstack.org/openstack/oslo.messaging': {
152 'required': True,
153 'short_name': 'oslo.messaging',
154 },
155 'git.openstack.org/openstack/devstack-plugin': {
156 'required': False,
157 'short_name': 'devstack-plugin',
158 },
159 }
160 p = dict(localrc=localrc,
161 base_services=[],
162 base_dir='./test',
163 path=os.path.join(self.tmpdir, 'test.local.conf'),
164 projects=projects)
165 lc = LocalConf(p.get('localrc'),
166 p.get('local_conf'),
167 p.get('base_services'),
168 p.get('services'),
169 p.get('plugins'),
170 p.get('base_dir'),
171 p.get('projects'))
172 lc.write(p['path'])
173
174 lfg = None
175 with open(p['path']) as f:
176 for line in f:
177 if line.startswith('LIBS_FROM_GIT'):
178 lfg = line.strip().split('=')[1]
179 self.assertEqual('oslo.db', lfg)
James E. Blair6f27fca2017-11-21 17:05:43 -0800180
181 def test_plugin_circular_deps(self):
182 "Test that plugins with circular dependencies fail"
183 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
184 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
185 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
186 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
187 with open(os.path.join(
188 self.tmpdir,
189 'foo-plugin', 'devstack', 'settings'), 'w') as f:
190 f.write('define_plugin foo\n')
191 f.write('plugin_requires foo bar\n')
192 with open(os.path.join(
193 self.tmpdir,
194 'bar-plugin', 'devstack', 'settings'), 'w') as f:
195 f.write('define_plugin bar\n')
196 f.write('plugin_requires bar foo\n')
197
198 localrc = {'test_localrc': '1'}
199 local_conf = {'install':
200 {'nova.conf':
201 {'main':
202 {'test_conf': '2'}}}}
203 services = {'cinder': True}
204 # We use ordereddict here to make sure the plugins are in the
205 # *wrong* order for testing.
206 plugins = OrderedDict([
207 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
208 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
209 ])
210 p = dict(localrc=localrc,
211 local_conf=local_conf,
212 base_services=[],
213 services=services,
214 plugins=plugins,
215 base_dir=self.tmpdir,
216 path=os.path.join(self.tmpdir, 'test.local.conf'))
217 with self.assertRaises(Exception):
218 lc = LocalConf(p.get('localrc'),
219 p.get('local_conf'),
220 p.get('base_services'),
221 p.get('services'),
222 p.get('plugins'),
223 p.get('base_dir'))
224 lc.write(p['path'])
225
226
227if __name__ == '__main__':
228 unittest.main()