James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 1 | # 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 | |
| 17 | import os |
| 18 | import shutil |
| 19 | import tempfile |
| 20 | import unittest |
| 21 | |
| 22 | from devstack_local_conf import LocalConf |
| 23 | from collections import OrderedDict |
| 24 | |
| 25 | class 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. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 59 | p.get('base_dir'), |
James E. Blair | 8e5f8c2 | 2018-06-15 10:10:35 -0700 | [diff] [blame] | 60 | p.get('projects'), |
| 61 | p.get('project')) |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 62 | 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. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 71 | |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 72 | 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 Harbott | 0b85500 | 2018-12-19 12:20:51 +0000 | [diff] [blame^] | 81 | f.write('define_plugin foo-plugin\n') |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 82 | with open(os.path.join( |
| 83 | self.tmpdir, |
| 84 | 'bar-plugin', 'devstack', 'settings'), 'w') as f: |
Jens Harbott | 0b85500 | 2018-12-19 12:20:51 +0000 | [diff] [blame^] | 85 | f.write('define_plugin bar-plugin\n') |
| 86 | f.write('plugin_requires bar-plugin foo-plugin\n') |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 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([ |
Jens Harbott | 0b85500 | 2018-12-19 12:20:51 +0000 | [diff] [blame^] | 97 | ('bar-plugin', 'git://git.openstack.org/openstack/bar-plugin'), |
| 98 | ('foo-plugin', 'git://git.openstack.org/openstack/foo-plugin'), |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 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')) |
Jens Harbott | 6d103a7 | 2018-12-19 11:53:16 +0000 | [diff] [blame] | 107 | 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 Harbott | 0b85500 | 2018-12-19 12:20:51 +0000 | [diff] [blame^] | 122 | self.assertEqual(['foo-plugin', 'bar-plugin'], plugins) |
James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 123 | |
| 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. Blair | 8e5f8c2 | 2018-06-15 10:10:35 -0700 | [diff] [blame] | 140 | project = { |
| 141 | 'short_name': 'glance', |
| 142 | } |
James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 143 | p = dict(base_services=[], |
| 144 | base_dir='./test', |
| 145 | path=os.path.join(self.tmpdir, 'test.local.conf'), |
James E. Blair | 8e5f8c2 | 2018-06-15 10:10:35 -0700 | [diff] [blame] | 146 | projects=projects, |
| 147 | project=project) |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 148 | 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. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 153 | p.get('base_dir'), |
James E. Blair | 8e5f8c2 | 2018-06-15 10:10:35 -0700 | [diff] [blame] | 154 | p.get('projects'), |
| 155 | p.get('project')) |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 156 | lc.write(p['path']) |
| 157 | |
James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 158 | lfg = None |
James E. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 159 | with open(p['path']) as f: |
| 160 | for line in f: |
James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 161 | if line.startswith('LIBS_FROM_GIT'): |
| 162 | lfg = line.strip().split('=')[1] |
James E. Blair | 8e5f8c2 | 2018-06-15 10:10:35 -0700 | [diff] [blame] | 163 | self.assertEqual('nova,oslo.messaging,glance', lfg) |
James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 164 | |
| 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. Blair | 8e5f8c2 | 2018-06-15 10:10:35 -0700 | [diff] [blame] | 193 | p.get('projects'), |
| 194 | p.get('project')) |
James E. Blair | e1edde3 | 2018-03-02 15:05:14 +0000 | [diff] [blame] | 195 | 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. Blair | 6f27fca | 2017-11-21 17:05:43 -0800 | [diff] [blame] | 203 | |
| 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 | |
| 250 | if __name__ == '__main__': |
| 251 | unittest.main() |