2019-11-30 10:24:33 +01:00
|
|
|
// Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2019-11-29 21:45:22 +01:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2019-11-30 10:24:33 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/apex"
|
|
|
|
"android/soong/cc"
|
2021-03-05 00:02:31 +01:00
|
|
|
"android/soong/genrule"
|
2019-11-30 10:24:33 +01:00
|
|
|
"android/soong/java"
|
2022-04-06 14:48:09 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
2019-11-30 10:24:33 +01:00
|
|
|
)
|
|
|
|
|
2021-03-15 14:08:51 +01:00
|
|
|
// Prepare for running an sdk test with an apex.
|
|
|
|
var prepareForSdkTestWithApex = android.GroupFixturePreparers(
|
2021-03-09 23:27:13 +01:00
|
|
|
apex.PrepareForTestWithApexBuildComponents,
|
|
|
|
android.FixtureAddTextFile("sdk/tests/Android.bp", `
|
2019-12-14 05:41:13 +01:00
|
|
|
apex_key {
|
|
|
|
name: "myapex.key",
|
|
|
|
public_key: "myapex.avbpubkey",
|
|
|
|
private_key: "myapex.pem",
|
|
|
|
}
|
|
|
|
|
|
|
|
android_app_certificate {
|
|
|
|
name: "myapex.cert",
|
|
|
|
certificate: "myapex",
|
|
|
|
}
|
2021-03-09 23:27:13 +01:00
|
|
|
`),
|
2019-12-14 05:41:13 +01:00
|
|
|
|
2021-03-09 23:27:13 +01:00
|
|
|
android.FixtureMergeMockFs(map[string][]byte{
|
2020-07-07 04:22:21 +02:00
|
|
|
"apex_manifest.json": nil,
|
|
|
|
"system/sepolicy/apex/myapex-file_contexts": nil,
|
|
|
|
"system/sepolicy/apex/myapex2-file_contexts": nil,
|
|
|
|
"system/sepolicy/apex/mysdkapex-file_contexts": nil,
|
2021-03-21 23:01:55 +01:00
|
|
|
"sdk/tests/myapex.avbpubkey": nil,
|
|
|
|
"sdk/tests/myapex.pem": nil,
|
|
|
|
"sdk/tests/myapex.x509.pem": nil,
|
|
|
|
"sdk/tests/myapex.pk8": nil,
|
2021-03-09 23:27:13 +01:00
|
|
|
}),
|
2021-03-15 14:08:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Legacy preparer used for running tests within the sdk package.
|
|
|
|
//
|
|
|
|
// This includes everything that was needed to run any test in the sdk package prior to the
|
|
|
|
// introduction of the test fixtures. Tests that are being converted to use fixtures directly
|
|
|
|
// rather than through the testSdkError() and testSdkWithFs() methods should avoid using this and
|
|
|
|
// instead should use the various preparers directly using android.GroupFixturePreparers(...) to
|
|
|
|
// group them when necessary.
|
|
|
|
//
|
|
|
|
// deprecated
|
|
|
|
var prepareForSdkTest = android.GroupFixturePreparers(
|
|
|
|
cc.PrepareForTestWithCcDefaultModules,
|
|
|
|
genrule.PrepareForTestWithGenRuleBuildComponents,
|
|
|
|
java.PrepareForTestWithJavaBuildComponents,
|
|
|
|
PrepareForTestWithSdkBuildComponents,
|
|
|
|
|
|
|
|
prepareForSdkTestWithApex,
|
2021-03-09 23:27:13 +01:00
|
|
|
|
|
|
|
cc.PrepareForTestOnWindows,
|
|
|
|
android.FixtureModifyConfig(func(config android.Config) {
|
|
|
|
// Add windows as a default disable OS to test behavior when some OS variants
|
|
|
|
// are disabled.
|
|
|
|
config.Targets[android.Windows] = []android.Target{
|
|
|
|
{android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
|
2020-07-11 05:33:29 +02:00
|
|
|
}
|
2021-03-09 23:27:13 +01:00
|
|
|
}),
|
2021-03-21 23:01:55 +01:00
|
|
|
|
2022-04-06 14:48:09 +02:00
|
|
|
// Add a build number file.
|
|
|
|
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
|
|
|
|
variables.BuildNumberFile = proptools.StringPtr(BUILD_NUMBER_FILE)
|
|
|
|
}),
|
|
|
|
|
2021-03-21 23:01:55 +01:00
|
|
|
// Make sure that every test provides all the source files.
|
|
|
|
android.PrepareForTestDisallowNonExistentPaths,
|
|
|
|
android.MockFS{
|
|
|
|
"Test.java": nil,
|
|
|
|
}.AddToFixture(),
|
2021-03-09 23:27:13 +01:00
|
|
|
)
|
2019-11-30 10:24:33 +01:00
|
|
|
|
2021-03-09 23:27:13 +01:00
|
|
|
var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
|
|
|
|
android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
|
|
|
|
android.FixtureRegisterWithContext(registerSdkBuildComponents),
|
|
|
|
)
|
2019-11-30 10:24:33 +01:00
|
|
|
|
2021-03-09 23:27:13 +01:00
|
|
|
func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
|
2020-07-11 05:33:29 +02:00
|
|
|
t.Helper()
|
2021-03-29 03:06:19 +02:00
|
|
|
return android.GroupFixturePreparers(
|
|
|
|
prepareForSdkTest,
|
|
|
|
fs.AddToFixture(),
|
|
|
|
).RunTestWithBp(t, bp)
|
2020-07-11 05:33:29 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 10:24:33 +01:00
|
|
|
func testSdkError(t *testing.T, pattern, bp string) {
|
|
|
|
t.Helper()
|
2021-03-20 01:36:55 +01:00
|
|
|
prepareForSdkTest.
|
2021-03-09 23:27:13 +01:00
|
|
|
ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
|
|
|
|
RunTestWithBp(t, bp)
|
2019-11-30 10:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ensureListContains(t *testing.T, result []string, expected string) {
|
|
|
|
t.Helper()
|
|
|
|
if !android.InList(expected, result) {
|
|
|
|
t.Errorf("%q is not found in %v", expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathsToStrings(paths android.Paths) []string {
|
|
|
|
var ret []string
|
|
|
|
for _, p := range paths {
|
|
|
|
ret = append(ret, p.String())
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// Analyse the sdk build rules to extract information about what it is doing.
|
2021-03-11 18:41:01 +01:00
|
|
|
//
|
2019-11-29 21:45:22 +01:00
|
|
|
// e.g. find the src/dest pairs from each cp command, the various zip files
|
|
|
|
// generated, etc.
|
2021-03-12 13:19:43 +01:00
|
|
|
func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
|
2019-11-29 21:45:22 +01:00
|
|
|
info := &snapshotBuildInfo{
|
2021-03-12 13:19:43 +01:00
|
|
|
t: t,
|
2021-03-11 13:32:12 +01:00
|
|
|
r: result,
|
2021-05-05 23:00:51 +02:00
|
|
|
version: sdk.builderForTests.version,
|
2021-02-17 12:23:00 +01:00
|
|
|
androidBpContents: sdk.GetAndroidBpContentsForTests(),
|
|
|
|
androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
|
|
|
|
androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
|
2021-03-21 13:56:33 +01:00
|
|
|
snapshotTestCustomizations: map[snapshotTest]*snapshotTestCustomization{},
|
2021-09-24 15:58:27 +02:00
|
|
|
targetBuildRelease: sdk.builderForTests.targetBuildRelease,
|
2019-11-30 10:24:33 +01:00
|
|
|
}
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
buildParams := sdk.BuildParamsForTests()
|
|
|
|
copyRules := &strings.Builder{}
|
2020-02-27 14:45:35 +01:00
|
|
|
otherCopyRules := &strings.Builder{}
|
2020-03-03 17:01:26 +01:00
|
|
|
snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
|
2022-04-06 14:48:09 +02:00
|
|
|
|
|
|
|
seenBuildNumberFile := false
|
2019-11-29 21:45:22 +01:00
|
|
|
for _, bp := range buildParams {
|
|
|
|
switch bp.Rule.String() {
|
|
|
|
case android.Cp.String():
|
2020-03-03 17:01:26 +01:00
|
|
|
output := bp.Output
|
2020-02-27 14:45:35 +01:00
|
|
|
// Get destination relative to the snapshot root
|
|
|
|
dest := output.Rel()
|
|
|
|
src := android.NormalizePathForTesting(bp.Input)
|
|
|
|
// We differentiate between copy rules for the snapshot, and copy rules for the install file.
|
2020-03-03 17:01:26 +01:00
|
|
|
if strings.HasPrefix(output.String(), snapshotDirPrefix) {
|
2022-04-06 14:48:09 +02:00
|
|
|
// Don't include the build-number.txt file in the copy rules as that would break lots of
|
|
|
|
// tests, just verify that it is copied here as it should appear in every snapshot.
|
|
|
|
if output.Base() == BUILD_NUMBER_FILE {
|
|
|
|
seenBuildNumberFile = true
|
|
|
|
} else {
|
|
|
|
// Get source relative to build directory.
|
|
|
|
_, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
|
|
|
|
}
|
2020-03-03 17:01:26 +01:00
|
|
|
info.snapshotContents = append(info.snapshotContents, dest)
|
2020-02-27 14:45:35 +01:00
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
|
2020-03-03 17:01:26 +01:00
|
|
|
}
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
case repackageZip.String():
|
|
|
|
// Add the destdir to the snapshot contents as that is effectively where
|
|
|
|
// the content of the repackaged zip is copied.
|
|
|
|
dest := bp.Args["destdir"]
|
|
|
|
info.snapshotContents = append(info.snapshotContents, dest)
|
|
|
|
|
|
|
|
case zipFiles.String():
|
|
|
|
// This could be an intermediate zip file and not the actual output zip.
|
|
|
|
// In that case this will be overridden when the rule to merge the zips
|
|
|
|
// is processed.
|
2019-12-10 14:41:51 +01:00
|
|
|
info.outputZip = android.NormalizePathForTesting(bp.Output)
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
case mergeZips.String():
|
|
|
|
// Copy the current outputZip to the intermediateZip.
|
|
|
|
info.intermediateZip = info.outputZip
|
2019-12-10 14:41:51 +01:00
|
|
|
mergeInput := android.NormalizePathForTesting(bp.Input)
|
2019-11-29 21:45:22 +01:00
|
|
|
if info.intermediateZip != mergeInput {
|
2021-03-12 13:19:43 +01:00
|
|
|
t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
|
2019-11-29 21:45:22 +01:00
|
|
|
info.intermediateZip, mergeInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override output zip (which was actually the intermediate zip file) with the actual
|
|
|
|
// output zip.
|
2019-12-10 14:41:51 +01:00
|
|
|
info.outputZip = android.NormalizePathForTesting(bp.Output)
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
// Save the zips to be merged into the intermediate zip.
|
2019-12-10 14:41:51 +01:00
|
|
|
info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:48:09 +02:00
|
|
|
if !seenBuildNumberFile {
|
|
|
|
panic(fmt.Sprintf("Every snapshot must include the %s file", BUILD_NUMBER_FILE))
|
|
|
|
}
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
info.copyRules = copyRules.String()
|
2020-02-27 14:45:35 +01:00
|
|
|
info.otherCopyRules = otherCopyRules.String()
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:56:33 +01:00
|
|
|
// The enum of different sdk snapshot tests performed by CheckSnapshot.
|
|
|
|
type snapshotTest int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The enumeration of the different test configurations.
|
|
|
|
// A test with the snapshot/Android.bp file but without the original Android.bp file.
|
|
|
|
checkSnapshotWithoutSource snapshotTest = iota
|
|
|
|
|
|
|
|
// A test with both the original source and the snapshot, with the source preferred.
|
|
|
|
checkSnapshotWithSourcePreferred
|
|
|
|
|
|
|
|
// A test with both the original source and the snapshot, with the snapshot preferred.
|
|
|
|
checkSnapshotPreferredWithSource
|
|
|
|
|
|
|
|
// The directory into which the snapshot will be 'unpacked'.
|
|
|
|
snapshotSubDir = "snapshot"
|
|
|
|
)
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// Check the snapshot build rules.
|
|
|
|
//
|
|
|
|
// Takes a list of functions which check different facets of the snapshot build rules.
|
|
|
|
// Allows each test to customize what is checked without duplicating lots of code
|
|
|
|
// or proliferating check methods of different flavors.
|
2021-03-12 13:19:43 +01:00
|
|
|
func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
|
|
|
|
t.Helper()
|
2019-11-29 21:45:22 +01:00
|
|
|
|
2020-02-25 20:26:33 +01:00
|
|
|
// The sdk CommonOS variant is always responsible for generating the snapshot.
|
|
|
|
variant := android.CommonOS.Name
|
|
|
|
|
2021-03-11 13:32:12 +01:00
|
|
|
sdk := result.Module(name, variant).(*sdk)
|
2019-11-29 21:45:22 +01:00
|
|
|
|
2021-03-12 13:19:43 +01:00
|
|
|
snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
// Check state of the snapshot build.
|
|
|
|
for _, checker := range checkers {
|
|
|
|
checker(snapshotBuildInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the generated zip file is in the correct place.
|
|
|
|
actual := snapshotBuildInfo.outputZip
|
2019-12-05 15:31:48 +01:00
|
|
|
if dir != "" {
|
|
|
|
dir = filepath.Clean(dir) + "/"
|
|
|
|
}
|
2021-05-05 23:00:51 +02:00
|
|
|
suffix := ""
|
|
|
|
if snapshotBuildInfo.version != soongSdkSnapshotVersionUnversioned {
|
|
|
|
suffix = "-" + snapshotBuildInfo.version
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedZipPath := fmt.Sprintf(".intermediates/%s%s/%s/%s%s.zip", dir, name, variant, name, suffix)
|
|
|
|
android.AssertStringEquals(t, "Snapshot zip file in wrong place", expectedZipPath, actual)
|
2019-11-29 21:45:22 +01:00
|
|
|
|
|
|
|
// Populate a mock filesystem with the files that would have been copied by
|
|
|
|
// the rules.
|
2021-03-20 02:32:50 +01:00
|
|
|
fs := android.MockFS{}
|
2019-11-29 21:45:22 +01:00
|
|
|
for _, dest := range snapshotBuildInfo.snapshotContents {
|
2021-03-20 02:32:50 +01:00
|
|
|
fs[filepath.Join(snapshotSubDir, dest)] = nil
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
2021-03-20 02:32:50 +01:00
|
|
|
fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
|
|
|
|
|
2021-09-24 15:58:27 +02:00
|
|
|
// If the generated snapshot builders not for the current release then it cannot be loaded by
|
|
|
|
// the current release.
|
|
|
|
currentBuildRelease := latestBuildRelease()
|
|
|
|
if snapshotBuildInfo.targetBuildRelease != currentBuildRelease {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:56:33 +01:00
|
|
|
// The preparers from the original source fixture.
|
|
|
|
sourcePreparers := result.Preparer()
|
|
|
|
|
|
|
|
// Preparer to combine the snapshot and the source.
|
|
|
|
snapshotPreparer := android.GroupFixturePreparers(sourcePreparers, fs.AddToFixture())
|
|
|
|
|
|
|
|
var runSnapshotTestWithCheckers = func(t *testing.T, testConfig snapshotTest, extraPreparer android.FixturePreparer) {
|
2021-04-22 02:45:29 +02:00
|
|
|
t.Helper()
|
2021-03-21 13:56:33 +01:00
|
|
|
customization := snapshotBuildInfo.snapshotTestCustomization(testConfig)
|
2021-04-19 14:23:06 +02:00
|
|
|
customizedPreparers := android.GroupFixturePreparers(customization.preparers...)
|
2021-03-21 13:56:33 +01:00
|
|
|
|
|
|
|
// TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
|
|
|
|
// files the snapshot needs are actually copied into the snapshot.
|
|
|
|
|
|
|
|
// Run the snapshot with the snapshot preparer and the extra preparer, which must come after as
|
|
|
|
// it may need to modify parts of the MockFS populated by the snapshot preparer.
|
2021-04-19 14:23:06 +02:00
|
|
|
result := android.GroupFixturePreparers(snapshotPreparer, extraPreparer, customizedPreparers).
|
2021-03-21 13:56:33 +01:00
|
|
|
ExtendWithErrorHandler(customization.errorHandler).
|
|
|
|
RunTest(t)
|
|
|
|
|
|
|
|
// Perform any additional checks the test need on the result of processing the snapshot.
|
|
|
|
for _, checker := range customization.checkers {
|
|
|
|
checker(t, result)
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 02:32:50 +01:00
|
|
|
|
|
|
|
t.Run("snapshot without source", func(t *testing.T) {
|
2021-03-21 13:56:33 +01:00
|
|
|
// Remove the source Android.bp file to make sure it works without.
|
|
|
|
removeSourceAndroidBp := android.FixtureModifyMockFS(func(fs android.MockFS) {
|
|
|
|
delete(fs, "Android.bp")
|
|
|
|
})
|
|
|
|
|
|
|
|
runSnapshotTestWithCheckers(t, checkSnapshotWithoutSource, removeSourceAndroidBp)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("snapshot with source preferred", func(t *testing.T) {
|
|
|
|
runSnapshotTestWithCheckers(t, checkSnapshotWithSourcePreferred, android.NullFixturePreparer)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("snapshot preferred with source", func(t *testing.T) {
|
|
|
|
// Replace the snapshot/Android.bp file with one where "prefer: false," has been replaced with
|
|
|
|
// "prefer: true,"
|
|
|
|
preferPrebuilts := android.FixtureModifyMockFS(func(fs android.MockFS) {
|
|
|
|
snapshotBpFile := filepath.Join(snapshotSubDir, "Android.bp")
|
|
|
|
unpreferred := string(fs[snapshotBpFile])
|
|
|
|
fs[snapshotBpFile] = []byte(strings.ReplaceAll(unpreferred, "prefer: false,", "prefer: true,"))
|
|
|
|
})
|
|
|
|
|
|
|
|
runSnapshotTestWithCheckers(t, checkSnapshotPreferredWithSource, preferPrebuilts)
|
2021-03-20 02:32:50 +01:00
|
|
|
})
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
|
|
|
|
|
|
|
|
// Check that the snapshot's generated Android.bp is correct.
|
|
|
|
//
|
|
|
|
// Both the expected and actual string are both trimmed before comparing.
|
|
|
|
func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Helper()
|
|
|
|
android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 12:23:00 +01:00
|
|
|
// Check that the snapshot's unversioned generated Android.bp is correct.
|
|
|
|
//
|
|
|
|
// This func should be used to check the general snapshot generation code.
|
|
|
|
//
|
|
|
|
// Both the expected and actual string are both trimmed before comparing.
|
|
|
|
func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Helper()
|
|
|
|
android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
|
2021-02-17 12:23:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the snapshot's versioned generated Android.bp is correct.
|
|
|
|
//
|
|
|
|
// This func should only be used to check the version specific snapshot generation code,
|
|
|
|
// i.e. the encoding of version into module names and the generation of the _snapshot module. The
|
|
|
|
// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
|
|
|
|
// func.
|
|
|
|
//
|
|
|
|
// Both the expected and actual string are both trimmed before comparing.
|
|
|
|
func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Helper()
|
|
|
|
android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
|
2021-02-17 12:23:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// Check that the snapshot's copy rules are correct.
|
|
|
|
//
|
|
|
|
// The copy rules are formatted as <src> -> <dest>, one per line and then compared
|
|
|
|
// to the supplied expected string. Both the expected and actual string are trimmed
|
|
|
|
// before comparing.
|
|
|
|
func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Helper()
|
|
|
|
android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:45:35 +01:00
|
|
|
func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Helper()
|
|
|
|
android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
|
2020-02-27 14:45:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:10:17 +02:00
|
|
|
// Check that the specified paths match the list of zips to merge with the intermediate zip.
|
|
|
|
func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
|
2019-11-29 21:45:22 +01:00
|
|
|
return func(info *snapshotBuildInfo) {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Helper()
|
2019-11-29 21:45:22 +01:00
|
|
|
if info.intermediateZip == "" {
|
2021-03-12 13:19:43 +01:00
|
|
|
info.t.Errorf("No intermediate zip file was created")
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
2020-04-09 01:10:17 +02:00
|
|
|
|
2021-03-12 13:19:43 +01:00
|
|
|
android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
|
2019-11-29 21:45:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:56:33 +01:00
|
|
|
type resultChecker func(t *testing.T, result *android.TestResult)
|
|
|
|
|
2021-04-19 14:23:06 +02:00
|
|
|
// snapshotTestPreparer registers a preparer that will be used to customize the specified
|
|
|
|
// snapshotTest.
|
|
|
|
func snapshotTestPreparer(snapshotTest snapshotTest, preparer android.FixturePreparer) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
|
|
|
customization := info.snapshotTestCustomization(snapshotTest)
|
|
|
|
customization.preparers = append(customization.preparers, preparer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:56:33 +01:00
|
|
|
// snapshotTestChecker registers a checker that will be run against the result of processing the
|
|
|
|
// generated snapshot for the specified snapshotTest.
|
|
|
|
func snapshotTestChecker(snapshotTest snapshotTest, checker resultChecker) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
|
|
|
customization := info.snapshotTestCustomization(snapshotTest)
|
|
|
|
customization.checkers = append(customization.checkers, checker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// snapshotTestErrorHandler registers an error handler to use when processing the snapshot
|
|
|
|
// in the specific test case.
|
|
|
|
//
|
|
|
|
// Generally, the snapshot should work with all the test cases but some do not and just in case
|
|
|
|
// there are a lot of issues to resolve, or it will take a lot of time this is a
|
|
|
|
// get-out-of-jail-free card that allows progress to be made.
|
|
|
|
//
|
|
|
|
// deprecated: should only be used as a temporary workaround with an attached to do and bug.
|
|
|
|
func snapshotTestErrorHandler(snapshotTest snapshotTest, handler android.FixtureErrorHandler) snapshotBuildInfoChecker {
|
|
|
|
return func(info *snapshotBuildInfo) {
|
|
|
|
customization := info.snapshotTestCustomization(snapshotTest)
|
|
|
|
customization.errorHandler = handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encapsulates information provided by each test to customize a specific snapshotTest.
|
|
|
|
type snapshotTestCustomization struct {
|
2021-04-19 14:23:06 +02:00
|
|
|
// Preparers that are used to customize the test fixture before running the test.
|
|
|
|
preparers []android.FixturePreparer
|
|
|
|
|
2021-03-21 13:56:33 +01:00
|
|
|
// Checkers that are run on the result of processing the preferred snapshot in a specific test
|
|
|
|
// case.
|
|
|
|
checkers []resultChecker
|
|
|
|
|
|
|
|
// Specify an error handler for when processing a specific test case.
|
|
|
|
//
|
|
|
|
// In some cases the generated snapshot cannot be used in a test configuration. Those cases are
|
|
|
|
// invariably bugs that need to be resolved but sometimes that can take a while. This provides a
|
|
|
|
// mechanism to temporarily ignore that error.
|
|
|
|
errorHandler android.FixtureErrorHandler
|
|
|
|
}
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// Encapsulates information about the snapshot build structure in order to insulate tests from
|
|
|
|
// knowing too much about internal structures.
|
|
|
|
//
|
|
|
|
// All source/input paths are relative either the build directory. All dest/output paths are
|
|
|
|
// relative to the snapshot root directory.
|
|
|
|
type snapshotBuildInfo struct {
|
2021-03-12 13:19:43 +01:00
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
// The result from RunTest()
|
2021-03-09 23:27:13 +01:00
|
|
|
r *android.TestResult
|
2019-11-29 21:45:22 +01:00
|
|
|
|
2021-05-05 23:00:51 +02:00
|
|
|
// The version of the generated snapshot.
|
|
|
|
//
|
|
|
|
// See snapshotBuilder.version for more information about this field.
|
|
|
|
version string
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// The contents of the generated Android.bp file
|
|
|
|
androidBpContents string
|
|
|
|
|
2021-02-17 12:23:00 +01:00
|
|
|
// The contents of the unversioned Android.bp file
|
|
|
|
androidUnversionedBpContents string
|
|
|
|
|
|
|
|
// The contents of the versioned Android.bp file
|
|
|
|
androidVersionedBpContents string
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// The paths, relative to the snapshot root, of all files and directories copied into the
|
|
|
|
// snapshot.
|
|
|
|
snapshotContents []string
|
|
|
|
|
2020-02-27 14:45:35 +01:00
|
|
|
// A formatted representation of the src/dest pairs for a snapshot, one pair per line,
|
|
|
|
// of the format src -> dest
|
2019-11-29 21:45:22 +01:00
|
|
|
copyRules string
|
|
|
|
|
2020-02-27 14:45:35 +01:00
|
|
|
// A formatted representation of the src/dest pairs for files not in a snapshot, one pair
|
|
|
|
// per line, of the format src -> dest
|
|
|
|
otherCopyRules string
|
|
|
|
|
2019-11-29 21:45:22 +01:00
|
|
|
// The path to the intermediate zip, which is a zip created from the source files copied
|
|
|
|
// into the snapshot directory and which will be merged with other zips to form the final output.
|
|
|
|
// Is am empty string if there is no intermediate zip because there are no zips to merge in.
|
|
|
|
intermediateZip string
|
|
|
|
|
|
|
|
// The paths to the zips to merge into the output zip, does not include the intermediate
|
|
|
|
// zip.
|
|
|
|
mergeZips []string
|
|
|
|
|
|
|
|
// The final output zip.
|
|
|
|
outputZip string
|
2021-03-21 13:56:33 +01:00
|
|
|
|
2021-09-24 15:58:27 +02:00
|
|
|
// The target build release.
|
|
|
|
targetBuildRelease *buildRelease
|
|
|
|
|
2021-03-21 13:56:33 +01:00
|
|
|
// The test specific customizations for each snapshot test.
|
|
|
|
snapshotTestCustomizations map[snapshotTest]*snapshotTestCustomization
|
|
|
|
}
|
|
|
|
|
|
|
|
// snapshotTestCustomization gets the test specific customization for the specified snapshotTest.
|
|
|
|
//
|
|
|
|
// If no customization was created previously then it creates a default customization.
|
|
|
|
func (i *snapshotBuildInfo) snapshotTestCustomization(snapshotTest snapshotTest) *snapshotTestCustomization {
|
|
|
|
customization := i.snapshotTestCustomizations[snapshotTest]
|
|
|
|
if customization == nil {
|
|
|
|
customization = &snapshotTestCustomization{
|
|
|
|
errorHandler: android.FixtureExpectsNoErrors,
|
|
|
|
}
|
|
|
|
i.snapshotTestCustomizations[snapshotTest] = customization
|
|
|
|
}
|
|
|
|
return customization
|
2019-11-30 10:24:33 +01:00
|
|
|
}
|