test_unit_template_j2.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """Unit tests: template_j2cli & template_envtpl"""
  2. import os
  3. import pytest
  4. FILE_MODE = 0o754
  5. LOCAL_CLASS = "j2_Test+@-!^Class"
  6. LOCAL_CLASS2 = "j2_Test+@-|^2nd_Class withSpace"
  7. LOCAL_ARCH = "j2_Test+@-!^Arch"
  8. LOCAL_SYSTEM = "j2_Test+@-!^System"
  9. LOCAL_HOST = "j2_Test+@-!^Host"
  10. LOCAL_USER = "j2_Test+@-!^User"
  11. LOCAL_DISTRO = "j2_Test+@-!^Distro"
  12. TEMPLATE = f'''
  13. start of template
  14. j2 class = >{{{{YADM_CLASS}}}}<
  15. j2 arch = >{{{{YADM_ARCH}}}}<
  16. j2 os = >{{{{YADM_OS}}}}<
  17. j2 host = >{{{{YADM_HOSTNAME}}}}<
  18. j2 user = >{{{{YADM_USER}}}}<
  19. j2 distro = >{{{{YADM_DISTRO}}}}<
  20. {{%- if YADM_CLASS == "wrongclass1" %}}
  21. wrong class 1
  22. {{%- endif %}}
  23. {{%- if YADM_CLASS == "{LOCAL_CLASS}" %}}
  24. Included section for class = {{{{YADM_CLASS}}}} ({{{{YADM_CLASS}}}} repeated)
  25. {{%- endif %}}
  26. {{%- if YADM_CLASS == "wrongclass2" %}}
  27. wrong class 2
  28. {{%- endif %}}
  29. {{%- if "{LOCAL_CLASS2}" in YADM_CLASSES.split("\\n") %}}
  30. Included section for second class
  31. {{%- endif %}}
  32. {{%- if YADM_ARCH == "wrongarch1" %}}
  33. wrong arch 1
  34. {{%- endif %}}
  35. {{%- if YADM_ARCH == "{LOCAL_ARCH}" %}}
  36. Included section for arch = {{{{YADM_ARCH}}}} ({{{{YADM_ARCH}}}} repeated)
  37. {{%- endif %}}
  38. {{%- if YADM_ARCH == "wrongarch2" %}}
  39. wrong arch 2
  40. {{%- endif %}}
  41. {{%- if YADM_OS == "wrongos1" %}}
  42. wrong os 1
  43. {{%- endif %}}
  44. {{%- if YADM_OS == "{LOCAL_SYSTEM}" %}}
  45. Included section for os = {{{{YADM_OS}}}} ({{{{YADM_OS}}}} repeated)
  46. {{%- endif %}}
  47. {{%- if YADM_OS == "wrongos2" %}}
  48. wrong os 2
  49. {{%- endif %}}
  50. {{%- if YADM_HOSTNAME == "wronghost1" %}}
  51. wrong host 1
  52. {{%- endif %}}
  53. {{%- if YADM_HOSTNAME == "{LOCAL_HOST}" %}}
  54. Included section for host = {{{{YADM_HOSTNAME}}}} ({{{{YADM_HOSTNAME}}}} again)
  55. {{%- endif %}}
  56. {{%- if YADM_HOSTNAME == "wronghost2" %}}
  57. wrong host 2
  58. {{%- endif %}}
  59. {{%- if YADM_USER == "wronguser1" %}}
  60. wrong user 1
  61. {{%- endif %}}
  62. {{%- if YADM_USER == "{LOCAL_USER}" %}}
  63. Included section for user = {{{{YADM_USER}}}} ({{{{YADM_USER}}}} repeated)
  64. {{%- endif %}}
  65. {{%- if YADM_USER == "wronguser2" %}}
  66. wrong user 2
  67. {{%- endif %}}
  68. {{%- if YADM_DISTRO == "wrongdistro1" %}}
  69. wrong distro 1
  70. {{%- endif %}}
  71. {{%- if YADM_DISTRO == "{LOCAL_DISTRO}" %}}
  72. Included section for distro = {{{{YADM_DISTRO}}}} ({{{{YADM_DISTRO}}}} again)
  73. {{%- endif %}}
  74. {{%- if YADM_DISTRO == "wrongdistro2" %}}
  75. wrong distro 2
  76. {{%- endif %}}
  77. end of template
  78. '''
  79. EXPECTED = f'''
  80. start of template
  81. j2 class = >{LOCAL_CLASS}<
  82. j2 arch = >{LOCAL_ARCH}<
  83. j2 os = >{LOCAL_SYSTEM}<
  84. j2 host = >{LOCAL_HOST}<
  85. j2 user = >{LOCAL_USER}<
  86. j2 distro = >{LOCAL_DISTRO}<
  87. Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
  88. Included section for second class
  89. Included section for arch = {LOCAL_ARCH} ({LOCAL_ARCH} repeated)
  90. Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
  91. Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
  92. Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
  93. Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
  94. end of template
  95. '''
  96. @pytest.mark.parametrize('processor', ('j2cli', 'envtpl'))
  97. def test_template_j2(runner, yadm, tmpdir, processor):
  98. """Test processing by j2cli & envtpl"""
  99. input_file = tmpdir.join('input')
  100. input_file.write(TEMPLATE, ensure=True)
  101. input_file.chmod(FILE_MODE)
  102. output_file = tmpdir.join('output')
  103. # ensure overwrite works when file exists as read-only (there is some
  104. # special processing when this is encountered because some environments do
  105. # not properly overwrite read-only files)
  106. output_file.write('existing')
  107. output_file.chmod(0o400)
  108. script = f"""
  109. YADM_TEST=1 source {yadm}
  110. local_class="{LOCAL_CLASS}"
  111. local_classes=("{LOCAL_CLASS2}" "{LOCAL_CLASS}")
  112. local_arch="{LOCAL_ARCH}"
  113. local_system="{LOCAL_SYSTEM}"
  114. local_host="{LOCAL_HOST}"
  115. local_user="{LOCAL_USER}"
  116. local_distro="{LOCAL_DISTRO}"
  117. template_{processor} "{input_file}" "{output_file}"
  118. """
  119. run = runner(command=['bash'], inp=script)
  120. assert run.success
  121. assert run.err == ''
  122. assert output_file.read() == EXPECTED
  123. assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
  124. @pytest.mark.parametrize('processor', ('j2cli', 'envtpl'))
  125. def test_source(runner, yadm, tmpdir, processor):
  126. """Test YADM_SOURCE"""
  127. input_file = tmpdir.join('input')
  128. input_file.write('{{YADM_SOURCE}}', ensure=True)
  129. input_file.chmod(FILE_MODE)
  130. output_file = tmpdir.join('output')
  131. script = f"""
  132. YADM_TEST=1 source {yadm}
  133. template_{processor} "{input_file}" "{output_file}"
  134. """
  135. run = runner(command=['bash'], inp=script)
  136. assert run.success
  137. assert run.err == ''
  138. assert output_file.read().strip() == str(input_file)
  139. assert os.stat(output_file).st_mode == os.stat(input_file).st_mode