• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Functions related to test planning.
2
3See proto definitions for descriptions of arguments.
4"""
5
6load(
7    "@proto//chromiumos/config/api/test/plan/v1/plan.proto",
8    plan_pb = "chromiumos.config.api.test.plan.v1",
9)
10load("//config/util/generate.star", "generate")
11
12# TODO (b/237300719): determine if this needs to be fixed.
13# def _get_exclusion_type(type):
14#     """Get the exclusion type enum.
15
16#     Args:
17#         type: the exclusion type string.
18#     Returns:
19#         Exclusion type enum
20#     """
21
22#     if type == "PERMANENT":
23#         return plan_pb.Exclusion.PERMANENT
24#     if type == "TEMPORARY_NEW_TEST":
25#         return plan_pb.Exclusion.TEMPORARY_NEW_TEST
26#     if type == "TEMPORARY_PENDING_FIX":
27#         return plan_pb.Exclusion.TEMPORARY_PENDING_FIX
28
29#     return plan_pb.Exclusion.TYPE_UNSPECIFIED
30
31# def _get_exclusion_action(action):
32#     """Get the exclusion action enum.
33
34#     Args:
35#         action: the action type string.
36#     Returns:
37#         Exclusion action enum
38#     """
39#     if action == "DO_NOT_SCHEDULE":
40#         return plan_pb.Exclusion.DO_NOT_SCHEDULE
41#     if action == "MARK_NON_CRITICAL":
42#         return plan_pb.Exclusion.MARK_NON_CRITICAL
43
44#     return plan_pb.Exclusion.ACTION_UNSPECIFIED
45
46# def _create_exclusion(
47#         type = None,
48#         action = None,
49#         references = None):
50#     """Builds a test exclusion proto.
51
52#     Args:
53#         type: the exclusion type.
54#         action: the exclusion action.
55#         references: list of reference(s) associated.
56#     Returns:
57#         the Exclusion protobuf.
58#     """
59#     return plan_pb.Exclusion(
60#         type = _get_exclusion_type(type),
61#         action = _get_exclusion_action(action),
62#         references = references if references else None,
63#     )
64
65def _create_dut_criterion(attribute, values):
66    """Builds a DutCriterion proto (see proto for args)
67    """
68    return plan_pb.DutCriterion(
69        attribute = attribute,
70        values = values,
71    )
72
73def _create_coverage_rule(name, dut_criteria, exclusion = None):
74    """Builds a CoverageRule proto (see proto for args)
75    """
76    return plan_pb.CoverageRule(
77        name = name,
78        dut_criteria = dut_criteria,
79        exclusion = exclusion,
80    )
81
82def _append_unit(
83        units,
84        name,
85        suite_names = None,
86        coverage_rules = None,
87        exclusion = None):
88    """Appends a test unit proto to existing units
89
90    Args:
91        units: the existing unit(s) to append to
92        name: name of the unit.
93        suite_names: list of test suite names to run.
94        coverage_rules: list of coverage rules that must be fulfilled.
95        exclusion: optional Exclusion for the entire unit.
96    Returns:
97        the Unit protobuf.
98    """
99    units.append(_create_unit(name, suite_names, coverage_rules, exclusion))
100
101def _create_unit(
102        name,
103        suite_names = None,
104        coverage_rules = None,
105        exclusion = None):
106    """Builds a test unit proto.
107
108    Args:
109        name: name of the unit.
110        suite_names: list of test suite names to run.
111        coverage_rules: list of coverage rules that must be fulfilled.
112        exclusion: optional Exclusion for the entire unit.
113    Returns:
114        the Unit protobuf.
115    """
116    suites = None
117    if suite_names:
118        suites = [plan_pb.Unit.Suite(name = name) for name in suite_names]
119
120    return plan_pb.Unit(
121        name = name,
122        suites = suites,
123        coverage_rules = coverage_rules,
124        exclusion = exclusion,
125    )
126
127def _create_plan(name, units = None):
128    """Builds a test plan proto.
129
130    Args:
131        name: name of the plan.
132        units: list of test units to include in the plan.
133    Returns:
134        the Plan protobuf.
135    """
136    sorted_units = sorted(units, key = lambda u: u.name) if units else None
137    return plan_pb.Plan(name = name, units = sorted_units)
138
139def _create_spec(plans):
140    """Builds a test specification proto.
141
142    Args:
143        plans: list of test plans
144    Returns:
145        the Specification protobuf.
146    """
147    return plan_pb.Specification(plans = plans if plans else None)
148
149def _generate_plan(test_plan):
150    """Compiles the starkark to generate a jsonproto output file
151
152    Args:
153        test_plan: test plan
154    """
155    file_name = test_plan.name.lower().replace(" ", "_")
156    generate.generate(_create_spec([test_plan]), "%s.jsonproto" % file_name)
157
158def _read_plan(file_path):
159    """Reads the test plan Specification proto and returns it
160
161    Args:
162        file_path: Source jsonproto file path
163    """
164    return proto.from_jsonpb(
165        plan_pb.Specification,
166        io.read_file(file_path),
167    )
168
169test_plan = struct(
170    create = _create_plan,
171    generate = _generate_plan,
172    read = _read_plan,
173    dut_criterion = struct(
174        create = _create_dut_criterion,
175    ),
176    coverage_rule = struct(
177        create = _create_coverage_rule,
178    ),
179    unit = struct(
180        append = _append_unit,
181        create = _create_unit,
182    ),
183    spec = struct(
184        create = _create_spec,
185    ),
186)
187