blob: ff530437391b2d502d65440a24ffec8edb45fe3c [file] [log] [blame]
Ronald Braunsteinfce43162024-02-02 12:37:20 -08001// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package tradefed_modules
15
16import (
17 "android/soong/android"
18 "android/soong/java"
19 "strings"
20 "testing"
21)
22
23const bp = `
24 android_app {
25 name: "foo",
26 srcs: ["a.java"],
27 sdk_version: "current",
28 }
29
30 android_test_helper_app {
31 name: "HelperApp",
32 srcs: ["helper.java"],
33 }
34
35 android_test {
36 name: "base",
37 sdk_version: "current",
38 data: [":HelperApp", "data/testfile"],
39 }
40
41 test_module_config {
42 name: "derived_test",
43 base: "base",
44 exclude_filters: ["android.test.example.devcodelab.DevCodelabTest#testHelloFail"],
45 include_annotations: ["android.platform.test.annotations.LargeTest"],
46 }
47
48`
49
50// Ensure we create files needed and set the AndroidMkEntries needed
51func TestModuleConfigAndroidTest(t *testing.T) {
52
53 ctx := android.GroupFixturePreparers(
54 java.PrepareForTestWithJavaDefaultModules,
55 android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
56 ).RunTestWithBp(t, bp)
57
58 derived := ctx.ModuleForTests("derived_test", "android_common")
59 // Assert there are rules to create these files.
60 derived.Output("test_module_config.manifest")
61 derived.Output("test_config_fixer/derived_test.config")
62
63 // Ensure some basic rules exist.
64 ctx.ModuleForTests("base", "android_common").Output("package-res.apk")
65 entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
66
67 // Ensure some entries from base are there, specifically support files for data and helper apps.
68 assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
69
70 // And some new derived entries are there.
71 android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE_TAGS"], []string{"tests"})
72
73 // And ones we override
74 android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
75
76 android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
77}
78
79// Make sure we call test-config-fixer with the right args.
80func TestModuleConfigOptions(t *testing.T) {
81
82 ctx := android.GroupFixturePreparers(
83 java.PrepareForTestWithJavaDefaultModules,
84 android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
85 ).RunTestWithBp(t, bp)
86
87 // Check that we generate a rule to make a new AndroidTest.xml/Module.config file.
88 derived := ctx.ModuleForTests("derived_test", "android_common")
89 rule_cmd := derived.Rule("fix_test_config").RuleParams.Command
90 android.AssertStringDoesContain(t, "Bad FixConfig rule inputs", rule_cmd,
91 `--test-file-name=derived_test.apk --orig-test-file-name=base.apk --test-runner-options='[{"Name":"exclude-filter","Key":"","Value":"android.test.example.devcodelab.DevCodelabTest#testHelloFail"},{"Name":"include-annotation","Key":"","Value":"android.platform.test.annotations.LargeTest"}]'`)
92}
93
94// Ensure we error for a base we don't support.
95func TestModuleConfigBadBaseShouldFail(t *testing.T) {
96 badBp := `
97 java_test_host {
98 name: "base",
99 srcs: ["a.java"],
100 }
101
102 test_module_config {
103 name: "derived_test",
104 base: "base",
105 exclude_filters: ["android.test.example.devcodelab.DevCodelabTest#testHelloFail"],
106 include_annotations: ["android.platform.test.annotations.LargeTest"],
107 }`
108
109 ctx := android.GroupFixturePreparers(
110 java.PrepareForTestWithJavaDefaultModules,
111 android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
112 ).ExtendWithErrorHandler(
113 android.FixtureExpectsAtLeastOneErrorMatchingPattern("does not provide test BaseTestProviderData")).
114 RunTestWithBp(t, badBp)
115
116 ctx.ModuleForTests("derived_test", "android_common")
117}
118
119// Ensure we error for a base we don't support.
120func TestModuleConfigNoFiltersOrAnnotationsShouldFail(t *testing.T) {
121 badBp := `
122 android_test {
123 name: "base",
124 sdk_version: "current",
125 srcs: ["a.java"],
126 }
127
128 test_module_config {
129 name: "derived_test",
130 base: "base",
131 }`
132
133 ctx := android.GroupFixturePreparers(
134 java.PrepareForTestWithJavaDefaultModules,
135 android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
136 ).ExtendWithErrorHandler(
137 android.FixtureExpectsAtLeastOneErrorMatchingPattern("Test options must be given")).
138 RunTestWithBp(t, badBp)
139
140 ctx.ModuleForTests("derived_test", "android_common")
141}
142
143func TestModuleConfigMultipleDerivedTestsWriteDistinctMakeEntries(t *testing.T) {
144 multiBp := `
145 android_test {
146 name: "base",
147 sdk_version: "current",
148 srcs: ["a.java"],
149 }
150
151 test_module_config {
152 name: "derived_test",
153 base: "base",
154 include_annotations: ["android.platform.test.annotations.LargeTest"],
155 }
156
157 test_module_config {
158 name: "another_derived_test",
159 base: "base",
160 include_annotations: ["android.platform.test.annotations.LargeTest"],
161 }`
162
163 ctx := android.GroupFixturePreparers(
164 java.PrepareForTestWithJavaDefaultModules,
165 android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
166 ).RunTestWithBp(t, multiBp)
167
168 {
169 derived := ctx.ModuleForTests("derived_test", "android_common")
170 entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
171 // All these should be the same in both derived tests
172 assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
173 android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
174 // Except this one, which points to the updated tradefed xml file.
175 android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
176 // And this one, the module name.
177 android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE"], []string{"derived_test"})
178 }
179
180 {
181 derived := ctx.ModuleForTests("another_derived_test", "android_common")
182 entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
183 // All these should be the same in both derived tests
184 assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
185 android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
186 // Except this one, which points to the updated tradefed xml file.
187 android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "another_derived_test/android_common/test_config_fixer/another_derived_test.config")
188 // And this one, the module name.
189 android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE"], []string{"another_derived_test"})
190 }
191}
192
193// Use for situations where the entries map contains pairs: [srcPath:installedPath1, srcPath2:installedPath2]
194// and we want to compare the RHS of the pairs, i.e. installedPath1, installedPath2
195func assertEntryPairValues(t *testing.T, actual []string, expected []string) {
196 for i, e := range actual {
197 parts := strings.Split(e, ":")
198 if len(parts) != 2 {
199 t.Errorf("Expected entry to have a value delimited by :, received: %s", e)
200 return
201 }
202 android.AssertStringEquals(t, "", parts[1], expected[i])
203 }
204}