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