blob: 89b8126f182c8253a7f0c1cad257e721a6ecc044 [file] [log] [blame]
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001package sh
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07002
3import (
Jaewoong Jung4b79e982020-06-01 10:45:49 -07004 "os"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -07005 "path/filepath"
Dan Shib40deac2021-05-24 12:04:54 -07006 "strconv"
Sam Delmericob3342ce2022-01-20 21:10:28 +00007 "strings"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07008 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -07009
10 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070011 "android/soong/cc"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070012)
13
Jaewoong Jung4b79e982020-06-01 10:45:49 -070014func TestMain(m *testing.M) {
Paul Duffin32b06c22021-03-16 07:50:37 +000015 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070016}
17
Paul Duffin89648f92021-03-20 00:36:55 +000018var prepareForShTest = android.GroupFixturePreparers(
Paul Duffin56fb8ee2021-03-08 15:05:52 +000019 cc.PrepareForTestWithCcBuildComponents,
20 PrepareForTestWithShBuildComponents,
21 android.FixtureMergeMockFs(android.MockFS{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070022 "test.sh": nil,
23 "testdata/data1": nil,
24 "testdata/sub/data2": nil,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000025 }),
26)
Colin Cross98be1bb2019-12-13 20:41:13 -080027
Paul Duffin89648f92021-03-20 00:36:55 +000028// testShBinary runs tests using the prepareForShTest
Paul Duffin56fb8ee2021-03-08 15:05:52 +000029//
Paul Duffin89648f92021-03-20 00:36:55 +000030// Do not add any new usages of this, instead use the prepareForShTest directly as it makes it much
Paul Duffin56fb8ee2021-03-08 15:05:52 +000031// easier to customize the test behavior.
32//
33// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin89648f92021-03-20 00:36:55 +000034// convert the test to using prepareForShTest first and then in a following change add the
Paul Duffin56fb8ee2021-03-08 15:05:52 +000035// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
36// that it did not change the test behavior unexpectedly.
37//
38// deprecated
39func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross06c80eb2022-02-10 10:34:19 -080040 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
41
Paul Duffin89648f92021-03-20 00:36:55 +000042 result := prepareForShTest.RunTestWithBp(t, bp)
Colin Cross98be1bb2019-12-13 20:41:13 -080043
Paul Duffin56fb8ee2021-03-08 15:05:52 +000044 return result.TestContext, result.Config
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070045}
46
Jaewoong Jung4aedc862020-06-10 17:23:46 -070047func TestShTestSubDir(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080048 result := android.GroupFixturePreparers(
49 prepareForShTest,
50 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
51 ).RunTestWithBp(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070052 sh_test {
53 name: "foo",
54 src: "test.sh",
55 sub_dir: "foo_test"
56 }
57 `)
58
Colin Crossc68db4b2021-11-11 18:59:15 -080059 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070060
Colin Crossc68db4b2021-11-11 18:59:15 -080061 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070062
Paul Duffin32b06c22021-03-16 07:50:37 +000063 expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070064 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080065 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070066}
67
68func TestShTest(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080069 result := android.GroupFixturePreparers(
70 prepareForShTest,
71 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
72 ).RunTestWithBp(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070073 sh_test {
74 name: "foo",
75 src: "test.sh",
76 filename: "test.sh",
77 data: [
78 "testdata/data1",
79 "testdata/sub/data2",
80 ],
81 }
82 `)
83
Colin Crossc68db4b2021-11-11 18:59:15 -080084 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070085
Colin Crossc68db4b2021-11-11 18:59:15 -080086 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070087
Paul Duffin32b06c22021-03-16 07:50:37 +000088 expectedPath := "out/target/product/test_device/data/nativetest64/foo"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070089 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080090 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070091
92 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
93 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +000094 android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070095}
Jaewoong Jung61a83682019-07-01 09:08:50 -070096
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070097func TestShTest_dataModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000098 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070099 sh_test {
100 name: "foo",
101 src: "test.sh",
102 host_supported: true,
103 data_bins: ["bar"],
104 data_libs: ["libbar"],
105 }
106
107 cc_binary {
108 name: "bar",
109 host_supported: true,
110 shared_libs: ["libbar"],
111 no_libcrt: true,
112 nocrt: true,
113 system_shared_libs: [],
114 stl: "none",
115 }
116
117 cc_library {
118 name: "libbar",
119 host_supported: true,
120 no_libcrt: true,
121 nocrt: true,
122 system_shared_libs: [],
123 stl: "none",
124 }
125 `)
126
Colin Cross0c66bc62021-07-20 09:47:41 -0700127 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700128 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
129 for _, arch := range arches {
130 variant := ctx.ModuleForTests("foo", arch)
131
132 libExt := ".so"
133 if arch == "darwin_x86_64" {
134 libExt = ".dylib"
135 }
136 relocated := variant.Output("relocated/lib64/libbar" + libExt)
Paul Duffin32b06c22021-03-16 07:50:37 +0000137 expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
138 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700139
140 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700141 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700142 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000143 filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
144 filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700145 }
146 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000147 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700148 }
149}
150
Jaewoong Jung61a83682019-07-01 09:08:50 -0700151func TestShTestHost(t *testing.T) {
152 ctx, _ := testShBinary(t, `
153 sh_test_host {
154 name: "foo",
155 src: "test.sh",
156 filename: "test.sh",
157 data: [
158 "testdata/data1",
159 "testdata/sub/data2",
160 ],
Dan Shib40deac2021-05-24 12:04:54 -0700161 test_options: {
162 unit_test: true,
163 },
Jaewoong Jung61a83682019-07-01 09:08:50 -0700164 }
165 `)
166
Colin Cross0c66bc62021-07-20 09:47:41 -0700167 buildOS := ctx.Config().BuildOS.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700168 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
169 if !mod.Host() {
170 t.Errorf("host bit is not set for a sh_test_host module.")
171 }
Dan Shib40deac2021-05-24 12:04:54 -0700172 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
173 actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
174 android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700175}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700176
177func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000178 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700179 sh_test_host {
180 name: "foo",
181 src: "test.sh",
182 data_device_bins: ["bar"],
183 data_device_libs: ["libbar"],
184 }
185
186 cc_binary {
187 name: "bar",
188 shared_libs: ["libbar"],
189 no_libcrt: true,
190 nocrt: true,
191 system_shared_libs: [],
192 stl: "none",
193 }
194
195 cc_library {
196 name: "libbar",
197 no_libcrt: true,
198 nocrt: true,
199 system_shared_libs: [],
200 stl: "none",
201 }
202 `)
203
Colin Cross0c66bc62021-07-20 09:47:41 -0700204 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700205 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
206
207 relocated := variant.Output("relocated/lib64/libbar.so")
Paul Duffin32b06c22021-03-16 07:50:37 +0000208 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
209 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700210
211 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700212 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700213 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000214 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700215 // libbar has been relocated, and so has a variant that matches the host arch.
Paul Duffin32b06c22021-03-16 07:50:37 +0000216 "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700217 }
218 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000219 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700220}
Sam Delmericob3342ce2022-01-20 21:10:28 +0000221
222func TestShTestHost_dataDeviceModulesAutogenTradefedConfig(t *testing.T) {
223 ctx, config := testShBinary(t, `
224 sh_test_host {
225 name: "foo",
226 src: "test.sh",
227 data_device_bins: ["bar"],
228 data_device_libs: ["libbar"],
229 }
230
231 cc_binary {
232 name: "bar",
233 shared_libs: ["libbar"],
234 no_libcrt: true,
235 nocrt: true,
236 system_shared_libs: [],
237 stl: "none",
238 }
239
240 cc_library {
241 name: "libbar",
242 no_libcrt: true,
243 nocrt: true,
244 system_shared_libs: [],
245 stl: "none",
246 }
247 `)
248
249 buildOS := config.BuildOS.String()
250 fooModule := ctx.ModuleForTests("foo", buildOS+"_x86_64")
251
252 expectedBinAutogenConfig := `<option name="push-file" key="bar" value="/data/local/tests/unrestricted/foo/bar" />`
253 autogen := fooModule.Rule("autogen")
254 if !strings.Contains(autogen.Args["extraConfigs"], expectedBinAutogenConfig) {
255 t.Errorf("foo extraConfings %v does not contain %q", autogen.Args["extraConfigs"], expectedBinAutogenConfig)
256 }
257}