Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 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): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 13 | """Container that represents the state of all U-Boot environment variables. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 14 | This enables quick determination of existant/non-existant variable |
| 15 | names. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 16 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 17 | |
| 18 | def __init__(self, u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 19 | """Initialize a new StateTestEnv object. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 20 | |
| 21 | Args: |
| 22 | u_boot_console: A U-Boot console. |
| 23 | |
| 24 | Returns: |
| 25 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 26 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 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): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 33 | """Read all current environment variables from U-Boot. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 34 | |
| 35 | Args: |
| 36 | None. |
| 37 | |
| 38 | Returns: |
| 39 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 41 | |
Stephen Warren | 7a8f886 | 2016-06-16 12:59:34 -0600 | [diff] [blame] | 42 | if self.u_boot_console.config.buildconfig.get( |
| 43 | 'config_version_variable', 'n') == 'y': |
Heiko Schocher | da37f00 | 2016-06-07 08:31:15 +0200 | [diff] [blame] | 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') |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 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): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 56 | """Return the name of an environment variable that exists. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 57 | |
| 58 | Args: |
| 59 | None. |
| 60 | |
| 61 | Returns: |
| 62 | The name of an environment variable. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 63 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 64 | |
| 65 | for var in self.env: |
| 66 | return var |
| 67 | |
| 68 | def get_non_existent_var(self): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 69 | """Return the name of an environment variable that does not exist. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 70 | |
| 71 | Args: |
| 72 | None. |
| 73 | |
| 74 | Returns: |
| 75 | The name of an environment variable. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 76 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 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 | |
Stephen Warren | 636f38d | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 85 | ste = None |
| 86 | @pytest.fixture(scope='function') |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 87 | def state_test_env(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 88 | """pytest fixture to provide a StateTestEnv object to tests.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 89 | |
Stephen Warren | 636f38d | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 90 | global ste |
| 91 | if not ste: |
| 92 | ste = StateTestEnv(u_boot_console) |
| 93 | return ste |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 94 | |
| 95 | def unset_var(state_test_env, var): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 96 | """Unset an environment variable. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 97 | |
| 98 | This both executes a U-Boot shell command and updates a StateTestEnv |
| 99 | object. |
| 100 | |
| 101 | Args: |
Stephen Warren | db261f0 | 2016-01-28 10:18:03 -0700 | [diff] [blame] | 102 | state_test_env: The StateTestEnv object to update. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 103 | var: The variable name to unset. |
| 104 | |
| 105 | Returns: |
| 106 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 107 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 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): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 114 | """Set an environment variable. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 115 | |
| 116 | This both executes a U-Boot shell command and updates a StateTestEnv |
| 117 | object. |
| 118 | |
| 119 | Args: |
Stephen Warren | db261f0 | 2016-01-28 10:18:03 -0700 | [diff] [blame] | 120 | state_test_env: The StateTestEnv object to update. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 121 | var: The variable name to set. |
| 122 | value: The value to set the variable to. |
| 123 | |
| 124 | Returns: |
| 125 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 126 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 127 | |
| 128 | state_test_env.u_boot_console.run_command('setenv %s "%s"' % (var, value)) |
| 129 | state_test_env.env[var] = value |
| 130 | |
| 131 | def validate_empty(state_test_env, var): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 132 | """Validate that a variable is not set, using U-Boot shell commands. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 133 | |
| 134 | Args: |
| 135 | var: The variable name to test. |
| 136 | |
| 137 | Returns: |
| 138 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 139 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 140 | |
| 141 | response = state_test_env.u_boot_console.run_command('echo $%s' % var) |
| 142 | assert response == '' |
| 143 | |
| 144 | def validate_set(state_test_env, var, value): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 145 | """Validate that a variable is set, using U-Boot shell commands. |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 146 | |
| 147 | Args: |
| 148 | var: The variable name to test. |
| 149 | value: The value the variable is expected to have. |
| 150 | |
| 151 | Returns: |
| 152 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 153 | """ |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 154 | |
| 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)) |
| 159 | |
| 160 | def test_env_echo_exists(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 161 | """Test echoing a variable that exists.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 162 | |
| 163 | var = state_test_env.get_existent_var() |
| 164 | value = state_test_env.env[var] |
| 165 | validate_set(state_test_env, var, value) |
| 166 | |
Michal Simek | 6b83c38 | 2017-05-15 14:29:02 +0200 | [diff] [blame] | 167 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 168 | def test_env_echo_non_existent(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 169 | """Test echoing a variable that doesn't exist.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 170 | |
| 171 | var = state_test_env.set_var |
| 172 | validate_empty(state_test_env, var) |
| 173 | |
| 174 | def test_env_printenv_non_existent(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 175 | """Test printenv error message for non-existant variables.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 176 | |
| 177 | var = state_test_env.set_var |
| 178 | c = state_test_env.u_boot_console |
| 179 | with c.disable_check('error_notification'): |
| 180 | response = c.run_command('printenv %s' % var) |
| 181 | assert(response == '## Error: "%s" not defined' % var) |
| 182 | |
Michal Simek | 6b83c38 | 2017-05-15 14:29:02 +0200 | [diff] [blame] | 183 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 184 | def test_env_unset_non_existent(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 185 | """Test unsetting a nonexistent variable.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 186 | |
| 187 | var = state_test_env.get_non_existent_var() |
| 188 | unset_var(state_test_env, var) |
| 189 | validate_empty(state_test_env, var) |
| 190 | |
| 191 | def test_env_set_non_existent(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 192 | """Test set a non-existant variable.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 193 | |
| 194 | var = state_test_env.set_var |
| 195 | value = 'foo' |
| 196 | set_var(state_test_env, var, value) |
| 197 | validate_set(state_test_env, var, value) |
| 198 | |
| 199 | def test_env_set_existing(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 200 | """Test setting an existant variable.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 201 | |
| 202 | var = state_test_env.set_var |
| 203 | value = 'bar' |
| 204 | set_var(state_test_env, var, value) |
| 205 | validate_set(state_test_env, var, value) |
| 206 | |
Michal Simek | 6b83c38 | 2017-05-15 14:29:02 +0200 | [diff] [blame] | 207 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 208 | def test_env_unset_existing(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 209 | """Test unsetting a variable.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 210 | |
| 211 | var = state_test_env.set_var |
| 212 | unset_var(state_test_env, var) |
| 213 | validate_empty(state_test_env, var) |
| 214 | |
| 215 | def test_env_expansion_spaces(state_test_env): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 216 | """Test expanding a variable that contains a space in its value.""" |
Stephen Warren | 5ab097a | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 217 | |
| 218 | var_space = None |
| 219 | var_test = None |
| 220 | try: |
| 221 | var_space = state_test_env.get_non_existent_var() |
| 222 | set_var(state_test_env, var_space, ' ') |
| 223 | |
| 224 | var_test = state_test_env.get_non_existent_var() |
| 225 | value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals() |
| 226 | set_var(state_test_env, var_test, value) |
| 227 | value = ' 1 2 ' |
| 228 | validate_set(state_test_env, var_test, value) |
| 229 | finally: |
| 230 | if var_space: |
| 231 | unset_var(state_test_env, var_space) |
| 232 | if var_test: |
| 233 | unset_var(state_test_env, var_test) |