]>
Commit | Line | Data |
---|---|---|
5ab097ab SW |
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 | ||
6 | # Test operation of shell commands relating to environment variables. | |
7 | ||
8 | import pytest | |
9 | ||
10 | # FIXME: This might be useful for other tests; | |
11 | # perhaps refactor it into ConsoleBase or some other state object? | |
12 | class StateTestEnv(object): | |
e8debf39 | 13 | """Container that represents the state of all U-Boot environment variables. |
5ab097ab SW |
14 | This enables quick determination of existant/non-existant variable |
15 | names. | |
e8debf39 | 16 | """ |
5ab097ab SW |
17 | |
18 | def __init__(self, u_boot_console): | |
e8debf39 | 19 | """Initialize a new StateTestEnv object. |
5ab097ab SW |
20 | |
21 | Args: | |
22 | u_boot_console: A U-Boot console. | |
23 | ||
24 | Returns: | |
25 | Nothing. | |
e8debf39 | 26 | """ |
5ab097ab SW |
27 | |
28 | self.u_boot_console = u_boot_console | |
29 | self.get_env() | |
30 | self.set_var = self.get_non_existent_var() | |
31 | ||
32 | def get_env(self): | |
e8debf39 | 33 | """Read all current environment variables from U-Boot. |
5ab097ab SW |
34 | |
35 | Args: | |
36 | None. | |
37 | ||
38 | Returns: | |
39 | Nothing. | |
e8debf39 | 40 | """ |
5ab097ab | 41 | |
7a8f8865 SW |
42 | if self.u_boot_console.config.buildconfig.get( |
43 | 'config_version_variable', 'n') == 'y': | |
da37f006 HS |
44 | with self.u_boot_console.disable_check('main_signon'): |
45 | response = self.u_boot_console.run_command('printenv') | |
46 | else: | |
47 | response = self.u_boot_console.run_command('printenv') | |
5ab097ab SW |
48 | self.env = {} |
49 | for l in response.splitlines(): | |
50 | if not '=' in l: | |
51 | continue | |
52 | (var, value) = l.strip().split('=', 1) | |
53 | self.env[var] = value | |
54 | ||
55 | def get_existent_var(self): | |
e8debf39 | 56 | """Return the name of an environment variable that exists. |
5ab097ab SW |
57 | |
58 | Args: | |
59 | None. | |
60 | ||
61 | Returns: | |
62 | The name of an environment variable. | |
e8debf39 | 63 | """ |
5ab097ab SW |
64 | |
65 | for var in self.env: | |
66 | return var | |
67 | ||
68 | def get_non_existent_var(self): | |
e8debf39 | 69 | """Return the name of an environment variable that does not exist. |
5ab097ab SW |
70 | |
71 | Args: | |
72 | None. | |
73 | ||
74 | Returns: | |
75 | The name of an environment variable. | |
e8debf39 | 76 | """ |
5ab097ab SW |
77 | |
78 | n = 0 | |
79 | while True: | |
80 | var = 'test_env_' + str(n) | |
81 | if var not in self.env: | |
82 | return var | |
83 | n += 1 | |
84 | ||
636f38d8 SW |
85 | ste = None |
86 | @pytest.fixture(scope='function') | |
5ab097ab | 87 | def state_test_env(u_boot_console): |
e8debf39 | 88 | """pytest fixture to provide a StateTestEnv object to tests.""" |
5ab097ab | 89 | |
636f38d8 SW |
90 | global ste |
91 | if not ste: | |
92 | ste = StateTestEnv(u_boot_console) | |
93 | return ste | |
5ab097ab SW |
94 | |
95 | def unset_var(state_test_env, var): | |
e8debf39 | 96 | """Unset an environment variable. |
5ab097ab SW |
97 | |
98 | This both executes a U-Boot shell command and updates a StateTestEnv | |
99 | object. | |
100 | ||
101 | Args: | |
db261f00 | 102 | state_test_env: The StateTestEnv object to update. |
5ab097ab SW |
103 | var: The variable name to unset. |
104 | ||
105 | Returns: | |
106 | Nothing. | |
e8debf39 | 107 | """ |
5ab097ab SW |
108 | |
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] | |
112 | ||
113 | def set_var(state_test_env, var, value): | |
e8debf39 | 114 | """Set an environment variable. |
5ab097ab SW |
115 | |
116 | This both executes a U-Boot shell command and updates a StateTestEnv | |
117 | object. | |
118 | ||
119 | Args: | |
db261f00 | 120 | state_test_env: The StateTestEnv object to update. |
5ab097ab SW |
121 | var: The variable name to set. |
122 | value: The value to set the variable to. | |
123 | ||
124 | Returns: | |
125 | Nothing. | |
e8debf39 | 126 | """ |
5ab097ab | 127 | |
3e229a83 SW |
128 | bc = state_test_env.u_boot_console.config.buildconfig |
129 | if bc.get('config_hush_parser', None): | |
130 | quote = '"' | |
131 | else: | |
132 | quote = '' | |
133 | if ' ' in value: | |
134 | pytest.skip('Space in variable value on non-Hush shell') | |
135 | ||
136 | state_test_env.u_boot_console.run_command( | |
137 | 'setenv %s %s%s%s' % (var, quote, value, quote)) | |
5ab097ab SW |
138 | state_test_env.env[var] = value |
139 | ||
140 | def validate_empty(state_test_env, var): | |
e8debf39 | 141 | """Validate that a variable is not set, using U-Boot shell commands. |
5ab097ab SW |
142 | |
143 | Args: | |
144 | var: The variable name to test. | |
145 | ||
146 | Returns: | |
147 | Nothing. | |
e8debf39 | 148 | """ |
5ab097ab SW |
149 | |
150 | response = state_test_env.u_boot_console.run_command('echo $%s' % var) | |
151 | assert response == '' | |
152 | ||
153 | def validate_set(state_test_env, var, value): | |
e8debf39 | 154 | """Validate that a variable is set, using U-Boot shell commands. |
5ab097ab SW |
155 | |
156 | Args: | |
157 | var: The variable name to test. | |
158 | value: The value the variable is expected to have. | |
159 | ||
160 | Returns: | |
161 | Nothing. | |
e8debf39 | 162 | """ |
5ab097ab SW |
163 | |
164 | # echo does not preserve leading, internal, or trailing whitespace in the | |
165 | # value. printenv does, and hence allows more complete testing. | |
166 | response = state_test_env.u_boot_console.run_command('printenv %s' % var) | |
167 | assert response == ('%s=%s' % (var, value)) | |
168 | ||
169 | def test_env_echo_exists(state_test_env): | |
e8debf39 | 170 | """Test echoing a variable that exists.""" |
5ab097ab SW |
171 | |
172 | var = state_test_env.get_existent_var() | |
173 | value = state_test_env.env[var] | |
174 | validate_set(state_test_env, var, value) | |
175 | ||
6b83c38d | 176 | @pytest.mark.buildconfigspec('cmd_echo') |
5ab097ab | 177 | def test_env_echo_non_existent(state_test_env): |
e8debf39 | 178 | """Test echoing a variable that doesn't exist.""" |
5ab097ab SW |
179 | |
180 | var = state_test_env.set_var | |
181 | validate_empty(state_test_env, var) | |
182 | ||
183 | def test_env_printenv_non_existent(state_test_env): | |
e8debf39 | 184 | """Test printenv error message for non-existant variables.""" |
5ab097ab SW |
185 | |
186 | var = state_test_env.set_var | |
187 | c = state_test_env.u_boot_console | |
188 | with c.disable_check('error_notification'): | |
189 | response = c.run_command('printenv %s' % var) | |
190 | assert(response == '## Error: "%s" not defined' % var) | |
191 | ||
6b83c38d | 192 | @pytest.mark.buildconfigspec('cmd_echo') |
5ab097ab | 193 | def test_env_unset_non_existent(state_test_env): |
e8debf39 | 194 | """Test unsetting a nonexistent variable.""" |
5ab097ab SW |
195 | |
196 | var = state_test_env.get_non_existent_var() | |
197 | unset_var(state_test_env, var) | |
198 | validate_empty(state_test_env, var) | |
199 | ||
200 | def test_env_set_non_existent(state_test_env): | |
e8debf39 | 201 | """Test set a non-existant variable.""" |
5ab097ab SW |
202 | |
203 | var = state_test_env.set_var | |
204 | value = 'foo' | |
205 | set_var(state_test_env, var, value) | |
206 | validate_set(state_test_env, var, value) | |
207 | ||
208 | def test_env_set_existing(state_test_env): | |
e8debf39 | 209 | """Test setting an existant variable.""" |
5ab097ab SW |
210 | |
211 | var = state_test_env.set_var | |
212 | value = 'bar' | |
213 | set_var(state_test_env, var, value) | |
214 | validate_set(state_test_env, var, value) | |
215 | ||
6b83c38d | 216 | @pytest.mark.buildconfigspec('cmd_echo') |
5ab097ab | 217 | def test_env_unset_existing(state_test_env): |
e8debf39 | 218 | """Test unsetting a variable.""" |
5ab097ab SW |
219 | |
220 | var = state_test_env.set_var | |
221 | unset_var(state_test_env, var) | |
222 | validate_empty(state_test_env, var) | |
223 | ||
224 | def test_env_expansion_spaces(state_test_env): | |
e8debf39 | 225 | """Test expanding a variable that contains a space in its value.""" |
5ab097ab SW |
226 | |
227 | var_space = None | |
228 | var_test = None | |
229 | try: | |
230 | var_space = state_test_env.get_non_existent_var() | |
231 | set_var(state_test_env, var_space, ' ') | |
232 | ||
233 | var_test = state_test_env.get_non_existent_var() | |
234 | value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals() | |
235 | set_var(state_test_env, var_test, value) | |
236 | value = ' 1 2 ' | |
237 | validate_set(state_test_env, var_test, value) | |
238 | finally: | |
239 | if var_space: | |
240 | unset_var(state_test_env, var_space) | |
241 | if var_test: | |
242 | unset_var(state_test_env, var_test) |