1 # Copyright (c) 2015 Stephen Warren
2 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4 # SPDX-License-Identifier: GPL-2.0
6 # Test operation of shell commands relating to environment variables.
10 # FIXME: This might be useful for other tests;
11 # perhaps refactor it into ConsoleBase or some other state object?
12 class StateTestEnv(object):
13 """Container that represents the state of all U-Boot environment variables.
14 This enables quick determination of existant/non-existant variable
18 def __init__(self, u_boot_console):
19 """Initialize a new StateTestEnv object.
22 u_boot_console: A U-Boot console.
28 self.u_boot_console = u_boot_console
30 self.set_var = self.get_non_existent_var()
33 """Read all current environment variables from U-Boot.
42 if self.u_boot_console.config.buildconfig.get(
43 'config_version_variable', 'n') == 'y':
44 with self.u_boot_console.disable_check('main_signon'):
45 response = self.u_boot_console.run_command('printenv')
47 response = self.u_boot_console.run_command('printenv')
49 for l in response.splitlines():
52 (var, value) = l.strip().split('=', 1)
55 def get_existent_var(self):
56 """Return the name of an environment variable that exists.
62 The name of an environment variable.
68 def get_non_existent_var(self):
69 """Return the name of an environment variable that does not exist.
75 The name of an environment variable.
80 var = 'test_env_' + str(n)
81 if var not in self.env:
86 @pytest.fixture(scope='function')
87 def state_test_env(u_boot_console):
88 """pytest fixture to provide a StateTestEnv object to tests."""
92 ste = StateTestEnv(u_boot_console)
95 def unset_var(state_test_env, var):
96 """Unset an environment variable.
98 This both executes a U-Boot shell command and updates a StateTestEnv
102 state_test_env: The StateTestEnv object to update.
103 var: The variable name to unset.
109 state_test_env.u_boot_console.run_command('setenv %s' % var)
110 if var in state_test_env.env:
111 del state_test_env.env[var]
113 def set_var(state_test_env, var, value):
114 """Set an environment variable.
116 This both executes a U-Boot shell command and updates a StateTestEnv
120 state_test_env: The StateTestEnv object to update.
121 var: The variable name to set.
122 value: The value to set the variable to.
128 state_test_env.u_boot_console.run_command('setenv %s "%s"' % (var, value))
129 state_test_env.env[var] = value
131 def validate_empty(state_test_env, var):
132 """Validate that a variable is not set, using U-Boot shell commands.
135 var: The variable name to test.
141 response = state_test_env.u_boot_console.run_command('echo $%s' % var)
142 assert response == ''
144 def validate_set(state_test_env, var, value):
145 """Validate that a variable is set, using U-Boot shell commands.
148 var: The variable name to test.
149 value: The value the variable is expected to have.
155 # echo does not preserve leading, internal, or trailing whitespace in the
156 # value. printenv does, and hence allows more complete testing.
157 response = state_test_env.u_boot_console.run_command('printenv %s' % var)
158 assert response == ('%s=%s' % (var, value))
160 def test_env_echo_exists(state_test_env):
161 """Test echoing a variable that exists."""
163 var = state_test_env.get_existent_var()
164 value = state_test_env.env[var]
165 validate_set(state_test_env, var, value)
167 def test_env_echo_non_existent(state_test_env):
168 """Test echoing a variable that doesn't exist."""
170 var = state_test_env.set_var
171 validate_empty(state_test_env, var)
173 def test_env_printenv_non_existent(state_test_env):
174 """Test printenv error message for non-existant variables."""
176 var = state_test_env.set_var
177 c = state_test_env.u_boot_console
178 with c.disable_check('error_notification'):
179 response = c.run_command('printenv %s' % var)
180 assert(response == '## Error: "%s" not defined' % var)
182 def test_env_unset_non_existent(state_test_env):
183 """Test unsetting a nonexistent variable."""
185 var = state_test_env.get_non_existent_var()
186 unset_var(state_test_env, var)
187 validate_empty(state_test_env, var)
189 def test_env_set_non_existent(state_test_env):
190 """Test set a non-existant variable."""
192 var = state_test_env.set_var
194 set_var(state_test_env, var, value)
195 validate_set(state_test_env, var, value)
197 def test_env_set_existing(state_test_env):
198 """Test setting an existant variable."""
200 var = state_test_env.set_var
202 set_var(state_test_env, var, value)
203 validate_set(state_test_env, var, value)
205 def test_env_unset_existing(state_test_env):
206 """Test unsetting a variable."""
208 var = state_test_env.set_var
209 unset_var(state_test_env, var)
210 validate_empty(state_test_env, var)
212 def test_env_expansion_spaces(state_test_env):
213 """Test expanding a variable that contains a space in its value."""
218 var_space = state_test_env.get_non_existent_var()
219 set_var(state_test_env, var_space, ' ')
221 var_test = state_test_env.get_non_existent_var()
222 value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
223 set_var(state_test_env, var_test, value)
225 validate_set(state_test_env, var_test, value)
228 unset_var(state_test_env, var_space)
230 unset_var(state_test_env, var_test)