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 | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 22 | "strings" |
| 23 | "testing" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | "android/soong/apex" |
| 27 | "android/soong/cc" |
Paul Duffin | d6ceb86 | 2021-03-04 23:02:31 +0000 | [diff] [blame] | 28 | "android/soong/genrule" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 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 | |
Paul Duffin | d6ceb86 | 2021-03-04 23:02:31 +0000 | [diff] [blame] | 112 | // from genrule package |
| 113 | genrule.RegisterGenruleBuildComponents(ctx) |
| 114 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 115 | // from cc package |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 116 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 117 | |
| 118 | // from apex package |
| 119 | ctx.RegisterModuleType("apex", apex.BundleFactory) |
| 120 | ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) |
| 121 | ctx.PostDepsMutators(apex.RegisterPostDepsMutators) |
| 122 | |
| 123 | // from this package |
Paul Duffin | 6d9108f0 | 2021-03-09 22:59:28 +0000 | [diff] [blame] | 124 | registerModuleExportsBuildComponents(ctx) |
| 125 | registerSdkBuildComponents(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 126 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 127 | ctx.Register() |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 128 | |
| 129 | return ctx, config |
| 130 | } |
| 131 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 132 | func runTests(t *testing.T, ctx *android.TestContext, config android.Config) *testSdkResult { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 133 | t.Helper() |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 134 | _, errs := ctx.ParseBlueprintsFiles(".") |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 135 | android.FailIfErrored(t, errs) |
| 136 | _, errs = ctx.PrepareBuildActions(config) |
| 137 | android.FailIfErrored(t, errs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 138 | return &testSdkResult{ |
Paul Duffin | 8306f25 | 2021-03-11 12:18:24 +0000 | [diff] [blame] | 139 | TestHelper: android.TestHelper{T: t}, |
| 140 | TestContext: ctx, |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 141 | } |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 144 | func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { |
| 145 | t.Helper() |
| 146 | ctx, config := testSdkContext(bp, fs, nil) |
| 147 | return runTests(t, ctx, config) |
| 148 | } |
| 149 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 150 | func testSdkError(t *testing.T, pattern, bp string) { |
| 151 | t.Helper() |
Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 152 | ctx, config := testSdkContext(bp, nil, nil) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 153 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 154 | if len(errs) > 0 { |
| 155 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 156 | return |
| 157 | } |
| 158 | _, errs = ctx.PrepareBuildActions(config) |
| 159 | if len(errs) > 0 { |
| 160 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 165 | } |
| 166 | |
| 167 | func ensureListContains(t *testing.T, result []string, expected string) { |
| 168 | t.Helper() |
| 169 | if !android.InList(expected, result) { |
| 170 | t.Errorf("%q is not found in %v", expected, result) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func pathsToStrings(paths android.Paths) []string { |
| 175 | var ret []string |
| 176 | for _, p := range paths { |
| 177 | ret = append(ret, p.String()) |
| 178 | } |
| 179 | return ret |
| 180 | } |
| 181 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 182 | // Encapsulates result of processing an SDK definition. Provides support for |
| 183 | // checking the state of the build structures. |
| 184 | type testSdkResult struct { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 185 | android.TestHelper |
Paul Duffin | 8306f25 | 2021-03-11 12:18:24 +0000 | [diff] [blame] | 186 | *android.TestContext |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 189 | func (result *testSdkResult) Module(name string, variant string) android.Module { |
| 190 | return result.ModuleForTests(name, variant).Module() |
| 191 | } |
| 192 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 193 | // Analyse the sdk build rules to extract information about what it is doing. |
Paul Duffin | fe9a9e3 | 2021-03-11 17:41:01 +0000 | [diff] [blame^] | 194 | // |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 195 | // e.g. find the src/dest pairs from each cp command, the various zip files |
| 196 | // generated, etc. |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 197 | func getSdkSnapshotBuildInfo(result *testSdkResult, sdk *sdk) *snapshotBuildInfo { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 198 | info := &snapshotBuildInfo{ |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 199 | r: result, |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 200 | androidBpContents: sdk.GetAndroidBpContentsForTests(), |
| 201 | androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(), |
| 202 | androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(), |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | buildParams := sdk.BuildParamsForTests() |
| 206 | copyRules := &strings.Builder{} |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 207 | otherCopyRules := &strings.Builder{} |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 208 | snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 209 | for _, bp := range buildParams { |
| 210 | switch bp.Rule.String() { |
| 211 | case android.Cp.String(): |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 212 | output := bp.Output |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 213 | // Get destination relative to the snapshot root |
| 214 | dest := output.Rel() |
| 215 | src := android.NormalizePathForTesting(bp.Input) |
| 216 | // 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] | 217 | if strings.HasPrefix(output.String(), snapshotDirPrefix) { |
| 218 | // Get source relative to build directory. |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 219 | _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) |
| 220 | info.snapshotContents = append(info.snapshotContents, dest) |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 221 | } else { |
| 222 | _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest) |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 223 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 224 | |
| 225 | case repackageZip.String(): |
| 226 | // Add the destdir to the snapshot contents as that is effectively where |
| 227 | // the content of the repackaged zip is copied. |
| 228 | dest := bp.Args["destdir"] |
| 229 | info.snapshotContents = append(info.snapshotContents, dest) |
| 230 | |
| 231 | case zipFiles.String(): |
| 232 | // This could be an intermediate zip file and not the actual output zip. |
| 233 | // In that case this will be overridden when the rule to merge the zips |
| 234 | // is processed. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 235 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 236 | |
| 237 | case mergeZips.String(): |
| 238 | // Copy the current outputZip to the intermediateZip. |
| 239 | info.intermediateZip = info.outputZip |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 240 | mergeInput := android.NormalizePathForTesting(bp.Input) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 241 | if info.intermediateZip != mergeInput { |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 242 | result.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 243 | info.intermediateZip, mergeInput) |
| 244 | } |
| 245 | |
| 246 | // Override output zip (which was actually the intermediate zip file) with the actual |
| 247 | // output zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 248 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 249 | |
| 250 | // Save the zips to be merged into the intermediate zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 251 | info.mergeZips = android.NormalizePathsForTesting(bp.Inputs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | info.copyRules = copyRules.String() |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 256 | info.otherCopyRules = otherCopyRules.String() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 257 | |
| 258 | return info |
| 259 | } |
| 260 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 261 | // Check the snapshot build rules. |
| 262 | // |
| 263 | // Takes a list of functions which check different facets of the snapshot build rules. |
| 264 | // Allows each test to customize what is checked without duplicating lots of code |
| 265 | // or proliferating check methods of different flavors. |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 266 | func CheckSnapshot(result *testSdkResult, name string, dir string, checkers ...snapshotBuildInfoChecker) { |
| 267 | result.Helper() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 268 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 269 | // The sdk CommonOS variant is always responsible for generating the snapshot. |
| 270 | variant := android.CommonOS.Name |
| 271 | |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 272 | sdk := result.Module(name, variant).(*sdk) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 273 | |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 274 | snapshotBuildInfo := getSdkSnapshotBuildInfo(result, sdk) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 275 | |
| 276 | // Check state of the snapshot build. |
| 277 | for _, checker := range checkers { |
| 278 | checker(snapshotBuildInfo) |
| 279 | } |
| 280 | |
| 281 | // Make sure that the generated zip file is in the correct place. |
| 282 | actual := snapshotBuildInfo.outputZip |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 283 | if dir != "" { |
| 284 | dir = filepath.Clean(dir) + "/" |
| 285 | } |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 286 | result.AssertStringEquals("Snapshot zip file in wrong place", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 287 | 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] | 288 | |
| 289 | // Populate a mock filesystem with the files that would have been copied by |
| 290 | // the rules. |
| 291 | fs := make(map[string][]byte) |
| 292 | for _, dest := range snapshotBuildInfo.snapshotContents { |
| 293 | fs[dest] = nil |
| 294 | } |
| 295 | |
| 296 | // Process the generated bp file to make sure it is valid. |
Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 297 | testSdkWithFs(result.T, snapshotBuildInfo.androidBpContents, fs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | type snapshotBuildInfoChecker func(info *snapshotBuildInfo) |
| 301 | |
| 302 | // Check that the snapshot's generated Android.bp is correct. |
| 303 | // |
| 304 | // Both the expected and actual string are both trimmed before comparing. |
| 305 | func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 306 | return func(info *snapshotBuildInfo) { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 307 | info.r.Helper() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 308 | info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) |
| 309 | } |
| 310 | } |
| 311 | |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 312 | // Check that the snapshot's unversioned generated Android.bp is correct. |
| 313 | // |
| 314 | // This func should be used to check the general snapshot generation code. |
| 315 | // |
| 316 | // Both the expected and actual string are both trimmed before comparing. |
| 317 | func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 318 | return func(info *snapshotBuildInfo) { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 319 | info.r.Helper() |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 320 | info.r.AssertTrimmedStringEquals("unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Check that the snapshot's versioned generated Android.bp is correct. |
| 325 | // |
| 326 | // This func should only be used to check the version specific snapshot generation code, |
| 327 | // i.e. the encoding of version into module names and the generation of the _snapshot module. The |
| 328 | // general snapshot generation code should be checked using the checkUnversionedAndroidBpContents() |
| 329 | // func. |
| 330 | // |
| 331 | // Both the expected and actual string are both trimmed before comparing. |
| 332 | func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 333 | return func(info *snapshotBuildInfo) { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 334 | info.r.Helper() |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 335 | info.r.AssertTrimmedStringEquals("versioned Android.bp contents do not match", expected, info.androidVersionedBpContents) |
| 336 | } |
| 337 | } |
| 338 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 339 | // Check that the snapshot's copy rules are correct. |
| 340 | // |
| 341 | // The copy rules are formatted as <src> -> <dest>, one per line and then compared |
| 342 | // to the supplied expected string. Both the expected and actual string are trimmed |
| 343 | // before comparing. |
| 344 | func checkAllCopyRules(expected string) snapshotBuildInfoChecker { |
| 345 | return func(info *snapshotBuildInfo) { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 346 | info.r.Helper() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 347 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) |
| 348 | } |
| 349 | } |
| 350 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 351 | func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker { |
| 352 | return func(info *snapshotBuildInfo) { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 353 | info.r.Helper() |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 354 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.otherCopyRules) |
| 355 | } |
| 356 | } |
| 357 | |
Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 358 | // Check that the specified paths match the list of zips to merge with the intermediate zip. |
| 359 | func checkMergeZips(expected ...string) snapshotBuildInfoChecker { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 360 | return func(info *snapshotBuildInfo) { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 361 | info.r.Helper() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 362 | if info.intermediateZip == "" { |
Paul Duffin | a3cb2b3 | 2021-03-10 09:15:22 +0000 | [diff] [blame] | 363 | info.r.Errorf("No intermediate zip file was created") |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 364 | } |
Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 365 | |
| 366 | info.r.AssertDeepEquals("mismatching merge zip files", expected, info.mergeZips) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // Encapsulates information about the snapshot build structure in order to insulate tests from |
| 371 | // knowing too much about internal structures. |
| 372 | // |
| 373 | // All source/input paths are relative either the build directory. All dest/output paths are |
| 374 | // relative to the snapshot root directory. |
| 375 | type snapshotBuildInfo struct { |
| 376 | r *testSdkResult |
| 377 | |
| 378 | // The contents of the generated Android.bp file |
| 379 | androidBpContents string |
| 380 | |
Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 381 | // The contents of the unversioned Android.bp file |
| 382 | androidUnversionedBpContents string |
| 383 | |
| 384 | // The contents of the versioned Android.bp file |
| 385 | androidVersionedBpContents string |
| 386 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 387 | // The paths, relative to the snapshot root, of all files and directories copied into the |
| 388 | // snapshot. |
| 389 | snapshotContents []string |
| 390 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 391 | // A formatted representation of the src/dest pairs for a snapshot, one pair per line, |
| 392 | // of the format src -> dest |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 393 | copyRules string |
| 394 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 395 | // A formatted representation of the src/dest pairs for files not in a snapshot, one pair |
| 396 | // per line, of the format src -> dest |
| 397 | otherCopyRules string |
| 398 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 399 | // The path to the intermediate zip, which is a zip created from the source files copied |
| 400 | // into the snapshot directory and which will be merged with other zips to form the final output. |
| 401 | // Is am empty string if there is no intermediate zip because there are no zips to merge in. |
| 402 | intermediateZip string |
| 403 | |
| 404 | // The paths to the zips to merge into the output zip, does not include the intermediate |
| 405 | // zip. |
| 406 | mergeZips []string |
| 407 | |
| 408 | // The final output zip. |
| 409 | outputZip string |
| 410 | } |
| 411 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 412 | var buildDir string |
| 413 | |
| 414 | func setUp() { |
| 415 | var err error |
| 416 | buildDir, err = ioutil.TempDir("", "soong_sdk_test") |
| 417 | if err != nil { |
| 418 | panic(err) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | func tearDown() { |
| 423 | _ = os.RemoveAll(buildDir) |
| 424 | } |
| 425 | |
| 426 | func runTestWithBuildDir(m *testing.M) { |
| 427 | run := func() int { |
| 428 | setUp() |
| 429 | defer tearDown() |
| 430 | |
| 431 | return m.Run() |
| 432 | } |
| 433 | |
| 434 | os.Exit(run()) |
| 435 | } |