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_ini_add.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 testtools
  19. from devstack import dsconf
  20. BASIC = """[default]
  21. a = b
  22. c = d
  23. [second]
  24. e = f
  25. g = h
  26. """
  27. RESULT1 = """[default]
  28. s = t
  29. a = b
  30. c = d
  31. [second]
  32. e = f
  33. g = h
  34. """
  35. RESULT2 = """[default]
  36. a = b
  37. c = d
  38. [second]
  39. s = t
  40. e = f
  41. g = h
  42. """
  43. RESULT3 = """[default]
  44. a = b
  45. c = d
  46. [second]
  47. e = f
  48. g = h
  49. [new]
  50. s = t
  51. """
  52. class TestIniAdd(testtools.TestCase):
  53. def setUp(self):
  54. super(TestIniAdd, self).setUp()
  55. self._path = self.useFixture(fixtures.TempDir()).path
  56. self._path += "/test.ini"
  57. with open(self._path, "w") as f:
  58. f.write(BASIC)
  59. def test_add_ini_default(self):
  60. conf = dsconf.IniFile(self._path)
  61. conf.add("default", "s", "t")
  62. with open(self._path) as f:
  63. content = f.read()
  64. self.assertEqual(content, RESULT1)
  65. def test_add_ini_second(self):
  66. conf = dsconf.IniFile(self._path)
  67. conf.add("second", "s", "t")
  68. with open(self._path) as f:
  69. content = f.read()
  70. self.assertEqual(content, RESULT2)
  71. def test_add_ini_new(self):
  72. conf = dsconf.IniFile(self._path)
  73. conf.add("new", "s", "t")
  74. with open(self._path) as f:
  75. content = f.read()
  76. self.assertEqual(content, RESULT3)