blob: 791552d1adb35b19bae50a4d95012286e3aa8e86 [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:
81 f.write('define_plugin foo\n')
82 with open(os.path.join(
83 self.tmpdir,
84 'bar-plugin', 'devstack', 'settings'), 'w') as f:
85 f.write('define_plugin bar\n')
86 f.write('plugin_requires bar foo\n')
87
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([
97 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
98 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
99 ])
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'))
James E. Blaire1edde32018-03-02 15:05:14 +0000107
108 def test_libs_from_git(self):
109 "Test that LIBS_FROM_GIT is auto-generated"
110 projects = {
111 'git.openstack.org/openstack/nova': {
112 'required': True,
113 'short_name': 'nova',
114 },
115 'git.openstack.org/openstack/oslo.messaging': {
116 'required': True,
117 'short_name': 'oslo.messaging',
118 },
119 'git.openstack.org/openstack/devstack-plugin': {
120 'required': False,
121 'short_name': 'devstack-plugin',
122 },
123 }
James E. Blair8e5f8c22018-06-15 10:10:35 -0700124 project = {
125 'short_name': 'glance',
126 }
James E. Blaire1edde32018-03-02 15:05:14 +0000127 p = dict(base_services=[],
128 base_dir='./test',
129 path=os.path.join(self.tmpdir, 'test.local.conf'),
James E. Blair8e5f8c22018-06-15 10:10:35 -0700130 projects=projects,
131 project=project)
James E. Blair6f27fca2017-11-21 17:05:43 -0800132 lc = LocalConf(p.get('localrc'),
133 p.get('local_conf'),
134 p.get('base_services'),
135 p.get('services'),
136 p.get('plugins'),
James E. Blaire1edde32018-03-02 15:05:14 +0000137 p.get('base_dir'),
James E. Blair8e5f8c22018-06-15 10:10:35 -0700138 p.get('projects'),
139 p.get('project'))
James E. Blair6f27fca2017-11-21 17:05:43 -0800140 lc.write(p['path'])
141
James E. Blaire1edde32018-03-02 15:05:14 +0000142 lfg = None
James E. Blair6f27fca2017-11-21 17:05:43 -0800143 with open(p['path']) as f:
144 for line in f:
James E. Blaire1edde32018-03-02 15:05:14 +0000145 if line.startswith('LIBS_FROM_GIT'):
146 lfg = line.strip().split('=')[1]
James E. Blair8e5f8c22018-06-15 10:10:35 -0700147 self.assertEqual('nova,oslo.messaging,glance', lfg)
James E. Blaire1edde32018-03-02 15:05:14 +0000148
149 def test_overridelibs_from_git(self):
150 "Test that LIBS_FROM_GIT can be overridden"
151 localrc = {'LIBS_FROM_GIT': 'oslo.db'}
152 projects = {
153 'git.openstack.org/openstack/nova': {
154 'required': True,
155 'short_name': 'nova',
156 },
157 'git.openstack.org/openstack/oslo.messaging': {
158 'required': True,
159 'short_name': 'oslo.messaging',
160 },
161 'git.openstack.org/openstack/devstack-plugin': {
162 'required': False,
163 'short_name': 'devstack-plugin',
164 },
165 }
166 p = dict(localrc=localrc,
167 base_services=[],
168 base_dir='./test',
169 path=os.path.join(self.tmpdir, 'test.local.conf'),
170 projects=projects)
171 lc = LocalConf(p.get('localrc'),
172 p.get('local_conf'),
173 p.get('base_services'),
174 p.get('services'),
175 p.get('plugins'),
176 p.get('base_dir'),
James E. Blair8e5f8c22018-06-15 10:10:35 -0700177 p.get('projects'),
178 p.get('project'))
James E. Blaire1edde32018-03-02 15:05:14 +0000179 lc.write(p['path'])
180
181 lfg = None
182 with open(p['path']) as f:
183 for line in f:
184 if line.startswith('LIBS_FROM_GIT'):
185 lfg = line.strip().split('=')[1]
186 self.assertEqual('oslo.db', lfg)
James E. Blair6f27fca2017-11-21 17:05:43 -0800187
188 def test_plugin_circular_deps(self):
189 "Test that plugins with circular dependencies fail"
190 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', 'devstack'))
191 os.makedirs(os.path.join(self.tmpdir, 'foo-plugin', '.git'))
192 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', 'devstack'))
193 os.makedirs(os.path.join(self.tmpdir, 'bar-plugin', '.git'))
194 with open(os.path.join(
195 self.tmpdir,
196 'foo-plugin', 'devstack', 'settings'), 'w') as f:
197 f.write('define_plugin foo\n')
198 f.write('plugin_requires foo bar\n')
199 with open(os.path.join(
200 self.tmpdir,
201 'bar-plugin', 'devstack', 'settings'), 'w') as f:
202 f.write('define_plugin bar\n')
203 f.write('plugin_requires bar foo\n')
204
205 localrc = {'test_localrc': '1'}
206 local_conf = {'install':
207 {'nova.conf':
208 {'main':
209 {'test_conf': '2'}}}}
210 services = {'cinder': True}
211 # We use ordereddict here to make sure the plugins are in the
212 # *wrong* order for testing.
213 plugins = OrderedDict([
214 ('bar', 'git://git.openstack.org/openstack/bar-plugin'),
215 ('foo', 'git://git.openstack.org/openstack/foo-plugin'),
216 ])
217 p = dict(localrc=localrc,
218 local_conf=local_conf,
219 base_services=[],
220 services=services,
221 plugins=plugins,
222 base_dir=self.tmpdir,
223 path=os.path.join(self.tmpdir, 'test.local.conf'))
224 with self.assertRaises(Exception):
225 lc = LocalConf(p.get('localrc'),
226 p.get('local_conf'),
227 p.get('base_services'),
228 p.get('services'),
229 p.get('plugins'),
230 p.get('base_dir'))
231 lc.write(p['path'])
232
233
234if __name__ == '__main__':
235 unittest.main()