summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Dague <sean@dague.net>2017-01-16 13:31:53 -0500
committerSean Dague <sean@dague.net>2017-01-16 13:31:53 -0500
commit925b1bc68267e0588f62b2a13a4fa92a30db805c (patch)
tree1764a5fe299ec76dd9050bc902b84d9f7f6ce179
parenta0f574ab9784e25963d42dee3c29ebbd54ea8f72 (diff)
add functions to set items in local.conf
-rw-r--r--devstack/dsconf.py38
-rw-r--r--devstack/tests/test_localconf_set_local.py88
2 files changed, 126 insertions, 0 deletions
diff --git a/devstack/dsconf.py b/devstack/dsconf.py
index 4b1d4dc..8d64b4e 100644
--- a/devstack/dsconf.py
+++ b/devstack/dsconf.py
@@ -173,3 +173,41 @@ class LocalConf(object):
173 with open(target, "a+") as f: 173 with open(target, "a+") as f:
174 for line in self._section("local", "localrc"): 174 for line in self._section("local", "localrc"):
175 f.write(line) 175 f.write(line)
176
177 def _at_insert_point_local(self, name, func):
178 temp = tempfile.NamedTemporaryFile(mode='r')
179 shutil.copyfile(self.fname, temp.name)
180 in_meta = False
181 done = False
182 with open(self.fname, "w+") as writer:
183 with open(temp.name) as reader:
184 for line in reader.readlines():
185 if done:
186 writer.write(line)
187 continue
188
189 if re.match(re.escape("[[local|localrc]]"), line):
190 in_meta = True
191 writer.write(line)
192 elif re.match("\[\[.*\|.*\]\]", line):
193 # if we're not done yet, we
194 if in_meta:
195 func(writer, None)
196 writer.write(line)
197 done = True
198 in_meta = False
199 continue
200 elif re.match("\s*%s\s*\=" % re.escape(name), line):
201 # we found our key, pass to the writer func
202 func(writer, line)
203 done = True
204 else:
205 # write out whatever we find
206 writer.write(line)
207 if not done:
208 func(writer, None)
209
210 def set_local(self, name, value):
211 def _do_set(writer, line):
212 writer.write("%s = %s\n" % (name, value))
213 self._at_insert_point_local(name, _do_set)
diff --git a/devstack/tests/test_localconf_set_local.py b/devstack/tests/test_localconf_set_local.py
new file mode 100644
index 0000000..35701d1
--- /dev/null
+++ b/devstack/tests/test_localconf_set_local.py
@@ -0,0 +1,88 @@
1# Copyright 2017 IBM
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15# Implementation of ini add / remove for devstack. We don't use the
16# python ConfigFile parser because that ends up rewriting the entire
17# file and doesn't ensure comments remain.
18
19import fixtures
20import os.path
21import testtools
22
23from devstack import dsconf
24
25
26BASIC = """
27[[local|localrc]]
28a = b
29c = d
30f = 1
31[[post-config|$NEUTRON_CONF]]
32[DEFAULT]
33global_physnet_mtu=1450
34[[post-config|$NOVA_CONF]]
35[upgrade_levels]
36compute = auto
37"""
38
39RESULT1 = """
40[[local|localrc]]
41a = b
42c = d
43f = 1
44g = 2
45[[post-config|$NEUTRON_CONF]]
46[DEFAULT]
47global_physnet_mtu=1450
48[[post-config|$NOVA_CONF]]
49[upgrade_levels]
50compute = auto
51"""
52
53RESULT2 = """
54[[local|localrc]]
55a = 2
56c = d
57f = 1
58[[post-config|$NEUTRON_CONF]]
59[DEFAULT]
60global_physnet_mtu=1450
61[[post-config|$NOVA_CONF]]
62[upgrade_levels]
63compute = auto
64"""
65
66
67class TestLcSet(testtools.TestCase):
68
69 def setUp(self):
70 super(TestLcSet, self).setUp()
71 self._path = self.useFixture(fixtures.TempDir()).path
72 self._path += "/local.conf"
73 with open(self._path, "w") as f:
74 f.write(BASIC)
75
76 def test_set_new(self):
77 conf = dsconf.LocalConf(self._path)
78 conf.set_local("g", "2")
79 with open(self._path) as f:
80 content = f.read()
81 self.assertEqual(content, RESULT1)
82
83 def test_set_existing(self):
84 conf = dsconf.LocalConf(self._path)
85 conf.set_local("a", "2")
86 with open(self._path) as f:
87 content = f.read()
88 self.assertEqual(content, RESULT2)