host_test.go 4.8 KB

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