prebuilt_etc_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright 2018 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. package etc
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. "android/soong/android"
  22. )
  23. var buildDir string
  24. func setUp() {
  25. var err error
  26. buildDir, err = ioutil.TempDir("", "soong_etc_test")
  27. if err != nil {
  28. panic(err)
  29. }
  30. }
  31. func tearDown() {
  32. os.RemoveAll(buildDir)
  33. }
  34. func TestMain(m *testing.M) {
  35. run := func() int {
  36. setUp()
  37. defer tearDown()
  38. return m.Run()
  39. }
  40. os.Exit(run())
  41. }
  42. func testPrebuiltEtcContext(t *testing.T, bp string) (*android.TestContext, android.Config) {
  43. fs := map[string][]byte{
  44. "foo.conf": nil,
  45. "bar.conf": nil,
  46. "baz.conf": nil,
  47. }
  48. config := android.TestArchConfig(buildDir, nil, bp, fs)
  49. ctx := android.NewTestArchContext()
  50. ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
  51. ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
  52. ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
  53. ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
  54. ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
  55. ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
  56. ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
  57. ctx.Register(config)
  58. return ctx, config
  59. }
  60. func testPrebuiltEtc(t *testing.T, bp string) (*android.TestContext, android.Config) {
  61. t.Helper()
  62. ctx, config := testPrebuiltEtcContext(t, bp)
  63. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  64. android.FailIfErrored(t, errs)
  65. _, errs = ctx.PrepareBuildActions(config)
  66. android.FailIfErrored(t, errs)
  67. return ctx, config
  68. }
  69. func testPrebuiltEtcError(t *testing.T, pattern, bp string) {
  70. t.Helper()
  71. ctx, config := testPrebuiltEtcContext(t, bp)
  72. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  73. if len(errs) > 0 {
  74. android.FailIfNoMatchingErrors(t, pattern, errs)
  75. return
  76. }
  77. _, errs = ctx.PrepareBuildActions(config)
  78. if len(errs) > 0 {
  79. android.FailIfNoMatchingErrors(t, pattern, errs)
  80. return
  81. }
  82. t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
  83. }
  84. func TestPrebuiltEtcVariants(t *testing.T) {
  85. ctx, _ := testPrebuiltEtc(t, `
  86. prebuilt_etc {
  87. name: "foo.conf",
  88. src: "foo.conf",
  89. }
  90. prebuilt_etc {
  91. name: "bar.conf",
  92. src: "bar.conf",
  93. recovery_available: true,
  94. }
  95. prebuilt_etc {
  96. name: "baz.conf",
  97. src: "baz.conf",
  98. recovery: true,
  99. }
  100. `)
  101. foo_variants := ctx.ModuleVariantsForTests("foo.conf")
  102. if len(foo_variants) != 1 {
  103. t.Errorf("expected 1, got %#v", foo_variants)
  104. }
  105. bar_variants := ctx.ModuleVariantsForTests("bar.conf")
  106. if len(bar_variants) != 2 {
  107. t.Errorf("expected 2, got %#v", bar_variants)
  108. }
  109. baz_variants := ctx.ModuleVariantsForTests("baz.conf")
  110. if len(baz_variants) != 1 {
  111. t.Errorf("expected 1, got %#v", bar_variants)
  112. }
  113. }
  114. func TestPrebuiltEtcOutputPath(t *testing.T) {
  115. ctx, _ := testPrebuiltEtc(t, `
  116. prebuilt_etc {
  117. name: "foo.conf",
  118. src: "foo.conf",
  119. filename: "foo.installed.conf",
  120. }
  121. `)
  122. p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  123. if p.outputFilePath.Base() != "foo.installed.conf" {
  124. t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
  125. }
  126. }
  127. func TestPrebuiltEtcGlob(t *testing.T) {
  128. ctx, _ := testPrebuiltEtc(t, `
  129. prebuilt_etc {
  130. name: "my_foo",
  131. src: "foo.*",
  132. }
  133. prebuilt_etc {
  134. name: "my_bar",
  135. src: "bar.*",
  136. filename_from_src: true,
  137. }
  138. `)
  139. p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  140. if p.outputFilePath.Base() != "my_foo" {
  141. t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
  142. }
  143. p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  144. if p.outputFilePath.Base() != "bar.conf" {
  145. t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
  146. }
  147. }
  148. func TestPrebuiltEtcAndroidMk(t *testing.T) {
  149. ctx, config := testPrebuiltEtc(t, `
  150. prebuilt_etc {
  151. name: "foo",
  152. src: "foo.conf",
  153. owner: "abc",
  154. filename_from_src: true,
  155. required: ["modA", "moduleB"],
  156. host_required: ["hostModA", "hostModB"],
  157. target_required: ["targetModA"],
  158. }
  159. `)
  160. expected := map[string][]string{
  161. "LOCAL_MODULE": {"foo"},
  162. "LOCAL_MODULE_CLASS": {"ETC"},
  163. "LOCAL_MODULE_OWNER": {"abc"},
  164. "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
  165. "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
  166. "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
  167. "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
  168. }
  169. mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  170. entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
  171. for k, expectedValue := range expected {
  172. if value, ok := entries.EntryMap[k]; ok {
  173. if !reflect.DeepEqual(value, expectedValue) {
  174. t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
  175. }
  176. } else {
  177. t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
  178. }
  179. }
  180. }
  181. func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
  182. ctx, _ := testPrebuiltEtc(t, `
  183. prebuilt_etc {
  184. name: "foo.conf",
  185. src: "foo.conf",
  186. relative_install_path: "bar",
  187. }
  188. `)
  189. p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  190. expected := buildDir + "/target/product/test_device/system/etc/bar"
  191. if p.installDirPath.String() != expected {
  192. t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
  193. }
  194. }
  195. func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
  196. testPrebuiltEtcError(t, "relative_install_path is set. Cannot set sub_dir", `
  197. prebuilt_etc {
  198. name: "foo.conf",
  199. src: "foo.conf",
  200. sub_dir: "bar",
  201. relative_install_path: "bar",
  202. }
  203. `)
  204. }
  205. func TestPrebuiltEtcHost(t *testing.T) {
  206. ctx, _ := testPrebuiltEtc(t, `
  207. prebuilt_etc_host {
  208. name: "foo.conf",
  209. src: "foo.conf",
  210. }
  211. `)
  212. buildOS := android.BuildOs.String()
  213. p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
  214. if !p.Host() {
  215. t.Errorf("host bit is not set for a prebuilt_etc_host module.")
  216. }
  217. }
  218. func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
  219. ctx, _ := testPrebuiltEtc(t, `
  220. prebuilt_usr_share {
  221. name: "foo.conf",
  222. src: "foo.conf",
  223. sub_dir: "bar",
  224. }
  225. `)
  226. p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  227. expected := buildDir + "/target/product/test_device/system/usr/share/bar"
  228. if p.installDirPath.String() != expected {
  229. t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
  230. }
  231. }
  232. func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
  233. ctx, config := testPrebuiltEtc(t, `
  234. prebuilt_usr_share_host {
  235. name: "foo.conf",
  236. src: "foo.conf",
  237. sub_dir: "bar",
  238. }
  239. `)
  240. buildOS := android.BuildOs.String()
  241. p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
  242. expected := filepath.Join(buildDir, "host", config.PrebuiltOS(), "usr", "share", "bar")
  243. if p.installDirPath.String() != expected {
  244. t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
  245. }
  246. }
  247. func TestPrebuiltFontInstallDirPath(t *testing.T) {
  248. ctx, _ := testPrebuiltEtc(t, `
  249. prebuilt_font {
  250. name: "foo.conf",
  251. src: "foo.conf",
  252. }
  253. `)
  254. p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  255. expected := buildDir + "/target/product/test_device/system/fonts"
  256. if p.installDirPath.String() != expected {
  257. t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
  258. }
  259. }
  260. func TestPrebuiltFirmwareDirPath(t *testing.T) {
  261. targetPath := buildDir + "/target/product/test_device"
  262. tests := []struct {
  263. description string
  264. config string
  265. expectedPath string
  266. }{{
  267. description: "prebuilt: system firmware",
  268. config: `
  269. prebuilt_firmware {
  270. name: "foo.conf",
  271. src: "foo.conf",
  272. }`,
  273. expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
  274. }, {
  275. description: "prebuilt: vendor firmware",
  276. config: `
  277. prebuilt_firmware {
  278. name: "foo.conf",
  279. src: "foo.conf",
  280. soc_specific: true,
  281. sub_dir: "sub_dir",
  282. }`,
  283. expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
  284. }}
  285. for _, tt := range tests {
  286. t.Run(tt.description, func(t *testing.T) {
  287. ctx, _ := testPrebuiltEtc(t, tt.config)
  288. p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  289. if p.installDirPath.String() != tt.expectedPath {
  290. t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
  291. }
  292. })
  293. }
  294. }
  295. func TestPrebuiltDSPDirPath(t *testing.T) {
  296. targetPath := filepath.Join(buildDir, "/target/product/test_device")
  297. tests := []struct {
  298. description string
  299. config string
  300. expectedPath string
  301. }{{
  302. description: "prebuilt: system dsp",
  303. config: `
  304. prebuilt_dsp {
  305. name: "foo.conf",
  306. src: "foo.conf",
  307. }`,
  308. expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
  309. }, {
  310. description: "prebuilt: vendor dsp",
  311. config: `
  312. prebuilt_dsp {
  313. name: "foo.conf",
  314. src: "foo.conf",
  315. soc_specific: true,
  316. sub_dir: "sub_dir",
  317. }`,
  318. expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
  319. }}
  320. for _, tt := range tests {
  321. t.Run(tt.description, func(t *testing.T) {
  322. ctx, _ := testPrebuiltEtc(t, tt.config)
  323. p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
  324. if p.installDirPath.String() != tt.expectedPath {
  325. t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
  326. }
  327. })
  328. }
  329. }