1 /*
2  * Copyright (C) 2017 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.common.tradefed.build;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.tradefed.build.IBuildInfo;
23 import com.android.tradefed.build.IDeviceBuildInfo;
24 import com.android.tradefed.config.OptionSetter;
25 import com.android.tradefed.device.ITestDevice;
26 import com.android.tradefed.invoker.ExecutionFiles;
27 import com.android.tradefed.util.FileUtil;
28 
29 import org.easymock.EasyMock;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.JUnit4;
35 
36 import java.io.File;
37 
38 /**
39  * Unit tests for {@link CompatibilityBuildProvider}.
40  */
41 @RunWith(JUnit4.class)
42 public class CompatibilityBuildProviderTest {
43 
44     private CompatibilityBuildProvider mProvider;
45     private ITestDevice mMockDevice;
46     private File mRootDir;
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         mMockDevice = EasyMock.createMock(ITestDevice.class);
51         mRootDir = FileUtil.createTempDir("cts-root-dir");
52         mProvider =
53                 new CompatibilityBuildProvider() {
54                     @Override
55                     String getRootDirPath() {
56                         return mRootDir.getAbsolutePath();
57                     }
58 
59                     @Override
60                     protected String getSuiteInfoName() {
61                         return "CTS";
62                     }
63 
64                     @Override
65                     ExecutionFiles getInvocationFiles() {
66                         return null;
67                     }
68                 };
69     }
70 
71     @After
tearDown()72     public void tearDown() {
73         FileUtil.recursiveDelete(mRootDir);
74     }
75 
76     /**
77      * Tests getting the build info without using the device information.
78      */
79     @Test
testBaseGetBuild()80     public void testBaseGetBuild() throws Exception {
81         EasyMock.replay(mMockDevice);
82         IBuildInfo info = mProvider.getBuild(mMockDevice);
83         EasyMock.verify(mMockDevice);
84         // Still creates a device build for us.
85         assertTrue(info instanceof IDeviceBuildInfo);
86         // tests dir should be populated
87         assertNotNull(((IDeviceBuildInfo)info).getTestsDir());
88     }
89 
90     /**
91      * Tests building build infos using the device information.
92      */
93     @Test
testBaseGetBuild_withDevice()94     public void testBaseGetBuild_withDevice() throws Exception {
95         // Create real testcases dir
96         new File(mRootDir, "android-cts/testcases").mkdirs();
97         OptionSetter setter = new OptionSetter(mProvider);
98         setter.setOptionValue("use-device-build-info", "true");
99         setter.setOptionValue("branch", "build_branch");
100         EasyMock.expect(mMockDevice.getBuildId()).andReturn("8888");
101         EasyMock.expect(mMockDevice.getBuildFlavor()).andReturn("flavor");
102         EasyMock.expect(mMockDevice.getBuildAlias()).andReturn("alias");
103         EasyMock.expect(mMockDevice.getProperty("ro.product.name")).andReturn("product");
104         EasyMock.expect(mMockDevice.getProperty("ro.build.type")).andReturn("userdebug");
105         EasyMock.replay(mMockDevice);
106         IBuildInfo info = mProvider.getBuild(mMockDevice);
107         EasyMock.verify(mMockDevice);
108         assertTrue(info instanceof IDeviceBuildInfo);
109         // tests dir should be populated
110         assertNotNull(((IDeviceBuildInfo)info).getTestsDir());
111         // ensure that tests dir is never clean up.
112         mProvider.cleanUp(info);
113         assertNotNull(((IDeviceBuildInfo) info).getTestsDir());
114     }
115 
116     /** Tests building build infos using the device information. */
117     @Test
testBaseGetBuild_withDeviceAndBuildFlavorPrefix()118     public void testBaseGetBuild_withDeviceAndBuildFlavorPrefix() throws Exception {
119         // Create real testcases dir
120         new File(mRootDir, "android-cts/testcases").mkdirs();
121         OptionSetter setter = new OptionSetter(mProvider);
122         setter.setOptionValue("use-device-build-info", "true");
123         setter.setOptionValue("branch", "build_branch");
124         setter.setOptionValue("build-flavor-prefix", "prefix-");
125         EasyMock.expect(mMockDevice.getBuildId()).andReturn("8888");
126         EasyMock.expect(mMockDevice.getBuildFlavor()).andReturn("flavor");
127         EasyMock.expect(mMockDevice.getBuildAlias()).andReturn("alias");
128         EasyMock.expect(mMockDevice.getProperty("ro.product.name")).andReturn("product");
129         EasyMock.expect(mMockDevice.getProperty("ro.build.type")).andReturn("userdebug");
130         EasyMock.replay(mMockDevice);
131         IBuildInfo info = mProvider.getBuild(mMockDevice);
132         EasyMock.verify(mMockDevice);
133         assertTrue(info instanceof IDeviceBuildInfo);
134         assertEquals("prefix-flavor", ((IDeviceBuildInfo) info).getBuildFlavor());
135         // tests dir should be populated
136         assertNotNull(((IDeviceBuildInfo) info).getTestsDir());
137         // ensure that tests dir is never clean up.
138         mProvider.cleanUp(info);
139         assertNotNull(((IDeviceBuildInfo) info).getTestsDir());
140     }
141 
142     /** Tests building build infos using the device information. */
143     @Test
testBaseGetBuild_withBuildFlavorAndPrefixOverride()144     public void testBaseGetBuild_withBuildFlavorAndPrefixOverride() throws Exception {
145         // Create real testcases dir
146         new File(mRootDir, "android-cts/testcases").mkdirs();
147         OptionSetter setter = new OptionSetter(mProvider);
148         setter.setOptionValue("use-device-build-info", "true");
149         setter.setOptionValue("branch", "build_branch");
150         setter.setOptionValue("build-flavor", "artificial-flavor");
151         setter.setOptionValue("build-flavor-prefix", "prefix-");
152         EasyMock.expect(mMockDevice.getBuildId()).andReturn("8888");
153         EasyMock.expect(mMockDevice.getBuildFlavor()).andReturn("flavor").anyTimes();
154         EasyMock.expect(mMockDevice.getBuildAlias()).andReturn("alias");
155         EasyMock.expect(mMockDevice.getProperty("ro.product.name")).andReturn("product");
156         EasyMock.expect(mMockDevice.getProperty("ro.build.type")).andReturn("userdebug");
157         EasyMock.replay(mMockDevice);
158         IBuildInfo info = mProvider.getBuild(mMockDevice);
159         EasyMock.verify(mMockDevice);
160         assertTrue(info instanceof IDeviceBuildInfo);
161         assertEquals("prefix-artificial-flavor", ((IDeviceBuildInfo) info).getBuildFlavor());
162         // tests dir should be populated
163         assertNotNull(((IDeviceBuildInfo) info).getTestsDir());
164         // ensure that tests dir is never clean up.
165         mProvider.cleanUp(info);
166         assertNotNull(((IDeviceBuildInfo)info).getTestsDir());
167     }
168 
169     /**
170      * Test that the {suite-name} pattern of the dynamic URL can be overriden by something different
171      * from the build-in suite name.
172      */
173     @Test
testDynamicUrlOverride()174     public void testDynamicUrlOverride() throws Exception {
175         final String uniquePattern = "UNIQUE_SUITE_NAME_PATTERN";
176         OptionSetter setter = new OptionSetter(mProvider);
177         setter.setOptionValue("url-suite-name-override", uniquePattern);
178         EasyMock.replay(mMockDevice);
179         IBuildInfo info = mProvider.getBuild(mMockDevice);
180         EasyMock.verify(mMockDevice);
181         String url = info.getBuildAttributes().get(
182                 CompatibilityBuildProvider.DYNAMIC_CONFIG_OVERRIDE_URL);
183         assertTrue(String.format("URL was %s and should have contained %s", url, uniquePattern),
184                 url.contains(uniquePattern));
185     }
186 
187     /* Test that getRootDirPath can handle suite name containing `-`. */
188     @Test
testGetRootDirPath()189     public void testGetRootDirPath() throws Exception {
190         CompatibilityBuildProvider provider =
191                 new CompatibilityBuildProvider() {
192                     @Override
193                     protected String getSuiteInfoName() {
194                         return "VTS-CORE";
195                     }
196                 };
197         final String path = "test/path";
198         System.setProperty("VTS_CORE_ROOT", path);
199         assertEquals(path, provider.getRootDirPath());
200     }
201 }
202