blob: ba6ab940060933145a0442bdfa695215c89b7e08 [file] [log] [blame]
Ronald Braunsteinfce43162024-02-02 12:37:20 -08001package tradefed_modules
2
3import (
4 "android/soong/android"
5 "android/soong/tradefed"
6 "encoding/json"
7 "fmt"
8
9 "github.com/google/blueprint"
10 "github.com/google/blueprint/proptools"
11)
12
13func init() {
14 RegisterTestModuleConfigBuildComponents(android.InitRegistrationContext)
15}
16
17// Register the license_kind module type.
18func RegisterTestModuleConfigBuildComponents(ctx android.RegistrationContext) {
19 ctx.RegisterModuleType("test_module_config", TestModuleConfigFactory)
20}
21
22type testModuleConfigModule struct {
23 android.ModuleBase
24 android.DefaultableModuleBase
25 base android.Module
26
27 tradefedProperties
28
29 // Our updated testConfig.
30 testConfig android.OutputPath
31 manifest android.InstallPath
32 provider tradefed.BaseTestProviderData
33}
34
35// Properties to list in Android.bp for this module.
36type tradefedProperties struct {
37 // Module name of the base test that we will run.
38 Base *string `android:"path,arch_variant"`
39
40 // Tradefed Options to add to tradefed xml when not one of the include or exclude filter or property.
41 // Sample: [{name: "TestRunnerOptionName", value: "OptionValue" }]
42 Options []tradefed.Option
43
44 // List of tradefed include annotations to add to tradefed xml, like "android.platform.test.annotations.Presubmit".
45 // Tests will be restricted to those matching an include_annotation or include_filter.
46 Include_annotations []string
47
48 // List of tradefed include annotations to add to tradefed xml, like "android.support.test.filters.FlakyTest".
49 // Tests matching an exclude annotation or filter will be skipped.
50 Exclude_annotations []string
51
52 // List of tradefed include filters to add to tradefed xml, like "fully.qualified.class#method".
53 // Tests will be restricted to those matching an include_annotation or include_filter.
54 Include_filters []string
55
56 // List of tradefed exclude filters to add to tradefed xml, like "fully.qualified.class#method".
57 // Tests matching an exclude annotation or filter will be skipped.
58 Exclude_filters []string
59
60 // List of compatibility suites (for example "cts", "vts") that the module should be
61 // installed into.
62 Test_suites []string
63}
64
65type dependencyTag struct {
66 blueprint.BaseDependencyTag
67 name string
68}
69
70var (
71 testModuleConfigTag = dependencyTag{name: "TestModuleConfigBase"}
72 pctx = android.NewPackageContext("android/soong/tradefed_modules")
73)
74
75func (m *testModuleConfigModule) InstallInTestcases() bool {
76 return true
77}
78
79func (m *testModuleConfigModule) DepsMutator(ctx android.BottomUpMutatorContext) {
80 ctx.AddDependency(ctx.Module(), testModuleConfigTag, *m.Base)
81}
82
83// Takes base's Tradefed Config xml file and generates a new one with the test properties
84// appeneded from this module.
85// Rewrite the name of the apk in "test-file-name" to be our module's name, rather than the original one.
86func (m *testModuleConfigModule) fixTestConfig(ctx android.ModuleContext, baseTestConfig android.Path) android.OutputPath {
87 // Test safe to do when no test_runner_options, but check for that earlier?
88 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", ctx.ModuleName()+".config")
89 rule := android.NewRuleBuilder(pctx, ctx)
90 command := rule.Command().BuiltTool("test_config_fixer").Input(baseTestConfig).Output(fixedConfig)
91 options := m.composeOptions()
92 if len(options) == 0 {
93 ctx.ModuleErrorf("Test options must be given when using test_module_config. Set include/exclude filter or annotation.")
94 }
95 xmlTestModuleConfigSnippet, _ := json.Marshal(options)
96 escaped := proptools.NinjaAndShellEscape(string(xmlTestModuleConfigSnippet))
97 command.FlagWithArg("--test-file-name=", ctx.ModuleName()+".apk").
98 FlagWithArg("--orig-test-file-name=", *m.tradefedProperties.Base+".apk").
99 FlagWithArg("--test-runner-options=", escaped)
100 rule.Build("fix_test_config", "fix test config")
101 return fixedConfig.OutputPath
102}
103
104// Convert --exclude_filters: ["filter1", "filter2"] ->
105// [ Option{Name: "exclude-filters", Value: "filter1"}, Option{Name: "exclude-filters", Value: "filter2"},
106// ... + include + annotations ]
107func (m *testModuleConfigModule) composeOptions() []tradefed.Option {
108 options := m.Options
109 for _, e := range m.Exclude_filters {
110 options = append(options, tradefed.Option{Name: "exclude-filter", Value: e})
111 }
112 for _, i := range m.Include_filters {
113 options = append(options, tradefed.Option{Name: "include-filter", Value: i})
114 }
115 for _, e := range m.Exclude_annotations {
116 options = append(options, tradefed.Option{Name: "exclude-annotation", Value: e})
117 }
118 for _, i := range m.Include_annotations {
119 options = append(options, tradefed.Option{Name: "include-annotation", Value: i})
120 }
121 return options
122}
123
124// Files to write and where they come from:
125// 1) test_module_config.manifest
126// - Leave a trail of where we got files from in case other tools need it.
127//
128// 2) $Module.config
129// - comes from base's module.config (AndroidTest.xml), and then we add our test_options.
130// provider.TestConfig
131// [rules via soong_app_prebuilt]
132//
133// 3) $ARCH/$Module.apk
134// - comes from base
135// provider.OutputFile
136// [rules via soong_app_prebuilt]
137//
138// 4) [bases data]
139// - We copy all of bases data (like helper apks) to our install directory too.
140// Since we call AndroidMkEntries on base, it will write out LOCAL_COMPATIBILITY_SUPPORT_FILES
141// with this data and app_prebuilt.mk will generate the rules to copy it from base.
142// We have no direct rules here to add to ninja.
143//
144// If we change to symlinks, this all needs to change.
145func (m *testModuleConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
146
147 ctx.VisitDirectDepsWithTag(testModuleConfigTag, func(dep android.Module) {
148 if provider, ok := android.OtherModuleProvider(ctx, dep, tradefed.BaseTestProviderKey); ok {
149 m.base = dep
150 m.provider = provider
151 } else {
152 ctx.ModuleErrorf("The base module '%s' does not provide test BaseTestProviderData. Only 'android_test' modules are supported.", dep.Name())
153 return
154 }
155 })
156
157 // 1) A manifest file listing the base.
158 installDir := android.PathForModuleInstall(ctx, ctx.ModuleName())
159 out := android.PathForModuleOut(ctx, "test_module_config.manifest")
160 android.WriteFileRule(ctx, out, fmt.Sprintf("{%q: %q}", "base", *m.tradefedProperties.Base))
161 ctx.InstallFile(installDir, out.Base(), out)
162
163 // 2) Module.config / AndroidTest.xml
164 // Note, there is still a "test-tag" element with base's module name, but
165 // Tradefed team says its ignored anyway.
166 m.testConfig = m.fixTestConfig(ctx, m.provider.TestConfig)
167
168 // 3) Write ARCH/Module.apk in testcases.
169 // Handled by soong_app_prebuilt and OutputFile in entries.
170 // Nothing to do here.
171
172 // 4) Copy base's data files.
173 // Handled by soong_app_prebuilt and LOCAL_COMPATIBILITY_SUPPORT_FILES.
174 // Nothing to do here.
175}
176
177func TestModuleConfigFactory() android.Module {
178 module := &testModuleConfigModule{}
179
180 module.AddProperties(&module.tradefedProperties)
181 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
182 android.InitDefaultableModule(module)
183
184 return module
185}
186
187// Implements android.AndroidMkEntriesProvider
188var _ android.AndroidMkEntriesProvider = (*testModuleConfigModule)(nil)
189
190func (m *testModuleConfigModule) AndroidMkEntries() []android.AndroidMkEntries {
191 // We rely on base writing LOCAL_COMPATIBILITY_SUPPORT_FILES for its data files
192 entriesList := m.base.(android.AndroidMkEntriesProvider).AndroidMkEntries()
193 entries := &entriesList[0]
194 entries.OutputFile = android.OptionalPathForPath(m.provider.OutputFile)
195 entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
196 entries.SetString("LOCAL_MODULE", m.Name()) // out module name, not base's
197
198 // Out update config file with extra options.
199 entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
200 entries.SetString("LOCAL_MODULE_TAGS", "tests")
201 // Required for atest to run additional tradefed testtypes
202 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", m.provider.HostRequiredModuleNames...)
203
204 // Clear the JNI symbols because they belong to base not us. Either transform the names in the string
205 // or clear the variable because we don't need it, we are copying bases libraries not generating
206 // new ones.
207 entries.SetString("LOCAL_SOONG_JNI_LIBS_SYMBOLS", "")
208
209 // Don't append to base's test-suites, only use the ones we define, so clear it before
210 // appending to it.
211 entries.SetString("LOCAL_COMPATIBILITY_SUITE", "")
212 if len(m.tradefedProperties.Test_suites) > 0 {
213 entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
214 } else {
215 entries.AddCompatibilityTestSuites("null-suite")
216 }
217 })
218 return entriesList
219}