Rob Seymour | 925aa09 | 2021-08-10 20:42:03 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Android Open Source Project |
| 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 snapshot |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | // host_snapshot and host-fake-snapshot test functions |
| 25 | |
| 26 | type hostTestModule struct { |
| 27 | android.ModuleBase |
| 28 | props struct { |
| 29 | Deps []string |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func hostTestBinOut(bin string) string { |
| 34 | return filepath.Join("out", "bin", bin) |
| 35 | } |
| 36 | |
| 37 | func (c *hostTestModule) HostToolPath() android.OptionalPath { |
| 38 | return (android.OptionalPathForPath(android.PathForTesting(hostTestBinOut(c.Name())))) |
| 39 | } |
| 40 | |
| 41 | func hostTestModuleFactory() android.Module { |
| 42 | m := &hostTestModule{} |
| 43 | m.AddProperties(&m.props) |
| 44 | android.InitAndroidArchModule(m, android.HostSupported, android.MultilibFirst) |
| 45 | return m |
| 46 | } |
| 47 | func (m *hostTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 48 | builtFile := android.PathForModuleOut(ctx, m.Name()) |
| 49 | dir := ctx.Target().Arch.ArchType.Multilib |
| 50 | installDir := android.PathForModuleInstall(ctx, dir) |
| 51 | ctx.InstallFile(installDir, m.Name(), builtFile) |
| 52 | } |
| 53 | |
| 54 | // Common blueprint used for testing |
| 55 | var hostTestBp = ` |
| 56 | license_kind { |
| 57 | name: "test_notice", |
| 58 | conditions: ["notice"], |
| 59 | } |
| 60 | license { |
| 61 | name: "host_test_license", |
| 62 | visibility: ["//visibility:public"], |
| 63 | license_kinds: [ |
| 64 | "test_notice" |
| 65 | ], |
| 66 | license_text: [ |
| 67 | "NOTICE", |
| 68 | ], |
| 69 | } |
| 70 | component { |
| 71 | name: "foo", |
| 72 | deps: ["bar"], |
| 73 | } |
| 74 | component { |
| 75 | name: "bar", |
| 76 | licenses: ["host_test_license"], |
| 77 | } |
| 78 | ` |
| 79 | |
| 80 | var hostTestModBp = ` |
| 81 | host_snapshot { |
| 82 | name: "test-host-snapshot", |
| 83 | deps: [ |
| 84 | "foo", |
| 85 | ], |
| 86 | } |
| 87 | ` |
| 88 | |
| 89 | var prepareForHostTest = android.GroupFixturePreparers( |
| 90 | android.PrepareForTestWithAndroidBuildComponents, |
| 91 | android.PrepareForTestWithLicenses, |
| 92 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 93 | ctx.RegisterModuleType("component", hostTestModuleFactory) |
| 94 | }), |
| 95 | ) |
| 96 | |
| 97 | // Prepare for host_snapshot test |
| 98 | var prepareForHostModTest = android.GroupFixturePreparers( |
| 99 | prepareForHostTest, |
| 100 | android.FixtureWithRootAndroidBp(hostTestBp+hostTestModBp), |
| 101 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 102 | registerHostBuildComponents(ctx) |
| 103 | }), |
| 104 | ) |
| 105 | |
| 106 | // Prepare for fake host snapshot test disabled |
| 107 | var prepareForFakeHostTest = android.GroupFixturePreparers( |
| 108 | prepareForHostTest, |
| 109 | android.FixtureWithRootAndroidBp(hostTestBp), |
| 110 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 111 | registerHostSnapshotComponents(ctx) |
| 112 | }), |
| 113 | ) |
| 114 | |
| 115 | // Prepare for fake host snapshot test enabled |
| 116 | var prepareForFakeHostTestEnabled = android.GroupFixturePreparers( |
| 117 | prepareForFakeHostTest, |
| 118 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 119 | variables.HostFakeSnapshotEnabled = true |
| 120 | }), |
| 121 | ) |
| 122 | |
| 123 | // Validate that a hostSnapshot object is created containing zip files and JSON file |
| 124 | // content of zip file is not validated as this is done by PackagingSpecs |
| 125 | func TestHostSnapshot(t *testing.T) { |
| 126 | result := prepareForHostModTest.RunTest(t) |
| 127 | t.Helper() |
| 128 | ctx := result.TestContext.ModuleForTests("test-host-snapshot", result.Config.BuildOS.String()+"_common") |
| 129 | mod := ctx.Module().(*hostSnapshot) |
| 130 | if ctx.MaybeOutput("host_snapshot.json").Rule == nil { |
| 131 | t.Error("Manifest file not found") |
| 132 | } |
| 133 | zips := []string{"_deps.zip", "_mods.zip", ".zip"} |
| 134 | |
| 135 | for _, zip := range zips { |
| 136 | zFile := mod.Name() + zip |
| 137 | if ctx.MaybeOutput(zFile).Rule == nil { |
| 138 | t.Error("Zip file ", zFile, "not found") |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Validate fake host snapshot contains binary modules as well as the JSON meta file |
| 145 | func TestFakeHostSnapshotEnable(t *testing.T) { |
| 146 | result := prepareForFakeHostTestEnabled.RunTest(t) |
| 147 | t.Helper() |
| 148 | bins := []string{"foo", "bar"} |
| 149 | ctx := result.TestContext.SingletonForTests("host-fake-snapshot") |
| 150 | if ctx.MaybeOutput(filepath.Join("host-fake-snapshot", "host_snapshot.json")).Rule == nil { |
| 151 | t.Error("Manifest file not found") |
| 152 | } |
| 153 | for _, bin := range bins { |
| 154 | if ctx.MaybeOutput(filepath.Join("host-fake-snapshot", hostTestBinOut(bin))).Rule == nil { |
| 155 | t.Error("Binary file ", bin, "not found") |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Validate not fake host snapshot if HostFakeSnapshotEnabled has not been set to true |
| 162 | func TestFakeHostSnapshotDisable(t *testing.T) { |
| 163 | result := prepareForFakeHostTest.RunTest(t) |
| 164 | t.Helper() |
| 165 | ctx := result.TestContext.SingletonForTests("host-fake-snapshot") |
| 166 | if len(ctx.AllOutputs()) != 0 { |
| 167 | t.Error("Fake host snapshot not empty when disabled") |
| 168 | } |
| 169 | |
| 170 | } |