• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.compatibility.tradefed;
17 
18 import static org.junit.Assert.*;
19 
20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
21 import com.android.compatibility.common.tradefed.build.CompatibilityBuildProvider;
22 import com.android.tradefed.build.IBuildInfo;
23 import com.android.tradefed.config.OptionSetter;
24 import com.android.tradefed.util.FileUtil;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 import com.google.common.base.Strings;
35 
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.BufferedReader;
39 import java.io.IOException;
40 import java.io.InputStreamReader;
41 import java.io.InputStream;
42 import java.io.Reader;
43 import java.util.Arrays;
44 import java.util.ArrayList;
45 import java.util.Enumeration;
46 import java.util.HashSet;
47 import java.util.jar.JarEntry;
48 import java.util.jar.JarFile;
49 import java.util.List;
50 import java.util.Set;
51 import java.util.regex.Pattern;
52 import java.util.zip.ZipEntry;
53 
54 import java.util.zip.ZipException;
55 import java.util.zip.ZipInputStream;
56 import java.util.zip.ZipOutputStream;
57 
58 
59 /**
60  * Tests for mts-tradefed.
61  */
62 @RunWith(JUnit4.class)
63 public class MtsTradefedTest {
64 
65     private static final String PROPERTY_NAME = "MTS_ROOT";
66     private static final String SUITE_FULL_NAME = "Mainline Test Suite";
67     private static final String SUITE_NAME = "MTS";
68     private static final String SUITE_PLAN = "mts";
69     private static final String DYNAMIC_CONFIG_URL = "";
70     private static final String REGEX_PATTERN_TEST_MODULE = "value=\\\"(.*)\\\"";
71     private static final String REGEX_PATTERN_CONFIG = "([^\\/]+)\\.config";
72 
73     private String mOriginalProperty = null;
74 
75     @Before
setUp()76     public void setUp() throws Exception {
77         mOriginalProperty = System.getProperty(PROPERTY_NAME);
78     }
79 
80     @After
tearDown()81     public void tearDown() throws Exception {
82         if (mOriginalProperty != null) {
83             System.setProperty(PROPERTY_NAME, mOriginalProperty);
84         }
85     }
86 
87     @Test
testSuiteInfoLoad()88     public void testSuiteInfoLoad() throws Exception {
89         // Test the values in the manifest can be loaded
90         File root = FileUtil.createTempDir("root");
91         System.setProperty(PROPERTY_NAME, root.getAbsolutePath());
92         File base = new File(root, "android-mts");
93         base.mkdirs();
94         File tests = new File(base, "testcases");
95         tests.mkdirs();
96         CompatibilityBuildProvider provider = new CompatibilityBuildProvider();
97         OptionSetter setter = new OptionSetter(provider);
98         setter.setOptionValue("plan", SUITE_PLAN);
99         setter.setOptionValue("dynamic-config-url", DYNAMIC_CONFIG_URL);
100         IBuildInfo info = provider.getBuild();
101         CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info);
102         assertEquals("Incorrect suite full name", SUITE_FULL_NAME, helper.getSuiteFullName());
103         assertEquals("Incorrect suite name", SUITE_NAME, helper.getSuiteName());
104         FileUtil.recursiveDelete(root);
105     }
106 
107     @Test
testModulesTaggedWithIncludeFilterLoad()108     public void testModulesTaggedWithIncludeFilterLoad() throws Exception {
109         String mtsRootVar = "MTS_ROOT";
110         String suiteRoot = System.getProperty(mtsRootVar);
111         if (Strings.isNullOrEmpty(suiteRoot)) {
112             fail(String.format("Should run within a suite context: %s doesn't exist", mtsRootVar));
113         }
114         File testcases = new File(suiteRoot, "/android-mts/testcases/");
115         if (!testcases.exists()) {
116             fail(String.format("%s does not exist", testcases));
117             return;
118         }
119 
120         // Get all the tests configs in the testcases/ folder
121         Set<File> listConfigs = FileUtil.findFilesObject(testcases, ".*\\.config");
122         assertTrue(listConfigs.size() > 0);
123 
124         // Get all the test modules tagged with include-filter in tools/ folder
125         File tools = new File(suiteRoot, "/android-mts/tools/");
126 
127         File mtsTestLists = new File(tools, "mts-tradefed.jar");
128         Set<String> testModules = getTestModulesTaggedWithIncludeFilterFromXML(mtsTestLists);
129         assertTrue(testModules.size() > 0);
130 
131         Set<String> configs = new HashSet<String>();
132         for (File config : listConfigs) {
133           Pattern pattern = Pattern.compile(REGEX_PATTERN_CONFIG);
134           Matcher matcher = pattern.matcher(config.getName());
135           if (matcher.find()) {
136             String configName = matcher.group(1);
137             configs.add(configName);
138           }
139         }
140         for (String testModule : testModules) {
141           assertTrue(String.format("%s not in configs", testModule), configs.contains(testModule));
142         }
143     }
144 
getTestModulesTaggedWithIncludeFilterFromXML(File jarFile)145     private Set<String> getTestModulesTaggedWithIncludeFilterFromXML(File jarFile)
146         throws ZipException, IOException {
147 
148     Set<String> testModules = new HashSet<String>();
149 
150     JarFile jar = new JarFile(jarFile);
151     // Getting the files into the jar
152     Enumeration<JarEntry> enumeration = jar.entries();
153 
154     // Iterates into the files in the jar file
155     while (enumeration.hasMoreElements()) {
156       ZipEntry zipEntry = enumeration.nextElement();
157 
158       if (zipEntry.getName().endsWith(".xml") && zipEntry.getName().contains("tests-list")) {
159         if (zipEntry.getName().contains("bluetooth") || zipEntry.getName().contains("smoke")) {
160           continue;
161         }
162         // Relative path of file into the jar.
163         String moduleTestList = zipEntry.getName();
164         InputStream inputStream = getClass().getClassLoader().getResourceAsStream(moduleTestList);
165         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
166         while (true) {
167           String s = reader.readLine();
168           if (s == null) {
169             break;
170           }
171           if (s.contains("compatibility:include-filter")) {
172             Pattern pattern = Pattern.compile(REGEX_PATTERN_TEST_MODULE);
173             Matcher matcher = pattern.matcher(s);
174             if (matcher.find()) {
175               String testModuleAndTestName = matcher.group(1);
176               if (testModuleAndTestName.contains(" ")) {
177                 String testModuleName = testModuleAndTestName.substring(0, testModuleAndTestName.indexOf(' '));
178                 testModules.add(testModuleName);
179               } else {
180                 testModules.add(testModuleAndTestName.trim());
181               }
182             }
183           }
184         }
185         reader.close();
186         inputStream.close();
187       }
188     }
189     jar.close();
190     return testModules;
191   }
192 }
193