Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 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 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 15 | package etc |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 20 | "path/filepath" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 21 | "testing" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 26 | var buildDir string |
| 27 | |
| 28 | func setUp() { |
| 29 | var err error |
| 30 | buildDir, err = ioutil.TempDir("", "soong_etc_test") |
| 31 | if err != nil { |
| 32 | panic(err) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func tearDown() { |
| 37 | os.RemoveAll(buildDir) |
| 38 | } |
| 39 | |
| 40 | func 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 Duffin | 1172fed | 2021-03-08 11:28:18 +0000 | [diff] [blame] | 51 | var prebuiltEtcFixtureFactory = android.NewFixtureFactory( |
| 52 | &buildDir, |
| 53 | android.PrepareForTestWithArchMutator, |
| 54 | PrepareForTestWithPrebuiltEtc, |
| 55 | android.FixtureMergeMockFs(android.MockFS{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 56 | "foo.conf": nil, |
| 57 | "bar.conf": nil, |
| 58 | "baz.conf": nil, |
Paul Duffin | 1172fed | 2021-03-08 11:28:18 +0000 | [diff] [blame] | 59 | }), |
| 60 | ) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 61 | |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 62 | func TestPrebuiltEtcVariants(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 63 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 64 | 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 Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 80 | foo_variants := result.ModuleVariantsForTests("foo.conf") |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 81 | if len(foo_variants) != 1 { |
| 82 | t.Errorf("expected 1, got %#v", foo_variants) |
| 83 | } |
| 84 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 85 | bar_variants := result.ModuleVariantsForTests("bar.conf") |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 86 | if len(bar_variants) != 2 { |
| 87 | t.Errorf("expected 2, got %#v", bar_variants) |
| 88 | } |
| 89 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 90 | baz_variants := result.ModuleVariantsForTests("baz.conf") |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 91 | if len(baz_variants) != 1 { |
| 92 | t.Errorf("expected 1, got %#v", bar_variants) |
| 93 | } |
| 94 | } |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 95 | |
| 96 | func TestPrebuiltEtcOutputPath(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 97 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 98 | prebuilt_etc { |
| 99 | name: "foo.conf", |
| 100 | src: "foo.conf", |
| 101 | filename: "foo.installed.conf", |
| 102 | } |
| 103 | `) |
| 104 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 105 | p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc) |
| 106 | result.AssertStringEquals("output file path", "foo.installed.conf", p.outputFilePath.Base()) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 107 | } |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 108 | |
| 109 | func TestPrebuiltEtcGlob(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 110 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 111 | 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 Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 122 | p := result.Module("my_foo", "android_arm64_armv8-a").(*PrebuiltEtc) |
| 123 | result.AssertStringEquals("my_foo output file path", "my_foo", p.outputFilePath.Base()) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 124 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 125 | p = result.Module("my_bar", "android_arm64_armv8-a").(*PrebuiltEtc) |
| 126 | result.AssertStringEquals("my_bar output file path", "bar.conf", p.outputFilePath.Base()) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 127 | } |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 128 | |
| 129 | func TestPrebuiltEtcAndroidMk(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 130 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 131 | prebuilt_etc { |
| 132 | name: "foo", |
| 133 | src: "foo.conf", |
| 134 | owner: "abc", |
| 135 | filename_from_src: true, |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 136 | required: ["modA", "moduleB"], |
| 137 | host_required: ["hostModA", "hostModB"], |
| 138 | target_required: ["targetModA"], |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 139 | } |
| 140 | `) |
| 141 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 142 | 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 Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 152 | mod := result.Module("foo", "android_arm64_armv8-a").(*PrebuiltEtc) |
| 153 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 154 | for k, expectedValue := range expected { |
| 155 | if value, ok := entries.EntryMap[k]; ok { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 156 | result.AssertDeepEquals(k, expectedValue, value) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 157 | } else { |
| 158 | t.Errorf("No %s defined, saw %q", k, entries.EntryMap) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 162 | |
Liz Kammer | 0449a63 | 2020-06-26 10:12:36 -0700 | [diff] [blame] | 163 | func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 164 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Liz Kammer | 0449a63 | 2020-06-26 10:12:36 -0700 | [diff] [blame] | 165 | prebuilt_etc { |
| 166 | name: "foo.conf", |
| 167 | src: "foo.conf", |
| 168 | relative_install_path: "bar", |
| 169 | } |
| 170 | `) |
| 171 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 172 | p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc) |
Liz Kammer | 0449a63 | 2020-06-26 10:12:36 -0700 | [diff] [blame] | 173 | expected := buildDir + "/target/product/test_device/system/etc/bar" |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 174 | result.AssertStringEquals("install dir", expected, p.installDirPath.String()) |
Liz Kammer | 0449a63 | 2020-06-26 10:12:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) { |
Paul Duffin | 1172fed | 2021-03-08 11:28:18 +0000 | [diff] [blame] | 178 | 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 Kammer | 0449a63 | 2020-06-26 10:12:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 190 | func TestPrebuiltEtcHost(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 191 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 192 | prebuilt_etc_host { |
| 193 | name: "foo.conf", |
| 194 | src: "foo.conf", |
| 195 | } |
| 196 | `) |
| 197 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 198 | buildOS := android.BuildOs.String() |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 199 | p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc) |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 200 | if !p.Host() { |
| 201 | t.Errorf("host bit is not set for a prebuilt_etc_host module.") |
| 202 | } |
| 203 | } |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 204 | |
| 205 | func TestPrebuiltUserShareInstallDirPath(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 206 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 207 | prebuilt_usr_share { |
| 208 | name: "foo.conf", |
| 209 | src: "foo.conf", |
| 210 | sub_dir: "bar", |
| 211 | } |
| 212 | `) |
| 213 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 214 | p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 215 | expected := buildDir + "/target/product/test_device/system/usr/share/bar" |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 216 | result.AssertStringEquals("install dir", expected, p.installDirPath.String()) |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 217 | } |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 218 | |
| 219 | func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 220 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 221 | prebuilt_usr_share_host { |
| 222 | name: "foo.conf", |
| 223 | src: "foo.conf", |
| 224 | sub_dir: "bar", |
| 225 | } |
| 226 | `) |
| 227 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 228 | buildOS := android.BuildOs.String() |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 229 | 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 Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 232 | } |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 233 | |
| 234 | func TestPrebuiltFontInstallDirPath(t *testing.T) { |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 235 | result := prebuiltEtcFixtureFactory.RunTestWithBp(t, ` |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 236 | prebuilt_font { |
| 237 | name: "foo.conf", |
| 238 | src: "foo.conf", |
| 239 | } |
| 240 | `) |
| 241 | |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 242 | p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 243 | expected := buildDir + "/target/product/test_device/system/fonts" |
Paul Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 244 | result.AssertStringEquals("install dir", expected, p.installDirPath.String()) |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 245 | } |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 246 | |
| 247 | func TestPrebuiltFirmwareDirPath(t *testing.T) { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 248 | targetPath := buildDir + "/target/product/test_device" |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 249 | 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 Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 274 | 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 Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 277 | }) |
| 278 | } |
| 279 | } |
Patrice Arruda | 0f68800 | 2020-06-08 21:40:25 +0000 | [diff] [blame] | 280 | |
| 281 | func 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 Duffin | 921fac7 | 2021-03-10 09:00:58 +0000 | [diff] [blame^] | 308 | 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 Arruda | 0f68800 | 2020-06-08 21:40:25 +0000 | [diff] [blame] | 311 | }) |
| 312 | } |
| 313 | } |