blob: 08700ae563e885faf40c901dcdf8ab19cdd113b1 [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
15package android
16
17import (
18 "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"
23)
24
Patrice Arruda300cef92019-02-22 15:47:57 -080025func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
Tao Bao0ba5c942018-08-14 22:20:22 -070026 config, buildDir := setUp(t)
27 defer tearDown(buildDir)
28 ctx := NewTestArchContext()
29 ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory))
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080030 ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory))
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080031 ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory))
Patrice Arruda300cef92019-02-22 15:47:57 -080032 ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory))
Tao Bao0ba5c942018-08-14 22:20:22 -070033 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 Arruda300cef92019-02-22 15:47:57 -080049 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070050}
51
52func 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
62func tearDown(buildDir string) {
63 os.RemoveAll(buildDir)
64}
65
66func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080067 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070068 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 Park139a2e62018-10-26 21:49:39 +090099
100func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800101 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900102 prebuilt_etc {
103 name: "foo.conf",
104 src: "foo.conf",
105 filename: "foo.installed.conf",
106 }
107 `)
108
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800109 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900110 if p.outputFilePath.Base() != "foo.installed.conf" {
111 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
112 }
113}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900114
115func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800116 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900117 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 Jungb9a11512019-01-15 10:47:05 -0800128 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900129 if p.outputFilePath.Base() != "my_foo" {
130 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
131 }
132
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800133 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900134 if p.outputFilePath.Base() != "bar.conf" {
135 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
136 }
137}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000138
139func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700140 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000141 prebuilt_etc {
142 name: "foo",
143 src: "foo.conf",
144 owner: "abc",
145 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700146 required: ["modA", "moduleB"],
147 host_required: ["hostModA", "hostModB"],
148 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000149 }
150 `)
151
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700152 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 Hanssonce0e2582019-02-04 14:19:27 +0000160 }
161
162 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700163 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 Hanssonce0e2582019-02-04 14:19:27 +0000168 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700169 } else {
170 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000171 }
172 }
173}
Jaewoong Jung24788182019-02-04 14:34:10 -0800174
175func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800176 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800177 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 Jungc3fcdb42019-02-13 05:50:33 -0800189
190func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800191 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800192 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 Arruda300cef92019-02-22 15:47:57 -0800205
206func 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}