• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Helper functions for generating v2 TestPlan protos.
2
3These fns. are mostly thin wrappers around the proto constructors. Reading the
4proto definitions may give more complete explanations of messages, arguments,
5etc.
6"""
7
8# TODO(b/196070598): Remove "v2" from file name once these are the only used
9# test plan protos.
10
11# Needed to load from @proto. Add @unused to silence lint.
12load("//config/util/bindings/proto.star", "protos")
13load("//config/util/generate.star", "generate")
14load(
15    "@proto//chromiumos/test/api/plan.proto",
16    plan_pb = "chromiumos.test.api",
17)
18load(
19    "@proto//chromiumos/test/api/coverage_rule.proto",
20    coverage_rule_pb = "chromiumos.test.api",
21)
22load(
23    "@proto//chromiumos/test/api/dut_attribute.proto",
24    dut_attribute_pb = "chromiumos.test.api",
25)
26load(
27    "@proto//chromiumos/test/api/test_suite.proto",
28    test_suite_pb = "chromiumos.test.api",
29)
30
31def _hw_test_plan(id, hw_test_units):
32    """Creates a HWTestPlan proto.
33
34    Args:
35        id: Unique name to identify the test plan, recommend reverse domain name
36            rooted at chromeos: chromeos.release.wifi..
37        hw_test_units: List of HWTestUnit protos.
38    """
39    return plan_pb.HWTestPlan(
40        id = plan_pb.HWTestPlan.TestPlanId(
41            value = id,
42        ),
43        test_units = hw_test_units,
44    )
45
46def _hw_test_unit(coverage_rule):
47    """Creates a HWTestUnit proto.
48
49    Args:
50        coverage_rule: CoverageRule proto describing tests to run and DUTs to
51            target.
52    """
53    return plan_pb.HWTestUnit(
54        coverage_rule = coverage_rule,
55    )
56
57def _coverage_rule(test_suites, dut_targets, name = None):
58    """Creates a CoverageRule proto.
59
60    Args:
61        test_suites: List of TestSuite protos.
62        dut_targets: List of DutTarget protos.
63        name: Optional human-readable name for the rule.
64    """
65    return coverage_rule_pb.CoverageRule(
66        name = name,
67        test_suites = test_suites,
68        dut_targets = dut_targets,
69    )
70
71def _dut_target(dut_criteria, provision_config = None):
72    """Creates a DutTarget proto.
73
74    Args:
75        dut_criteria: A list of DutCriterion targetting a DUT.
76        provision_config: Optional provisioning config for the DUT.
77    """
78    return dut_attribute_pb.DutTarget(
79        criteria = dut_criteria,
80        provision_config = provision_config,
81    )
82
83def _dut_criterion(attribute_id, values):
84    """Creates a DutCriterion proto.
85
86    Args:
87        attribute_id: Globally unique string identifier for the attribute.
88        values: List of ANY acceptable string values to match (OR logic is
89        applied). Values are compared as simple strings case insensitively.
90    """
91    return dut_attribute_pb.DutCriterion(
92        attribute_id = dut_attribute_pb.DutAttribute.Id(
93            value = attribute_id,
94        ),
95        values = values,
96    )
97
98def _test_suite_with_tags(name, tags, excluded_tags = None):
99    """Creates a TestSuite proto with a tag-based selction criteria.
100
101    Tests are included if they meet the following:
102      - MATCH ALL of the [include] tags.
103      - DO NOT MATCH ANY of the exclude tags.
104
105    Tags must match exactly (i.e. no regexp, wildcard, etc. allowed).
106
107    Args:
108        name: Human-readable name for the test suite.
109        tags: Tags to include in the test case selection criteria.
110        excluded_tags: Tags to exclude in the test case selection criteria.
111    """
112    return test_suite_pb.TestSuite(
113        name = name,
114        test_case_tag_criteria = test_suite_pb.TestSuite.TestCaseTagCriteria(
115            tags = tags,
116            tag_excludes = excluded_tags,
117        ),
118    )
119
120def _generate(test_plan):
121    """Serialize a test plan to jsonproto.
122
123    The output file name is based on the id of the test plan.
124
125    Args:
126        test_plan: A HWTestPlan to serialize.
127    """
128    file_name = test_plan.id.value.lower().replace(" ", "_")
129    generate.generate(test_plan, "{}.jsonproto".format(file_name))
130
131test_plan_v2 = struct(
132    generate = _generate,
133    dut_criterion = _dut_criterion,
134    dut_target = _dut_target,
135    hw_test_plan = _hw_test_plan,
136    hw_test_unit = _hw_test_unit,
137    coverage_rule = _coverage_rule,
138    test_suite_with_tags = _test_suite_with_tags,
139)
140