| 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( | 
|  | 27 | PrepareForTestWithTeamBuildComponents, | 
|  | 28 | FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 29 | ctx.RegisterModuleType("fake", fakeModuleFactory) | 
|  | 30 | ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory) | 
|  | 31 | }), | 
|  | 32 | ).RunTestWithBp(t, ` | 
|  | 33 | fake { | 
|  | 34 | name: "main_test", | 
|  | 35 | team: "someteam", | 
|  | 36 | } | 
|  | 37 | team { | 
|  | 38 | name: "someteam", | 
|  | 39 | trendy_team_id: "cool_team", | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | team { | 
|  | 43 | name: "team2", | 
|  | 44 | trendy_team_id: "22222", | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | fake { | 
|  | 48 | name: "tool", | 
|  | 49 | team: "team2", | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | fake { | 
|  | 53 | name: "noteam", | 
|  | 54 | } | 
|  | 55 | `) | 
|  | 56 |  | 
|  | 57 | var teams *team_proto.AllTeams | 
|  | 58 | teams = getTeamProtoOutput(t, ctx) | 
|  | 59 |  | 
|  | 60 | // map of module name -> trendy team name. | 
|  | 61 | actualTeams := make(map[string]*string) | 
|  | 62 | for _, teamProto := range teams.Teams { | 
|  | 63 | actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId | 
|  | 64 | } | 
|  | 65 | expectedTeams := map[string]*string{ | 
|  | 66 | "main_test": proto.String("cool_team"), | 
|  | 67 | "tool":      proto.String("22222"), | 
|  | 68 | "noteam":    nil, | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams) | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | func getTeamProtoOutput(t *testing.T, ctx *TestResult) *team_proto.AllTeams { | 
|  | 75 | teams := new(team_proto.AllTeams) | 
|  | 76 | config := ctx.SingletonForTests("all_teams") | 
|  | 77 | allOutputs := config.AllOutputs() | 
|  | 78 |  | 
|  | 79 | protoPath := allOutputs[0] | 
|  | 80 |  | 
|  | 81 | out := config.MaybeOutput(protoPath) | 
|  | 82 | outProto := []byte(ContentFromFileRuleForTests(t, ctx.TestContext, out)) | 
|  | 83 | if err := proto.Unmarshal(outProto, teams); err != nil { | 
|  | 84 | log.Fatalln("Failed to parse teams proto:", err) | 
|  | 85 | } | 
|  | 86 | return teams | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | // Android.bp | 
|  | 90 | // | 
|  | 91 | //	team: team_top | 
|  | 92 | // | 
|  | 93 | // # dir1 has no modules with teams, | 
|  | 94 | // # but has a dir with no Android.bp | 
|  | 95 | // dir1/Android.bp | 
|  | 96 | // | 
|  | 97 | //	module_dir1 | 
|  | 98 | // | 
|  | 99 | // # dirs without and Android.bp should be fine. | 
|  | 100 | // dir1/dir2/dir3/Android.bp | 
|  | 101 | // | 
|  | 102 | //	package {} | 
|  | 103 | //	module_dir123 | 
|  | 104 | // | 
|  | 105 | // teams_dir/Android.bp | 
|  | 106 | // | 
|  | 107 | //	module_with_team1: team1 | 
|  | 108 | //	team1: 111 | 
|  | 109 | // | 
|  | 110 | // # team comes from upper package default | 
|  | 111 | // teams_dir/deeper/Android.bp | 
|  | 112 | // | 
|  | 113 | //	module2_with_team1: team1 | 
|  | 114 | // | 
|  | 115 | // package_defaults/Android.bp | 
|  | 116 | // package_defaults/pd2/Android.bp | 
|  | 117 | // | 
|  | 118 | //	package{ default_team: team_top} | 
|  | 119 | //	module_pd2   ## should get team_top | 
|  | 120 | // | 
|  | 121 | // package_defaults/pd2/pd3/Android.bp | 
|  | 122 | // | 
|  | 123 | //	module_pd3  ## should get team_top | 
|  | 124 | func TestPackageLookup(t *testing.T) { | 
|  | 125 | t.Parallel() | 
|  | 126 | rootBp := ` | 
|  | 127 | team { | 
|  | 128 | name: "team_top", | 
|  | 129 | trendy_team_id: "trendy://team_top", | 
|  | 130 | } ` | 
|  | 131 |  | 
|  | 132 | dir1Bp := ` | 
|  | 133 | fake { | 
|  | 134 | name: "module_dir1", | 
|  | 135 | } ` | 
|  | 136 | dir3Bp := ` | 
|  | 137 | package {} | 
|  | 138 | fake { | 
|  | 139 | name: "module_dir123", | 
|  | 140 | } ` | 
|  | 141 | teamsDirBp := ` | 
|  | 142 | fake { | 
|  | 143 | name: "module_with_team1", | 
|  | 144 | team: "team1" | 
|  | 145 |  | 
|  | 146 | } | 
|  | 147 | team { | 
|  | 148 | name: "team1", | 
|  | 149 | trendy_team_id: "111", | 
|  | 150 | } ` | 
|  | 151 | teamsDirDeeper := ` | 
|  | 152 | fake { | 
|  | 153 | name: "module2_with_team1", | 
|  | 154 | team: "team1" | 
|  | 155 | } ` | 
|  | 156 | // create an empty one. | 
|  | 157 | packageDefaultsBp := "" | 
|  | 158 | packageDefaultspd2 := ` | 
|  | 159 | package { default_team: "team_top"} | 
|  | 160 | fake { | 
|  | 161 | name: "modulepd2", | 
|  | 162 | } ` | 
|  | 163 |  | 
|  | 164 | packageDefaultspd3 := ` | 
|  | 165 | fake { | 
|  | 166 | name: "modulepd3", | 
|  | 167 | } | 
|  | 168 | fake { | 
|  | 169 | name: "modulepd3b", | 
|  | 170 | team: "team1" | 
|  | 171 | } ` | 
|  | 172 |  | 
|  | 173 | ctx := GroupFixturePreparers( | 
|  | 174 | PrepareForTestWithTeamBuildComponents, | 
|  | 175 | PrepareForTestWithPackageModule, | 
|  | 176 | FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 177 | ctx.RegisterModuleType("fake", fakeModuleFactory) | 
|  | 178 | ctx.RegisterParallelSingletonType("all_teams", AllTeamsFactory) | 
|  | 179 | }), | 
|  | 180 | FixtureAddTextFile("Android.bp", rootBp), | 
|  | 181 | FixtureAddTextFile("dir1/Android.bp", dir1Bp), | 
|  | 182 | FixtureAddTextFile("dir1/dir2/dir3/Android.bp", dir3Bp), | 
|  | 183 | FixtureAddTextFile("teams_dir/Android.bp", teamsDirBp), | 
|  | 184 | FixtureAddTextFile("teams_dir/deeper/Android.bp", teamsDirDeeper), | 
|  | 185 | FixtureAddTextFile("package_defaults/Android.bp", packageDefaultsBp), | 
|  | 186 | FixtureAddTextFile("package_defaults/pd2/Android.bp", packageDefaultspd2), | 
|  | 187 | FixtureAddTextFile("package_defaults/pd2/pd3/Android.bp", packageDefaultspd3), | 
|  | 188 | ).RunTest(t) | 
|  | 189 |  | 
|  | 190 | var teams *team_proto.AllTeams | 
|  | 191 | teams = getTeamProtoOutput(t, ctx) | 
|  | 192 |  | 
|  | 193 | // map of module name -> trendy team name. | 
|  | 194 | actualTeams := make(map[string]*string) | 
|  | 195 | for _, teamProto := range teams.Teams { | 
|  | 196 | actualTeams[teamProto.GetTargetName()] = teamProto.TrendyTeamId | 
|  | 197 | } | 
|  | 198 | expectedTeams := map[string]*string{ | 
|  | 199 | "module_with_team1":  proto.String("111"), | 
|  | 200 | "module2_with_team1": proto.String("111"), | 
|  | 201 | "modulepd2":          proto.String("trendy://team_top"), | 
|  | 202 | "modulepd3":          proto.String("trendy://team_top"), | 
|  | 203 | "modulepd3b":         proto.String("111"), | 
|  | 204 | "module_dir1":        nil, | 
|  | 205 | "module_dir123":      nil, | 
|  | 206 | } | 
|  | 207 | AssertDeepEquals(t, "compare maps", expectedTeams, actualTeams) | 
|  | 208 | } |