| 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 | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 19 | "path/filepath" | 
| Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 20 | "strings" | 
|  | 21 | "testing" | 
|  | 22 |  | 
|  | 23 | "android/soong/android" | 
|  | 24 | "android/soong/apex" | 
|  | 25 | "android/soong/cc" | 
| Paul Duffin | d6ceb86 | 2021-03-04 23:02:31 +0000 | [diff] [blame] | 26 | "android/soong/genrule" | 
| Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 27 | "android/soong/java" | 
|  | 28 | ) | 
|  | 29 |  | 
| Paul Duffin | cf3ee2f | 2021-03-15 13:08:51 +0000 | [diff] [blame] | 30 | // Prepare for running an sdk test with an apex. | 
|  | 31 | var prepareForSdkTestWithApex = android.GroupFixturePreparers( | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 32 | apex.PrepareForTestWithApexBuildComponents, | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 33 | android.FixtureAddTextFile("sdk/tests/Android.bp", ` | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 34 | apex_key { | 
|  | 35 | name: "myapex.key", | 
|  | 36 | public_key: "myapex.avbpubkey", | 
|  | 37 | private_key: "myapex.pem", | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | android_app_certificate { | 
|  | 41 | name: "myapex.cert", | 
|  | 42 | certificate: "myapex", | 
|  | 43 | } | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 44 | `), | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 45 |  | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 46 | android.FixtureMergeMockFs(map[string][]byte{ | 
| Martin Stjernholm | cc77601 | 2020-07-07 03:22:21 +0100 | [diff] [blame] | 47 | "apex_manifest.json":                           nil, | 
|  | 48 | "system/sepolicy/apex/myapex-file_contexts":    nil, | 
|  | 49 | "system/sepolicy/apex/myapex2-file_contexts":   nil, | 
|  | 50 | "system/sepolicy/apex/mysdkapex-file_contexts": nil, | 
| Paul Duffin | db462dd | 2021-03-21 22:01:55 +0000 | [diff] [blame] | 51 | "sdk/tests/myapex.avbpubkey":                   nil, | 
|  | 52 | "sdk/tests/myapex.pem":                         nil, | 
|  | 53 | "sdk/tests/myapex.x509.pem":                    nil, | 
|  | 54 | "sdk/tests/myapex.pk8":                         nil, | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 55 | }), | 
| Paul Duffin | cf3ee2f | 2021-03-15 13:08:51 +0000 | [diff] [blame] | 56 | ) | 
|  | 57 |  | 
|  | 58 | // Legacy preparer used for running tests within the sdk package. | 
|  | 59 | // | 
|  | 60 | // This includes everything that was needed to run any test in the sdk package prior to the | 
|  | 61 | // introduction of the test fixtures. Tests that are being converted to use fixtures directly | 
|  | 62 | // rather than through the testSdkError() and testSdkWithFs() methods should avoid using this and | 
|  | 63 | // instead should use the various preparers directly using android.GroupFixturePreparers(...) to | 
|  | 64 | // group them when necessary. | 
|  | 65 | // | 
|  | 66 | // deprecated | 
|  | 67 | var prepareForSdkTest = android.GroupFixturePreparers( | 
|  | 68 | cc.PrepareForTestWithCcDefaultModules, | 
|  | 69 | genrule.PrepareForTestWithGenRuleBuildComponents, | 
|  | 70 | java.PrepareForTestWithJavaBuildComponents, | 
|  | 71 | PrepareForTestWithSdkBuildComponents, | 
|  | 72 |  | 
|  | 73 | prepareForSdkTestWithApex, | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 74 |  | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 75 | cc.PrepareForTestOnWindows, | 
|  | 76 | android.FixtureModifyConfig(func(config android.Config) { | 
|  | 77 | // Add windows as a default disable OS to test behavior when some OS variants | 
|  | 78 | // are disabled. | 
|  | 79 | config.Targets[android.Windows] = []android.Target{ | 
|  | 80 | {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true}, | 
| Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 81 | } | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 82 | }), | 
| Paul Duffin | db462dd | 2021-03-21 22:01:55 +0000 | [diff] [blame] | 83 |  | 
|  | 84 | // Make sure that every test provides all the source files. | 
|  | 85 | android.PrepareForTestDisallowNonExistentPaths, | 
|  | 86 | android.MockFS{ | 
|  | 87 | "Test.java": nil, | 
|  | 88 | }.AddToFixture(), | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 89 | ) | 
| Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 90 |  | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 91 | var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers( | 
|  | 92 | android.FixtureRegisterWithContext(registerModuleExportsBuildComponents), | 
|  | 93 | android.FixtureRegisterWithContext(registerSdkBuildComponents), | 
|  | 94 | ) | 
| Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 95 |  | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 96 | func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult { | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 97 | t.Helper() | 
| Paul Duffin | 55e740e | 2021-03-29 02:06:19 +0100 | [diff] [blame] | 98 | return android.GroupFixturePreparers( | 
|  | 99 | prepareForSdkTest, | 
|  | 100 | fs.AddToFixture(), | 
|  | 101 | ).RunTestWithBp(t, bp) | 
| Martin Stjernholm | 7feceb2 | 2020-07-11 04:33:29 +0100 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 104 | func testSdkError(t *testing.T, pattern, bp string) { | 
|  | 105 | t.Helper() | 
| Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 106 | prepareForSdkTest. | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 107 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). | 
|  | 108 | RunTestWithBp(t, bp) | 
| Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | func ensureListContains(t *testing.T, result []string, expected string) { | 
|  | 112 | t.Helper() | 
|  | 113 | if !android.InList(expected, result) { | 
|  | 114 | t.Errorf("%q is not found in %v", expected, result) | 
|  | 115 | } | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | func pathsToStrings(paths android.Paths) []string { | 
|  | 119 | var ret []string | 
|  | 120 | for _, p := range paths { | 
|  | 121 | ret = append(ret, p.String()) | 
|  | 122 | } | 
|  | 123 | return ret | 
|  | 124 | } | 
|  | 125 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 126 | // 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] | 127 | // | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 128 | // e.g. find the src/dest pairs from each cp command, the various zip files | 
|  | 129 | // generated, etc. | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 130 | func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo { | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 131 | info := &snapshotBuildInfo{ | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 132 | t:                            t, | 
| Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 133 | r:                            result, | 
| Paul Duffin | 973bedb | 2021-05-05 22:00:51 +0100 | [diff] [blame^] | 134 | version:                      sdk.builderForTests.version, | 
| Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 135 | androidBpContents:            sdk.GetAndroidBpContentsForTests(), | 
|  | 136 | androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(), | 
|  | 137 | androidVersionedBpContents:   sdk.GetVersionedAndroidBpContentsForTests(), | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 138 | snapshotTestCustomizations:   map[snapshotTest]*snapshotTestCustomization{}, | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | buildParams := sdk.BuildParamsForTests() | 
|  | 142 | copyRules := &strings.Builder{} | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 143 | otherCopyRules := &strings.Builder{} | 
| Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 144 | snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/" | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 145 | for _, bp := range buildParams { | 
|  | 146 | switch bp.Rule.String() { | 
|  | 147 | case android.Cp.String(): | 
| Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 148 | output := bp.Output | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 149 | // Get destination relative to the snapshot root | 
|  | 150 | dest := output.Rel() | 
|  | 151 | src := android.NormalizePathForTesting(bp.Input) | 
|  | 152 | // 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] | 153 | if strings.HasPrefix(output.String(), snapshotDirPrefix) { | 
|  | 154 | // Get source relative to build directory. | 
| Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 155 | _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) | 
|  | 156 | info.snapshotContents = append(info.snapshotContents, dest) | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 157 | } else { | 
|  | 158 | _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest) | 
| Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 159 | } | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 160 |  | 
|  | 161 | case repackageZip.String(): | 
|  | 162 | // Add the destdir to the snapshot contents as that is effectively where | 
|  | 163 | // the content of the repackaged zip is copied. | 
|  | 164 | dest := bp.Args["destdir"] | 
|  | 165 | info.snapshotContents = append(info.snapshotContents, dest) | 
|  | 166 |  | 
|  | 167 | case zipFiles.String(): | 
|  | 168 | // This could be an intermediate zip file and not the actual output zip. | 
|  | 169 | // In that case this will be overridden when the rule to merge the zips | 
|  | 170 | // is processed. | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 171 | info.outputZip = android.NormalizePathForTesting(bp.Output) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 172 |  | 
|  | 173 | case mergeZips.String(): | 
|  | 174 | // Copy the current outputZip to the intermediateZip. | 
|  | 175 | info.intermediateZip = info.outputZip | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 176 | mergeInput := android.NormalizePathForTesting(bp.Input) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 177 | if info.intermediateZip != mergeInput { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 178 | t.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] | 179 | info.intermediateZip, mergeInput) | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | // Override output zip (which was actually the intermediate zip file) with the actual | 
|  | 183 | // output zip. | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 184 | info.outputZip = android.NormalizePathForTesting(bp.Output) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 185 |  | 
|  | 186 | // Save the zips to be merged into the intermediate zip. | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 187 | info.mergeZips = android.NormalizePathsForTesting(bp.Inputs) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 188 | } | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | info.copyRules = copyRules.String() | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 192 | info.otherCopyRules = otherCopyRules.String() | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 193 |  | 
|  | 194 | return info | 
|  | 195 | } | 
|  | 196 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 197 | // The enum of different sdk snapshot tests performed by CheckSnapshot. | 
|  | 198 | type snapshotTest int | 
|  | 199 |  | 
|  | 200 | const ( | 
|  | 201 | // The enumeration of the different test configurations. | 
|  | 202 | // A test with the snapshot/Android.bp file but without the original Android.bp file. | 
|  | 203 | checkSnapshotWithoutSource snapshotTest = iota | 
|  | 204 |  | 
|  | 205 | // A test with both the original source and the snapshot, with the source preferred. | 
|  | 206 | checkSnapshotWithSourcePreferred | 
|  | 207 |  | 
|  | 208 | // A test with both the original source and the snapshot, with the snapshot preferred. | 
|  | 209 | checkSnapshotPreferredWithSource | 
|  | 210 |  | 
|  | 211 | // The directory into which the snapshot will be 'unpacked'. | 
|  | 212 | snapshotSubDir = "snapshot" | 
|  | 213 | ) | 
|  | 214 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 215 | // Check the snapshot build rules. | 
|  | 216 | // | 
|  | 217 | // Takes a list of functions which check different facets of the snapshot build rules. | 
|  | 218 | // Allows each test to customize what is checked without duplicating lots of code | 
|  | 219 | // or proliferating check methods of different flavors. | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 220 | func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) { | 
|  | 221 | t.Helper() | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 222 |  | 
| Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 223 | // The sdk CommonOS variant is always responsible for generating the snapshot. | 
|  | 224 | variant := android.CommonOS.Name | 
|  | 225 |  | 
| Paul Duffin | 981b94b | 2021-03-11 12:32:12 +0000 | [diff] [blame] | 226 | sdk := result.Module(name, variant).(*sdk) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 227 |  | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 228 | snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 229 |  | 
|  | 230 | // Check state of the snapshot build. | 
|  | 231 | for _, checker := range checkers { | 
|  | 232 | checker(snapshotBuildInfo) | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | // Make sure that the generated zip file is in the correct place. | 
|  | 236 | actual := snapshotBuildInfo.outputZip | 
| Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 237 | if dir != "" { | 
|  | 238 | dir = filepath.Clean(dir) + "/" | 
|  | 239 | } | 
| Paul Duffin | 973bedb | 2021-05-05 22:00:51 +0100 | [diff] [blame^] | 240 | suffix := "" | 
|  | 241 | if snapshotBuildInfo.version != soongSdkSnapshotVersionUnversioned { | 
|  | 242 | suffix = "-" + snapshotBuildInfo.version | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | expectedZipPath := fmt.Sprintf(".intermediates/%s%s/%s/%s%s.zip", dir, name, variant, name, suffix) | 
|  | 246 | android.AssertStringEquals(t, "Snapshot zip file in wrong place", expectedZipPath, actual) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 247 |  | 
|  | 248 | // Populate a mock filesystem with the files that would have been copied by | 
|  | 249 | // the rules. | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 250 | fs := android.MockFS{} | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 251 | for _, dest := range snapshotBuildInfo.snapshotContents { | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 252 | fs[filepath.Join(snapshotSubDir, dest)] = nil | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 253 | } | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 254 | fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 255 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 256 | // The preparers from the original source fixture. | 
|  | 257 | sourcePreparers := result.Preparer() | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 258 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 259 | // Preparer to combine the snapshot and the source. | 
|  | 260 | snapshotPreparer := android.GroupFixturePreparers(sourcePreparers, fs.AddToFixture()) | 
|  | 261 |  | 
|  | 262 | var runSnapshotTestWithCheckers = func(t *testing.T, testConfig snapshotTest, extraPreparer android.FixturePreparer) { | 
| Paul Duffin | 023dba0 | 2021-04-22 01:45:29 +0100 | [diff] [blame] | 263 | t.Helper() | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 264 | customization := snapshotBuildInfo.snapshotTestCustomization(testConfig) | 
| Paul Duffin | a57835e | 2021-04-19 13:23:06 +0100 | [diff] [blame] | 265 | customizedPreparers := android.GroupFixturePreparers(customization.preparers...) | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 266 |  | 
|  | 267 | // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the | 
|  | 268 | //  files the snapshot needs are actually copied into the snapshot. | 
|  | 269 |  | 
|  | 270 | // Run the snapshot with the snapshot preparer and the extra preparer, which must come after as | 
|  | 271 | // it may need to modify parts of the MockFS populated by the snapshot preparer. | 
| Paul Duffin | a57835e | 2021-04-19 13:23:06 +0100 | [diff] [blame] | 272 | result := android.GroupFixturePreparers(snapshotPreparer, extraPreparer, customizedPreparers). | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 273 | ExtendWithErrorHandler(customization.errorHandler). | 
|  | 274 | RunTest(t) | 
|  | 275 |  | 
|  | 276 | // Perform any additional checks the test need on the result of processing the snapshot. | 
|  | 277 | for _, checker := range customization.checkers { | 
|  | 278 | checker(t, result) | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 |  | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 282 | t.Run("snapshot without source", func(t *testing.T) { | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 283 | // Remove the source Android.bp file to make sure it works without. | 
|  | 284 | removeSourceAndroidBp := android.FixtureModifyMockFS(func(fs android.MockFS) { | 
|  | 285 | delete(fs, "Android.bp") | 
|  | 286 | }) | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 287 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 288 | runSnapshotTestWithCheckers(t, checkSnapshotWithoutSource, removeSourceAndroidBp) | 
|  | 289 | }) | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 290 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 291 | t.Run("snapshot with source preferred", func(t *testing.T) { | 
|  | 292 | runSnapshotTestWithCheckers(t, checkSnapshotWithSourcePreferred, android.NullFixturePreparer) | 
|  | 293 | }) | 
|  | 294 |  | 
|  | 295 | t.Run("snapshot preferred with source", func(t *testing.T) { | 
|  | 296 | // Replace the snapshot/Android.bp file with one where "prefer: false," has been replaced with | 
|  | 297 | // "prefer: true," | 
|  | 298 | preferPrebuilts := android.FixtureModifyMockFS(func(fs android.MockFS) { | 
|  | 299 | snapshotBpFile := filepath.Join(snapshotSubDir, "Android.bp") | 
|  | 300 | unpreferred := string(fs[snapshotBpFile]) | 
|  | 301 | fs[snapshotBpFile] = []byte(strings.ReplaceAll(unpreferred, "prefer: false,", "prefer: true,")) | 
|  | 302 | }) | 
|  | 303 |  | 
|  | 304 | runSnapshotTestWithCheckers(t, checkSnapshotPreferredWithSource, preferPrebuilts) | 
| Paul Duffin | c93c98e | 2021-03-20 01:32:50 +0000 | [diff] [blame] | 305 | }) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
|  | 308 | type snapshotBuildInfoChecker func(info *snapshotBuildInfo) | 
|  | 309 |  | 
|  | 310 | // Check that the snapshot's generated Android.bp is correct. | 
|  | 311 | // | 
|  | 312 | // Both the expected and actual string are both trimmed before comparing. | 
|  | 313 | func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { | 
|  | 314 | return func(info *snapshotBuildInfo) { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 315 | info.t.Helper() | 
|  | 316 | android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 317 | } | 
|  | 318 | } | 
|  | 319 |  | 
| Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 320 | // Check that the snapshot's unversioned generated Android.bp is correct. | 
|  | 321 | // | 
|  | 322 | // This func should be used to check the general snapshot generation code. | 
|  | 323 | // | 
|  | 324 | // Both the expected and actual string are both trimmed before comparing. | 
|  | 325 | func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker { | 
|  | 326 | return func(info *snapshotBuildInfo) { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 327 | info.t.Helper() | 
|  | 328 | android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents) | 
| Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 329 | } | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | // Check that the snapshot's versioned generated Android.bp is correct. | 
|  | 333 | // | 
|  | 334 | // This func should only be used to check the version specific snapshot generation code, | 
|  | 335 | // i.e. the encoding of version into module names and the generation of the _snapshot module. The | 
|  | 336 | // general snapshot generation code should be checked using the checkUnversionedAndroidBpContents() | 
|  | 337 | // func. | 
|  | 338 | // | 
|  | 339 | // Both the expected and actual string are both trimmed before comparing. | 
|  | 340 | func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker { | 
|  | 341 | return func(info *snapshotBuildInfo) { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 342 | info.t.Helper() | 
|  | 343 | android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents) | 
| Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 344 | } | 
|  | 345 | } | 
|  | 346 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 347 | // Check that the snapshot's copy rules are correct. | 
|  | 348 | // | 
|  | 349 | // The copy rules are formatted as <src> -> <dest>, one per line and then compared | 
|  | 350 | // to the supplied expected string. Both the expected and actual string are trimmed | 
|  | 351 | // before comparing. | 
|  | 352 | func checkAllCopyRules(expected string) snapshotBuildInfoChecker { | 
|  | 353 | return func(info *snapshotBuildInfo) { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 354 | info.t.Helper() | 
|  | 355 | android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 356 | } | 
|  | 357 | } | 
|  | 358 |  | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 359 | func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker { | 
|  | 360 | return func(info *snapshotBuildInfo) { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 361 | info.t.Helper() | 
|  | 362 | android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules) | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 363 | } | 
|  | 364 | } | 
|  | 365 |  | 
| Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 366 | // Check that the specified paths match the list of zips to merge with the intermediate zip. | 
|  | 367 | func checkMergeZips(expected ...string) snapshotBuildInfoChecker { | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 368 | return func(info *snapshotBuildInfo) { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 369 | info.t.Helper() | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 370 | if info.intermediateZip == "" { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 371 | info.t.Errorf("No intermediate zip file was created") | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 372 | } | 
| Paul Duffin | 3d1248c | 2020-04-09 00:10:17 +0100 | [diff] [blame] | 373 |  | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 374 | android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips) | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 375 | } | 
|  | 376 | } | 
|  | 377 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 378 | type resultChecker func(t *testing.T, result *android.TestResult) | 
|  | 379 |  | 
| Paul Duffin | a57835e | 2021-04-19 13:23:06 +0100 | [diff] [blame] | 380 | // snapshotTestPreparer registers a preparer that will be used to customize the specified | 
|  | 381 | // snapshotTest. | 
|  | 382 | func snapshotTestPreparer(snapshotTest snapshotTest, preparer android.FixturePreparer) snapshotBuildInfoChecker { | 
|  | 383 | return func(info *snapshotBuildInfo) { | 
|  | 384 | customization := info.snapshotTestCustomization(snapshotTest) | 
|  | 385 | customization.preparers = append(customization.preparers, preparer) | 
|  | 386 | } | 
|  | 387 | } | 
|  | 388 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 389 | // snapshotTestChecker registers a checker that will be run against the result of processing the | 
|  | 390 | // generated snapshot for the specified snapshotTest. | 
|  | 391 | func snapshotTestChecker(snapshotTest snapshotTest, checker resultChecker) snapshotBuildInfoChecker { | 
|  | 392 | return func(info *snapshotBuildInfo) { | 
|  | 393 | customization := info.snapshotTestCustomization(snapshotTest) | 
|  | 394 | customization.checkers = append(customization.checkers, checker) | 
|  | 395 | } | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | // snapshotTestErrorHandler registers an error handler to use when processing the snapshot | 
|  | 399 | // in the specific test case. | 
|  | 400 | // | 
|  | 401 | // Generally, the snapshot should work with all the test cases but some do not and just in case | 
|  | 402 | // there are a lot of issues to resolve, or it will take a lot of time this is a | 
|  | 403 | // get-out-of-jail-free card that allows progress to be made. | 
|  | 404 | // | 
|  | 405 | // deprecated: should only be used as a temporary workaround with an attached to do and bug. | 
|  | 406 | func snapshotTestErrorHandler(snapshotTest snapshotTest, handler android.FixtureErrorHandler) snapshotBuildInfoChecker { | 
|  | 407 | return func(info *snapshotBuildInfo) { | 
|  | 408 | customization := info.snapshotTestCustomization(snapshotTest) | 
|  | 409 | customization.errorHandler = handler | 
|  | 410 | } | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | // Encapsulates information provided by each test to customize a specific snapshotTest. | 
|  | 414 | type snapshotTestCustomization struct { | 
| Paul Duffin | a57835e | 2021-04-19 13:23:06 +0100 | [diff] [blame] | 415 | // Preparers that are used to customize the test fixture before running the test. | 
|  | 416 | preparers []android.FixturePreparer | 
|  | 417 |  | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 418 | // Checkers that are run on the result of processing the preferred snapshot in a specific test | 
|  | 419 | // case. | 
|  | 420 | checkers []resultChecker | 
|  | 421 |  | 
|  | 422 | // Specify an error handler for when processing a specific test case. | 
|  | 423 | // | 
|  | 424 | // In some cases the generated snapshot cannot be used in a test configuration. Those cases are | 
|  | 425 | // invariably bugs that need to be resolved but sometimes that can take a while. This provides a | 
|  | 426 | // mechanism to temporarily ignore that error. | 
|  | 427 | errorHandler android.FixtureErrorHandler | 
|  | 428 | } | 
|  | 429 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 430 | // Encapsulates information about the snapshot build structure in order to insulate tests from | 
|  | 431 | // knowing too much about internal structures. | 
|  | 432 | // | 
|  | 433 | // All source/input paths are relative either the build directory. All dest/output paths are | 
|  | 434 | // relative to the snapshot root directory. | 
|  | 435 | type snapshotBuildInfo struct { | 
| Paul Duffin | 36474d3 | 2021-03-12 12:19:43 +0000 | [diff] [blame] | 436 | t *testing.T | 
|  | 437 |  | 
|  | 438 | // The result from RunTest() | 
| Paul Duffin | 4a2a29c | 2021-03-09 22:27:13 +0000 | [diff] [blame] | 439 | r *android.TestResult | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 440 |  | 
| Paul Duffin | 973bedb | 2021-05-05 22:00:51 +0100 | [diff] [blame^] | 441 | // The version of the generated snapshot. | 
|  | 442 | // | 
|  | 443 | // See snapshotBuilder.version for more information about this field. | 
|  | 444 | version string | 
|  | 445 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 446 | // The contents of the generated Android.bp file | 
|  | 447 | androidBpContents string | 
|  | 448 |  | 
| Paul Duffin | d075907 | 2021-02-17 11:23:00 +0000 | [diff] [blame] | 449 | // The contents of the unversioned Android.bp file | 
|  | 450 | androidUnversionedBpContents string | 
|  | 451 |  | 
|  | 452 | // The contents of the versioned Android.bp file | 
|  | 453 | androidVersionedBpContents string | 
|  | 454 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 455 | // The paths, relative to the snapshot root, of all files and directories copied into the | 
|  | 456 | // snapshot. | 
|  | 457 | snapshotContents []string | 
|  | 458 |  | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 459 | // A formatted representation of the src/dest pairs for a snapshot, one pair per line, | 
|  | 460 | // of the format src -> dest | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 461 | copyRules string | 
|  | 462 |  | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 463 | // A formatted representation of the src/dest pairs for files not in a snapshot, one pair | 
|  | 464 | // per line, of the format src -> dest | 
|  | 465 | otherCopyRules string | 
|  | 466 |  | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 467 | // The path to the intermediate zip, which is a zip created from the source files copied | 
|  | 468 | // into the snapshot directory and which will be merged with other zips to form the final output. | 
|  | 469 | // Is am empty string if there is no intermediate zip because there are no zips to merge in. | 
|  | 470 | intermediateZip string | 
|  | 471 |  | 
|  | 472 | // The paths to the zips to merge into the output zip, does not include the intermediate | 
|  | 473 | // zip. | 
|  | 474 | mergeZips []string | 
|  | 475 |  | 
|  | 476 | // The final output zip. | 
|  | 477 | outputZip string | 
| Paul Duffin | 1822a0a | 2021-03-21 12:56:33 +0000 | [diff] [blame] | 478 |  | 
|  | 479 | // The test specific customizations for each snapshot test. | 
|  | 480 | snapshotTestCustomizations map[snapshotTest]*snapshotTestCustomization | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | // snapshotTestCustomization gets the test specific customization for the specified snapshotTest. | 
|  | 484 | // | 
|  | 485 | // If no customization was created previously then it creates a default customization. | 
|  | 486 | func (i *snapshotBuildInfo) snapshotTestCustomization(snapshotTest snapshotTest) *snapshotTestCustomization { | 
|  | 487 | customization := i.snapshotTestCustomizations[snapshotTest] | 
|  | 488 | if customization == nil { | 
|  | 489 | customization = &snapshotTestCustomization{ | 
|  | 490 | errorHandler: android.FixtureExpectsNoErrors, | 
|  | 491 | } | 
|  | 492 | i.snapshotTestCustomizations[snapshotTest] = customization | 
|  | 493 | } | 
|  | 494 | return customization | 
| Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 495 | } |