test_files.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from __future__ import print_function
  16. import os
  17. import fixtures
  18. from pbr.hooks import files
  19. from pbr.tests import base
  20. class FilesConfigTest(base.BaseTestCase):
  21. def setUp(self):
  22. super(FilesConfigTest, self).setUp()
  23. pkg_fixture = fixtures.PythonPackage(
  24. "fake_package", [
  25. ("fake_module.py", b""),
  26. ("other_fake_module.py", b""),
  27. ])
  28. self.useFixture(pkg_fixture)
  29. pkg_etc = os.path.join(pkg_fixture.base, 'etc')
  30. pkg_ansible = os.path.join(pkg_fixture.base, 'ansible',
  31. 'kolla-ansible', 'test')
  32. dir_spcs = os.path.join(pkg_fixture.base, 'dir with space')
  33. dir_subdir_spc = os.path.join(pkg_fixture.base, 'multi space',
  34. 'more spaces')
  35. pkg_sub = os.path.join(pkg_etc, 'sub')
  36. subpackage = os.path.join(
  37. pkg_fixture.base, 'fake_package', 'subpackage')
  38. os.makedirs(pkg_sub)
  39. os.makedirs(subpackage)
  40. os.makedirs(pkg_ansible)
  41. os.makedirs(dir_spcs)
  42. os.makedirs(dir_subdir_spc)
  43. with open(os.path.join(pkg_etc, "foo"), 'w') as foo_file:
  44. foo_file.write("Foo Data")
  45. with open(os.path.join(pkg_sub, "bar"), 'w') as foo_file:
  46. foo_file.write("Bar Data")
  47. with open(os.path.join(pkg_ansible, "baz"), 'w') as baz_file:
  48. baz_file.write("Baz Data")
  49. with open(os.path.join(subpackage, "__init__.py"), 'w') as foo_file:
  50. foo_file.write("# empty")
  51. with open(os.path.join(dir_spcs, "file with spc"), 'w') as spc_file:
  52. spc_file.write("# empty")
  53. with open(os.path.join(dir_subdir_spc, "file with spc"), 'w') as file_:
  54. file_.write("# empty")
  55. self.useFixture(base.DiveDir(pkg_fixture.base))
  56. def test_implicit_auto_package(self):
  57. config = dict(
  58. files=dict(
  59. )
  60. )
  61. files.FilesConfig(config, 'fake_package').run()
  62. self.assertIn('subpackage', config['files']['packages'])
  63. def test_auto_package(self):
  64. config = dict(
  65. files=dict(
  66. packages='fake_package',
  67. )
  68. )
  69. files.FilesConfig(config, 'fake_package').run()
  70. self.assertIn('subpackage', config['files']['packages'])
  71. def test_data_files_globbing(self):
  72. config = dict(
  73. files=dict(
  74. data_files="\n etc/pbr = etc/*"
  75. )
  76. )
  77. files.FilesConfig(config, 'fake_package').run()
  78. self.assertIn(
  79. "\n'etc/pbr/' = \n 'etc/foo'\n'etc/pbr/sub' = \n 'etc/sub/bar'",
  80. config['files']['data_files'])
  81. def test_data_files_with_spaces(self):
  82. config = dict(
  83. files=dict(
  84. data_files="\n 'i like spaces' = 'dir with space'/*"
  85. )
  86. )
  87. files.FilesConfig(config, 'fake_package').run()
  88. self.assertIn(
  89. "\n'i like spaces/' = \n 'dir with space/file with spc'",
  90. config['files']['data_files'])
  91. def test_data_files_with_spaces_subdirectories(self):
  92. # test that we can handle whitespace in subdirectories
  93. data_files = "\n 'one space/two space' = 'multi space/more spaces'/*"
  94. expected = (
  95. "\n'one space/two space/' = "
  96. "\n 'multi space/more spaces/file with spc'")
  97. config = dict(
  98. files=dict(
  99. data_files=data_files
  100. )
  101. )
  102. files.FilesConfig(config, 'fake_package').run()
  103. self.assertIn(expected, config['files']['data_files'])
  104. def test_data_files_with_spaces_quoted_components(self):
  105. # test that we can quote individual path components
  106. data_files = (
  107. "\n'one space'/'two space' = 'multi space'/'more spaces'/*"
  108. )
  109. expected = ("\n'one space/two space/' = "
  110. "\n 'multi space/more spaces/file with spc'")
  111. config = dict(
  112. files=dict(
  113. data_files=data_files
  114. )
  115. )
  116. files.FilesConfig(config, 'fake_package').run()
  117. self.assertIn(expected, config['files']['data_files'])
  118. def test_data_files_globbing_source_prefix_in_directory_name(self):
  119. # We want to test that the string, "docs", is not replaced in a
  120. # subdirectory name, "sub-docs"
  121. config = dict(
  122. files=dict(
  123. data_files="\n share/ansible = ansible/*"
  124. )
  125. )
  126. files.FilesConfig(config, 'fake_package').run()
  127. self.assertIn(
  128. "\n'share/ansible/' = "
  129. "\n'share/ansible/kolla-ansible' = "
  130. "\n'share/ansible/kolla-ansible/test' = "
  131. "\n 'ansible/kolla-ansible/test/baz'",
  132. config['files']['data_files'])