blob: b49e1679cf518da614559329bbb97058364c3227 [file] [log] [blame]
Jiyong Park6446b622021-02-01 20:08:28 +09001// Copyright 2021 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 kernel
16
17import (
18 "io/ioutil"
19 "os"
20 "reflect"
21 "strings"
22 "testing"
23
24 "android/soong/android"
25 "android/soong/cc"
26)
27
28func testKernelModules(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
29 bp = bp + `
30 cc_binary_host {
31 name: "depmod",
32 srcs: ["depmod.cpp"],
33 stl: "none",
34 static_executable: true,
35 system_shared_libs: [],
36 }
37 `
38 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
39
40 fs["depmod.cpp"] = nil
41 cc.GatherRequiredFilesForTest(fs)
42
43 config := android.TestArchConfig(buildDir, nil, bp, fs)
44
45 ctx := android.NewTestArchContext(config)
46 ctx.RegisterModuleType("prebuilt_kernel_modules", prebuiltKernelModulesFactory)
47 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
48 cc.RegisterRequiredBuildComponentsForTest(ctx)
49
50 ctx.Register()
51 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
52 android.FailIfErrored(t, errs)
53 _, errs = ctx.PrepareBuildActions(config)
54 android.FailIfErrored(t, errs)
55
56 return ctx, config
57}
58
59func ensureListContains(t *testing.T, result []string, expected string) {
60 t.Helper()
61 if !android.InList(expected, result) {
62 t.Errorf("%q is not found in %v", expected, result)
63 }
64}
65
66func ensureContains(t *testing.T, result string, expected string) {
67 t.Helper()
68 if !strings.Contains(result, expected) {
69 t.Errorf("%q is not found in %q", expected, result)
70 }
71}
72
73func TestKernelModulesFilelist(t *testing.T) {
74 ctx, _ := testKernelModules(t, `
75 prebuilt_kernel_modules {
76 name: "foo",
77 srcs: ["*.ko"],
78 kernel_version: "5.10",
79 }
80 `,
81 map[string][]byte{
82 "mod1.ko": nil,
83 "mod2.ko": nil,
84 })
85
86 expected := []string{
87 "lib/module/5.10/mod1.ko",
88 "lib/module/5.10/mod2.ko",
89 "lib/module/5.10/modules.load",
90 "lib/module/5.10/modules.dep",
91 "lib/module/5.10/modules.softdep",
92 "lib/module/5.10/modules.alias",
93 }
94
95 var actual []string
96 for _, ps := range ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().PackagingSpecs() {
97 actual = append(actual, ps.RelPathInPackage())
98 }
99 actual = android.SortedUniqueStrings(actual)
100 expected = android.SortedUniqueStrings(expected)
101 if !reflect.DeepEqual(actual, expected) {
102 t.Errorf("\ngot: %v\nexpected: %v\n", actual, expected)
103 }
104}
105
106var buildDir string
107
108func setUp() {
109 var err error
110 buildDir, err = ioutil.TempDir("", "soong_kernel_test")
111 if err != nil {
112 panic(err)
113 }
114}
115
116func tearDown() {
117 os.RemoveAll(buildDir)
118}
119
120func TestMain(m *testing.M) {
121 run := func() int {
122 setUp()
123 defer tearDown()
124
125 return m.Run()
126 }
127
128 os.Exit(run())
129}