diff --git a/apex/Android.bp b/apex/Android.bp index 8a2edebdb..1890b89c3 100644 --- a/apex/Android.bp +++ b/apex/Android.bp @@ -25,6 +25,7 @@ bootstrap_go_package { "deapexer.go", "key.go", "prebuilt.go", + "testing.go", "vndk.go", ], testSrcs: [ diff --git a/apex/apex_test.go b/apex/apex_test.go index 08f54f724..3fde14458 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -127,6 +127,94 @@ func withUnbundledBuild(_ map[string][]byte, config android.Config) { config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) } +var apexFixtureFactory = android.NewFixtureFactory( + &buildDir, + // General preparers in alphabetical order as test infrastructure will enforce correct + // registration order. + android.PrepareForTestWithAndroidBuildComponents, + bpf.PrepareForTestWithBpf, + cc.PrepareForTestWithCcBuildComponents, + java.PrepareForTestWithJavaDefaultModules, + prebuilt_etc.PrepareForTestWithPrebuiltEtc, + rust.PrepareForTestWithRustDefaultModules, + sh.PrepareForTestWithShBuildComponents, + + PrepareForTestWithApexBuildComponents, + + // Additional apex test specific preparers. + android.FixtureAddTextFile("system/sepolicy/Android.bp", ` + filegroup { + name: "myapex-file_contexts", + srcs: [ + "apex/myapex-file_contexts", + ], + } + `), + android.FixtureMergeMockFs(android.MockFS{ + "a.java": nil, + "PrebuiltAppFoo.apk": nil, + "PrebuiltAppFooPriv.apk": nil, + "build/make/target/product/security": nil, + "apex_manifest.json": nil, + "AndroidManifest.xml": nil, + "system/sepolicy/apex/myapex-file_contexts": nil, + "system/sepolicy/apex/myapex.updatable-file_contexts": nil, + "system/sepolicy/apex/myapex2-file_contexts": nil, + "system/sepolicy/apex/otherapex-file_contexts": nil, + "system/sepolicy/apex/com.android.vndk-file_contexts": nil, + "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil, + "mylib.cpp": nil, + "mytest.cpp": nil, + "mytest1.cpp": nil, + "mytest2.cpp": nil, + "mytest3.cpp": nil, + "myprebuilt": nil, + "my_include": nil, + "foo/bar/MyClass.java": nil, + "prebuilt.jar": nil, + "prebuilt.so": nil, + "vendor/foo/devkeys/test.x509.pem": nil, + "vendor/foo/devkeys/test.pk8": nil, + "testkey.x509.pem": nil, + "testkey.pk8": nil, + "testkey.override.x509.pem": nil, + "testkey.override.pk8": nil, + "vendor/foo/devkeys/testkey.avbpubkey": nil, + "vendor/foo/devkeys/testkey.pem": nil, + "NOTICE": nil, + "custom_notice": nil, + "custom_notice_for_static_lib": nil, + "testkey2.avbpubkey": nil, + "testkey2.pem": nil, + "myapex-arm64.apex": nil, + "myapex-arm.apex": nil, + "myapex.apks": nil, + "frameworks/base/api/current.txt": nil, + "framework/aidl/a.aidl": nil, + "build/make/core/proguard.flags": nil, + "build/make/core/proguard_basic_keeps.flags": nil, + "dummy.txt": nil, + "baz": nil, + "bar/baz": nil, + "testdata/baz": nil, + "AppSet.apks": nil, + "foo.rs": nil, + "libfoo.jar": nil, + "libbar.jar": nil, + }, + ), + + android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { + variables.DeviceVndkVersion = proptools.StringPtr("current") + variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test") + variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"} + variables.Platform_sdk_codename = proptools.StringPtr("Q") + variables.Platform_sdk_final = proptools.BoolPtr(false) + variables.Platform_version_active_codenames = []string{"Q"} + variables.Platform_vndk_version = proptools.StringPtr("VER") + }), +) + func testApexContext(_ *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) { bp = bp + ` filegroup { diff --git a/apex/testing.go b/apex/testing.go new file mode 100644 index 000000000..e662cade3 --- /dev/null +++ b/apex/testing.go @@ -0,0 +1,22 @@ +// Copyright 2021 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 apex + +import "android/soong/android" + +var PrepareForTestWithApexBuildComponents = android.GroupFixturePreparers( + android.FixtureRegisterWithContext(registerApexBuildComponents), + android.FixtureRegisterWithContext(registerApexKeyBuildComponents), +) diff --git a/sdk/cc_sdk_test.go b/sdk/cc_sdk_test.go index 6da135a32..28c9e9e28 100644 --- a/sdk/cc_sdk_test.go +++ b/sdk/cc_sdk_test.go @@ -21,7 +21,7 @@ import ( "android/soong/cc" ) -var ccTestFs = map[string][]byte{ +var ccTestFs = android.MockFS{ "Test.cpp": nil, "myinclude/Test.h": nil, "myinclude-android/AndroidTest.h": nil, @@ -32,7 +32,7 @@ var ccTestFs = map[string][]byte{ "some/where/stubslib.map.txt": nil, } -func testSdkWithCc(t *testing.T, bp string) *testSdkResult { +func testSdkWithCc(t *testing.T, bp string) *android.TestResult { t.Helper() return testSdkWithFs(t, bp, ccTestFs) } @@ -808,7 +808,15 @@ module_exports_snapshot { } func TestSnapshotWithSingleHostOsType(t *testing.T) { - ctx, config := testSdkContext(` + result := sdkFixtureFactory.Extend( + ccTestFs.AddToFixture(), + cc.PrepareForTestOnLinuxBionic, + android.FixtureModifyConfig(func(config android.Config) { + config.Targets[android.LinuxBionic] = []android.Target{ + {android.LinuxBionic, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", false}, + } + }), + ).RunTestWithBp(t, ` cc_defaults { name: "mydefaults", device_supported: false, @@ -849,9 +857,7 @@ func TestSnapshotWithSingleHostOsType(t *testing.T) { ], stl: "none", } - `, ccTestFs, []android.OsType{android.LinuxBionic}) - - result := runTests(t, ctx, config) + `) CheckSnapshot(result, "myexports", "", checkUnversionedAndroidBpContents(` diff --git a/sdk/java_sdk_test.go b/sdk/java_sdk_test.go index ef8e4a00d..40f276919 100644 --- a/sdk/java_sdk_test.go +++ b/sdk/java_sdk_test.go @@ -17,10 +17,11 @@ package sdk import ( "testing" + "android/soong/android" "android/soong/java" ) -func testSdkWithJava(t *testing.T, bp string) *testSdkResult { +func testSdkWithJava(t *testing.T, bp string) *android.TestResult { t.Helper() fs := map[string][]byte{ diff --git a/sdk/testing.go b/sdk/testing.go index 3fb27ca27..41280fc5c 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -29,10 +29,15 @@ import ( "android/soong/java" ) -func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsType) (*android.TestContext, android.Config) { - extraOsTypes = append(extraOsTypes, android.Android, android.Windows) +var sdkFixtureFactory = android.NewFixtureFactory( + &buildDir, + apex.PrepareForTestWithApexBuildComponents, + cc.PrepareForTestWithCcDefaultModules, + genrule.PrepareForTestWithGenRuleBuildComponents, + java.PrepareForTestWithJavaBuildComponents, + PrepareForTestWithSdkBuildComponents, - bp = bp + ` + android.FixtureAddTextFile("sdk/tests/Android.bp", ` apex_key { name: "myapex.key", public_key: "myapex.avbpubkey", @@ -43,9 +48,9 @@ func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsTy name: "myapex.cert", certificate: "myapex", } - ` + cc.GatherRequiredDepsForTest(extraOsTypes...) + `), - mockFS := map[string][]byte{ + android.FixtureMergeMockFs(map[string][]byte{ "build/make/target/product/security": nil, "apex_manifest.json": nil, "system/sepolicy/apex/myapex-file_contexts": nil, @@ -55,113 +60,33 @@ func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsTy "myapex.pem": nil, "myapex.x509.pem": nil, "myapex.pk8": nil, - } + }), - cc.GatherRequiredFilesForTest(mockFS) - - for k, v := range fs { - mockFS[k] = v - } - - config := android.TestArchConfig(buildDir, nil, bp, mockFS) - - // 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}, - } - - for _, extraOsType := range extraOsTypes { - switch extraOsType { - case android.LinuxBionic: - config.Targets[android.LinuxBionic] = []android.Target{ - {android.LinuxBionic, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", false}, - } + 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}, } - } + }), +) - ctx := android.NewTestArchContext(config) +var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers( + android.FixtureRegisterWithContext(registerModuleExportsBuildComponents), + android.FixtureRegisterWithContext(registerSdkBuildComponents), +) - // Enable androidmk support. - // * Register the singleton - // * Configure that we are inside make - // * Add CommonOS to ensure that androidmk processing works. - android.RegisterAndroidMkBuildComponents(ctx) - android.SetKatiEnabledForTests(config) - config.Targets[android.CommonOS] = []android.Target{ - {android.CommonOS, android.Arch{ArchType: android.Common}, android.NativeBridgeDisabled, "", "", true}, - } - - // from android package - android.RegisterPackageBuildComponents(ctx) - ctx.RegisterModuleType("filegroup", android.FileGroupFactory) - ctx.PreArchMutators(android.RegisterVisibilityRuleChecker) - ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) - ctx.PreArchMutators(android.RegisterComponentsMutator) - - android.RegisterPrebuiltMutators(ctx) - - // Register these after the prebuilt mutators have been registered to match what - // happens at runtime. - ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer) - ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer) - - // from java package - java.RegisterRequiredBuildComponentsForTest(ctx) - - // from genrule package - genrule.RegisterGenruleBuildComponents(ctx) - - // from cc package - cc.RegisterRequiredBuildComponentsForTest(ctx) - - // from apex package - ctx.RegisterModuleType("apex", apex.BundleFactory) - ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) - ctx.PostDepsMutators(apex.RegisterPostDepsMutators) - - // from this package - registerModuleExportsBuildComponents(ctx) - registerSdkBuildComponents(ctx) - - ctx.Register() - - return ctx, config -} - -func runTests(t *testing.T, ctx *android.TestContext, config android.Config) *testSdkResult { +func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult { t.Helper() - _, errs := ctx.ParseBlueprintsFiles(".") - android.FailIfErrored(t, errs) - _, errs = ctx.PrepareBuildActions(config) - android.FailIfErrored(t, errs) - return &testSdkResult{ - TestHelper: android.TestHelper{T: t}, - TestContext: ctx, - } -} - -func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { - t.Helper() - ctx, config := testSdkContext(bp, fs, nil) - return runTests(t, ctx, config) + return sdkFixtureFactory.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp)) } func testSdkError(t *testing.T, pattern, bp string) { t.Helper() - ctx, config := testSdkContext(bp, nil, nil) - _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) - if len(errs) > 0 { - android.FailIfNoMatchingErrors(t, pattern, errs) - return - } - _, errs = ctx.PrepareBuildActions(config) - if len(errs) > 0 { - android.FailIfNoMatchingErrors(t, pattern, errs) - return - } - - t.Fatalf("missing expected error %q (0 errors are returned)", pattern) + sdkFixtureFactory. + ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). + RunTestWithBp(t, bp) } func ensureListContains(t *testing.T, result []string, expected string) { @@ -179,22 +104,11 @@ func pathsToStrings(paths android.Paths) []string { return ret } -// Encapsulates result of processing an SDK definition. Provides support for -// checking the state of the build structures. -type testSdkResult struct { - android.TestHelper - *android.TestContext -} - -func (result *testSdkResult) Module(name string, variant string) android.Module { - return result.ModuleForTests(name, variant).Module() -} - // Analyse the sdk build rules to extract information about what it is doing. // // e.g. find the src/dest pairs from each cp command, the various zip files // generated, etc. -func getSdkSnapshotBuildInfo(result *testSdkResult, sdk *sdk) *snapshotBuildInfo { +func getSdkSnapshotBuildInfo(result *android.TestResult, sdk *sdk) *snapshotBuildInfo { info := &snapshotBuildInfo{ r: result, androidBpContents: sdk.GetAndroidBpContentsForTests(), @@ -263,7 +177,7 @@ func getSdkSnapshotBuildInfo(result *testSdkResult, sdk *sdk) *snapshotBuildInfo // 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. -func CheckSnapshot(result *testSdkResult, name string, dir string, checkers ...snapshotBuildInfoChecker) { +func CheckSnapshot(result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) { result.Helper() // The sdk CommonOS variant is always responsible for generating the snapshot. @@ -373,7 +287,7 @@ func checkMergeZips(expected ...string) snapshotBuildInfoChecker { // All source/input paths are relative either the build directory. All dest/output paths are // relative to the snapshot root directory. type snapshotBuildInfo struct { - r *testSdkResult + r *android.TestResult // The contents of the generated Android.bp file androidBpContents string