Add test-only and test-target fields to all_teams proto.

The `test-only` flag designates the module contains test-only, not
production code.  In order to generate code-coverage reports, we wanted
a way to filter out code (like java_library) that is test-only and
doesn't need to be in the report.
   The XXX_test modules will have test-only set automatically.
   For modules like `java_library`, users will be a able to set this in
   the Android.bp file.
   As a follow-up, I'll run some queries to find modules that are only
   reachable from top level test targets and mark them test-only as
   appropriate.

`test-only` is being added to the team.proto and will be written via the
`all_teams` target.

Currently, it is challenging to find "all top level test targets".
I'm adding another field to mark the target as a "top level test
target" if it is a XXX_test or XXX_test_host module.  The goal is to
mark all modules the user intended to run as a test, either with
tradefed or directly as a native test.

I added 'module-type/kind' to the proto so I can do some queries:

 gqui from  "flatten(out/soong/ownership/all_teams.pb, teams)" proto team.proto:AllTeams 'select teams.kind, count(*) where teams.top_level_target = true group by teams.kind'
+--------------+----------+
|  teams.kind  | count(*) |
+--------------+----------+
| android_test |     1379 |
| art_cc_test  |       56 |
| cc_benchmark |       68 |
| cc_fuzz      |      515 |
| cc_test      |     3519 |
| cc_test_host |        6 |
| java_fuzz    |        5 |
| java_test    |      773 |
+--------------+----------+

% gqui from  "flatten(~/aosp-main-with-phones/out/soong/ownership/all_teams.pb, teams)" proto team.proto:AllTeams 'select teams.kind ,count(*) where teams.test_only = true group by teams.kind'
+--------------------------+----------+
|        teams.kind        | count(*) |
+--------------------------+----------+
| android_test             |     1379 |
| android_test_helper_app  |     1678 |
| art_cc_test              |       56 |
| art_cc_test_library      |       13 |
| cc_benchmark             |       68 |
| cc_fuzz                  |      515 |
| cc_test                  |     3519 |
| cc_test_host             |        6 |
| cc_test_library          |      484 |
| java_library             |        2 |
| java_test                |      773 |
| java_test_helper_library |       29 |
+--------------------------+----------+

All modules can be seen here: https://docs.google.com/spreadsheets/d/1Zqbh7lDDdlI1xVmrN9fZ8bm8XD7EoORjjiPqbMvAKgQ/edit#gid=396553017

FOLLOW UP cls:
  *) Add more top level tests, like sh_test and python_test
  *) Add validation so that only modules currently marked test-only
     can depend on modules marked test-only
  *) Remove test_spec, code_metadata, TestModuleProviderKey: aosp/2928500

Test: go test ./java ./cc ./android
Test: m blueprint_tests
Test: m nothing --no-skip-soong-tests
    !!  android already failing on selects_test
Test: m all_teams  && gqui from  "flatten(out/soong/ownership/all_teams.pb, teams)"

Change-Id: Ib97dca60989aa9d7f000727c92af2e354926f072
This commit is contained in:
Ronald Braunstein
2024-03-27 06:46:47 -07:00
parent 8982b1c49e
commit c560309e30
8 changed files with 220 additions and 83 deletions

View File

@@ -24,9 +24,8 @@ import (
func TestAllTeams(t *testing.T) {
t.Parallel()
ctx := GroupFixturePreparers(
PrepareForTestWithTeamBuildComponents,
prepareForTestWithTeamAndFakes,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("fake", fakeModuleFactory)
ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory)
}),
).RunTestWithBp(t, `
@@ -51,6 +50,12 @@ func TestAllTeams(t *testing.T) {
fake {
name: "noteam",
test_only: true,
}
fake {
name: "test-and-team-and-top",
test_only: true,
team: "team2",
}
`)
@@ -59,16 +64,31 @@ func TestAllTeams(t *testing.T) {
// map of module name -> trendy team name.
actualTeams := make(map[string]*string)
actualTests := []string{}
actualTopLevelTests := []string{}
for _, teamProto := range teams.Teams {
actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId
if teamProto.GetTestOnly() {
actualTests = append(actualTests, teamProto.GetTargetName())
}
if teamProto.GetTopLevelTarget() {
actualTopLevelTests = append(actualTopLevelTests, teamProto.GetTargetName())
}
}
expectedTeams := map[string]*string{
"main_test": proto.String("cool_team"),
"tool": proto.String("22222"),
"noteam": nil,
"main_test": proto.String("cool_team"),
"tool": proto.String("22222"),
"test-and-team-and-top": proto.String("22222"),
"noteam": nil,
}
expectedTests := []string{
"noteam",
"test-and-team-and-top",
}
AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams)
AssertDeepEquals(t, "test matchup", expectedTests, actualTests)
}
func getTeamProtoOutput(t *testing.T, ctx *TestResult) *team_proto.AllTeams {
@@ -171,10 +191,9 @@ func TestPackageLookup(t *testing.T) {
} `
ctx := GroupFixturePreparers(
PrepareForTestWithTeamBuildComponents,
prepareForTestWithTeamAndFakes,
PrepareForTestWithPackageModule,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("fake", fakeModuleFactory)
ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory)
}),
FixtureAddTextFile("Android.bp", rootBp),
@@ -206,3 +225,33 @@ func TestPackageLookup(t *testing.T) {
}
AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams)
}
type fakeForTests struct {
ModuleBase
sourceProperties SourceProperties
}
func fakeFactory() Module {
module := &fakeForTests{}
module.AddProperties(&module.sourceProperties)
InitAndroidModule(module)
return module
}
var prepareForTestWithTeamAndFakes = GroupFixturePreparers(
FixtureRegisterWithContext(RegisterTeamBuildComponents),
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("fake", fakeFactory)
}),
)
func (f *fakeForTests) GenerateAndroidBuildActions(ctx ModuleContext) {
if Bool(f.sourceProperties.Test_only) {
SetProvider(ctx, TestOnlyProviderKey, TestModuleInformation{
TestOnly: Bool(f.sourceProperties.Test_only),
TopLevelTarget: false,
})
}
}