test/py: move U-Boot respawn trigger to the test core
authorStephen Warren <swarren@nvidia.com>
Fri, 22 Jan 2016 19:30:08 +0000 (12:30 -0700)
committerSimon Glass <sjg@chromium.org>
Fri, 29 Jan 2016 04:01:22 +0000 (21:01 -0700)
Prior to this change, U-Boot was lazilly (re-)spawned if/when a test
attempted to interact with it, and no active connection existed. This
approach was simple, yet had the disadvantage that U-Boot might be
spawned in the middle of a test function, e.g. after the test had already
performed actions such as creating data files, etc. In that case, this
could cause the log to contain the sequence (1) some test logs, (2)
U-Boot's boot process, (3) the rest of that test's logs. This isn't
optimally readable. This issue will affect the upcoming DFU and enhanced
UMS tests.

This change converts u_boot_console to be a function-scoped fixture, so
that pytest attempts to re-create the object for each test invocation.
This allows the fixture factory function to ensure that U-Boot is spawned
prior to every test. In practice, the same object is returned each time
so there is essentially no additional overhead due to this change.

This allows us to remove:

- The explicit ensure_spawned() call from test_sleep, since the core now
ensures that the spawn happens before the test code is executed.

- The laxy calls to ensure_spawned() in the u_boot_console_*
implementations.

The one downside is that test_env's "state_ttest_env" fixture must be
converted to a function-scoped fixture too, since a module-scoped fixture
cannot use a function-scoped fixture. To avoid overhead, we use the same
trick of returning the same object each time.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org>
test/py/conftest.py
test/py/tests/test_env.py
test/py/tests/test_sandbox_exit.py
test/py/tests/test_sleep.py
test/py/u_boot_console_base.py
test/py/u_boot_console_sandbox.py

index e1674df..38aa3f9 100644 (file)
@@ -227,7 +227,7 @@ def pytest_generate_tests(metafunc):
             vals = subconfig.get(fn + 's', [])
         metafunc.parametrize(fn, vals)
 
-@pytest.fixture(scope='session')
+@pytest.fixture(scope='function')
 def u_boot_console(request):
     '''Generate the value of a test's u_boot_console fixture.
 
@@ -238,6 +238,7 @@ def u_boot_console(request):
         The fixture value.
     '''
 
+    console.ensure_spawned()
     return console
 
 tests_not_run = set()
index a3e8dd3..557c3af 100644 (file)
@@ -77,11 +77,15 @@ class StateTestEnv(object):
                 return var
             n += 1
 
-@pytest.fixture(scope='module')
+ste = None
+@pytest.fixture(scope='function')
 def state_test_env(u_boot_console):
     '''pytest fixture to provide a StateTestEnv object to tests.'''
 
-    return StateTestEnv(u_boot_console)
+    global ste
+    if not ste:
+        ste = StateTestEnv(u_boot_console)
+    return ste
 
 def unset_var(state_test_env, var):
     '''Unset an environment variable.
index 2aa8eb4..1ec3607 100644 (file)
@@ -13,7 +13,6 @@ def test_reset(u_boot_console):
 
     u_boot_console.run_command('reset', wait_for_prompt=False)
     assert(u_boot_console.validate_exited())
-    u_boot_console.ensure_spawned()
 
 @pytest.mark.boardspec('sandbox')
 def test_ctrl_c(u_boot_console):
@@ -21,4 +20,3 @@ def test_ctrl_c(u_boot_console):
 
     u_boot_console.kill(signal.SIGINT)
     assert(u_boot_console.validate_exited())
-    u_boot_console.ensure_spawned()
index 64f1ddf..437b6bb 100644 (file)
@@ -9,10 +9,6 @@ def test_sleep(u_boot_console):
     '''Test the sleep command, and validate that it sleeps for approximately
     the correct amount of time.'''
 
-    # Do this before we time anything, to make sure U-Boot is already running.
-    # Otherwise, the system boot time is included in the time measurement.
-    u_boot_console.ensure_spawned()
-
     # 3s isn't too long, but is enough to cross a few second boundaries.
     sleep_time = 3
     tstart = time.time()
index 520f9a9..10fe3db 100644 (file)
@@ -144,8 +144,6 @@ class ConsoleBase(object):
                 command string and emitted the subsequent command prompts.
         '''
 
-        self.ensure_spawned()
-
         if self.at_prompt and \
                 self.at_prompt_logevt != self.logstream.logfile.cur_evt:
             self.logstream.write(self.prompt, implicit=True)
index 88b137e..eb84150 100644 (file)
@@ -51,7 +51,6 @@ class ConsoleSandbox(ConsoleBase):
             Nothing.
         '''
 
-        self.ensure_spawned()
         self.log.action('kill %d' % sig)
         self.p.kill(sig)