Debian Codegraph @

crt/s2n/tests/integrationv2/fixtures.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import pytest
4
5 from processes import ManagedProcess
6 from providers import Provider, S2N
7
8 from common import ProviderOptions
9 from conftest import PATH_CONFIGURATION_KEY
10
11
12 @pytest.fixture
13 def managed_process(request: pytest.FixtureRequest):
14 """
15 Generic process manager. This could be used to launch any process as a background
16 task and cleanup when finished.
17
18 The reason a fixture is used, instead of creating a ManagedProcess() directly
19 from the test, is to control the life of the process. Using the fixture
20 allows cleanup after a test, even if a failure occurred.
21 """
22 processes = []
23 # Indicates whether a launch was aborted. If so, non-graceful shutdown is allowed
24 aborted = False
25
26 def _fn(
27 provider_class: Provider,
28 options: ProviderOptions,
29 timeout=5,
30 send_marker=None,
31 close_marker=None,
32 expect_stderr=None,
33 kill_marker=None,
34 send_with_newline=None,
35 ):
36 best_effort_mode = request.config.getoption("--best-effort-NOT-FOR-CI")
37 if best_effort_mode:
38 # modify the `aborted` field in the generator object
39 nonlocal aborted
40 available_providers = request.config.stash[PATH_CONFIGURATION_KEY]
41 if provider_class not in available_providers:
42 aborted = True
43 pytest.skip(f"{provider_class} is not available")
44
45 provider = provider_class(options)
46 cmd_line = provider.get_cmd_line()
47
48 if (
49 best_effort_mode
50 and provider_class is S2N
51 and not (cmd_line[0] == "s2nc" or cmd_line[0] == "s2nd")
52 ):
53 aborted = True
54 pytest.skip("s2nc_head or s2nd_head not supported for best-effort")
55
56 # The process will default to send markers in the providers.py file
57 # if not specified by a test.
58 if send_marker is not None:
59 provider.ready_to_send_input_marker = send_marker
60 if expect_stderr is None:
61 expect_stderr = provider.expect_stderr
62 if send_with_newline is None:
63 send_with_newline = provider.send_with_newline
64 p = ManagedProcess(
65 cmd_line,
66 provider.set_provider_ready,
67 name=provider.get_name(cmd_line),
68 wait_for_marker=provider.ready_to_test_marker,
69 send_marker_list=provider.ready_to_send_input_marker,
70 close_marker=close_marker,
71 data_source=options.data_to_send,
72 timeout=timeout,
73 env_overrides=options.env_overrides,
74 expect_stderr=expect_stderr,
75 kill_marker=kill_marker,
76 send_with_newline=send_with_newline,
77 )
78
79 processes.append(p)
80 with p.ready_condition:
81 p.start()
82 with provider._provider_ready_condition:
83 # Don't continue processing until the provider has indicated it is ready.
84 provider._provider_ready_condition.wait_for(
85 provider.is_provider_ready, timeout
86 )
87 return p
88
89 try:
90 yield _fn
91 except Exception as _:
92 # The ManagedProcess already prints information to stdout, so there
93 # is nothing to capture here.
94 pass
95 finally:
96 for p in processes:
97 # Always print the results
98 if p.results:
99 width = 90
100 padchar = "#"
101
102 print(padchar * width)
103 print(f" {p.cmd_line[0]} ".center(width, padchar))
104 print(padchar * width)
105
106 print(f"Command line:\n\t{' '.join(p.cmd_line)}")
107 print(f"Exit code:\n\t {p.results.exit_code}")
108 print("")
109
110 print(" Stdout ".center(width, padchar))
111 print(p.results.stdout.decode("utf-8", "backslashreplace"))
112 print("")
113
114 print(" Stderr ".center(width, padchar))
115 print(p.results.stderr.decode("utf-8", "backslashreplace"))
116 print("")
117
118 # Whether the processes succeeded or not, clean them up.
119 if aborted:
120 p.kill()
121 else:
122 p.join()