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