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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 20 | "path/filepath" |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame^] | 21 | "reflect" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | ) |
| 24 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 25 | func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) { |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 26 | config, buildDir := setUp(t) |
| 27 | defer tearDown(buildDir) |
| 28 | ctx := NewTestArchContext() |
| 29 | ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory)) |
Jaewoong Jung | 4b44fcd | 2019-02-07 08:28:03 -0800 | [diff] [blame] | 30 | ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory)) |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 31 | ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory)) |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 32 | ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory)) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 33 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 34 | ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel() |
| 35 | }) |
| 36 | ctx.Register() |
| 37 | mockFiles := map[string][]byte{ |
| 38 | "Android.bp": []byte(bp), |
| 39 | "foo.conf": nil, |
| 40 | "bar.conf": nil, |
| 41 | "baz.conf": nil, |
| 42 | } |
| 43 | ctx.MockFileSystem(mockFiles) |
| 44 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 45 | FailIfErrored(t, errs) |
| 46 | _, errs = ctx.PrepareBuildActions(config) |
| 47 | FailIfErrored(t, errs) |
| 48 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 49 | return ctx, config |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func setUp(t *testing.T) (config Config, buildDir string) { |
| 53 | buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test") |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | |
| 58 | config = TestArchConfig(buildDir, nil) |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | func tearDown(buildDir string) { |
| 63 | os.RemoveAll(buildDir) |
| 64 | } |
| 65 | |
| 66 | func TestPrebuiltEtcVariants(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 67 | ctx, _ := testPrebuiltEtc(t, ` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 68 | prebuilt_etc { |
| 69 | name: "foo.conf", |
| 70 | src: "foo.conf", |
| 71 | } |
| 72 | prebuilt_etc { |
| 73 | name: "bar.conf", |
| 74 | src: "bar.conf", |
| 75 | recovery_available: true, |
| 76 | } |
| 77 | prebuilt_etc { |
| 78 | name: "baz.conf", |
| 79 | src: "baz.conf", |
| 80 | recovery: true, |
| 81 | } |
| 82 | `) |
| 83 | |
| 84 | foo_variants := ctx.ModuleVariantsForTests("foo.conf") |
| 85 | if len(foo_variants) != 1 { |
| 86 | t.Errorf("expected 1, got %#v", foo_variants) |
| 87 | } |
| 88 | |
| 89 | bar_variants := ctx.ModuleVariantsForTests("bar.conf") |
| 90 | if len(bar_variants) != 2 { |
| 91 | t.Errorf("expected 2, got %#v", bar_variants) |
| 92 | } |
| 93 | |
| 94 | baz_variants := ctx.ModuleVariantsForTests("baz.conf") |
| 95 | if len(baz_variants) != 1 { |
| 96 | t.Errorf("expected 1, got %#v", bar_variants) |
| 97 | } |
| 98 | } |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 99 | |
| 100 | func TestPrebuiltEtcOutputPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 101 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 102 | prebuilt_etc { |
| 103 | name: "foo.conf", |
| 104 | src: "foo.conf", |
| 105 | filename: "foo.installed.conf", |
| 106 | } |
| 107 | `) |
| 108 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 109 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 110 | if p.outputFilePath.Base() != "foo.installed.conf" { |
| 111 | t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base()) |
| 112 | } |
| 113 | } |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 114 | |
| 115 | func TestPrebuiltEtcGlob(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 116 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 117 | prebuilt_etc { |
| 118 | name: "my_foo", |
| 119 | src: "foo.*", |
| 120 | } |
| 121 | prebuilt_etc { |
| 122 | name: "my_bar", |
| 123 | src: "bar.*", |
| 124 | filename_from_src: true, |
| 125 | } |
| 126 | `) |
| 127 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 128 | p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 129 | if p.outputFilePath.Base() != "my_foo" { |
| 130 | t.Errorf("expected my_foo, got %q", p.outputFilePath.Base()) |
| 131 | } |
| 132 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 133 | p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 134 | if p.outputFilePath.Base() != "bar.conf" { |
| 135 | t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base()) |
| 136 | } |
| 137 | } |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 138 | |
| 139 | func TestPrebuiltEtcAndroidMk(t *testing.T) { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame^] | 140 | ctx, config := testPrebuiltEtc(t, ` |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 141 | prebuilt_etc { |
| 142 | name: "foo", |
| 143 | src: "foo.conf", |
| 144 | owner: "abc", |
| 145 | filename_from_src: true, |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame^] | 146 | required: ["modA", "moduleB"], |
| 147 | host_required: ["hostModA", "hostModB"], |
| 148 | target_required: ["targetModA"], |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 149 | } |
| 150 | `) |
| 151 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame^] | 152 | expected := map[string][]string{ |
| 153 | "LOCAL_MODULE": {"foo"}, |
| 154 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 155 | "LOCAL_MODULE_OWNER": {"abc"}, |
| 156 | "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"}, |
| 157 | "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"}, |
| 158 | "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"}, |
| 159 | "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"}, |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame^] | 163 | entries := AndroidMkEntriesForTest(t, config, "", mod) |
| 164 | for k, expectedValue := range expected { |
| 165 | if value, ok := entries.EntryMap[k]; ok { |
| 166 | if !reflect.DeepEqual(value, expectedValue) { |
| 167 | t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 168 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame^] | 169 | } else { |
| 170 | t.Errorf("No %s defined, saw %q", k, entries.EntryMap) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 174 | |
| 175 | func TestPrebuiltEtcHost(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 176 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 177 | prebuilt_etc_host { |
| 178 | name: "foo.conf", |
| 179 | src: "foo.conf", |
| 180 | } |
| 181 | `) |
| 182 | |
| 183 | buildOS := BuildOs.String() |
| 184 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 185 | if !p.Host() { |
| 186 | t.Errorf("host bit is not set for a prebuilt_etc_host module.") |
| 187 | } |
| 188 | } |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 189 | |
| 190 | func TestPrebuiltUserShareInstallDirPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 191 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 192 | prebuilt_usr_share { |
| 193 | name: "foo.conf", |
| 194 | src: "foo.conf", |
| 195 | sub_dir: "bar", |
| 196 | } |
| 197 | `) |
| 198 | |
| 199 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
| 200 | expected := "target/product/test_device/system/usr/share/bar" |
| 201 | if p.installDirPath.RelPathString() != expected { |
| 202 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 203 | } |
| 204 | } |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 205 | |
| 206 | func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) { |
| 207 | ctx, config := testPrebuiltEtc(t, ` |
| 208 | prebuilt_usr_share_host { |
| 209 | name: "foo.conf", |
| 210 | src: "foo.conf", |
| 211 | sub_dir: "bar", |
| 212 | } |
| 213 | `) |
| 214 | |
| 215 | buildOS := BuildOs.String() |
| 216 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 217 | expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar") |
| 218 | if p.installDirPath.RelPathString() != expected { |
| 219 | t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString()) |
| 220 | } |
| 221 | } |