blob: 6727e59e44d6a1094b74dd9812d1e3a1c32eeafa [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"
Tao Bao0ba5c942018-08-14 22:20:22 -070021 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070022
23 "android/soong/android"
Tao Bao0ba5c942018-08-14 22:20:22 -070024)
25
Jaewoong Jung4b79e982020-06-01 10:45:49 -070026var buildDir string
27
28func setUp() {
29 var err error
30 buildDir, err = ioutil.TempDir("", "soong_etc_test")
31 if err != nil {
32 panic(err)
33 }
34}
35
36func tearDown() {
37 os.RemoveAll(buildDir)
38}
39
40func TestMain(m *testing.M) {
41 run := func() int {
42 setUp()
43 defer tearDown()
44
45 return m.Run()
46 }
47
48 os.Exit(run())
49}
50
Paul Duffin1172fed2021-03-08 11:28:18 +000051var prebuiltEtcFixtureFactory = android.NewFixtureFactory(
52 &buildDir,
53 android.PrepareForTestWithArchMutator,
54 PrepareForTestWithPrebuiltEtc,
55 android.FixtureMergeMockFs(android.MockFS{
Colin Cross98be1bb2019-12-13 20:41:13 -080056 "foo.conf": nil,
57 "bar.conf": nil,
58 "baz.conf": nil,
Paul Duffin1172fed2021-03-08 11:28:18 +000059 }),
60)
Colin Cross98be1bb2019-12-13 20:41:13 -080061
Tao Bao0ba5c942018-08-14 22:20:22 -070062func TestPrebuiltEtcVariants(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +000063 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070064 prebuilt_etc {
65 name: "foo.conf",
66 src: "foo.conf",
67 }
68 prebuilt_etc {
69 name: "bar.conf",
70 src: "bar.conf",
71 recovery_available: true,
72 }
73 prebuilt_etc {
74 name: "baz.conf",
75 src: "baz.conf",
76 recovery: true,
77 }
78 `)
79
Paul Duffin921fac72021-03-10 09:00:58 +000080 foo_variants := result.ModuleVariantsForTests("foo.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070081 if len(foo_variants) != 1 {
82 t.Errorf("expected 1, got %#v", foo_variants)
83 }
84
Paul Duffin921fac72021-03-10 09:00:58 +000085 bar_variants := result.ModuleVariantsForTests("bar.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070086 if len(bar_variants) != 2 {
87 t.Errorf("expected 2, got %#v", bar_variants)
88 }
89
Paul Duffin921fac72021-03-10 09:00:58 +000090 baz_variants := result.ModuleVariantsForTests("baz.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070091 if len(baz_variants) != 1 {
92 t.Errorf("expected 1, got %#v", bar_variants)
93 }
94}
Jiyong Park139a2e62018-10-26 21:49:39 +090095
96func TestPrebuiltEtcOutputPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +000097 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +090098 prebuilt_etc {
99 name: "foo.conf",
100 src: "foo.conf",
101 filename: "foo.installed.conf",
102 }
103 `)
104
Paul Duffin921fac72021-03-10 09:00:58 +0000105 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
106 result.AssertStringEquals("output file path", "foo.installed.conf", p.outputFilePath.Base())
Jiyong Park139a2e62018-10-26 21:49:39 +0900107}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900108
109func TestPrebuiltEtcGlob(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000110 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900111 prebuilt_etc {
112 name: "my_foo",
113 src: "foo.*",
114 }
115 prebuilt_etc {
116 name: "my_bar",
117 src: "bar.*",
118 filename_from_src: true,
119 }
120 `)
121
Paul Duffin921fac72021-03-10 09:00:58 +0000122 p := result.Module("my_foo", "android_arm64_armv8-a").(*PrebuiltEtc)
123 result.AssertStringEquals("my_foo output file path", "my_foo", p.outputFilePath.Base())
Jiyong Park1a7cf082018-11-13 11:59:12 +0900124
Paul Duffin921fac72021-03-10 09:00:58 +0000125 p = result.Module("my_bar", "android_arm64_armv8-a").(*PrebuiltEtc)
126 result.AssertStringEquals("my_bar output file path", "bar.conf", p.outputFilePath.Base())
Jiyong Park1a7cf082018-11-13 11:59:12 +0900127}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000128
129func TestPrebuiltEtcAndroidMk(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000130 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000131 prebuilt_etc {
132 name: "foo",
133 src: "foo.conf",
134 owner: "abc",
135 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700136 required: ["modA", "moduleB"],
137 host_required: ["hostModA", "hostModB"],
138 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000139 }
140 `)
141
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700142 expected := map[string][]string{
143 "LOCAL_MODULE": {"foo"},
144 "LOCAL_MODULE_CLASS": {"ETC"},
145 "LOCAL_MODULE_OWNER": {"abc"},
146 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
147 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
148 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
149 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000150 }
151
Paul Duffin921fac72021-03-10 09:00:58 +0000152 mod := result.Module("foo", "android_arm64_armv8-a").(*PrebuiltEtc)
153 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700154 for k, expectedValue := range expected {
155 if value, ok := entries.EntryMap[k]; ok {
Paul Duffin921fac72021-03-10 09:00:58 +0000156 result.AssertDeepEquals(k, expectedValue, value)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700157 } else {
158 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000159 }
160 }
161}
Jaewoong Jung24788182019-02-04 14:34:10 -0800162
Liz Kammer0449a632020-06-26 10:12:36 -0700163func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000164 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Liz Kammer0449a632020-06-26 10:12:36 -0700165 prebuilt_etc {
166 name: "foo.conf",
167 src: "foo.conf",
168 relative_install_path: "bar",
169 }
170 `)
171
Paul Duffin921fac72021-03-10 09:00:58 +0000172 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Liz Kammer0449a632020-06-26 10:12:36 -0700173 expected := buildDir + "/target/product/test_device/system/etc/bar"
Paul Duffin921fac72021-03-10 09:00:58 +0000174 result.AssertStringEquals("install dir", expected, p.installDirPath.String())
Liz Kammer0449a632020-06-26 10:12:36 -0700175}
176
177func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
Paul Duffin1172fed2021-03-08 11:28:18 +0000178 prebuiltEtcFixtureFactory.
179 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")).
180 RunTestWithBp(t, `
181 prebuilt_etc {
182 name: "foo.conf",
183 src: "foo.conf",
184 sub_dir: "bar",
185 relative_install_path: "bar",
186 }
187 `)
Liz Kammer0449a632020-06-26 10:12:36 -0700188}
189
Jaewoong Jung24788182019-02-04 14:34:10 -0800190func TestPrebuiltEtcHost(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000191 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800192 prebuilt_etc_host {
193 name: "foo.conf",
194 src: "foo.conf",
195 }
196 `)
197
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700198 buildOS := android.BuildOs.String()
Paul Duffin921fac72021-03-10 09:00:58 +0000199 p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
Jaewoong Jung24788182019-02-04 14:34:10 -0800200 if !p.Host() {
201 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
202 }
203}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800204
205func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000206 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800207 prebuilt_usr_share {
208 name: "foo.conf",
209 src: "foo.conf",
210 sub_dir: "bar",
211 }
212 `)
213
Paul Duffin921fac72021-03-10 09:00:58 +0000214 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700215 expected := buildDir + "/target/product/test_device/system/usr/share/bar"
Paul Duffin921fac72021-03-10 09:00:58 +0000216 result.AssertStringEquals("install dir", expected, p.installDirPath.String())
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800217}
Patrice Arruda300cef92019-02-22 15:47:57 -0800218
219func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000220 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Patrice Arruda300cef92019-02-22 15:47:57 -0800221 prebuilt_usr_share_host {
222 name: "foo.conf",
223 src: "foo.conf",
224 sub_dir: "bar",
225 }
226 `)
227
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700228 buildOS := android.BuildOs.String()
Paul Duffin921fac72021-03-10 09:00:58 +0000229 p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
230 expected := filepath.Join(buildDir, "host", result.Config.PrebuiltOS(), "usr", "share", "bar")
231 result.AssertStringEquals("install dir", expected, p.installDirPath.String())
Patrice Arruda300cef92019-02-22 15:47:57 -0800232}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700233
234func TestPrebuiltFontInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000235 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Patrice Arruda61583eb2019-05-14 08:20:45 -0700236 prebuilt_font {
237 name: "foo.conf",
238 src: "foo.conf",
239 }
240 `)
241
Paul Duffin921fac72021-03-10 09:00:58 +0000242 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700243 expected := buildDir + "/target/product/test_device/system/fonts"
Paul Duffin921fac72021-03-10 09:00:58 +0000244 result.AssertStringEquals("install dir", expected, p.installDirPath.String())
Patrice Arruda61583eb2019-05-14 08:20:45 -0700245}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700246
247func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700248 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700249 tests := []struct {
250 description string
251 config string
252 expectedPath string
253 }{{
254 description: "prebuilt: system firmware",
255 config: `
256 prebuilt_firmware {
257 name: "foo.conf",
258 src: "foo.conf",
259 }`,
260 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
261 }, {
262 description: "prebuilt: vendor firmware",
263 config: `
264 prebuilt_firmware {
265 name: "foo.conf",
266 src: "foo.conf",
267 soc_specific: true,
268 sub_dir: "sub_dir",
269 }`,
270 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
271 }}
272 for _, tt := range tests {
273 t.Run(tt.description, func(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000274 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, tt.config)
275 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
276 result.AssertStringEquals("install dir", tt.expectedPath, p.installDirPath.String())
Patrice Arruda057a8b12019-06-03 15:29:27 -0700277 })
278 }
279}
Patrice Arruda0f688002020-06-08 21:40:25 +0000280
281func TestPrebuiltDSPDirPath(t *testing.T) {
282 targetPath := filepath.Join(buildDir, "/target/product/test_device")
283 tests := []struct {
284 description string
285 config string
286 expectedPath string
287 }{{
288 description: "prebuilt: system dsp",
289 config: `
290 prebuilt_dsp {
291 name: "foo.conf",
292 src: "foo.conf",
293 }`,
294 expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
295 }, {
296 description: "prebuilt: vendor dsp",
297 config: `
298 prebuilt_dsp {
299 name: "foo.conf",
300 src: "foo.conf",
301 soc_specific: true,
302 sub_dir: "sub_dir",
303 }`,
304 expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
305 }}
306 for _, tt := range tests {
307 t.Run(tt.description, func(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000308 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, tt.config)
309 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
310 result.AssertStringEquals("install dir", tt.expectedPath, p.installDirPath.String())
Patrice Arruda0f688002020-06-08 21:40:25 +0000311 })
312 }
313}