blob: b0f5fdc62d4e24af79f648cd94794070ad39c271 [file] [log] [blame]
Paul Duffin82d90432019-11-30 09:24:33 +00001// Copyright 2019 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.
14
15package sdk
16
17import (
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000018 "fmt"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000019 "path/filepath"
Paul Duffin82d90432019-11-30 09:24:33 +000020 "strings"
21 "testing"
22
23 "android/soong/android"
24 "android/soong/apex"
25 "android/soong/cc"
Paul Duffind6ceb862021-03-04 23:02:31 +000026 "android/soong/genrule"
Paul Duffin82d90432019-11-30 09:24:33 +000027 "android/soong/java"
Paul Duffin51509a12022-04-06 12:48:09 +000028
29 "github.com/google/blueprint/proptools"
Paul Duffin82d90432019-11-30 09:24:33 +000030)
31
Paul Duffincf3ee2f2021-03-15 13:08:51 +000032// Prepare for running an sdk test with an apex.
33var prepareForSdkTestWithApex = android.GroupFixturePreparers(
Paul Duffin4a2a29c2021-03-09 22:27:13 +000034 apex.PrepareForTestWithApexBuildComponents,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000035 android.FixtureAddTextFile("sdk/tests/Android.bp", `
Colin Cross98be1bb2019-12-13 20:41:13 -080036 apex_key {
37 name: "myapex.key",
38 public_key: "myapex.avbpubkey",
39 private_key: "myapex.pem",
40 }
41
42 android_app_certificate {
43 name: "myapex.cert",
44 certificate: "myapex",
45 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000046 `),
Colin Cross98be1bb2019-12-13 20:41:13 -080047
Paul Duffin4a2a29c2021-03-09 22:27:13 +000048 android.FixtureMergeMockFs(map[string][]byte{
Martin Stjernholmcc776012020-07-07 03:22:21 +010049 "apex_manifest.json": nil,
50 "system/sepolicy/apex/myapex-file_contexts": nil,
51 "system/sepolicy/apex/myapex2-file_contexts": nil,
52 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
Paul Duffindb462dd2021-03-21 22:01:55 +000053 "sdk/tests/myapex.avbpubkey": nil,
54 "sdk/tests/myapex.pem": nil,
55 "sdk/tests/myapex.x509.pem": nil,
56 "sdk/tests/myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000057 }),
Paul Duffincf3ee2f2021-03-15 13:08:51 +000058)
59
60// Legacy preparer used for running tests within the sdk package.
61//
62// This includes everything that was needed to run any test in the sdk package prior to the
63// introduction of the test fixtures. Tests that are being converted to use fixtures directly
64// rather than through the testSdkError() and testSdkWithFs() methods should avoid using this and
65// instead should use the various preparers directly using android.GroupFixturePreparers(...) to
66// group them when necessary.
67//
68// deprecated
69var prepareForSdkTest = android.GroupFixturePreparers(
70 cc.PrepareForTestWithCcDefaultModules,
71 genrule.PrepareForTestWithGenRuleBuildComponents,
72 java.PrepareForTestWithJavaBuildComponents,
73 PrepareForTestWithSdkBuildComponents,
74
75 prepareForSdkTestWithApex,
Colin Cross98be1bb2019-12-13 20:41:13 -080076
Paul Duffin4a2a29c2021-03-09 22:27:13 +000077 cc.PrepareForTestOnWindows,
78 android.FixtureModifyConfig(func(config android.Config) {
79 // Add windows as a default disable OS to test behavior when some OS variants
80 // are disabled.
81 config.Targets[android.Windows] = []android.Target{
82 {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010083 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000084 }),
Paul Duffindb462dd2021-03-21 22:01:55 +000085
Paul Duffin51509a12022-04-06 12:48:09 +000086 // Add a build number file.
87 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
88 variables.BuildNumberFile = proptools.StringPtr(BUILD_NUMBER_FILE)
89 }),
90
Paul Duffindb462dd2021-03-21 22:01:55 +000091 // Make sure that every test provides all the source files.
92 android.PrepareForTestDisallowNonExistentPaths,
93 android.MockFS{
94 "Test.java": nil,
95 }.AddToFixture(),
Paul Duffin4a2a29c2021-03-09 22:27:13 +000096)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010097
Paul Duffin4a2a29c2021-03-09 22:27:13 +000098var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
99 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
100 android.FixtureRegisterWithContext(registerSdkBuildComponents),
101)
Paul Duffin82d90432019-11-30 09:24:33 +0000102
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000103func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000104 t.Helper()
Paul Duffin55e740e2021-03-29 02:06:19 +0100105 return android.GroupFixturePreparers(
106 prepareForSdkTest,
107 fs.AddToFixture(),
108 ).RunTestWithBp(t, bp)
Martin Stjernholm7feceb22020-07-11 04:33:29 +0100109}
110
Paul Duffin82d90432019-11-30 09:24:33 +0000111func testSdkError(t *testing.T, pattern, bp string) {
112 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +0000113 prepareForSdkTest.
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000114 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
115 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +0000116}
117
118func ensureListContains(t *testing.T, result []string, expected string) {
119 t.Helper()
120 if !android.InList(expected, result) {
121 t.Errorf("%q is not found in %v", expected, result)
122 }
123}
124
125func pathsToStrings(paths android.Paths) []string {
126 var ret []string
127 for _, p := range paths {
128 ret = append(ret, p.String())
129 }
130 return ret
131}
132
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000133// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000134//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000135// e.g. find the src/dest pairs from each cp command, the various zip files
136// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000137func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000138 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000139 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000140 r: result,
Paul Duffin43f7bf02021-05-05 22:00:51 +0100141 version: sdk.builderForTests.version,
Paul Duffind0759072021-02-17 11:23:00 +0000142 androidBpContents: sdk.GetAndroidBpContentsForTests(),
143 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
144 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc6ba1822022-05-06 09:38:02 +0000145 infoContents: sdk.GetInfoContentsForTests(),
Paul Duffin1822a0a2021-03-21 12:56:33 +0000146 snapshotTestCustomizations: map[snapshotTest]*snapshotTestCustomization{},
Paul Duffin39abf8f2021-09-24 14:58:27 +0100147 targetBuildRelease: sdk.builderForTests.targetBuildRelease,
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000148 }
149
150 buildParams := sdk.BuildParamsForTests()
151 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000152 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000153 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffin51509a12022-04-06 12:48:09 +0000154
155 seenBuildNumberFile := false
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000156 for _, bp := range buildParams {
157 switch bp.Rule.String() {
158 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000159 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000160 // Get destination relative to the snapshot root
161 dest := output.Rel()
162 src := android.NormalizePathForTesting(bp.Input)
163 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000164 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
Paul Duffin51509a12022-04-06 12:48:09 +0000165 // Don't include the build-number.txt file in the copy rules as that would break lots of
166 // tests, just verify that it is copied here as it should appear in every snapshot.
167 if output.Base() == BUILD_NUMBER_FILE {
168 seenBuildNumberFile = true
169 } else {
170 // Get source relative to build directory.
171 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
172 }
Paul Duffine1ddcc92020-03-03 16:01:26 +0000173 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000174 } else {
175 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000176 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000177
178 case repackageZip.String():
179 // Add the destdir to the snapshot contents as that is effectively where
180 // the content of the repackaged zip is copied.
181 dest := bp.Args["destdir"]
182 info.snapshotContents = append(info.snapshotContents, dest)
183
184 case zipFiles.String():
185 // This could be an intermediate zip file and not the actual output zip.
186 // In that case this will be overridden when the rule to merge the zips
187 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000188 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000189
190 case mergeZips.String():
191 // Copy the current outputZip to the intermediateZip.
192 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000193 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000194 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000195 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000196 info.intermediateZip, mergeInput)
197 }
198
199 // Override output zip (which was actually the intermediate zip file) with the actual
200 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000201 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000202
203 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000204 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000205 }
206 }
207
Paul Duffin51509a12022-04-06 12:48:09 +0000208 if !seenBuildNumberFile {
209 panic(fmt.Sprintf("Every snapshot must include the %s file", BUILD_NUMBER_FILE))
210 }
211
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000212 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000213 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000214
215 return info
216}
217
Paul Duffin1822a0a2021-03-21 12:56:33 +0000218// The enum of different sdk snapshot tests performed by CheckSnapshot.
219type snapshotTest int
220
221const (
222 // The enumeration of the different test configurations.
223 // A test with the snapshot/Android.bp file but without the original Android.bp file.
224 checkSnapshotWithoutSource snapshotTest = iota
225
226 // A test with both the original source and the snapshot, with the source preferred.
227 checkSnapshotWithSourcePreferred
228
229 // A test with both the original source and the snapshot, with the snapshot preferred.
230 checkSnapshotPreferredWithSource
231
232 // The directory into which the snapshot will be 'unpacked'.
233 snapshotSubDir = "snapshot"
234)
235
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000236// Check the snapshot build rules.
237//
238// Takes a list of functions which check different facets of the snapshot build rules.
239// Allows each test to customize what is checked without duplicating lots of code
240// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000241func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
242 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000243
Paul Duffin1356d8c2020-02-25 19:26:33 +0000244 // The sdk CommonOS variant is always responsible for generating the snapshot.
245 variant := android.CommonOS.Name
246
Paul Duffin981b94b2021-03-11 12:32:12 +0000247 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000248
Paul Duffin36474d32021-03-12 12:19:43 +0000249 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000250
251 // Check state of the snapshot build.
252 for _, checker := range checkers {
253 checker(snapshotBuildInfo)
254 }
255
256 // Make sure that the generated zip file is in the correct place.
257 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000258 if dir != "" {
259 dir = filepath.Clean(dir) + "/"
260 }
Paul Duffin43f7bf02021-05-05 22:00:51 +0100261 suffix := ""
262 if snapshotBuildInfo.version != soongSdkSnapshotVersionUnversioned {
263 suffix = "-" + snapshotBuildInfo.version
264 }
265
266 expectedZipPath := fmt.Sprintf(".intermediates/%s%s/%s/%s%s.zip", dir, name, variant, name, suffix)
267 android.AssertStringEquals(t, "Snapshot zip file in wrong place", expectedZipPath, actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000268
269 // Populate a mock filesystem with the files that would have been copied by
270 // the rules.
Paul Duffinc93c98e2021-03-20 01:32:50 +0000271 fs := android.MockFS{}
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000272 for _, dest := range snapshotBuildInfo.snapshotContents {
Paul Duffinc93c98e2021-03-20 01:32:50 +0000273 fs[filepath.Join(snapshotSubDir, dest)] = nil
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000274 }
Paul Duffinc93c98e2021-03-20 01:32:50 +0000275 fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000276
Paul Duffin39abf8f2021-09-24 14:58:27 +0100277 // If the generated snapshot builders not for the current release then it cannot be loaded by
278 // the current release.
279 currentBuildRelease := latestBuildRelease()
280 if snapshotBuildInfo.targetBuildRelease != currentBuildRelease {
281 return
282 }
283
Paul Duffin1822a0a2021-03-21 12:56:33 +0000284 // The preparers from the original source fixture.
285 sourcePreparers := result.Preparer()
Paul Duffinc93c98e2021-03-20 01:32:50 +0000286
Paul Duffin1822a0a2021-03-21 12:56:33 +0000287 // Preparer to combine the snapshot and the source.
288 snapshotPreparer := android.GroupFixturePreparers(sourcePreparers, fs.AddToFixture())
289
290 var runSnapshotTestWithCheckers = func(t *testing.T, testConfig snapshotTest, extraPreparer android.FixturePreparer) {
Paul Duffin023dba02021-04-22 01:45:29 +0100291 t.Helper()
Paul Duffin1822a0a2021-03-21 12:56:33 +0000292 customization := snapshotBuildInfo.snapshotTestCustomization(testConfig)
Paul Duffina57835e2021-04-19 13:23:06 +0100293 customizedPreparers := android.GroupFixturePreparers(customization.preparers...)
Paul Duffin1822a0a2021-03-21 12:56:33 +0000294
295 // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
296 // files the snapshot needs are actually copied into the snapshot.
297
298 // Run the snapshot with the snapshot preparer and the extra preparer, which must come after as
299 // it may need to modify parts of the MockFS populated by the snapshot preparer.
Paul Duffina57835e2021-04-19 13:23:06 +0100300 result := android.GroupFixturePreparers(snapshotPreparer, extraPreparer, customizedPreparers).
Paul Duffin1822a0a2021-03-21 12:56:33 +0000301 ExtendWithErrorHandler(customization.errorHandler).
302 RunTest(t)
303
304 // Perform any additional checks the test need on the result of processing the snapshot.
305 for _, checker := range customization.checkers {
306 checker(t, result)
307 }
308 }
309
Paul Duffinc93c98e2021-03-20 01:32:50 +0000310 t.Run("snapshot without source", func(t *testing.T) {
Paul Duffin1822a0a2021-03-21 12:56:33 +0000311 // Remove the source Android.bp file to make sure it works without.
312 removeSourceAndroidBp := android.FixtureModifyMockFS(func(fs android.MockFS) {
313 delete(fs, "Android.bp")
314 })
Paul Duffinc93c98e2021-03-20 01:32:50 +0000315
Paul Duffin1822a0a2021-03-21 12:56:33 +0000316 runSnapshotTestWithCheckers(t, checkSnapshotWithoutSource, removeSourceAndroidBp)
317 })
Paul Duffinc93c98e2021-03-20 01:32:50 +0000318
Paul Duffin1822a0a2021-03-21 12:56:33 +0000319 t.Run("snapshot with source preferred", func(t *testing.T) {
320 runSnapshotTestWithCheckers(t, checkSnapshotWithSourcePreferred, android.NullFixturePreparer)
321 })
322
323 t.Run("snapshot preferred with source", func(t *testing.T) {
324 // Replace the snapshot/Android.bp file with one where "prefer: false," has been replaced with
325 // "prefer: true,"
326 preferPrebuilts := android.FixtureModifyMockFS(func(fs android.MockFS) {
327 snapshotBpFile := filepath.Join(snapshotSubDir, "Android.bp")
328 unpreferred := string(fs[snapshotBpFile])
329 fs[snapshotBpFile] = []byte(strings.ReplaceAll(unpreferred, "prefer: false,", "prefer: true,"))
330 })
331
332 runSnapshotTestWithCheckers(t, checkSnapshotPreferredWithSource, preferPrebuilts)
Paul Duffinc93c98e2021-03-20 01:32:50 +0000333 })
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000334}
335
336type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
337
338// Check that the snapshot's generated Android.bp is correct.
339//
340// Both the expected and actual string are both trimmed before comparing.
341func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
342 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000343 info.t.Helper()
344 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000345 }
346}
347
Paul Duffind0759072021-02-17 11:23:00 +0000348// Check that the snapshot's unversioned generated Android.bp is correct.
349//
350// This func should be used to check the general snapshot generation code.
351//
352// Both the expected and actual string are both trimmed before comparing.
353func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
354 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000355 info.t.Helper()
356 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000357 }
358}
359
360// Check that the snapshot's versioned generated Android.bp is correct.
361//
362// This func should only be used to check the version specific snapshot generation code,
363// i.e. the encoding of version into module names and the generation of the _snapshot module. The
364// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
365// func.
366//
367// Both the expected and actual string are both trimmed before comparing.
368func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
369 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000370 info.t.Helper()
371 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000372 }
373}
374
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000375// Check that the snapshot's copy rules are correct.
376//
377// The copy rules are formatted as <src> -> <dest>, one per line and then compared
378// to the supplied expected string. Both the expected and actual string are trimmed
379// before comparing.
380func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
381 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000382 info.t.Helper()
383 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000384 }
385}
386
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000387func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
388 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000389 info.t.Helper()
390 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000391 }
392}
393
Paul Duffin3d1248c2020-04-09 00:10:17 +0100394// Check that the specified paths match the list of zips to merge with the intermediate zip.
395func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000396 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000397 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000398 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000399 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000400 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100401
Paul Duffin36474d32021-03-12 12:19:43 +0000402 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000403 }
404}
405
Paul Duffinc6ba1822022-05-06 09:38:02 +0000406// Check that the snapshot's info contents are ciorrect.
407//
408// Both the expected and actual string are both trimmed before comparing.
409func checkInfoContents(expected string) snapshotBuildInfoChecker {
410 return func(info *snapshotBuildInfo) {
411 info.t.Helper()
412 android.AssertTrimmedStringEquals(info.t, "info contents do not match",
413 expected, info.infoContents)
414 }
415}
416
Paul Duffin1822a0a2021-03-21 12:56:33 +0000417type resultChecker func(t *testing.T, result *android.TestResult)
418
Paul Duffina57835e2021-04-19 13:23:06 +0100419// snapshotTestPreparer registers a preparer that will be used to customize the specified
420// snapshotTest.
421func snapshotTestPreparer(snapshotTest snapshotTest, preparer android.FixturePreparer) snapshotBuildInfoChecker {
422 return func(info *snapshotBuildInfo) {
423 customization := info.snapshotTestCustomization(snapshotTest)
424 customization.preparers = append(customization.preparers, preparer)
425 }
426}
427
Paul Duffin1822a0a2021-03-21 12:56:33 +0000428// snapshotTestChecker registers a checker that will be run against the result of processing the
429// generated snapshot for the specified snapshotTest.
430func snapshotTestChecker(snapshotTest snapshotTest, checker resultChecker) snapshotBuildInfoChecker {
431 return func(info *snapshotBuildInfo) {
432 customization := info.snapshotTestCustomization(snapshotTest)
433 customization.checkers = append(customization.checkers, checker)
434 }
435}
436
437// snapshotTestErrorHandler registers an error handler to use when processing the snapshot
438// in the specific test case.
439//
440// Generally, the snapshot should work with all the test cases but some do not and just in case
441// there are a lot of issues to resolve, or it will take a lot of time this is a
442// get-out-of-jail-free card that allows progress to be made.
443//
444// deprecated: should only be used as a temporary workaround with an attached to do and bug.
445func snapshotTestErrorHandler(snapshotTest snapshotTest, handler android.FixtureErrorHandler) snapshotBuildInfoChecker {
446 return func(info *snapshotBuildInfo) {
447 customization := info.snapshotTestCustomization(snapshotTest)
448 customization.errorHandler = handler
449 }
450}
451
452// Encapsulates information provided by each test to customize a specific snapshotTest.
453type snapshotTestCustomization struct {
Paul Duffina57835e2021-04-19 13:23:06 +0100454 // Preparers that are used to customize the test fixture before running the test.
455 preparers []android.FixturePreparer
456
Paul Duffin1822a0a2021-03-21 12:56:33 +0000457 // Checkers that are run on the result of processing the preferred snapshot in a specific test
458 // case.
459 checkers []resultChecker
460
461 // Specify an error handler for when processing a specific test case.
462 //
463 // In some cases the generated snapshot cannot be used in a test configuration. Those cases are
464 // invariably bugs that need to be resolved but sometimes that can take a while. This provides a
465 // mechanism to temporarily ignore that error.
466 errorHandler android.FixtureErrorHandler
467}
468
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000469// Encapsulates information about the snapshot build structure in order to insulate tests from
470// knowing too much about internal structures.
471//
472// All source/input paths are relative either the build directory. All dest/output paths are
473// relative to the snapshot root directory.
474type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000475 t *testing.T
476
477 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000478 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000479
Paul Duffin43f7bf02021-05-05 22:00:51 +0100480 // The version of the generated snapshot.
481 //
482 // See snapshotBuilder.version for more information about this field.
483 version string
484
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000485 // The contents of the generated Android.bp file
486 androidBpContents string
487
Paul Duffind0759072021-02-17 11:23:00 +0000488 // The contents of the unversioned Android.bp file
489 androidUnversionedBpContents string
490
491 // The contents of the versioned Android.bp file
492 androidVersionedBpContents string
493
Paul Duffinc6ba1822022-05-06 09:38:02 +0000494 // The contents of the info file.
495 infoContents string
496
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000497 // The paths, relative to the snapshot root, of all files and directories copied into the
498 // snapshot.
499 snapshotContents []string
500
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000501 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
502 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000503 copyRules string
504
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000505 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
506 // per line, of the format src -> dest
507 otherCopyRules string
508
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000509 // The path to the intermediate zip, which is a zip created from the source files copied
510 // into the snapshot directory and which will be merged with other zips to form the final output.
511 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
512 intermediateZip string
513
514 // The paths to the zips to merge into the output zip, does not include the intermediate
515 // zip.
516 mergeZips []string
517
518 // The final output zip.
519 outputZip string
Paul Duffin1822a0a2021-03-21 12:56:33 +0000520
Paul Duffin39abf8f2021-09-24 14:58:27 +0100521 // The target build release.
522 targetBuildRelease *buildRelease
523
Paul Duffin1822a0a2021-03-21 12:56:33 +0000524 // The test specific customizations for each snapshot test.
525 snapshotTestCustomizations map[snapshotTest]*snapshotTestCustomization
526}
527
528// snapshotTestCustomization gets the test specific customization for the specified snapshotTest.
529//
530// If no customization was created previously then it creates a default customization.
531func (i *snapshotBuildInfo) snapshotTestCustomization(snapshotTest snapshotTest) *snapshotTestCustomization {
532 customization := i.snapshotTestCustomizations[snapshotTest]
533 if customization == nil {
534 customization = &snapshotTestCustomization{
535 errorHandler: android.FixtureExpectsNoErrors,
536 }
537 i.snapshotTestCustomizations[snapshotTest] = customization
538 }
539 return customization
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000540}