Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 21 | "path/filepath" |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 22 | "reflect" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 23 | "strings" |
| 24 | "testing" |
| 25 | |
| 26 | "android/soong/android" |
| 27 | "android/soong/apex" |
| 28 | "android/soong/cc" |
| 29 | "android/soong/java" |
| 30 | ) |
| 31 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 32 | func testSdkContext(bp string, fs map[string][]byte, extraOsTypes []android.OsType) (*android.TestContext, android.Config) { |
| 33 | extraOsTypes = append(extraOsTypes, android.Android, android.Windows) |
| 34 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 35 | bp = bp + ` |
| 36 | apex_key { |
| 37 | name: "myapex.key", |
| 38 | public_key: "myapex.avbpubkey", |
| 39 | private_key: "myapex.pem", |
| 40 | } |
| 41 | |
| 42 | android_app_certificate { |
| 43 | name: "myapex.cert", |
| 44 | certificate: "myapex", |
| 45 | } |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 46 | ` + cc.GatherRequiredDepsForTest(extraOsTypes...) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 47 | |
| 48 | mockFS := map[string][]byte{ |
Martin Stjernholm | cc77601 | 2020-07-07 03:22:21 +0100 | [diff] [blame] | 49 | "build/make/target/product/security": nil, |
| 50 | "apex_manifest.json": nil, |
| 51 | "system/sepolicy/apex/myapex-file_contexts": nil, |
| 52 | "system/sepolicy/apex/myapex2-file_contexts": nil, |
| 53 | "system/sepolicy/apex/mysdkapex-file_contexts": nil, |
| 54 | "myapex.avbpubkey": nil, |
| 55 | "myapex.pem": nil, |
| 56 | "myapex.x509.pem": nil, |
| 57 | "myapex.pk8": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 60 | cc.GatherRequiredFilesForTest(mockFS) |
| 61 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 62 | for k, v := range fs { |
| 63 | mockFS[k] = v |
| 64 | } |
| 65 | |
| 66 | config := android.TestArchConfig(buildDir, nil, bp, mockFS) |
| 67 | |
Paul Duffin | 08798aa | 2020-02-27 13:12:46 +0000 | [diff] [blame] | 68 | // Add windows as a default disable OS to test behavior when some OS variants |
| 69 | // are disabled. |
| 70 | config.Targets[android.Windows] = []android.Target{ |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 71 | {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true}, |
Paul Duffin | 08798aa | 2020-02-27 13:12:46 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 74 | for _, extraOsType := range extraOsTypes { |
| 75 | switch extraOsType { |
| 76 | case android.LinuxBionic: |
| 77 | config.Targets[android.LinuxBionic] = []android.Target{ |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 78 | {android.LinuxBionic, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", false}, |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 83 | ctx := android.NewTestArchContext(config) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 84 | |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 85 | // Enable androidmk support. |
| 86 | // * Register the singleton |
| 87 | // * Configure that we are inside make |
| 88 | // * Add CommonOS to ensure that androidmk processing works. |
| 89 | android.RegisterAndroidMkBuildComponents(ctx) |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 90 | android.SetKatiEnabledForTests(config) |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 91 | config.Targets[android.CommonOS] = []android.Target{ |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 92 | {android.CommonOS, android.Arch{ArchType: android.Common}, android.NativeBridgeDisabled, "", "", true}, |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 95 | // from android package |
Paul Duffin | c132742 | 2020-01-14 12:15:29 +0000 | [diff] [blame] | 96 | android.RegisterPackageBuildComponents(ctx) |
Paul Duffin | a2ae7e0 | 2020-09-11 11:55:00 +0100 | [diff] [blame] | 97 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 98 | ctx.PreArchMutators(android.RegisterVisibilityRuleChecker) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 99 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 100 | ctx.PreArchMutators(android.RegisterComponentsMutator) |
Paul Duffin | 021f4e5 | 2020-07-30 16:04:17 +0100 | [diff] [blame] | 101 | |
| 102 | android.RegisterPrebuiltMutators(ctx) |
| 103 | |
| 104 | // Register these after the prebuilt mutators have been registered to match what |
| 105 | // happens at runtime. |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 106 | ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer) |
| 107 | ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer) |
| 108 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 109 | // from java package |
Paul Duffin | c059c8c | 2021-01-20 17:13:52 +0000 | [diff] [blame] | 110 | java.RegisterRequiredBuildComponentsForTest(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 111 | |
| 112 | // from cc package |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 113 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 114 | |
| 115 | // from apex package |
| 116 | ctx.RegisterModuleType("apex", apex.BundleFactory) |
| 117 | ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) |
| 118 | ctx.PostDepsMutators(apex.RegisterPostDepsMutators) |
| 119 | |
| 120 | // from this package |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 121 | ctx.RegisterModuleType("sdk", SdkModuleFactory) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 122 | ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 123 | ctx.RegisterModuleType("module_exports", ModuleExportsFactory) |
| 124 | ctx.RegisterModuleType("module_exports_snapshot", ModuleExportsSnapshotsFactory) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 125 | ctx.PreDepsMutators(RegisterPreDepsMutators) |
| 126 | ctx.PostDepsMutators(RegisterPostDepsMutators) |
| 127 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 128 | ctx.Register() |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 129 | |
| 130 | return ctx, config |
| 131 | } |
| 132 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 133 | func runTests(t *testing.T, ctx *android.TestContext, config android.Config) *testSdkResult { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 134 | t.Helper() |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 135 | _, errs := ctx.ParseBlueprintsFiles(".") |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 136 | android.FailIfErrored(t, errs) |
| 137 | _, errs = ctx.PrepareBuildActions(config) |
| 138 | android.FailIfErrored(t, errs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 139 | return &testSdkResult{ |
| 140 | TestHelper: TestHelper{t: t}, |
| 141 | ctx: ctx, |
| 142 | config: config, |
| 143 | } |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 146 | func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { |
| 147 | t.Helper() |
| 148 | ctx, config := testSdkContext(bp, fs, nil) |
| 149 | return runTests(t, ctx, config) |
| 150 | } |
| 151 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 152 | func testSdkError(t *testing.T, pattern, bp string) { |
| 153 | t.Helper() |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 154 | ctx, config := testSdkContext(bp, nil, nil) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 155 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 156 | if len(errs) > 0 { |
| 157 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 158 | return |
| 159 | } |
| 160 | _, errs = ctx.PrepareBuildActions(config) |
| 161 | if len(errs) > 0 { |
| 162 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 167 | } |
| 168 | |
| 169 | func ensureListContains(t *testing.T, result []string, expected string) { |
| 170 | t.Helper() |
| 171 | if !android.InList(expected, result) { |
| 172 | t.Errorf("%q is not found in %v", expected, result) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func pathsToStrings(paths android.Paths) []string { |
| 177 | var ret []string |
| 178 | for _, p := range paths { |
| 179 | ret = append(ret, p.String()) |
| 180 | } |
| 181 | return ret |
| 182 | } |
| 183 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 184 | // Provides general test support. |
| 185 | type TestHelper struct { |
| 186 | t *testing.T |
| 187 | } |
| 188 | |
| 189 | func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) { |
| 190 | h.t.Helper() |
| 191 | if actual != expected { |
| 192 | h.t.Errorf("%s: expected %s, actual %s", message, expected, actual) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Paul Duffin | 4b8b793 | 2020-05-06 12:35:38 +0100 | [diff] [blame] | 196 | func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) { |
| 197 | h.t.Helper() |
| 198 | if actual == nil { |
| 199 | h.t.Errorf("Expected error but was nil") |
| 200 | } else if actual.Error() != expected { |
| 201 | h.t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error()) |
| 202 | } |
| 203 | } |
| 204 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 205 | func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { |
| 206 | h.t.Helper() |
| 207 | h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual)) |
| 208 | } |
| 209 | |
Paul Duffin | b07fa51 | 2020-03-10 22:17:04 +0000 | [diff] [blame] | 210 | func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) { |
| 211 | h.t.Helper() |
| 212 | if !reflect.DeepEqual(actual, expected) { |
| 213 | h.t.Errorf("%s: expected %#v, actual %#v", message, expected, actual) |
| 214 | } |
| 215 | } |
| 216 | |
Martin Stjernholm | 191c25f | 2020-09-10 00:40:37 +0100 | [diff] [blame] | 217 | func (h *TestHelper) AssertPanic(message string, funcThatShouldPanic func()) { |
| 218 | h.t.Helper() |
| 219 | panicked := false |
| 220 | func() { |
| 221 | defer func() { |
| 222 | if x := recover(); x != nil { |
| 223 | panicked = true |
| 224 | } |
| 225 | }() |
| 226 | funcThatShouldPanic() |
| 227 | }() |
| 228 | if !panicked { |
| 229 | h.t.Error(message) |
| 230 | } |
| 231 | } |
| 232 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 233 | // Encapsulates result of processing an SDK definition. Provides support for |
| 234 | // checking the state of the build structures. |
| 235 | type testSdkResult struct { |
| 236 | TestHelper |
| 237 | ctx *android.TestContext |
| 238 | config android.Config |
| 239 | } |
| 240 | |
| 241 | // Analyse the sdk build rules to extract information about what it is doing. |
| 242 | |
| 243 | // e.g. find the src/dest pairs from each cp command, the various zip files |
| 244 | // generated, etc. |
| 245 | func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 246 | info := &snapshotBuildInfo{ |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 247 | r: r, |
| 248 | androidBpContents: sdk.GetAndroidBpContentsForTests(), |
| 249 | androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(), |
| 250 | androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(), |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | buildParams := sdk.BuildParamsForTests() |
| 254 | copyRules := &strings.Builder{} |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 255 | otherCopyRules := &strings.Builder{} |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 256 | snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 257 | for _, bp := range buildParams { |
| 258 | switch bp.Rule.String() { |
| 259 | case android.Cp.String(): |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 260 | output := bp.Output |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 261 | // Get destination relative to the snapshot root |
| 262 | dest := output.Rel() |
| 263 | src := android.NormalizePathForTesting(bp.Input) |
| 264 | // We differentiate between copy rules for the snapshot, and copy rules for the install file. |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 265 | if strings.HasPrefix(output.String(), snapshotDirPrefix) { |
| 266 | // Get source relative to build directory. |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 267 | _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) |
| 268 | info.snapshotContents = append(info.snapshotContents, dest) |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 269 | } else { |
| 270 | _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest) |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 271 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 272 | |
| 273 | case repackageZip.String(): |
| 274 | // Add the destdir to the snapshot contents as that is effectively where |
| 275 | // the content of the repackaged zip is copied. |
| 276 | dest := bp.Args["destdir"] |
| 277 | info.snapshotContents = append(info.snapshotContents, dest) |
| 278 | |
| 279 | case zipFiles.String(): |
| 280 | // This could be an intermediate zip file and not the actual output zip. |
| 281 | // In that case this will be overridden when the rule to merge the zips |
| 282 | // is processed. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 283 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 284 | |
| 285 | case mergeZips.String(): |
| 286 | // Copy the current outputZip to the intermediateZip. |
| 287 | info.intermediateZip = info.outputZip |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 288 | mergeInput := android.NormalizePathForTesting(bp.Input) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 289 | if info.intermediateZip != mergeInput { |
| 290 | r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", |
| 291 | info.intermediateZip, mergeInput) |
| 292 | } |
| 293 | |
| 294 | // Override output zip (which was actually the intermediate zip file) with the actual |
| 295 | // output zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 296 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 297 | |
| 298 | // Save the zips to be merged into the intermediate zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 299 | info.mergeZips = android.NormalizePathsForTesting(bp.Inputs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | info.copyRules = copyRules.String() |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 304 | info.otherCopyRules = otherCopyRules.String() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 305 | |
| 306 | return info |
| 307 | } |
| 308 | |
| 309 | func (r *testSdkResult) Module(name string, variant string) android.Module { |
| 310 | return r.ctx.ModuleForTests(name, variant).Module() |
| 311 | } |
| 312 | |
| 313 | func (r *testSdkResult) ModuleForTests(name string, variant string) android.TestingModule { |
| 314 | return r.ctx.ModuleForTests(name, variant) |
| 315 | } |
| 316 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 317 | // Check the snapshot build rules. |
| 318 | // |
| 319 | // Takes a list of functions which check different facets of the snapshot build rules. |
| 320 | // Allows each test to customize what is checked without duplicating lots of code |
| 321 | // or proliferating check methods of different flavors. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 322 | func (r *testSdkResult) CheckSnapshot(name string, dir string, checkers ...snapshotBuildInfoChecker) { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 323 | r.t.Helper() |
| 324 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 325 | // The sdk CommonOS variant is always responsible for generating the snapshot. |
| 326 | variant := android.CommonOS.Name |
| 327 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 328 | sdk := r.Module(name, variant).(*sdk) |
| 329 | |
| 330 | snapshotBuildInfo := r.getSdkSnapshotBuildInfo(sdk) |
| 331 | |
| 332 | // Check state of the snapshot build. |
| 333 | for _, checker := range checkers { |
| 334 | checker(snapshotBuildInfo) |
| 335 | } |
| 336 | |
| 337 | // Make sure that the generated zip file is in the correct place. |
| 338 | actual := snapshotBuildInfo.outputZip |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 339 | if dir != "" { |
| 340 | dir = filepath.Clean(dir) + "/" |
| 341 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 342 | r.AssertStringEquals("Snapshot zip file in wrong place", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 343 | fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 344 | |
| 345 | // Populate a mock filesystem with the files that would have been copied by |
| 346 | // the rules. |
| 347 | fs := make(map[string][]byte) |
| 348 | for _, dest := range snapshotBuildInfo.snapshotContents { |
| 349 | fs[dest] = nil |
| 350 | } |
| 351 | |
| 352 | // Process the generated bp file to make sure it is valid. |
| 353 | testSdkWithFs(r.t, snapshotBuildInfo.androidBpContents, fs) |
| 354 | } |
| 355 | |
| 356 | type snapshotBuildInfoChecker func(info *snapshotBuildInfo) |
| 357 | |
| 358 | // Check that the snapshot's generated Android.bp is correct. |
| 359 | // |
| 360 | // Both the expected and actual string are both trimmed before comparing. |
| 361 | func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 362 | return func(info *snapshotBuildInfo) { |
| 363 | info.r.t.Helper() |
| 364 | info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) |
| 365 | } |
| 366 | } |
| 367 | |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 368 | // Check that the snapshot's unversioned generated Android.bp is correct. |
| 369 | // |
| 370 | // This func should be used to check the general snapshot generation code. |
| 371 | // |
| 372 | // Both the expected and actual string are both trimmed before comparing. |
| 373 | func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 374 | return func(info *snapshotBuildInfo) { |
| 375 | info.r.t.Helper() |
| 376 | info.r.AssertTrimmedStringEquals("unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Check that the snapshot's versioned generated Android.bp is correct. |
| 381 | // |
| 382 | // This func should only be used to check the version specific snapshot generation code, |
| 383 | // i.e. the encoding of version into module names and the generation of the _snapshot module. The |
| 384 | // general snapshot generation code should be checked using the checkUnversionedAndroidBpContents() |
| 385 | // func. |
| 386 | // |
| 387 | // Both the expected and actual string are both trimmed before comparing. |
| 388 | func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 389 | return func(info *snapshotBuildInfo) { |
| 390 | info.r.t.Helper() |
| 391 | info.r.AssertTrimmedStringEquals("versioned Android.bp contents do not match", expected, info.androidVersionedBpContents) |
| 392 | } |
| 393 | } |
| 394 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 395 | // Check that the snapshot's copy rules are correct. |
| 396 | // |
| 397 | // The copy rules are formatted as <src> -> <dest>, one per line and then compared |
| 398 | // to the supplied expected string. Both the expected and actual string are trimmed |
| 399 | // before comparing. |
| 400 | func checkAllCopyRules(expected string) snapshotBuildInfoChecker { |
| 401 | return func(info *snapshotBuildInfo) { |
| 402 | info.r.t.Helper() |
| 403 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) |
| 404 | } |
| 405 | } |
| 406 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 407 | func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker { |
| 408 | return func(info *snapshotBuildInfo) { |
| 409 | info.r.t.Helper() |
| 410 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.otherCopyRules) |
| 411 | } |
| 412 | } |
| 413 | |
Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 414 | // Check that the specified paths match the list of zips to merge with the intermediate zip. |
| 415 | func checkMergeZips(expected ...string) snapshotBuildInfoChecker { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 416 | return func(info *snapshotBuildInfo) { |
| 417 | info.r.t.Helper() |
| 418 | if info.intermediateZip == "" { |
| 419 | info.r.t.Errorf("No intermediate zip file was created") |
| 420 | } |
Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 421 | |
| 422 | info.r.AssertDeepEquals("mismatching merge zip files", expected, info.mergeZips) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | // Encapsulates information about the snapshot build structure in order to insulate tests from |
| 427 | // knowing too much about internal structures. |
| 428 | // |
| 429 | // All source/input paths are relative either the build directory. All dest/output paths are |
| 430 | // relative to the snapshot root directory. |
| 431 | type snapshotBuildInfo struct { |
| 432 | r *testSdkResult |
| 433 | |
| 434 | // The contents of the generated Android.bp file |
| 435 | androidBpContents string |
| 436 | |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 437 | // The contents of the unversioned Android.bp file |
| 438 | androidUnversionedBpContents string |
| 439 | |
| 440 | // The contents of the versioned Android.bp file |
| 441 | androidVersionedBpContents string |
| 442 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 443 | // The paths, relative to the snapshot root, of all files and directories copied into the |
| 444 | // snapshot. |
| 445 | snapshotContents []string |
| 446 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 447 | // A formatted representation of the src/dest pairs for a snapshot, one pair per line, |
| 448 | // of the format src -> dest |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 449 | copyRules string |
| 450 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 451 | // A formatted representation of the src/dest pairs for files not in a snapshot, one pair |
| 452 | // per line, of the format src -> dest |
| 453 | otherCopyRules string |
| 454 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 455 | // The path to the intermediate zip, which is a zip created from the source files copied |
| 456 | // into the snapshot directory and which will be merged with other zips to form the final output. |
| 457 | // Is am empty string if there is no intermediate zip because there are no zips to merge in. |
| 458 | intermediateZip string |
| 459 | |
| 460 | // The paths to the zips to merge into the output zip, does not include the intermediate |
| 461 | // zip. |
| 462 | mergeZips []string |
| 463 | |
| 464 | // The final output zip. |
| 465 | outputZip string |
| 466 | } |
| 467 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 468 | var buildDir string |
| 469 | |
| 470 | func setUp() { |
| 471 | var err error |
| 472 | buildDir, err = ioutil.TempDir("", "soong_sdk_test") |
| 473 | if err != nil { |
| 474 | panic(err) |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | func tearDown() { |
| 479 | _ = os.RemoveAll(buildDir) |
| 480 | } |
| 481 | |
| 482 | func runTestWithBuildDir(m *testing.M) { |
| 483 | run := func() int { |
| 484 | setUp() |
| 485 | defer tearDown() |
| 486 | |
| 487 | return m.Run() |
| 488 | } |
| 489 | |
| 490 | os.Exit(run()) |
| 491 | } |