test_env.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. response = self.u_boot_console.run_command('printenv')
  32. self.env = {}
  33. for l in response.splitlines():
  34. if not '=' in l:
  35. continue
  36. (var, value) = l.strip().split('=', 1)
  37. self.env[var] = value
  38. def get_existent_var(self):
  39. """Return the name of an environment variable that exists.
  40. Args:
  41. None.
  42. Returns:
  43. The name of an environment variable.
  44. """
  45. for var in self.env:
  46. return var
  47. def get_non_existent_var(self):
  48. """Return the name of an environment variable that does not exist.
  49. Args:
  50. None.
  51. Returns:
  52. The name of an environment variable.
  53. """
  54. n = 0
  55. while True:
  56. var = 'test_env_' + str(n)
  57. if var not in self.env:
  58. return var
  59. n += 1
  60. ste = None
  61. @pytest.fixture(scope='function')
  62. def state_test_env(u_boot_console):
  63. """pytest fixture to provide a StateTestEnv object to tests."""
  64. global ste
  65. if not ste:
  66. ste = StateTestEnv(u_boot_console)
  67. return ste
  68. def unset_var(state_test_env, var):
  69. """Unset an environment variable.
  70. This both executes a U-Boot shell command and updates a StateTestEnv
  71. object.
  72. Args:
  73. state_test_env: The StateTestEnv object to updata.
  74. var: The variable name to unset.
  75. Returns:
  76. Nothing.
  77. """
  78. state_test_env.u_boot_console.run_command('setenv %s' % var)
  79. if var in state_test_env.env:
  80. del state_test_env.env[var]
  81. def set_var(state_test_env, var, value):
  82. """Set an environment variable.
  83. This both executes a U-Boot shell command and updates a StateTestEnv
  84. object.
  85. Args:
  86. state_test_env: The StateTestEnv object to updata.
  87. var: The variable name to set.
  88. value: The value to set the variable to.
  89. Returns:
  90. Nothing.
  91. """
  92. state_test_env.u_boot_console.run_command('setenv %s "%s"' % (var, value))
  93. state_test_env.env[var] = value
  94. def validate_empty(state_test_env, var):
  95. """Validate that a variable is not set, using U-Boot shell commands.
  96. Args:
  97. var: The variable name to test.
  98. Returns:
  99. Nothing.
  100. """
  101. response = state_test_env.u_boot_console.run_command('echo $%s' % var)
  102. assert response == ''
  103. def validate_set(state_test_env, var, value):
  104. """Validate that a variable is set, using U-Boot shell commands.
  105. Args:
  106. var: The variable name to test.
  107. value: The value the variable is expected to have.
  108. Returns:
  109. Nothing.
  110. """
  111. # echo does not preserve leading, internal, or trailing whitespace in the
  112. # value. printenv does, and hence allows more complete testing.
  113. response = state_test_env.u_boot_console.run_command('printenv %s' % var)
  114. assert response == ('%s=%s' % (var, value))
  115. def test_env_echo_exists(state_test_env):
  116. """Test echoing a variable that exists."""
  117. var = state_test_env.get_existent_var()
  118. value = state_test_env.env[var]
  119. validate_set(state_test_env, var, value)
  120. def test_env_echo_non_existent(state_test_env):
  121. """Test echoing a variable that doesn't exist."""
  122. var = state_test_env.set_var
  123. validate_empty(state_test_env, var)
  124. def test_env_printenv_non_existent(state_test_env):
  125. """Test printenv error message for non-existant variables."""
  126. var = state_test_env.set_var
  127. c = state_test_env.u_boot_console
  128. with c.disable_check('error_notification'):
  129. response = c.run_command('printenv %s' % var)
  130. assert(response == '## Error: "%s" not defined' % var)
  131. def test_env_unset_non_existent(state_test_env):
  132. """Test unsetting a nonexistent variable."""
  133. var = state_test_env.get_non_existent_var()
  134. unset_var(state_test_env, var)
  135. validate_empty(state_test_env, var)
  136. def test_env_set_non_existent(state_test_env):
  137. """Test set a non-existant variable."""
  138. var = state_test_env.set_var
  139. value = 'foo'
  140. set_var(state_test_env, var, value)
  141. validate_set(state_test_env, var, value)
  142. def test_env_set_existing(state_test_env):
  143. """Test setting an existant variable."""
  144. var = state_test_env.set_var
  145. value = 'bar'
  146. set_var(state_test_env, var, value)
  147. validate_set(state_test_env, var, value)
  148. def test_env_unset_existing(state_test_env):
  149. """Test unsetting a variable."""
  150. var = state_test_env.set_var
  151. unset_var(state_test_env, var)
  152. validate_empty(state_test_env, var)
  153. def test_env_expansion_spaces(state_test_env):
  154. """Test expanding a variable that contains a space in its value."""
  155. var_space = None
  156. var_test = None
  157. try:
  158. var_space = state_test_env.get_non_existent_var()
  159. set_var(state_test_env, var_space, ' ')
  160. var_test = state_test_env.get_non_existent_var()
  161. value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
  162. set_var(state_test_env, var_test, value)
  163. value = ' 1 2 '
  164. validate_set(state_test_env, var_test, value)
  165. finally:
  166. if var_space:
  167. unset_var(state_test_env, var_space)
  168. if var_test:
  169. unset_var(state_test_env, var_test)