diff options
author | Yi-Yo Chiang <yochiang@google.com> | 2022-08-03 10:41:34 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-08-03 10:41:34 +0000 |
commit | 586138ef74e6faab8f18d46242b9263e3cf8371a (patch) | |
tree | 7d7ac30fd8306d069966ab7cd8cbcc820d6a7cbb | |
parent | 892d2626d68e46fb6f051f99ac8fbdffc536669c (diff) | |
parent | edcf8a559b97a7ea84a1865060149a3c17135c8b (diff) | |
download | gsid-586138ef74e6faab8f18d46242b9263e3cf8371a.tar.gz |
Factor out common test utilities to helper library am: cbf3814da7 am: f90ec324ee am: 09ffe048f3 am: edcf8a559b
Original change: https://android-review.googlesource.com/c/platform/system/gsid/+/2170086
Change-Id: I6d22df8a7ba6d10f3daa1bc9a9c5bfe00aee0413
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | tests/Android.bp | 19 | ||||
-rw-r--r-- | tests/DSUEndtoEndTest.java | 33 | ||||
-rw-r--r-- | tests/DsuGsiIntegrationTest.java | 46 | ||||
-rw-r--r-- | tests/DsuTestBase.java | 79 |
4 files changed, 108 insertions, 69 deletions
diff --git a/tests/Android.bp b/tests/Android.bp index 78c8872..3035c10 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -57,9 +57,25 @@ cc_test { require_root: true, } +java_library_host { + name: "DsuTestBase", + srcs: [ + "DsuTestBase.java", + ], + libs: [ + "tradefed", + ], + visibility: [ + "//visibility:private", + ], +} + java_test_host { name: "DSUEndtoEndTest", srcs: ["DSUEndtoEndTest.java"], + static_libs: [ + "DsuTestBase", + ], libs: ["tradefed"], test_config: "dsu-test.xml", test_suites: ["general-tests"], @@ -70,6 +86,9 @@ java_test_host { srcs: [ "DsuGsiIntegrationTest.java", ], + static_libs: [ + "DsuTestBase", + ], libs: [ "tradefed", ], diff --git a/tests/DSUEndtoEndTest.java b/tests/DSUEndtoEndTest.java index e717079..de8b790 100644 --- a/tests/DSUEndtoEndTest.java +++ b/tests/DSUEndtoEndTest.java @@ -16,19 +16,14 @@ package com.android.tests.dsu; -import com.android.tradefed.build.BuildRetrievalError; import com.android.tradefed.build.IBuildInfo; import com.android.tradefed.build.IDeviceBuildInfo; import com.android.tradefed.config.Option; import com.android.tradefed.config.Option.Importance; -import com.android.tradefed.device.DeviceNotAvailableException; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; -import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; -import com.android.tradefed.util.CommandResult; import com.android.tradefed.util.ZipUtil2; import org.apache.commons.compress.archivers.zip.ZipFile; - import org.junit.After; import org.junit.Assert; import org.junit.Test; @@ -36,16 +31,13 @@ import org.junit.runner.RunWith; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.IOException; -import java.lang.Process; -import java.lang.Runtime; import java.util.concurrent.TimeUnit; /** * Test Dynamic System Updates by booting in and out of a supplied system image */ @RunWith(DeviceJUnit4ClassRunner.class) -public class DSUEndtoEndTest extends BaseHostJUnit4Test { +public class DSUEndtoEndTest extends DsuTestBase { private static final long kDefaultUserdataSize = 4L * 1024 * 1024 * 1024; private static final String LPUNPACK_PATH = "bin/lpunpack"; private static final String SIMG2IMG_PATH = "bin/simg2img"; @@ -121,7 +113,7 @@ public class DSUEndtoEndTest extends BaseHostJUnit4Test { if (!wasRoot) Assert.assertTrue("Test requires root", getDevice().enableAdbRoot()); - expectGsiStatus("normal"); + assertDsuStatus("normal"); // Sleep after installing to allow time for gsi_tool to reboot. This prevents a race between // the device rebooting and waitForDeviceAvailable() returning. @@ -130,38 +122,31 @@ public class DSUEndtoEndTest extends BaseHostJUnit4Test { getDevice().waitForDeviceAvailable(); getDevice().enableAdbRoot(); - expectGsiStatus("running"); + assertDsuStatus("running"); getDevice().rebootUntilOnline(); - expectGsiStatus("installed"); + assertDsuStatus("installed"); - CommandResult result = getDevice().executeShellV2Command("gsi_tool enable"); - Assert.assertEquals("gsi_tool enable failed", 0, result.getExitCode().longValue()); + assertShellCommand("gsi_tool enable"); getDevice().reboot(); - expectGsiStatus("running"); + assertDsuStatus("running"); getDevice().reboot(); - expectGsiStatus("running"); + assertDsuStatus("running"); - getDevice().executeShellV2Command("gsi_tool wipe"); + assertShellCommand("gsi_tool wipe"); getDevice().rebootUntilOnline(); - expectGsiStatus("normal"); + assertDsuStatus("normal"); if (wasRoot) { getDevice().enableAdbRoot(); } } - - private void expectGsiStatus(String expected) throws Exception { - CommandResult result = getDevice().executeShellV2Command("gsi_tool status"); - String status = result.getStdout().split("\n", 2)[0].trim(); - Assert.assertEquals("Device not in expected DSU state", expected, status); - } } diff --git a/tests/DsuGsiIntegrationTest.java b/tests/DsuGsiIntegrationTest.java index cb85575..7c4ac35 100644 --- a/tests/DsuGsiIntegrationTest.java +++ b/tests/DsuGsiIntegrationTest.java @@ -17,7 +17,6 @@ package com.android.tests.dsu; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -25,9 +24,6 @@ import com.android.tradefed.config.Option; import com.android.tradefed.device.DeviceNotAvailableException; import com.android.tradefed.log.LogUtil.CLog; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; -import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; -import com.android.tradefed.util.CommandResult; -import com.android.tradefed.util.CommandStatus; import com.android.tradefed.util.FileUtil; import com.android.tradefed.util.StreamUtil; @@ -47,7 +43,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @RunWith(DeviceJUnit4ClassRunner.class) -public class DsuGsiIntegrationTest extends BaseHostJUnit4Test { +public class DsuGsiIntegrationTest extends DsuTestBase { private static final long DSU_MAX_WAIT_SEC = 10 * 60; private static final long DSU_USERDATA_SIZE = 8L << 30; @@ -136,46 +132,6 @@ public class DsuGsiIntegrationTest extends BaseHostJUnit4Test { } } - private CommandResult assertShellCommand(String command) throws DeviceNotAvailableException { - CommandResult result = getDevice().executeShellV2Command(command); - assertEquals(CommandStatus.SUCCESS, result.getStatus()); - assertNotNull(result.getExitCode()); - assertEquals(0, result.getExitCode().intValue()); - return result; - } - - private boolean isDsuRunning() throws DeviceNotAvailableException { - CommandResult result; - try { - result = assertShellCommand("gsi_tool status"); - } catch (AssertionError e) { - CLog.e(e); - return false; - } - return result.getStdout().split("\n", 2)[0].trim().equals("running"); - } - - private void assertDsuRunning() throws DeviceNotAvailableException { - assertTrue("Expected DSU running", isDsuRunning()); - } - - private void assertDsuNotRunning() throws DeviceNotAvailableException { - assertFalse("Expected DSU not running", isDsuRunning()); - } - - private void assertAdbRoot() throws DeviceNotAvailableException { - assertTrue("Failed to 'adb root'", getDevice().enableAdbRoot()); - } - - private void assertDevicePathExist(String path) throws DeviceNotAvailableException { - assertTrue(String.format("Expected '%s' to exist", path), getDevice().doesFileExist(path)); - } - - private void assertDevicePathNotExist(String path) throws DeviceNotAvailableException { - assertFalse( - String.format("Expected '%s' to not exist", path), getDevice().doesFileExist(path)); - } - @Test public void testDsuGsi() throws DeviceNotAvailableException { if (isDsuRunning()) { diff --git a/tests/DsuTestBase.java b/tests/DsuTestBase.java new file mode 100644 index 0000000..d60af57 --- /dev/null +++ b/tests/DsuTestBase.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tests.dsu; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.log.LogUtil.CLog; +import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; +import com.android.tradefed.util.CommandResult; +import com.android.tradefed.util.CommandStatus; + +abstract class DsuTestBase extends BaseHostJUnit4Test { + public CommandResult assertShellCommand(String command) throws DeviceNotAvailableException { + CommandResult result = getDevice().executeShellV2Command(command); + assertEquals( + String.format("'%s' status", command), CommandStatus.SUCCESS, result.getStatus()); + assertNotNull(String.format("'%s' exit code", command), result.getExitCode()); + assertEquals(String.format("'%s' exit code", command), 0, result.getExitCode().intValue()); + return result; + } + + private String getDsuStatus() throws DeviceNotAvailableException { + CommandResult result; + try { + result = assertShellCommand("gsi_tool status"); + } catch (AssertionError e) { + CLog.e(e); + return null; + } + return result.getStdout().split("\n", 2)[0].trim(); + } + + public void assertDsuStatus(String expected) throws DeviceNotAvailableException { + assertEquals("DSU status", expected, getDsuStatus()); + } + + public boolean isDsuRunning() throws DeviceNotAvailableException { + return "running".equals(getDsuStatus()); + } + + public void assertDsuRunning() throws DeviceNotAvailableException { + assertTrue("Expected DSU running", isDsuRunning()); + } + + public void assertDsuNotRunning() throws DeviceNotAvailableException { + assertFalse("Expected DSU not running", isDsuRunning()); + } + + public void assertAdbRoot() throws DeviceNotAvailableException { + assertTrue("Failed to 'adb root'", getDevice().enableAdbRoot()); + } + + public void assertDevicePathExist(String path) throws DeviceNotAvailableException { + assertTrue(String.format("Expected '%s' to exist", path), getDevice().doesFileExist(path)); + } + + public void assertDevicePathNotExist(String path) throws DeviceNotAvailableException { + assertFalse( + String.format("Expected '%s' to not exist", path), getDevice().doesFileExist(path)); + } +} |