xref: /aosp_15_r20/system/gsid/tests/DsuTestBase.java (revision 4e2b41f188908a2ae9d9a2089f1f10779d080021)
1*4e2b41f1SAndroid Build Coastguard Worker /*
2*4e2b41f1SAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*4e2b41f1SAndroid Build Coastguard Worker  *
4*4e2b41f1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4e2b41f1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4e2b41f1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4e2b41f1SAndroid Build Coastguard Worker  *
8*4e2b41f1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4e2b41f1SAndroid Build Coastguard Worker  *
10*4e2b41f1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4e2b41f1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4e2b41f1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4e2b41f1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4e2b41f1SAndroid Build Coastguard Worker  * limitations under the License.
15*4e2b41f1SAndroid Build Coastguard Worker  */
16*4e2b41f1SAndroid Build Coastguard Worker 
17*4e2b41f1SAndroid Build Coastguard Worker package com.android.tests.dsu;
18*4e2b41f1SAndroid Build Coastguard Worker 
19*4e2b41f1SAndroid Build Coastguard Worker import static org.junit.Assert.assertEquals;
20*4e2b41f1SAndroid Build Coastguard Worker import static org.junit.Assert.assertFalse;
21*4e2b41f1SAndroid Build Coastguard Worker import static org.junit.Assert.assertNotNull;
22*4e2b41f1SAndroid Build Coastguard Worker import static org.junit.Assert.assertTrue;
23*4e2b41f1SAndroid Build Coastguard Worker 
24*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.config.Option;
25*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.device.DeviceNotAvailableException;
26*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.log.LogUtil.CLog;
27*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
28*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.util.CommandResult;
29*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.util.CommandStatus;
30*4e2b41f1SAndroid Build Coastguard Worker 
31*4e2b41f1SAndroid Build Coastguard Worker abstract class DsuTestBase extends BaseHostJUnit4Test {
32*4e2b41f1SAndroid Build Coastguard Worker     @Option(
33*4e2b41f1SAndroid Build Coastguard Worker             name = "dsu-userdata-size-in-gb",
34*4e2b41f1SAndroid Build Coastguard Worker             description = "Userdata partition size of the DSU installation")
35*4e2b41f1SAndroid Build Coastguard Worker     private long mDsuUserdataSizeInGb;
36*4e2b41f1SAndroid Build Coastguard Worker 
getDsuUserdataSize(long defaultValue)37*4e2b41f1SAndroid Build Coastguard Worker     protected long getDsuUserdataSize(long defaultValue) {
38*4e2b41f1SAndroid Build Coastguard Worker         return mDsuUserdataSizeInGb > 0 ? mDsuUserdataSizeInGb << 30 : defaultValue;
39*4e2b41f1SAndroid Build Coastguard Worker     }
40*4e2b41f1SAndroid Build Coastguard Worker 
assertShellCommand(String command)41*4e2b41f1SAndroid Build Coastguard Worker     public CommandResult assertShellCommand(String command) throws DeviceNotAvailableException {
42*4e2b41f1SAndroid Build Coastguard Worker         CommandResult result = getDevice().executeShellV2Command(command);
43*4e2b41f1SAndroid Build Coastguard Worker         assertEquals(
44*4e2b41f1SAndroid Build Coastguard Worker                 String.format("'%s' status", command), CommandStatus.SUCCESS, result.getStatus());
45*4e2b41f1SAndroid Build Coastguard Worker         assertNotNull(String.format("'%s' exit code", command), result.getExitCode());
46*4e2b41f1SAndroid Build Coastguard Worker         assertEquals(String.format("'%s' exit code", command), 0, result.getExitCode().intValue());
47*4e2b41f1SAndroid Build Coastguard Worker         return result;
48*4e2b41f1SAndroid Build Coastguard Worker     }
49*4e2b41f1SAndroid Build Coastguard Worker 
getDsuStatus()50*4e2b41f1SAndroid Build Coastguard Worker     private String getDsuStatus() throws DeviceNotAvailableException {
51*4e2b41f1SAndroid Build Coastguard Worker         CommandResult result;
52*4e2b41f1SAndroid Build Coastguard Worker         try {
53*4e2b41f1SAndroid Build Coastguard Worker             result = assertShellCommand("gsi_tool status");
54*4e2b41f1SAndroid Build Coastguard Worker         } catch (AssertionError e) {
55*4e2b41f1SAndroid Build Coastguard Worker             CLog.e(e);
56*4e2b41f1SAndroid Build Coastguard Worker             return null;
57*4e2b41f1SAndroid Build Coastguard Worker         }
58*4e2b41f1SAndroid Build Coastguard Worker         return result.getStdout().split("\n", 2)[0].trim();
59*4e2b41f1SAndroid Build Coastguard Worker     }
60*4e2b41f1SAndroid Build Coastguard Worker 
assertDsuStatus(String expected)61*4e2b41f1SAndroid Build Coastguard Worker     public void assertDsuStatus(String expected) throws DeviceNotAvailableException {
62*4e2b41f1SAndroid Build Coastguard Worker         assertEquals("DSU status", expected, getDsuStatus());
63*4e2b41f1SAndroid Build Coastguard Worker     }
64*4e2b41f1SAndroid Build Coastguard Worker 
isDsuRunning()65*4e2b41f1SAndroid Build Coastguard Worker     public boolean isDsuRunning() throws DeviceNotAvailableException {
66*4e2b41f1SAndroid Build Coastguard Worker         return "running".equals(getDsuStatus());
67*4e2b41f1SAndroid Build Coastguard Worker     }
68*4e2b41f1SAndroid Build Coastguard Worker 
assertDsuRunning()69*4e2b41f1SAndroid Build Coastguard Worker     public void assertDsuRunning() throws DeviceNotAvailableException {
70*4e2b41f1SAndroid Build Coastguard Worker         assertTrue("Expected DSU running", isDsuRunning());
71*4e2b41f1SAndroid Build Coastguard Worker     }
72*4e2b41f1SAndroid Build Coastguard Worker 
assertDsuNotRunning()73*4e2b41f1SAndroid Build Coastguard Worker     public void assertDsuNotRunning() throws DeviceNotAvailableException {
74*4e2b41f1SAndroid Build Coastguard Worker         assertFalse("Expected DSU not running", isDsuRunning());
75*4e2b41f1SAndroid Build Coastguard Worker     }
76*4e2b41f1SAndroid Build Coastguard Worker 
assertAdbRoot()77*4e2b41f1SAndroid Build Coastguard Worker     public void assertAdbRoot() throws DeviceNotAvailableException {
78*4e2b41f1SAndroid Build Coastguard Worker         assertTrue("Failed to 'adb root'", getDevice().enableAdbRoot());
79*4e2b41f1SAndroid Build Coastguard Worker     }
80*4e2b41f1SAndroid Build Coastguard Worker 
assertDevicePathExist(String path)81*4e2b41f1SAndroid Build Coastguard Worker     public void assertDevicePathExist(String path) throws DeviceNotAvailableException {
82*4e2b41f1SAndroid Build Coastguard Worker         assertTrue(String.format("Expected '%s' to exist", path), getDevice().doesFileExist(path));
83*4e2b41f1SAndroid Build Coastguard Worker     }
84*4e2b41f1SAndroid Build Coastguard Worker 
assertDevicePathNotExist(String path)85*4e2b41f1SAndroid Build Coastguard Worker     public void assertDevicePathNotExist(String path) throws DeviceNotAvailableException {
86*4e2b41f1SAndroid Build Coastguard Worker         assertFalse(
87*4e2b41f1SAndroid Build Coastguard Worker                 String.format("Expected '%s' to not exist", path), getDevice().doesFileExist(path));
88*4e2b41f1SAndroid Build Coastguard Worker     }
89*4e2b41f1SAndroid Build Coastguard Worker }
90