test_env.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2015 Stephen Warren
  3. # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
  4. # Test operation of shell commands relating to environment variables.
  5. import pytest
  6. # FIXME: This might be useful for other tests;
  7. # perhaps refactor it into ConsoleBase or some other state object?
  8. class StateTestEnv(object):
  9. """Container that represents the state of all U-Boot environment variables.
  10. This enables quick determination of existant/non-existant variable
  11. names.
  12. """
  13. def __init__(self, u_boot_console):
  14. """Initialize a new StateTestEnv object.
  15. Args:
  16. u_boot_console: A U-Boot console.
  17. Returns:
  18. Nothing.
  19. """
  20. self.u_boot_console = u_boot_console
  21. self.get_env()
  22. self.set_var = self.get_non_existent_var()
  23. def get_env(self):
  24. """Read all current environment variables from U-Boot.
  25. Args:
  26. None.
  27. Returns:
  28. Nothing.
  29. """
  30. if self.u_boot_console.config.buildconfig.get(
  31. 'config_version_variable', 'n') == 'y':
  32. with self.u_boot_console.disable_check('main_signon'):
  33. response = self.u_boot_console.run_command('printenv')
  34. else:
  35. response = self.u_boot_console.run_command('printenv')
  36. self.env = {}
  37. for l in response.splitlines():
  38. if not '=' in l:
  39. continue
  40. (var, value) = l.strip().split('=', 1)
  41. self.env[var] = value
  42. def get_existent_var(self):
  43. """Return the name of an environment variable that exists.
  44. Args:
  45. None.
  46. Returns:
  47. The name of an environment variable.
  48. """
  49. for var in self.env:
  50. return var
  51. def get_non_existent_var(self):
  52. """Return the name of an environment variable that does not exist.
  53. Args:
  54. None.
  55. Returns:
  56. The name of an environment variable.
  57. """
  58. n = 0
  59. while True:
  60. var = 'test_env_' + str(n)
  61. if var not in self.env:
  62. return var
  63. n += 1
  64. ste = None
  65. @pytest.fixture(scope='function')
  66. def state_test_env(u_boot_console):
  67. """pytest fixture to provide a StateTestEnv object to tests."""
  68. global ste
  69. if not ste:
  70. ste = StateTestEnv(u_boot_console)
  71. return ste
  72. def unset_var(state_test_env, var):
  73. """Unset an environment variable.
  74. This both executes a U-Boot shell command and updates a StateTestEnv
  75. object.
  76. Args:
  77. state_test_env: The StateTestEnv object to update.
  78. var: The variable name to unset.
  79. Returns:
  80. Nothing.
  81. """
  82. state_test_env.u_boot_console.run_command('setenv %s' % var)
  83. if var in state_test_env.env:
  84. del state_test_env.env[var]
  85. def set_var(state_test_env, var, value):
  86. """Set an environment variable.
  87. This both executes a U-Boot shell command and updates a StateTestEnv
  88. object.
  89. Args:
  90. state_test_env: The StateTestEnv object to update.
  91. var: The variable name to set.
  92. value: The value to set the variable to.
  93. Returns:
  94. Nothing.
  95. """
  96. bc = state_test_env.u_boot_console.config.buildconfig
  97. if bc.get('config_hush_parser', None):
  98. quote = '"'
  99. else:
  100. quote = ''
  101. if ' ' in value:
  102. pytest.skip('Space in variable value on non-Hush shell')
  103. state_test_env.u_boot_console.run_command(
  104. 'setenv %s %s%s%s' % (var, quote, value, quote))
  105. state_test_env.env[var] = value
  106. def validate_empty(state_test_env, var):
  107. """Validate that a variable is not set, using U-Boot shell commands.
  108. Args:
  109. var: The variable name to test.
  110. Returns:
  111. Nothing.
  112. """
  113. response = state_test_env.u_boot_console.run_command('echo $%s' % var)
  114. assert response == ''
  115. def validate_set(state_test_env, var, value):
  116. """Validate that a variable is set, using U-Boot shell commands.
  117. Args:
  118. var: The variable name to test.
  119. value: The value the variable is expected to have.
  120. Returns:
  121. Nothing.
  122. """
  123. # echo does not preserve leading, internal, or trailing whitespace in the
  124. # value. printenv does, and hence allows more complete testing.
  125. response = state_test_env.u_boot_console.run_command('printenv %s' % var)
  126. assert response == ('%s=%s' % (var, value))
  127. def test_env_echo_exists(state_test_env):
  128. """Test echoing a variable that exists."""
  129. var = state_test_env.get_existent_var()
  130. value = state_test_env.env[var]
  131. validate_set(state_test_env, var, value)
  132. @pytest.mark.buildconfigspec('cmd_echo')
  133. def test_env_echo_non_existent(state_test_env):
  134. """Test echoing a variable that doesn't exist."""
  135. var = state_test_env.set_var
  136. validate_empty(state_test_env, var)
  137. def test_env_printenv_non_existent(state_test_env):
  138. """Test printenv error message for non-existant variables."""
  139. var = state_test_env.set_var
  140. c = state_test_env.u_boot_console
  141. with c.disable_check('error_notification'):
  142. response = c.run_command('printenv %s' % var)
  143. assert(response == '## Error: "%s" not defined' % var)
  144. @pytest.mark.buildconfigspec('cmd_echo')
  145. def test_env_unset_non_existent(state_test_env):
  146. """Test unsetting a nonexistent variable."""
  147. var = state_test_env.get_non_existent_var()
  148. unset_var(state_test_env, var)
  149. validate_empty(state_test_env, var)
  150. def test_env_set_non_existent(state_test_env):
  151. """Test set a non-existant variable."""
  152. var = state_test_env.set_var
  153. value = 'foo'
  154. set_var(state_test_env, var, value)
  155. validate_set(state_test_env, var, value)
  156. def test_env_set_existing(state_test_env):
  157. """Test setting an existant variable."""
  158. var = state_test_env.set_var
  159. value = 'bar'
  160. set_var(state_test_env, var, value)
  161. validate_set(state_test_env, var, value)
  162. @pytest.mark.buildconfigspec('cmd_echo')
  163. def test_env_unset_existing(state_test_env):
  164. """Test unsetting a variable."""
  165. var = state_test_env.set_var
  166. unset_var(state_test_env, var)
  167. validate_empty(state_test_env, var)
  168. def test_env_expansion_spaces(state_test_env):
  169. """Test expanding a variable that contains a space in its value."""
  170. var_space = None
  171. var_test = None
  172. try:
  173. var_space = state_test_env.get_non_existent_var()
  174. set_var(state_test_env, var_space, ' ')
  175. var_test = state_test_env.get_non_existent_var()
  176. value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
  177. set_var(state_test_env, var_test, value)
  178. value = ' 1 2 '
  179. validate_set(state_test_env, var_test, value)
  180. finally:
  181. if var_space:
  182. unset_var(state_test_env, var_space)
  183. if var_test:
  184. unset_var(state_test_env, var_test)