xref: /aosp_15_r20/external/autotest/server/cros/pvs/wrapper_job_with_name.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright 2022 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5PASSCRITERIA_PREFIX = 'passcriteria_'
6
7
8class wrapper_job_with_name():
9    """
10    wrapper_job_with_name wraps the server_job which is passed from the control
11    file. This is used to alter the name of the job, by using the job.record
12    function to represent the results of the internal "wrapped" job. Without
13    the wrapper job, all tests will have the generic wrapper name, regardless
14    of the test that is being wrapped.
15    """
16
17    def __init__(self,
18                 job,
19                 job_name,
20                 wrapper_url,
21                 args_dict,
22                 default_pass_criteria={}):
23        """
24        wrapper_job_with_name wraps the server_job which is passed from the control
25        file. This takes in the necessary parameters to execute that wrapper job.
26
27        @param job: server_job object in the control file
28        @param job_name: the name with which to overwrite the generic name
29        @param wrapper_url: the name of the generic wrapper to call
30        @param args_dict: passed in args_dict from the control file
31        @param default_pass_criteria: the pass criteria to use if none are given
32        """
33        self._job = job
34        self._name = job_name
35        self._wrapper_url = wrapper_url
36        self._pass_criteria = default_pass_criteria
37        self._args_dict = args_dict
38        self._parse_arg_dict_to_criteria()
39        self._args_dict["pass_criteria"] = self._pass_criteria
40
41    def run(self, host, test_to_wrap, wrap_args):
42        """
43        run executes the generic wrapper with the test_to_wrap and wrap_args
44        necessary for that test wrapper, as well as recording the outer job
45        state (which will overwrite the name of the test in results)
46
47        @param host: host from the control file
48        @param test_to_wrap: test name to pass into the generic wrapper
49        @param wrap_args: test args to pass into the generic wrapper
50        """
51        self._job.record('START', None, self._name)
52        if self._job.run_test(
53                self._wrapper_url,
54                host=host,
55                test_to_wrap=test_to_wrap,
56                wrap_args=wrap_args,
57                disable_sysinfo=True,
58                results_path=self._job._server_offload_dir_path(),
59                **self._args_dict):
60            self._job.record('INFO', None, self._name)
61            self._job.record('END GOOD', None, self._name, "")
62        else:
63            self._job.record('INFO', None, self._name)
64            self._job.record('END FAIL', None, self._name, "")
65
66    def _parse_arg_dict_to_criteria(self):
67        """
68        _parse_arg_dict_to_criteria takes in the generic arg dict and looks
69        for items with the prefix passcriteria_*. These are pass criteria values, and
70        will be appended to the dictionary passed into the wrapper run.
71
72        @param: arg_dict, the argv from autoserv parsed into a dict
73        """
74        for key in self._args_dict.keys():
75            if key.startswith(PASSCRITERIA_PREFIX):
76                self._pass_criteria[
77                        key[len(PASSCRITERIA_PREFIX):]] = self._args_dict[key]
78