Support test fixtures in sdk package
Bug: 181070625 Test: m nothing Change-Id: Ifc96992e54c1b1d89a82b88ab27e555ae267a51e
This commit is contained in:
@@ -21,7 +21,7 @@ import (
|
|||||||
"android/soong/cc"
|
"android/soong/cc"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ccTestFs = map[string][]byte{
|
var ccTestFs = android.MockFS{
|
||||||
"Test.cpp": nil,
|
"Test.cpp": nil,
|
||||||
"myinclude/Test.h": nil,
|
"myinclude/Test.h": nil,
|
||||||
"myinclude-android/AndroidTest.h": nil,
|
"myinclude-android/AndroidTest.h": nil,
|
||||||
@@ -32,7 +32,7 @@ var ccTestFs = map[string][]byte{
|
|||||||
"some/where/stubslib.map.txt": nil,
|
"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()
|
t.Helper()
|
||||||
return testSdkWithFs(t, bp, ccTestFs)
|
return testSdkWithFs(t, bp, ccTestFs)
|
||||||
}
|
}
|
||||||
@@ -808,7 +808,15 @@ module_exports_snapshot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSnapshotWithSingleHostOsType(t *testing.T) {
|
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 {
|
cc_defaults {
|
||||||
name: "mydefaults",
|
name: "mydefaults",
|
||||||
device_supported: false,
|
device_supported: false,
|
||||||
@@ -849,9 +857,7 @@ func TestSnapshotWithSingleHostOsType(t *testing.T) {
|
|||||||
],
|
],
|
||||||
stl: "none",
|
stl: "none",
|
||||||
}
|
}
|
||||||
`, ccTestFs, []android.OsType{android.LinuxBionic})
|
`)
|
||||||
|
|
||||||
result := runTests(t, ctx, config)
|
|
||||||
|
|
||||||
CheckSnapshot(result, "myexports", "",
|
CheckSnapshot(result, "myexports", "",
|
||||||
checkUnversionedAndroidBpContents(`
|
checkUnversionedAndroidBpContents(`
|
||||||
|
@@ -17,10 +17,11 @@ package sdk
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"android/soong/android"
|
||||||
"android/soong/java"
|
"android/soong/java"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testSdkWithJava(t *testing.T, bp string) *testSdkResult {
|
func testSdkWithJava(t *testing.T, bp string) *android.TestResult {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
fs := map[string][]byte{
|
fs := map[string][]byte{
|
||||||
|
140
sdk/testing.go
140
sdk/testing.go
@@ -29,10 +29,15 @@ import (
|
|||||||
"android/soong/java"
|
"android/soong/java"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsType) (*android.TestContext, android.Config) {
|
var sdkFixtureFactory = android.NewFixtureFactory(
|
||||||
extraOsTypes = append(extraOsTypes, android.Android, android.Windows)
|
&buildDir,
|
||||||
|
apex.PrepareForTestWithApexBuildComponents,
|
||||||
|
cc.PrepareForTestWithCcDefaultModules,
|
||||||
|
genrule.PrepareForTestWithGenRuleBuildComponents,
|
||||||
|
java.PrepareForTestWithJavaBuildComponents,
|
||||||
|
PrepareForTestWithSdkBuildComponents,
|
||||||
|
|
||||||
bp = bp + `
|
android.FixtureAddTextFile("sdk/tests/Android.bp", `
|
||||||
apex_key {
|
apex_key {
|
||||||
name: "myapex.key",
|
name: "myapex.key",
|
||||||
public_key: "myapex.avbpubkey",
|
public_key: "myapex.avbpubkey",
|
||||||
@@ -43,9 +48,9 @@ func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsTy
|
|||||||
name: "myapex.cert",
|
name: "myapex.cert",
|
||||||
certificate: "myapex",
|
certificate: "myapex",
|
||||||
}
|
}
|
||||||
` + cc.GatherRequiredDepsForTest(extraOsTypes...)
|
`),
|
||||||
|
|
||||||
mockFS := map[string][]byte{
|
android.FixtureMergeMockFs(map[string][]byte{
|
||||||
"build/make/target/product/security": nil,
|
"build/make/target/product/security": nil,
|
||||||
"apex_manifest.json": nil,
|
"apex_manifest.json": nil,
|
||||||
"system/sepolicy/apex/myapex-file_contexts": 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.pem": nil,
|
||||||
"myapex.x509.pem": nil,
|
"myapex.x509.pem": nil,
|
||||||
"myapex.pk8": nil,
|
"myapex.pk8": nil,
|
||||||
}
|
}),
|
||||||
|
|
||||||
cc.GatherRequiredFilesForTest(mockFS)
|
|
||||||
|
|
||||||
for k, v := range fs {
|
|
||||||
mockFS[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
config := android.TestArchConfig(buildDir, nil, bp, mockFS)
|
|
||||||
|
|
||||||
|
cc.PrepareForTestOnWindows,
|
||||||
|
android.FixtureModifyConfig(func(config android.Config) {
|
||||||
// Add windows as a default disable OS to test behavior when some OS variants
|
// Add windows as a default disable OS to test behavior when some OS variants
|
||||||
// are disabled.
|
// are disabled.
|
||||||
config.Targets[android.Windows] = []android.Target{
|
config.Targets[android.Windows] = []android.Target{
|
||||||
{android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
|
{android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
for _, extraOsType := range extraOsTypes {
|
var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
|
||||||
switch extraOsType {
|
android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
|
||||||
case android.LinuxBionic:
|
android.FixtureRegisterWithContext(registerSdkBuildComponents),
|
||||||
config.Targets[android.LinuxBionic] = []android.Target{
|
)
|
||||||
{android.LinuxBionic, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", false},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := android.NewTestArchContext(config)
|
func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
|
||||||
|
|
||||||
// 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 {
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
_, errs := ctx.ParseBlueprintsFiles(".")
|
return sdkFixtureFactory.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSdkError(t *testing.T, pattern, bp string) {
|
func testSdkError(t *testing.T, pattern, bp string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
ctx, config := testSdkContext(bp, nil, nil)
|
sdkFixtureFactory.
|
||||||
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
|
||||||
if len(errs) > 0 {
|
RunTestWithBp(t, bp)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureListContains(t *testing.T, result []string, expected string) {
|
func ensureListContains(t *testing.T, result []string, expected string) {
|
||||||
@@ -179,22 +104,11 @@ func pathsToStrings(paths android.Paths) []string {
|
|||||||
return ret
|
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.
|
// 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
|
// e.g. find the src/dest pairs from each cp command, the various zip files
|
||||||
// generated, etc.
|
// generated, etc.
|
||||||
func getSdkSnapshotBuildInfo(result *testSdkResult, sdk *sdk) *snapshotBuildInfo {
|
func getSdkSnapshotBuildInfo(result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
|
||||||
info := &snapshotBuildInfo{
|
info := &snapshotBuildInfo{
|
||||||
r: result,
|
r: result,
|
||||||
androidBpContents: sdk.GetAndroidBpContentsForTests(),
|
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.
|
// 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
|
// Allows each test to customize what is checked without duplicating lots of code
|
||||||
// or proliferating check methods of different flavors.
|
// 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()
|
result.Helper()
|
||||||
|
|
||||||
// The sdk CommonOS variant is always responsible for generating the snapshot.
|
// 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
|
// All source/input paths are relative either the build directory. All dest/output paths are
|
||||||
// relative to the snapshot root directory.
|
// relative to the snapshot root directory.
|
||||||
type snapshotBuildInfo struct {
|
type snapshotBuildInfo struct {
|
||||||
r *testSdkResult
|
r *android.TestResult
|
||||||
|
|
||||||
// The contents of the generated Android.bp file
|
// The contents of the generated Android.bp file
|
||||||
androidBpContents string
|
androidBpContents string
|
||||||
|
Reference in New Issue
Block a user