blob: 88d404b8564e7046cb8d8ed225c8e4cf556b8529 [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'),
James E. Blair8e5f8c22018-06-15 10:10:35 -070060 p.get('projects'),
61 p.get('project'))
James E. Blair6f27fca2017-11-21 17:05:43 -080062 lc.write(p['path'])
63
64 plugins = []
65 with open(p['path']) as f:
66 for line in f:
67 if line.startswith('enable_plugin'):
68 plugins.append(line.split()[1])
69 self.assertEqual(['bar', 'baz', 'foo'], plugins)
70
James E. Blaire1edde32018-03-02 15:05:14 +000071
James E. Blair6f27fca2017-11-21 17:05:43 -080072 def test_plugin_deps(self):
73 "Test that plugins with dependencies work"
74 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
75 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
76 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
77 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
78 with open(os.path.join(
79 self.tmpdir,
80 'foo-plugin', 'devstack', 'settings'), 'w') as f:
Jens Harbott0b855002018-12-19 12:20:51 +000081 f.write('define_plugin foo-plugin\n')
James E. Blair6f27fca2017-11-21 17:05:43 -080082 with open(os.path.join(
83 self.tmpdir,
84 'bar-plugin', 'devstack', 'settings'), 'w') as f:
Jens Harbott0b855002018-12-19 12:20:51 +000085 f.write('define_plugin bar-plugin\n')
86 f.write('plugin_requires bar-plugin foo-plugin\n')
James E. Blair6f27fca2017-11-21 17:05:43 -080087
88 localrc = {'test_localrc': '1'}
89 local_conf = {'install':
90 {'nova.conf':
91 {'main':
92 {'test_conf': '2'}}}}
93 services = {'cinder': True}
94 # We use ordereddict here to make sure the plugins are in the
95 # *wrong* order for testing.
96 plugins = OrderedDict([
Jens Harbott0b855002018-12-19 12:20:51 +000097 ('bar-plugin', 'git://git.openstack.org/openstack/bar-plugin'),
98 ('foo-plugin', 'git://git.openstack.org/openstack/foo-plugin'),
James E. Blair6f27fca2017-11-21 17:05:43 -080099 ])
100 p = dict(localrc=localrc,
101 local_conf=local_conf,
102 base_services=[],
103 services=services,
104 plugins=plugins,
105 base_dir=self.tmpdir,
106 path=os.path.join(self.tmpdir, 'test.local.conf'))
Jens Harbott6d103a72018-12-19 11:53:16 +0000107 lc = LocalConf(p.get('localrc'),
108 p.get('local_conf'),
109 p.get('base_services'),
110 p.get('services'),
111 p.get('plugins'),
112 p.get('base_dir'),
113 p.get('projects'),
114 p.get('project'))
115 lc.write(p['path'])
116
117 plugins = []
118 with open(p['path']) as f:
119 for line in f:
120 if line.startswith('enable_plugin'):
121 plugins.append(line.split()[1])
Jens Harbott0b855002018-12-19 12:20:51 +0000122 self.assertEqual(['foo-plugin', 'bar-plugin'], plugins)
James E. Blaire1edde32018-03-02 15:05:14 +0000123
124 def test_libs_from_git(self):
125 "Test that LIBS_FROM_GIT is auto-generated"
126 projects = {
127 'git.openstack.org/openstack/nova': {
128 'required': True,
129 'short_name': 'nova',
130 },
131 'git.openstack.org/openstack/oslo.messaging': {
132 'required': True,
133 'short_name': 'oslo.messaging',
134 },
135 'git.openstack.org/openstack/devstack-plugin': {
136 'required': False,
137 'short_name': 'devstack-plugin',
138 },
139 }
James E. Blair8e5f8c22018-06-15 10:10:35 -0700140 project = {
141 'short_name': 'glance',
142 }
James E. Blaire1edde32018-03-02 15:05:14 +0000143 p = dict(base_services=[],
144 base_dir='./test',
145 path=os.path.join(self.tmpdir, 'test.local.conf'),
James E. Blair8e5f8c22018-06-15 10:10:35 -0700146 projects=projects,
147 project=project)
James E. Blair6f27fca2017-11-21 17:05:43 -0800148 lc = LocalConf(p.get('localrc'),
149 p.get('local_conf'),
150 p.get('base_services'),
151 p.get('services'),
152 p.get('plugins'),
James E. Blaire1edde32018-03-02 15:05:14 +0000153 p.get('base_dir'),
James E. Blair8e5f8c22018-06-15 10:10:35 -0700154 p.get('projects'),
155 p.get('project'))
James E. Blair6f27fca2017-11-21 17:05:43 -0800156 lc.write(p['path'])
157
James E. Blaire1edde32018-03-02 15:05:14 +0000158 lfg = None
James E. Blair6f27fca2017-11-21 17:05:43 -0800159 with open(p['path']) as f:
160 for line in f:
James E. Blaire1edde32018-03-02 15:05:14 +0000161 if line.startswith('LIBS_FROM_GIT'):
162 lfg = line.strip().split('=')[1]
James E. Blair8e5f8c22018-06-15 10:10:35 -0700163 self.assertEqual('nova,oslo.messaging,glance', lfg)
James E. Blaire1edde32018-03-02 15:05:14 +0000164
165 def test_overridelibs_from_git(self):
166 "Test that LIBS_FROM_GIT can be overridden"
167 localrc = {'LIBS_FROM_GIT': 'oslo.db'}
168 projects = {
169 'git.openstack.org/openstack/nova': {
170 'required': True,
171 'short_name': 'nova',
172 },
173 'git.openstack.org/openstack/oslo.messaging': {
174 'required': True,
175 'short_name': 'oslo.messaging',
176 },
177 'git.openstack.org/openstack/devstack-plugin': {
178 'required': False,
179 'short_name': 'devstack-plugin',
180 },
181 }
182 p = dict(localrc=localrc,
183 base_services=[],
184 base_dir='./test',
185 path=os.path.join(self.tmpdir, 'test.local.conf'),
186 projects=projects)
187 lc = LocalConf(p.get('localrc'),
188 p.get('local_conf'),
189 p.get('base_services'),
190 p.get('services'),
191 p.get('plugins'),
192 p.get('base_dir'),
James E. Blair8e5f8c22018-06-15 10:10:35 -0700193 p.get('projects'),
194 p.get('project'))
James E. Blaire1edde32018-03-02 15:05:14 +0000195 lc.write(p['path'])
196
197 lfg = None
198 with open(p['path']) as f:
199 for line in f:
200 if line.startswith('LIBS_FROM_GIT'):
201 lfg = line.strip().split('=')[1]
202 self.assertEqual('oslo.db', lfg)
James E. Blair6f27fca2017-11-21 17:05:43 -0800203
204 def test_plugin_circular_deps(self):
205 "Test that plugins with circular dependencies fail"
206 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
207 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
208 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
209 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
210 with open(os.path.join(
211 self.tmpdir,
212 'foo-plugin', 'devstack', 'settings'), 'w') as f:
213 f.write('define_plugin foo\n')
214 f.write('plugin_requires foo bar\n')
215 with open(os.path.join(
216 self.tmpdir,
217 'bar-plugin', 'devstack', 'settings'), 'w') as f:
218 f.write('define_plugin bar\n')
219 f.write('plugin_requires bar foo\n')
220
221 localrc = {'test_localrc': '1'}
222 local_conf = {'install':
223 {'nova.conf':
224 {'main':
225 {'test_conf': '2'}}}}
226 services = {'cinder': True}
227 # We use ordereddict here to make sure the plugins are in the
228 # *wrong* order for testing.
229 plugins = OrderedDict([
230 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
231 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
232 ])
233 p = dict(localrc=localrc,
234 local_conf=local_conf,
235 base_services=[],
236 services=services,
237 plugins=plugins,
238 base_dir=self.tmpdir,
239 path=os.path.join(self.tmpdir, 'test.local.conf'))
240 with self.assertRaises(Exception):
241 lc = LocalConf(p.get('localrc'),
242 p.get('local_conf'),
243 p.get('base_services'),
244 p.get('services'),
245 p.get('plugins'),
246 p.get('base_dir'))
247 lc.write(p['path'])
248
249
250if __name__ == '__main__':
251 unittest.main()