Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 1 | // Copyright 2024 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 | package android |
| 15 | |
| 16 | import ( |
| 17 | "android/soong/android/team_proto" |
| 18 | "log" |
| 19 | "testing" |
| 20 | |
| 21 | "google.golang.org/protobuf/proto" |
| 22 | ) |
| 23 | |
| 24 | func TestAllTeams(t *testing.T) { |
| 25 | t.Parallel() |
| 26 | ctx := GroupFixturePreparers( |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 27 | prepareForTestWithTeamAndFakes, |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 28 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 29 | ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory) |
| 30 | }), |
| 31 | ).RunTestWithBp(t, ` |
| 32 | fake { |
| 33 | name: "main_test", |
| 34 | team: "someteam", |
| 35 | } |
| 36 | team { |
| 37 | name: "someteam", |
| 38 | trendy_team_id: "cool_team", |
| 39 | } |
| 40 | |
| 41 | team { |
| 42 | name: "team2", |
| 43 | trendy_team_id: "22222", |
| 44 | } |
| 45 | |
| 46 | fake { |
| 47 | name: "tool", |
| 48 | team: "team2", |
| 49 | } |
| 50 | |
| 51 | fake { |
| 52 | name: "noteam", |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 53 | test_only: true, |
| 54 | } |
| 55 | fake { |
| 56 | name: "test-and-team-and-top", |
| 57 | test_only: true, |
| 58 | team: "team2", |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 59 | } |
| 60 | `) |
| 61 | |
| 62 | var teams *team_proto.AllTeams |
| 63 | teams = getTeamProtoOutput(t, ctx) |
| 64 | |
| 65 | // map of module name -> trendy team name. |
| 66 | actualTeams := make(map[string]*string) |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 67 | actualTests := []string{} |
| 68 | actualTopLevelTests := []string{} |
| 69 | |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 70 | for _, teamProto := range teams.Teams { |
| 71 | actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 72 | if teamProto.GetTestOnly() { |
| 73 | actualTests = append(actualTests, teamProto.GetTargetName()) |
| 74 | } |
| 75 | if teamProto.GetTopLevelTarget() { |
| 76 | actualTopLevelTests = append(actualTopLevelTests, teamProto.GetTargetName()) |
| 77 | } |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 78 | } |
| 79 | expectedTeams := map[string]*string{ |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 80 | "main_test": proto.String("cool_team"), |
| 81 | "tool": proto.String("22222"), |
| 82 | "test-and-team-and-top": proto.String("22222"), |
| 83 | "noteam": nil, |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 86 | expectedTests := []string{ |
| 87 | "noteam", |
| 88 | "test-and-team-and-top", |
| 89 | } |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 90 | AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams) |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 91 | AssertDeepEquals(t, "test matchup", expectedTests, actualTests) |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func getTeamProtoOutput(t *testing.T, ctx *TestResult) *team_proto.AllTeams { |
| 95 | teams := new(team_proto.AllTeams) |
| 96 | config := ctx.SingletonForTests("all_teams") |
| 97 | allOutputs := config.AllOutputs() |
| 98 | |
| 99 | protoPath := allOutputs[0] |
| 100 | |
| 101 | out := config.MaybeOutput(protoPath) |
| 102 | outProto := []byte(ContentFromFileRuleForTests(t, ctx.TestContext, out)) |
| 103 | if err := proto.Unmarshal(outProto, teams); err != nil { |
| 104 | log.Fatalln("Failed to parse teams proto:", err) |
| 105 | } |
| 106 | return teams |
| 107 | } |
| 108 | |
| 109 | // Android.bp |
| 110 | // |
| 111 | // team: team_top |
| 112 | // |
| 113 | // # dir1 has no modules with teams, |
| 114 | // # but has a dir with no Android.bp |
| 115 | // dir1/Android.bp |
| 116 | // |
| 117 | // module_dir1 |
| 118 | // |
| 119 | // # dirs without and Android.bp should be fine. |
| 120 | // dir1/dir2/dir3/Android.bp |
| 121 | // |
| 122 | // package {} |
| 123 | // module_dir123 |
| 124 | // |
| 125 | // teams_dir/Android.bp |
| 126 | // |
| 127 | // module_with_team1: team1 |
| 128 | // team1: 111 |
| 129 | // |
| 130 | // # team comes from upper package default |
| 131 | // teams_dir/deeper/Android.bp |
| 132 | // |
| 133 | // module2_with_team1: team1 |
| 134 | // |
| 135 | // package_defaults/Android.bp |
| 136 | // package_defaults/pd2/Android.bp |
| 137 | // |
| 138 | // package{ default_team: team_top} |
| 139 | // module_pd2 ## should get team_top |
| 140 | // |
| 141 | // package_defaults/pd2/pd3/Android.bp |
| 142 | // |
| 143 | // module_pd3 ## should get team_top |
| 144 | func TestPackageLookup(t *testing.T) { |
| 145 | t.Parallel() |
| 146 | rootBp := ` |
| 147 | team { |
| 148 | name: "team_top", |
| 149 | trendy_team_id: "trendy://team_top", |
| 150 | } ` |
| 151 | |
| 152 | dir1Bp := ` |
| 153 | fake { |
| 154 | name: "module_dir1", |
| 155 | } ` |
| 156 | dir3Bp := ` |
| 157 | package {} |
| 158 | fake { |
| 159 | name: "module_dir123", |
| 160 | } ` |
| 161 | teamsDirBp := ` |
| 162 | fake { |
| 163 | name: "module_with_team1", |
| 164 | team: "team1" |
| 165 | |
| 166 | } |
| 167 | team { |
| 168 | name: "team1", |
| 169 | trendy_team_id: "111", |
| 170 | } ` |
| 171 | teamsDirDeeper := ` |
| 172 | fake { |
| 173 | name: "module2_with_team1", |
| 174 | team: "team1" |
| 175 | } ` |
| 176 | // create an empty one. |
| 177 | packageDefaultsBp := "" |
| 178 | packageDefaultspd2 := ` |
| 179 | package { default_team: "team_top"} |
| 180 | fake { |
| 181 | name: "modulepd2", |
| 182 | } ` |
| 183 | |
| 184 | packageDefaultspd3 := ` |
| 185 | fake { |
| 186 | name: "modulepd3", |
| 187 | } |
| 188 | fake { |
| 189 | name: "modulepd3b", |
| 190 | team: "team1" |
| 191 | } ` |
| 192 | |
| 193 | ctx := GroupFixturePreparers( |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 194 | prepareForTestWithTeamAndFakes, |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 195 | PrepareForTestWithPackageModule, |
| 196 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
Ronald Braunstein | 73b08ff | 2023-12-19 10:24:47 -0800 | [diff] [blame] | 197 | ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory) |
| 198 | }), |
| 199 | FixtureAddTextFile("Android.bp", rootBp), |
| 200 | FixtureAddTextFile("dir1/Android.bp", dir1Bp), |
| 201 | FixtureAddTextFile("dir1/dir2/dir3/Android.bp", dir3Bp), |
| 202 | FixtureAddTextFile("teams_dir/Android.bp", teamsDirBp), |
| 203 | FixtureAddTextFile("teams_dir/deeper/Android.bp", teamsDirDeeper), |
| 204 | FixtureAddTextFile("package_defaults/Android.bp", packageDefaultsBp), |
| 205 | FixtureAddTextFile("package_defaults/pd2/Android.bp", packageDefaultspd2), |
| 206 | FixtureAddTextFile("package_defaults/pd2/pd3/Android.bp", packageDefaultspd3), |
| 207 | ).RunTest(t) |
| 208 | |
| 209 | var teams *team_proto.AllTeams |
| 210 | teams = getTeamProtoOutput(t, ctx) |
| 211 | |
| 212 | // map of module name -> trendy team name. |
| 213 | actualTeams := make(map[string]*string) |
| 214 | for _, teamProto := range teams.Teams { |
| 215 | actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId |
| 216 | } |
| 217 | expectedTeams := map[string]*string{ |
| 218 | "module_with_team1": proto.String("111"), |
| 219 | "module2_with_team1": proto.String("111"), |
| 220 | "modulepd2": proto.String("trendy://team_top"), |
| 221 | "modulepd3": proto.String("trendy://team_top"), |
| 222 | "modulepd3b": proto.String("111"), |
| 223 | "module_dir1": nil, |
| 224 | "module_dir123": nil, |
| 225 | } |
| 226 | AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams) |
| 227 | } |
Ronald Braunstein | c560309 | 2024-03-27 06:46:47 -0700 | [diff] [blame^] | 228 | |
| 229 | type fakeForTests struct { |
| 230 | ModuleBase |
| 231 | |
| 232 | sourceProperties SourceProperties |
| 233 | } |
| 234 | |
| 235 | func fakeFactory() Module { |
| 236 | module := &fakeForTests{} |
| 237 | module.AddProperties(&module.sourceProperties) |
| 238 | InitAndroidModule(module) |
| 239 | |
| 240 | return module |
| 241 | } |
| 242 | |
| 243 | var prepareForTestWithTeamAndFakes = GroupFixturePreparers( |
| 244 | FixtureRegisterWithContext(RegisterTeamBuildComponents), |
| 245 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 246 | ctx.RegisterModuleType("fake", fakeFactory) |
| 247 | }), |
| 248 | ) |
| 249 | |
| 250 | func (f *fakeForTests) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 251 | if Bool(f.sourceProperties.Test_only) { |
| 252 | SetProvider(ctx, TestOnlyProviderKey, TestModuleInformation{ |
| 253 | TestOnly: Bool(f.sourceProperties.Test_only), |
| 254 | TopLevelTarget: false, |
| 255 | }) |
| 256 | } |
| 257 | } |