123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- // Copyright 2021 The Android Open Source Project
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package snapshot
- import (
- "path/filepath"
- "testing"
- "android/soong/android"
- )
- // host_snapshot and host-fake-snapshot test functions
- type hostTestModule struct {
- android.ModuleBase
- props struct {
- Deps []string
- }
- }
- func hostTestBinOut(bin string) string {
- return filepath.Join("out", "bin", bin)
- }
- func (c *hostTestModule) HostToolPath() android.OptionalPath {
- return (android.OptionalPathForPath(android.PathForTesting(hostTestBinOut(c.Name()))))
- }
- func hostTestModuleFactory() android.Module {
- m := &hostTestModule{}
- m.AddProperties(&m.props)
- android.InitAndroidArchModule(m, android.HostSupported, android.MultilibFirst)
- return m
- }
- func (m *hostTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- builtFile := android.PathForModuleOut(ctx, m.Name())
- dir := ctx.Target().Arch.ArchType.Multilib
- installDir := android.PathForModuleInstall(ctx, dir)
- ctx.InstallFile(installDir, m.Name(), builtFile)
- }
- // Common blueprint used for testing
- var hostTestBp = `
- license_kind {
- name: "test_notice",
- conditions: ["notice"],
- }
- license {
- name: "host_test_license",
- visibility: ["//visibility:public"],
- license_kinds: [
- "test_notice"
- ],
- license_text: [
- "NOTICE",
- ],
- }
- component {
- name: "foo",
- deps: ["bar"],
- }
- component {
- name: "bar",
- licenses: ["host_test_license"],
- }
- `
- var hostTestModBp = `
- host_snapshot {
- name: "test-host-snapshot",
- deps: [
- "foo",
- ],
- }
- `
- var prepareForHostTest = android.GroupFixturePreparers(
- android.PrepareForTestWithAndroidBuildComponents,
- android.PrepareForTestWithLicenses,
- android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
- ctx.RegisterModuleType("component", hostTestModuleFactory)
- }),
- )
- // Prepare for host_snapshot test
- var prepareForHostModTest = android.GroupFixturePreparers(
- prepareForHostTest,
- android.FixtureWithRootAndroidBp(hostTestBp+hostTestModBp),
- android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
- registerHostBuildComponents(ctx)
- }),
- )
- // Prepare for fake host snapshot test disabled
- var prepareForFakeHostTest = android.GroupFixturePreparers(
- prepareForHostTest,
- android.FixtureWithRootAndroidBp(hostTestBp),
- android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
- registerHostSnapshotComponents(ctx)
- }),
- )
- // Prepare for fake host snapshot test enabled
- var prepareForFakeHostTestEnabled = android.GroupFixturePreparers(
- prepareForFakeHostTest,
- android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
- variables.HostFakeSnapshotEnabled = true
- }),
- )
- // Validate that a hostSnapshot object is created containing zip files and JSON file
- // content of zip file is not validated as this is done by PackagingSpecs
- func TestHostSnapshot(t *testing.T) {
- result := prepareForHostModTest.RunTest(t)
- t.Helper()
- ctx := result.TestContext.ModuleForTests("test-host-snapshot", result.Config.BuildOS.String()+"_common")
- mod := ctx.Module().(*hostSnapshot)
- if ctx.MaybeOutput("host_snapshot.json").Rule == nil {
- t.Error("Manifest file not found")
- }
- zips := []string{"_deps.zip", "_mods.zip", ".zip"}
- for _, zip := range zips {
- zFile := mod.Name() + zip
- if ctx.MaybeOutput(zFile).Rule == nil {
- t.Error("Zip file ", zFile, "not found")
- }
- }
- }
- // Validate fake host snapshot contains binary modules as well as the JSON meta file
- func TestFakeHostSnapshotEnable(t *testing.T) {
- result := prepareForFakeHostTestEnabled.RunTest(t)
- t.Helper()
- bins := []string{"foo", "bar"}
- ctx := result.TestContext.SingletonForTests("host-fake-snapshot")
- if ctx.MaybeOutput(filepath.Join("host-fake-snapshot", "host_snapshot.json")).Rule == nil {
- t.Error("Manifest file not found")
- }
- for _, bin := range bins {
- if ctx.MaybeOutput(filepath.Join("host-fake-snapshot", hostTestBinOut(bin))).Rule == nil {
- t.Error("Binary file ", bin, "not found")
- }
- }
- }
- // Validate not fake host snapshot if HostFakeSnapshotEnabled has not been set to true
- func TestFakeHostSnapshotDisable(t *testing.T) {
- result := prepareForFakeHostTest.RunTest(t)
- t.Helper()
- ctx := result.TestContext.SingletonForTests("host-fake-snapshot")
- if len(ctx.AllOutputs()) != 0 {
- t.Error("Fake host snapshot not empty when disabled")
- }
- }
|