1 /* 2 * Copyright (C) 2022 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 17 package com.android.tests.dsu; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 24 import com.android.tradefed.config.Option; 25 import com.android.tradefed.device.DeviceNotAvailableException; 26 import com.android.tradefed.log.LogUtil.CLog; 27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 28 import com.android.tradefed.util.CommandResult; 29 import com.android.tradefed.util.CommandStatus; 30 31 abstract class DsuTestBase extends BaseHostJUnit4Test { 32 @Option( 33 name = "dsu-userdata-size-in-gb", 34 description = "Userdata partition size of the DSU installation") 35 private long mDsuUserdataSizeInGb; 36 getDsuUserdataSize(long defaultValue)37 protected long getDsuUserdataSize(long defaultValue) { 38 return mDsuUserdataSizeInGb > 0 ? mDsuUserdataSizeInGb << 30 : defaultValue; 39 } 40 assertShellCommand(String command)41 public CommandResult assertShellCommand(String command) throws DeviceNotAvailableException { 42 CommandResult result = getDevice().executeShellV2Command(command); 43 assertEquals( 44 String.format("'%s' status", command), CommandStatus.SUCCESS, result.getStatus()); 45 assertNotNull(String.format("'%s' exit code", command), result.getExitCode()); 46 assertEquals(String.format("'%s' exit code", command), 0, result.getExitCode().intValue()); 47 return result; 48 } 49 getDsuStatus()50 private String getDsuStatus() throws DeviceNotAvailableException { 51 CommandResult result; 52 try { 53 result = assertShellCommand("gsi_tool status"); 54 } catch (AssertionError e) { 55 CLog.e(e); 56 return null; 57 } 58 return result.getStdout().split("\n", 2)[0].trim(); 59 } 60 assertDsuStatus(String expected)61 public void assertDsuStatus(String expected) throws DeviceNotAvailableException { 62 assertEquals("DSU status", expected, getDsuStatus()); 63 } 64 isDsuRunning()65 public boolean isDsuRunning() throws DeviceNotAvailableException { 66 return "running".equals(getDsuStatus()); 67 } 68 assertDsuRunning()69 public void assertDsuRunning() throws DeviceNotAvailableException { 70 assertTrue("Expected DSU running", isDsuRunning()); 71 } 72 assertDsuNotRunning()73 public void assertDsuNotRunning() throws DeviceNotAvailableException { 74 assertFalse("Expected DSU not running", isDsuRunning()); 75 } 76 assertAdbRoot()77 public void assertAdbRoot() throws DeviceNotAvailableException { 78 assertTrue("Failed to 'adb root'", getDevice().enableAdbRoot()); 79 } 80 assertDevicePathExist(String path)81 public void assertDevicePathExist(String path) throws DeviceNotAvailableException { 82 assertTrue(String.format("Expected '%s' to exist", path), getDevice().doesFileExist(path)); 83 } 84 assertDevicePathNotExist(String path)85 public void assertDevicePathNotExist(String path) throws DeviceNotAvailableException { 86 assertFalse( 87 String.format("Expected '%s' to not exist", path), getDevice().doesFileExist(path)); 88 } 89 } 90