Stephen Warren | d631062 | 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 | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 13 | """Container that represents the state of all U-Boot environment variables. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 14 | This enables quick determination of existant/non-existant variable |
| 15 | names. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 16 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 17 | |
| 18 | def __init__(self, u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 19 | """Initialize a new StateTestEnv object. |
Stephen Warren | d631062 | 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 | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 26 | """ |
Stephen Warren | d631062 | 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 | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 33 | """Read all current environment variables from U-Boot. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 34 | |
| 35 | Args: |
| 36 | None. |
| 37 | |
| 38 | Returns: |
| 39 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 41 | |
| 42 | response = self.u_boot_console.run_command('printenv') |
| 43 | self.env = {} |
| 44 | for l in response.splitlines(): |
| 45 | if not '=' in l: |
| 46 | continue |
| 47 | (var, value) = l.strip().split('=', 1) |
| 48 | self.env[var] = value |
| 49 | |
| 50 | def get_existent_var(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 51 | """Return the name of an environment variable that exists. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 52 | |
| 53 | Args: |
| 54 | None. |
| 55 | |
| 56 | Returns: |
| 57 | The name of an environment variable. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 58 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 59 | |
| 60 | for var in self.env: |
| 61 | return var |
| 62 | |
| 63 | def get_non_existent_var(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 64 | """Return the name of an environment variable that does not exist. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 65 | |
| 66 | Args: |
| 67 | None. |
| 68 | |
| 69 | Returns: |
| 70 | The name of an environment variable. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 71 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 72 | |
| 73 | n = 0 |
| 74 | while True: |
| 75 | var = 'test_env_' + str(n) |
| 76 | if var not in self.env: |
| 77 | return var |
| 78 | n += 1 |
| 79 | |
Stephen Warren | e1d24d0 | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 80 | ste = None |
| 81 | @pytest.fixture(scope='function') |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 82 | def state_test_env(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 83 | """pytest fixture to provide a StateTestEnv object to tests.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 84 | |
Stephen Warren | e1d24d0 | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 85 | global ste |
| 86 | if not ste: |
| 87 | ste = StateTestEnv(u_boot_console) |
| 88 | return ste |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 89 | |
| 90 | def unset_var(state_test_env, var): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 91 | """Unset an environment variable. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 92 | |
| 93 | This both executes a U-Boot shell command and updates a StateTestEnv |
| 94 | object. |
| 95 | |
| 96 | Args: |
Stephen Warren | 2b46724 | 2016-01-28 10:18:03 -0700 | [diff] [blame] | 97 | state_test_env: The StateTestEnv object to update. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 98 | var: The variable name to unset. |
| 99 | |
| 100 | Returns: |
| 101 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 102 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 103 | |
| 104 | state_test_env.u_boot_console.run_command('setenv %s' % var) |
| 105 | if var in state_test_env.env: |
| 106 | del state_test_env.env[var] |
| 107 | |
| 108 | def set_var(state_test_env, var, value): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 109 | """Set an environment variable. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 110 | |
| 111 | This both executes a U-Boot shell command and updates a StateTestEnv |
| 112 | object. |
| 113 | |
| 114 | Args: |
Stephen Warren | 2b46724 | 2016-01-28 10:18:03 -0700 | [diff] [blame] | 115 | state_test_env: The StateTestEnv object to update. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 116 | var: The variable name to set. |
| 117 | value: The value to set the variable to. |
| 118 | |
| 119 | Returns: |
| 120 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 121 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 122 | |
| 123 | state_test_env.u_boot_console.run_command('setenv %s "%s"' % (var, value)) |
| 124 | state_test_env.env[var] = value |
| 125 | |
| 126 | def validate_empty(state_test_env, var): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 127 | """Validate that a variable is not set, using U-Boot shell commands. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 128 | |
| 129 | Args: |
| 130 | var: The variable name to test. |
| 131 | |
| 132 | Returns: |
| 133 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 134 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 135 | |
| 136 | response = state_test_env.u_boot_console.run_command('echo $%s' % var) |
| 137 | assert response == '' |
| 138 | |
| 139 | def validate_set(state_test_env, var, value): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 140 | """Validate that a variable is set, using U-Boot shell commands. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 141 | |
| 142 | Args: |
| 143 | var: The variable name to test. |
| 144 | value: The value the variable is expected to have. |
| 145 | |
| 146 | Returns: |
| 147 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 148 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 149 | |
| 150 | # echo does not preserve leading, internal, or trailing whitespace in the |
| 151 | # value. printenv does, and hence allows more complete testing. |
| 152 | response = state_test_env.u_boot_console.run_command('printenv %s' % var) |
| 153 | assert response == ('%s=%s' % (var, value)) |
| 154 | |
| 155 | def test_env_echo_exists(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 156 | """Test echoing a variable that exists.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 157 | |
| 158 | var = state_test_env.get_existent_var() |
| 159 | value = state_test_env.env[var] |
| 160 | validate_set(state_test_env, var, value) |
| 161 | |
| 162 | def test_env_echo_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 163 | """Test echoing a variable that doesn't exist.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 164 | |
| 165 | var = state_test_env.set_var |
| 166 | validate_empty(state_test_env, var) |
| 167 | |
| 168 | def test_env_printenv_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 169 | """Test printenv error message for non-existant variables.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 170 | |
| 171 | var = state_test_env.set_var |
| 172 | c = state_test_env.u_boot_console |
| 173 | with c.disable_check('error_notification'): |
| 174 | response = c.run_command('printenv %s' % var) |
| 175 | assert(response == '## Error: "%s" not defined' % var) |
| 176 | |
| 177 | def test_env_unset_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 178 | """Test unsetting a nonexistent variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 179 | |
| 180 | var = state_test_env.get_non_existent_var() |
| 181 | unset_var(state_test_env, var) |
| 182 | validate_empty(state_test_env, var) |
| 183 | |
| 184 | def test_env_set_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 185 | """Test set a non-existant variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 186 | |
| 187 | var = state_test_env.set_var |
| 188 | value = 'foo' |
| 189 | set_var(state_test_env, var, value) |
| 190 | validate_set(state_test_env, var, value) |
| 191 | |
| 192 | def test_env_set_existing(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 193 | """Test setting an existant variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 194 | |
| 195 | var = state_test_env.set_var |
| 196 | value = 'bar' |
| 197 | set_var(state_test_env, var, value) |
| 198 | validate_set(state_test_env, var, value) |
| 199 | |
| 200 | def test_env_unset_existing(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 201 | """Test unsetting a variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 202 | |
| 203 | var = state_test_env.set_var |
| 204 | unset_var(state_test_env, var) |
| 205 | validate_empty(state_test_env, var) |
| 206 | |
| 207 | def test_env_expansion_spaces(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 208 | """Test expanding a variable that contains a space in its value.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 209 | |
| 210 | var_space = None |
| 211 | var_test = None |
| 212 | try: |
| 213 | var_space = state_test_env.get_non_existent_var() |
| 214 | set_var(state_test_env, var_space, ' ') |
| 215 | |
| 216 | var_test = state_test_env.get_non_existent_var() |
| 217 | value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals() |
| 218 | set_var(state_test_env, var_test, value) |
| 219 | value = ' 1 2 ' |
| 220 | validate_set(state_test_env, var_test, value) |
| 221 | finally: |
| 222 | if var_space: |
| 223 | unset_var(state_test_env, var_space) |
| 224 | if var_test: |
| 225 | unset_var(state_test_env, var_test) |