blob: 3c0a9eb2c2b5ae3c476f072620cfd66463be6bce [file] [log] [blame]
Weijia He32282f62024-11-05 22:21:04 +00001// Copyright 2024 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.
14package tradefed_modules
15
16import (
17 "android/soong/android"
18 "android/soong/java"
Weijia He0c79e262024-11-06 19:57:25 +000019 "encoding/json"
20 "slices"
Weijia He32282f62024-11-05 22:21:04 +000021 "testing"
22)
23
24func TestTestSuites(t *testing.T) {
25 t.Parallel()
Weijia Heaa37c162024-11-06 19:46:03 +000026 ctx := android.GroupFixturePreparers(
Weijia He32282f62024-11-05 22:21:04 +000027 java.PrepareForTestWithJavaDefaultModules,
28 android.FixtureRegisterWithContext(RegisterTestSuiteBuildComponents),
29 ).RunTestWithBp(t, `
30 android_test {
31 name: "TestModule1",
32 sdk_version: "current",
33 }
34
35 android_test {
36 name: "TestModule2",
37 sdk_version: "current",
38 }
39
40 test_suite {
41 name: "my-suite",
42 description: "a test suite",
43 tests: [
44 "TestModule1",
45 "TestModule2",
46 ]
47 }
48 `)
Weijia He32a95572024-11-06 23:56:33 +000049 manifestPath := ctx.ModuleForTests("my-suite", "android_common").Output("out/soong/test_suites/my-suite/my-suite.json")
Weijia He0c79e262024-11-06 19:57:25 +000050 var actual testSuiteManifest
51 if err := json.Unmarshal([]byte(android.ContentFromFileRuleForTests(t, ctx.TestContext, manifestPath)), &actual); err != nil {
52 t.Errorf("failed to unmarshal manifest: %v", err)
Weijia Heaa37c162024-11-06 19:46:03 +000053 }
Weijia He0c79e262024-11-06 19:57:25 +000054 slices.Sort(actual.Files)
55
56 expected := testSuiteManifest{
57 Name: "my-suite",
58 Files: []string{
59 "target/testcases/TestModule1/TestModule1.config",
60 "target/testcases/TestModule1/arm64/TestModule1.apk",
61 "target/testcases/TestModule2/TestModule2.config",
62 "target/testcases/TestModule2/arm64/TestModule2.apk",
63 },
64 }
65
66 android.AssertDeepEquals(t, "manifests differ", expected, actual)
67}
68
Weijia He32a95572024-11-06 23:56:33 +000069func TestTestSuitesWithNested(t *testing.T) {
70 t.Parallel()
71 ctx := android.GroupFixturePreparers(
72 java.PrepareForTestWithJavaDefaultModules,
73 android.FixtureRegisterWithContext(RegisterTestSuiteBuildComponents),
74 ).RunTestWithBp(t, `
75 android_test {
76 name: "TestModule1",
77 sdk_version: "current",
78 }
79
80 android_test {
81 name: "TestModule2",
82 sdk_version: "current",
83 }
84
85 android_test {
86 name: "TestModule3",
87 sdk_version: "current",
88 }
89
90 test_suite {
91 name: "my-child-suite",
92 description: "a child test suite",
93 tests: [
94 "TestModule1",
95 "TestModule2",
96 ]
97 }
98
99 test_suite {
100 name: "my-all-tests-suite",
101 description: "a parent test suite",
102 tests: [
103 "TestModule1",
104 "TestModule3",
105 "my-child-suite",
106 ]
107 }
108 `)
109 manifestPath := ctx.ModuleForTests("my-all-tests-suite", "android_common").Output("out/soong/test_suites/my-all-tests-suite/my-all-tests-suite.json")
110 var actual testSuiteManifest
111 if err := json.Unmarshal([]byte(android.ContentFromFileRuleForTests(t, ctx.TestContext, manifestPath)), &actual); err != nil {
112 t.Errorf("failed to unmarshal manifest: %v", err)
113 }
114 slices.Sort(actual.Files)
115
116 expected := testSuiteManifest{
117 Name: "my-all-tests-suite",
118 Files: []string{
119 "target/testcases/TestModule1/TestModule1.config",
120 "target/testcases/TestModule1/arm64/TestModule1.apk",
121 "target/testcases/TestModule2/TestModule2.config",
122 "target/testcases/TestModule2/arm64/TestModule2.apk",
123 "target/testcases/TestModule3/TestModule3.config",
124 "target/testcases/TestModule3/arm64/TestModule3.apk",
125 },
126 }
127
128 android.AssertDeepEquals(t, "manifests differ", expected, actual)
129}
130
Weijia He0c79e262024-11-06 19:57:25 +0000131func TestTestSuitesNotInstalledInTestcases(t *testing.T) {
132 t.Parallel()
133 android.GroupFixturePreparers(
Weijia He0c79e262024-11-06 19:57:25 +0000134 java.PrepareForTestWithJavaDefaultModules,
135 android.FixtureRegisterWithContext(RegisterTestSuiteBuildComponents),
136 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{
137 `"SomeHostTest" is not installed in testcases`,
138 })).RunTestWithBp(t, `
139 java_test_host {
140 name: "SomeHostTest",
141 srcs: ["a.java"],
142 }
143 test_suite {
144 name: "my-suite",
145 description: "a test suite",
146 tests: [
147 "SomeHostTest",
148 ]
149 }
150 `)
Weijia He32282f62024-11-05 22:21:04 +0000151}