blob: 9c3db3bfbadb8b7374e3f445995477827c3c7dfa [file] [log] [blame]
Tao Bao0ba5c942018-08-14 22:20:22 -07001// 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
Jaewoong Jung4b79e982020-06-01 10:45:49 -070015package etc
Tao Bao0ba5c942018-08-14 22:20:22 -070016
17import (
Jaewoong Jung4b79e982020-06-01 10:45:49 -070018 "os"
Patrice Arruda300cef92019-02-22 15:47:57 -080019 "path/filepath"
Tao Bao0ba5c942018-08-14 22:20:22 -070020 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070021
22 "android/soong/android"
Tao Bao0ba5c942018-08-14 22:20:22 -070023)
24
Jaewoong Jung4b79e982020-06-01 10:45:49 -070025func TestMain(m *testing.M) {
Paul Duffin5f9f7712021-03-15 15:35:49 +000026 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070027}
28
Paul Duffin89648f92021-03-20 00:36:55 +000029var prepareForPrebuiltEtcTest = android.GroupFixturePreparers(
Paul Duffin1172fed2021-03-08 11:28:18 +000030 android.PrepareForTestWithArchMutator,
31 PrepareForTestWithPrebuiltEtc,
32 android.FixtureMergeMockFs(android.MockFS{
Colin Cross98be1bb2019-12-13 20:41:13 -080033 "foo.conf": nil,
34 "bar.conf": nil,
35 "baz.conf": nil,
Paul Duffin1172fed2021-03-08 11:28:18 +000036 }),
37)
Colin Cross98be1bb2019-12-13 20:41:13 -080038
Tao Bao0ba5c942018-08-14 22:20:22 -070039func TestPrebuiltEtcVariants(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +000040 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070041 prebuilt_etc {
42 name: "foo.conf",
43 src: "foo.conf",
44 }
45 prebuilt_etc {
46 name: "bar.conf",
47 src: "bar.conf",
48 recovery_available: true,
49 }
50 prebuilt_etc {
51 name: "baz.conf",
52 src: "baz.conf",
53 recovery: true,
54 }
55 `)
56
Paul Duffin921fac72021-03-10 09:00:58 +000057 foo_variants := result.ModuleVariantsForTests("foo.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070058 if len(foo_variants) != 1 {
59 t.Errorf("expected 1, got %#v", foo_variants)
60 }
61
Paul Duffin921fac72021-03-10 09:00:58 +000062 bar_variants := result.ModuleVariantsForTests("bar.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070063 if len(bar_variants) != 2 {
64 t.Errorf("expected 2, got %#v", bar_variants)
65 }
66
Paul Duffin921fac72021-03-10 09:00:58 +000067 baz_variants := result.ModuleVariantsForTests("baz.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070068 if len(baz_variants) != 1 {
69 t.Errorf("expected 1, got %#v", bar_variants)
70 }
71}
Jiyong Park139a2e62018-10-26 21:49:39 +090072
73func TestPrebuiltEtcOutputPath(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +000074 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +090075 prebuilt_etc {
76 name: "foo.conf",
77 src: "foo.conf",
78 filename: "foo.installed.conf",
79 }
80 `)
81
Paul Duffin921fac72021-03-10 09:00:58 +000082 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffine84b1332021-03-12 11:59:43 +000083 android.AssertStringEquals(t, "output file path", "foo.installed.conf", p.outputFilePath.Base())
Jiyong Park139a2e62018-10-26 21:49:39 +090084}
Jiyong Park1a7cf082018-11-13 11:59:12 +090085
86func TestPrebuiltEtcGlob(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +000087 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +090088 prebuilt_etc {
89 name: "my_foo",
90 src: "foo.*",
91 }
92 prebuilt_etc {
93 name: "my_bar",
94 src: "bar.*",
95 filename_from_src: true,
96 }
97 `)
98
Paul Duffin921fac72021-03-10 09:00:58 +000099 p := result.Module("my_foo", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffine84b1332021-03-12 11:59:43 +0000100 android.AssertStringEquals(t, "my_foo output file path", "my_foo", p.outputFilePath.Base())
Jiyong Park1a7cf082018-11-13 11:59:12 +0900101
Paul Duffin921fac72021-03-10 09:00:58 +0000102 p = result.Module("my_bar", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffine84b1332021-03-12 11:59:43 +0000103 android.AssertStringEquals(t, "my_bar output file path", "bar.conf", p.outputFilePath.Base())
Jiyong Park1a7cf082018-11-13 11:59:12 +0900104}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000105
106func TestPrebuiltEtcAndroidMk(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000107 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000108 prebuilt_etc {
109 name: "foo",
110 src: "foo.conf",
111 owner: "abc",
112 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700113 required: ["modA", "moduleB"],
114 host_required: ["hostModA", "hostModB"],
115 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000116 }
117 `)
118
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700119 expected := map[string][]string{
120 "LOCAL_MODULE": {"foo"},
121 "LOCAL_MODULE_CLASS": {"ETC"},
122 "LOCAL_MODULE_OWNER": {"abc"},
123 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
124 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
125 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
126 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000127 }
128
Paul Duffin921fac72021-03-10 09:00:58 +0000129 mod := result.Module("foo", "android_arm64_armv8-a").(*PrebuiltEtc)
130 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700131 for k, expectedValue := range expected {
132 if value, ok := entries.EntryMap[k]; ok {
Paul Duffine84b1332021-03-12 11:59:43 +0000133 android.AssertDeepEquals(t, k, expectedValue, value)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700134 } else {
135 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000136 }
137 }
138}
Jaewoong Jung24788182019-02-04 14:34:10 -0800139
Liz Kammer0449a632020-06-26 10:12:36 -0700140func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000141 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Liz Kammer0449a632020-06-26 10:12:36 -0700142 prebuilt_etc {
143 name: "foo.conf",
144 src: "foo.conf",
145 relative_install_path: "bar",
146 }
147 `)
148
Paul Duffin921fac72021-03-10 09:00:58 +0000149 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000150 expected := "out/soong/target/product/test_device/system/etc/bar"
151 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Liz Kammer0449a632020-06-26 10:12:36 -0700152}
153
154func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000155 prepareForPrebuiltEtcTest.
Paul Duffin1172fed2021-03-08 11:28:18 +0000156 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")).
157 RunTestWithBp(t, `
158 prebuilt_etc {
159 name: "foo.conf",
160 src: "foo.conf",
161 sub_dir: "bar",
162 relative_install_path: "bar",
163 }
164 `)
Liz Kammer0449a632020-06-26 10:12:36 -0700165}
166
Jaewoong Jung24788182019-02-04 14:34:10 -0800167func TestPrebuiltEtcHost(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000168 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800169 prebuilt_etc_host {
170 name: "foo.conf",
171 src: "foo.conf",
172 }
173 `)
174
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700175 buildOS := android.BuildOs.String()
Paul Duffin921fac72021-03-10 09:00:58 +0000176 p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
Jaewoong Jung24788182019-02-04 14:34:10 -0800177 if !p.Host() {
178 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
179 }
180}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800181
182func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000183 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800184 prebuilt_usr_share {
185 name: "foo.conf",
186 src: "foo.conf",
187 sub_dir: "bar",
188 }
189 `)
190
Paul Duffin921fac72021-03-10 09:00:58 +0000191 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000192 expected := "out/soong/target/product/test_device/system/usr/share/bar"
193 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800194}
Patrice Arruda300cef92019-02-22 15:47:57 -0800195
196func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000197 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Patrice Arruda300cef92019-02-22 15:47:57 -0800198 prebuilt_usr_share_host {
199 name: "foo.conf",
200 src: "foo.conf",
201 sub_dir: "bar",
202 }
203 `)
204
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700205 buildOS := android.BuildOs.String()
Paul Duffin921fac72021-03-10 09:00:58 +0000206 p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000207 expected := filepath.Join("out/soong/host", result.Config.PrebuiltOS(), "usr", "share", "bar")
208 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Patrice Arruda300cef92019-02-22 15:47:57 -0800209}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700210
211func TestPrebuiltFontInstallDirPath(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000212 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
Patrice Arruda61583eb2019-05-14 08:20:45 -0700213 prebuilt_font {
214 name: "foo.conf",
215 src: "foo.conf",
216 }
217 `)
218
Paul Duffin921fac72021-03-10 09:00:58 +0000219 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000220 expected := "out/soong/target/product/test_device/system/fonts"
221 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700222}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700223
224func TestPrebuiltFirmwareDirPath(t *testing.T) {
Paul Duffin5f9f7712021-03-15 15:35:49 +0000225 targetPath := "out/soong/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700226 tests := []struct {
227 description string
228 config string
229 expectedPath string
230 }{{
231 description: "prebuilt: system firmware",
232 config: `
233 prebuilt_firmware {
234 name: "foo.conf",
235 src: "foo.conf",
236 }`,
237 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
238 }, {
239 description: "prebuilt: vendor firmware",
240 config: `
241 prebuilt_firmware {
242 name: "foo.conf",
243 src: "foo.conf",
244 soc_specific: true,
245 sub_dir: "sub_dir",
246 }`,
247 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
248 }}
249 for _, tt := range tests {
250 t.Run(tt.description, func(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000251 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, tt.config)
Paul Duffin921fac72021-03-10 09:00:58 +0000252 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000253 android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700254 })
255 }
256}
Patrice Arruda0f688002020-06-08 21:40:25 +0000257
258func TestPrebuiltDSPDirPath(t *testing.T) {
Paul Duffin5f9f7712021-03-15 15:35:49 +0000259 targetPath := "out/soong/target/product/test_device"
Patrice Arruda0f688002020-06-08 21:40:25 +0000260 tests := []struct {
261 description string
262 config string
263 expectedPath string
264 }{{
265 description: "prebuilt: system dsp",
266 config: `
267 prebuilt_dsp {
268 name: "foo.conf",
269 src: "foo.conf",
270 }`,
271 expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
272 }, {
273 description: "prebuilt: vendor dsp",
274 config: `
275 prebuilt_dsp {
276 name: "foo.conf",
277 src: "foo.conf",
278 soc_specific: true,
279 sub_dir: "sub_dir",
280 }`,
281 expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
282 }}
283 for _, tt := range tests {
284 t.Run(tt.description, func(t *testing.T) {
Paul Duffin89648f92021-03-20 00:36:55 +0000285 result := prepareForPrebuiltEtcTest.RunTestWithBp(t, tt.config)
Paul Duffin921fac72021-03-10 09:00:58 +0000286 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000287 android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
Patrice Arruda0f688002020-06-08 21:40:25 +0000288 })
289 }
290}