test_env.py 6.4 KB

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