DevStack supporting tools in python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_localconf_merge.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. # Implementation of ini add / remove for devstack. We don't use the
  15. # python ConfigFile parser because that ends up rewriting the entire
  16. # file and doesn't ensure comments remain.
  17. import fixtures
  18. import os.path
  19. import testtools
  20. from devstack import dsconf
  21. BASIC = """
  22. [[local|localrc]]
  23. a=b
  24. c=d
  25. f=1
  26. [[post-config|$NEUTRON_CONF]]
  27. [DEFAULT]
  28. global_physnet_mtu=1450
  29. [[post-config|$NOVA_CONF]]
  30. [upgrade_levels]
  31. compute = auto
  32. """
  33. BASIC2 = """
  34. [[local|localrc]]
  35. a=b
  36. c=d
  37. f=1
  38. [[post-config|$NEUTRON_CONF]]
  39. [quotas]
  40. quota_network = 100
  41. """
  42. LC1 = """
  43. [[local|localrc]]
  44. a=5
  45. g=2
  46. [[post-config|$NEUTRON_CONF]]
  47. [DEFAULT]
  48. global_physnet_mtu=1400
  49. [[post-config|$GLANCE_CONF]]
  50. [upgrade_levels]
  51. compute = auto
  52. """
  53. LC2 = """
  54. [[local|localrc]]
  55. # some other comment
  56. enable_plugin ironic https://github.com/openstack/ironic
  57. TEMPEST_PLUGINS+=" /opt/stack/new/ironic"
  58. """
  59. LC3 = """
  60. [[post-config|$NEUTRON_CONF]]
  61. [quotas]
  62. quota_port = 500
  63. """
  64. LC4 = """
  65. [[post-config|$NEUTRON_CONF]]
  66. [DEFAULT]
  67. global_physnet_mtu=1400
  68. """
  69. RESULT1 = """
  70. [[local|localrc]]
  71. a=b
  72. c=d
  73. f=1
  74. a=5
  75. g=2
  76. [[post-config|$NEUTRON_CONF]]
  77. [DEFAULT]
  78. global_physnet_mtu = 1400
  79. [[post-config|$NOVA_CONF]]
  80. [upgrade_levels]
  81. compute = auto
  82. [[post-config|$GLANCE_CONF]]
  83. [upgrade_levels]
  84. compute = auto
  85. """
  86. RESULT2 = """
  87. [[local|localrc]]
  88. a=b
  89. c=d
  90. f=1
  91. # some other comment
  92. enable_plugin ironic https://github.com/openstack/ironic
  93. TEMPEST_PLUGINS+=" /opt/stack/new/ironic"
  94. [[post-config|$NEUTRON_CONF]]
  95. [DEFAULT]
  96. global_physnet_mtu=1450
  97. [[post-config|$NOVA_CONF]]
  98. [upgrade_levels]
  99. compute = auto
  100. """
  101. RESULT3 = """
  102. [[local|localrc]]
  103. a=b
  104. c=d
  105. f=1
  106. [[post-config|$NEUTRON_CONF]]
  107. [quotas]
  108. quota_network = 100
  109. quota_port = 500
  110. """
  111. RESULT4 = """
  112. [[local|localrc]]
  113. a=b
  114. c=d
  115. f=1
  116. [[post-config|$NEUTRON_CONF]]
  117. [quotas]
  118. quota_network = 100
  119. [DEFAULT]
  120. global_physnet_mtu = 1400
  121. """
  122. class TestLcMerge(testtools.TestCase):
  123. def setUp(self):
  124. super(TestLcMerge, self).setUp()
  125. self._path = self.useFixture(fixtures.TempDir()).path
  126. self._path += "/local.conf"
  127. with open(self._path, "w") as f:
  128. f.write(BASIC)
  129. def test_merge_lc1(self):
  130. dirname = self.useFixture(fixtures.TempDir()).path
  131. lc1 = os.path.join(dirname, "local1.conf")
  132. with open(lc1, "w+") as f:
  133. f.write(LC1)
  134. conf = dsconf.LocalConf(self._path)
  135. conf.merge_lc(lc1)
  136. with open(self._path) as f:
  137. content = f.read()
  138. self.assertEqual(content, RESULT1)
  139. def test_merge_lc2(self):
  140. dirname = self.useFixture(fixtures.TempDir()).path
  141. lc2 = os.path.join(dirname, "local2.conf")
  142. with open(lc2, "w+") as f:
  143. f.write(LC2)
  144. conf = dsconf.LocalConf(self._path)
  145. conf.merge_lc(lc2)
  146. with open(self._path) as f:
  147. content = f.read()
  148. self.assertEqual(content, RESULT2)
  149. class TestLcMergePost(testtools.TestCase):
  150. def setUp(self):
  151. super(TestLcMergePost, self).setUp()
  152. self._path = self.useFixture(fixtures.TempDir()).path
  153. self._path += "/local.conf"
  154. with open(self._path, "w") as f:
  155. f.write(BASIC2)
  156. def test_merge_lc3(self):
  157. """Test merging with 2 post-config sections that should overlap."""
  158. dirname = self.useFixture(fixtures.TempDir()).path
  159. lc = os.path.join(dirname, "local.conf")
  160. with open(lc, "w+") as f:
  161. f.write(LC3)
  162. conf = dsconf.LocalConf(self._path)
  163. conf.merge_lc(lc)
  164. with open(self._path) as f:
  165. content = f.read()
  166. self.assertEqual(content, RESULT3)
  167. def test_merge_lc4(self):
  168. """Test merging with 2 post-config sections that should overlap."""
  169. dirname = self.useFixture(fixtures.TempDir()).path
  170. lc = os.path.join(dirname, "local2.conf")
  171. with open(lc, "w+") as f:
  172. f.write(LC4)
  173. conf = dsconf.LocalConf(self._path)
  174. conf.merge_lc(lc)
  175. with open(self._path) as f:
  176. content = f.read()
  177. self.assertEqual(content, RESULT4)