blob: 87b3adf3e6bb45a82553b0d359b1003a4874c4b9 [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 "io/ioutil"
19 "os"
Patrice Arruda300cef92019-02-22 15:47:57 -080020 "path/filepath"
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070021 "reflect"
Tao Bao0ba5c942018-08-14 22:20:22 -070022 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070023
24 "android/soong/android"
Tao Bao0ba5c942018-08-14 22:20:22 -070025)
26
Jaewoong Jung4b79e982020-06-01 10:45:49 -070027var buildDir string
28
29func setUp() {
30 var err error
31 buildDir, err = ioutil.TempDir("", "soong_etc_test")
32 if err != nil {
33 panic(err)
34 }
35}
36
37func tearDown() {
38 os.RemoveAll(buildDir)
39}
40
41func TestMain(m *testing.M) {
42 run := func() int {
43 setUp()
44 defer tearDown()
45
46 return m.Run()
47 }
48
49 os.Exit(run())
50}
51
Paul Duffin1172fed2021-03-08 11:28:18 +000052var prebuiltEtcFixtureFactory = android.NewFixtureFactory(
53 &buildDir,
54 android.PrepareForTestWithArchMutator,
55 PrepareForTestWithPrebuiltEtc,
56 android.FixtureMergeMockFs(android.MockFS{
Colin Cross98be1bb2019-12-13 20:41:13 -080057 "foo.conf": nil,
58 "bar.conf": nil,
59 "baz.conf": nil,
Paul Duffin1172fed2021-03-08 11:28:18 +000060 }),
61)
Colin Cross98be1bb2019-12-13 20:41:13 -080062
Paul Duffin1172fed2021-03-08 11:28:18 +000063// testPrebuiltEtc runs tests using the prebuiltEtcFixtureFactory
64//
65// Do not add any new usages of this, instead use the prebuiltEtcFixtureFactory directly as it
66// makes it much easier to customize the test behavior.
67//
68// If it is necessary to customize the behavior of an existing test that uses this then please first
69// convert the test to using prebuiltEtcFixtureFactory first and then in a following change add the
70// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
71// that it did not change the test behavior unexpectedly.
72//
73// deprecated
Liz Kammer0449a632020-06-26 10:12:36 -070074func testPrebuiltEtc(t *testing.T, bp string) (*android.TestContext, android.Config) {
75 t.Helper()
Paul Duffin1172fed2021-03-08 11:28:18 +000076 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, bp)
77 return result.TestContext, result.Config
Tao Bao0ba5c942018-08-14 22:20:22 -070078}
79
Tao Bao0ba5c942018-08-14 22:20:22 -070080func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080081 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070082 prebuilt_etc {
83 name: "foo.conf",
84 src: "foo.conf",
85 }
86 prebuilt_etc {
87 name: "bar.conf",
88 src: "bar.conf",
89 recovery_available: true,
90 }
91 prebuilt_etc {
92 name: "baz.conf",
93 src: "baz.conf",
94 recovery: true,
95 }
96 `)
97
98 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
99 if len(foo_variants) != 1 {
100 t.Errorf("expected 1, got %#v", foo_variants)
101 }
102
103 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
104 if len(bar_variants) != 2 {
105 t.Errorf("expected 2, got %#v", bar_variants)
106 }
107
108 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
109 if len(baz_variants) != 1 {
110 t.Errorf("expected 1, got %#v", bar_variants)
111 }
112}
Jiyong Park139a2e62018-10-26 21:49:39 +0900113
114func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800115 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900116 prebuilt_etc {
117 name: "foo.conf",
118 src: "foo.conf",
119 filename: "foo.installed.conf",
120 }
121 `)
122
Colin Cross7113d202019-11-20 16:39:12 -0800123 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900124 if p.outputFilePath.Base() != "foo.installed.conf" {
125 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
126 }
127}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900128
129func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800130 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900131 prebuilt_etc {
132 name: "my_foo",
133 src: "foo.*",
134 }
135 prebuilt_etc {
136 name: "my_bar",
137 src: "bar.*",
138 filename_from_src: true,
139 }
140 `)
141
Colin Cross7113d202019-11-20 16:39:12 -0800142 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900143 if p.outputFilePath.Base() != "my_foo" {
144 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
145 }
146
Colin Cross7113d202019-11-20 16:39:12 -0800147 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900148 if p.outputFilePath.Base() != "bar.conf" {
149 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
150 }
151}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000152
153func TestPrebuiltEtcAndroidMk(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700154 ctx, _ := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000155 prebuilt_etc {
156 name: "foo",
157 src: "foo.conf",
158 owner: "abc",
159 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700160 required: ["modA", "moduleB"],
161 host_required: ["hostModA", "hostModB"],
162 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000163 }
164 `)
165
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700166 expected := map[string][]string{
167 "LOCAL_MODULE": {"foo"},
168 "LOCAL_MODULE_CLASS": {"ETC"},
169 "LOCAL_MODULE_OWNER": {"abc"},
170 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
171 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
172 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
173 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000174 }
175
Colin Cross7113d202019-11-20 16:39:12 -0800176 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossaa255532020-07-03 13:18:24 -0700177 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700178 for k, expectedValue := range expected {
179 if value, ok := entries.EntryMap[k]; ok {
180 if !reflect.DeepEqual(value, expectedValue) {
181 t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000182 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700183 } else {
184 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000185 }
186 }
187}
Jaewoong Jung24788182019-02-04 14:34:10 -0800188
Liz Kammer0449a632020-06-26 10:12:36 -0700189func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
190 ctx, _ := testPrebuiltEtc(t, `
191 prebuilt_etc {
192 name: "foo.conf",
193 src: "foo.conf",
194 relative_install_path: "bar",
195 }
196 `)
197
198 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
199 expected := buildDir + "/target/product/test_device/system/etc/bar"
200 if p.installDirPath.String() != expected {
201 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
202 }
203}
204
205func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
Paul Duffin1172fed2021-03-08 11:28:18 +0000206 prebuiltEtcFixtureFactory.
207 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")).
208 RunTestWithBp(t, `
209 prebuilt_etc {
210 name: "foo.conf",
211 src: "foo.conf",
212 sub_dir: "bar",
213 relative_install_path: "bar",
214 }
215 `)
Liz Kammer0449a632020-06-26 10:12:36 -0700216}
217
Jaewoong Jung24788182019-02-04 14:34:10 -0800218func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800219 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800220 prebuilt_etc_host {
221 name: "foo.conf",
222 src: "foo.conf",
223 }
224 `)
225
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700226 buildOS := android.BuildOs.String()
Jaewoong Jung24788182019-02-04 14:34:10 -0800227 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
228 if !p.Host() {
229 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
230 }
231}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800232
233func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800234 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800235 prebuilt_usr_share {
236 name: "foo.conf",
237 src: "foo.conf",
238 sub_dir: "bar",
239 }
240 `)
241
Colin Cross7113d202019-11-20 16:39:12 -0800242 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700243 expected := buildDir + "/target/product/test_device/system/usr/share/bar"
244 if p.installDirPath.String() != expected {
245 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800246 }
247}
Patrice Arruda300cef92019-02-22 15:47:57 -0800248
249func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
250 ctx, config := testPrebuiltEtc(t, `
251 prebuilt_usr_share_host {
252 name: "foo.conf",
253 src: "foo.conf",
254 sub_dir: "bar",
255 }
256 `)
257
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700258 buildOS := android.BuildOs.String()
Patrice Arruda300cef92019-02-22 15:47:57 -0800259 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700260 expected := filepath.Join(buildDir, "host", config.PrebuiltOS(), "usr", "share", "bar")
261 if p.installDirPath.String() != expected {
262 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda300cef92019-02-22 15:47:57 -0800263 }
264}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700265
266func TestPrebuiltFontInstallDirPath(t *testing.T) {
267 ctx, _ := testPrebuiltEtc(t, `
268 prebuilt_font {
269 name: "foo.conf",
270 src: "foo.conf",
271 }
272 `)
273
Colin Cross7113d202019-11-20 16:39:12 -0800274 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700275 expected := buildDir + "/target/product/test_device/system/fonts"
276 if p.installDirPath.String() != expected {
277 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda61583eb2019-05-14 08:20:45 -0700278 }
279}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700280
281func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700282 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700283 tests := []struct {
284 description string
285 config string
286 expectedPath string
287 }{{
288 description: "prebuilt: system firmware",
289 config: `
290 prebuilt_firmware {
291 name: "foo.conf",
292 src: "foo.conf",
293 }`,
294 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
295 }, {
296 description: "prebuilt: vendor firmware",
297 config: `
298 prebuilt_firmware {
299 name: "foo.conf",
300 src: "foo.conf",
301 soc_specific: true,
302 sub_dir: "sub_dir",
303 }`,
304 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
305 }}
306 for _, tt := range tests {
307 t.Run(tt.description, func(t *testing.T) {
308 ctx, _ := testPrebuiltEtc(t, tt.config)
Colin Cross7113d202019-11-20 16:39:12 -0800309 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700310 if p.installDirPath.String() != tt.expectedPath {
Patrice Arruda057a8b12019-06-03 15:29:27 -0700311 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
312 }
313 })
314 }
315}
Patrice Arruda0f688002020-06-08 21:40:25 +0000316
317func TestPrebuiltDSPDirPath(t *testing.T) {
318 targetPath := filepath.Join(buildDir, "/target/product/test_device")
319 tests := []struct {
320 description string
321 config string
322 expectedPath string
323 }{{
324 description: "prebuilt: system dsp",
325 config: `
326 prebuilt_dsp {
327 name: "foo.conf",
328 src: "foo.conf",
329 }`,
330 expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
331 }, {
332 description: "prebuilt: vendor dsp",
333 config: `
334 prebuilt_dsp {
335 name: "foo.conf",
336 src: "foo.conf",
337 soc_specific: true,
338 sub_dir: "sub_dir",
339 }`,
340 expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
341 }}
342 for _, tt := range tests {
343 t.Run(tt.description, func(t *testing.T) {
344 ctx, _ := testPrebuiltEtc(t, tt.config)
345 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
346 if p.installDirPath.String() != tt.expectedPath {
347 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
348 }
349 })
350 }
351}