1#!/usr/bin/env python3
2#
3# Copyright 2023, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Tests to check if atest commands were executed with success exit codes."""
18
19import atest_integration_test
20
21
22class CommandSuccessTests(atest_integration_test.AtestTestCase):
23  """Test whether the atest commands run with success exit codes."""
24
25  def test_example_instrumentation_tests(self):
26    """Test if atest can run for the example instrumentation test path."""
27    test_path = 'platform_testing/tests/example/instrumentation'
28    self._verify_atest_command_success(
29        test_path, is_device_required=True, snapshot_include_paths=[test_path]
30    )
31
32  def test_csuite_harness_tests(self):
33    """Test if csuite-harness-tests command runs successfully."""
34    self._verify_atest_command_success(
35        'csuite-harness-tests --no-bazel-mode --host', is_device_required=False
36    )
37
38  def test_csuite_cli_test(self):
39    """Test if csuite_cli_test command runs successfully."""
40    self._verify_atest_command_success(
41        'csuite_cli_test --no-bazel-mode --host', is_device_required=False
42    )
43
44  def _verify_atest_command_success(
45      self,
46      cmd: str,
47      is_device_required: bool,
48      snapshot_include_paths: list[str] = None,
49  ) -> None:
50    """Verifies whether an Atest command run completed with exit code 0.
51
52    Args:
53        cmd: The atest command to run. Note to leave 'atest' or 'atest-dev' out
54          from the command.
55        is_device_required: Whether the test requires a device.
56        snapshot_include_paths: Any source paths needed to run the test in test
57          environment.
58    """
59    script = self.create_atest_script()
60
61    def build_step(
62        step_in: atest_integration_test.StepInput,
63    ) -> atest_integration_test.StepOutput:
64      self.run_atest_command(
65          cmd + ' -cb', step_in, include_device_serial=False
66      ).check_returncode()
67      step_out = self.create_step_output()
68      if snapshot_include_paths:
69        step_out.add_snapshot_include_paths(snapshot_include_paths)
70      return step_out
71
72    def test_step(step_in: atest_integration_test.StepInput) -> None:
73      self.run_atest_command(
74          cmd + ' -it', step_in, include_device_serial=is_device_required
75      ).check_returncode()
76
77    script.add_build_step(build_step)
78    script.add_test_step(test_step)
79    script.run()
80
81
82if __name__ == '__main__':
83  atest_integration_test.main()
84