diff options
author | JW Wang <wangchun@google.com> | 2020-05-13 10:57:27 +0800 |
---|---|---|
committer | JW Wang <wangchun@google.com> | 2020-05-29 15:49:54 +0800 |
commit | e90db5aba3050dd378453a2cfca80f8b8e9b6ee7 (patch) | |
tree | 550a9597afdb23f4a35ae54685816ac93cff6d19 /tests/RollbackTest | |
parent | 68739e632eeac4e78127a08d912e1cb9991207d0 (diff) |
Add tests for snapshot-deletion (2/n)
1. Document behavior changes due to ag/11475396
2. Test snapshots are deleted after restoration
See go/rollbackmanager-snapshot-deletion for more details.
Bug: 151805360
Test: atest StagedRollbackTest
Change-Id: Ia8993f82073a145889243bcefc74b6336bf71749
Diffstat (limited to 'tests/RollbackTest')
-rw-r--r-- | tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java b/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java index 78775be84828..9169ef517bf7 100644 --- a/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java +++ b/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java @@ -21,9 +21,12 @@ import static com.android.tests.rollback.host.WatchdogEventLogger.watchdogEventO import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.device.IFileEntry; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; import com.android.tradefed.util.CommandResult; @@ -35,8 +38,10 @@ import org.junit.Test; import org.junit.runner.RunWith; import java.io.File; +import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; /** * Runs the staged rollback tests. @@ -304,6 +309,7 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { */ @Test public void testRollbackApexDataDirectories_DeSys() throws Exception { + List<String> before = getSnapshotDirectories("/data/misc/apexrollback"); pushTestApex(); // Push files to apex data directory @@ -335,6 +341,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { assertEquals(TEST_STRING_2, getDevice().pullFileContents(oldFilePath2)); assertNull(getDevice().pullFile(newFilePath3)); assertNull(getDevice().pullFile(newFilePath4)); + + // Verify snapshots are deleted after restoration + List<String> after = getSnapshotDirectories("/data/misc/apexrollback"); + // Only check directories newly created during the test + after.removeAll(before); + after.forEach(dir -> assertDirectoryIsEmpty(dir)); } /** @@ -342,6 +354,7 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { */ @Test public void testRollbackApexDataDirectories_DeUser() throws Exception { + List<String> before = getSnapshotDirectories("/data/misc_de/0/apexrollback"); pushTestApex(); // Push files to apex data directory @@ -375,6 +388,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { assertEquals(TEST_STRING_2, getDevice().pullFileContents(oldFilePath2)); assertNull(getDevice().pullFile(newFilePath3)); assertNull(getDevice().pullFile(newFilePath4)); + + // Verify snapshots are deleted after restoration + List<String> after = getSnapshotDirectories("/data/misc_de/0/apexrollback"); + // Only check directories newly created during the test + after.removeAll(before); + after.forEach(dir -> assertDirectoryIsEmpty(dir)); } /** @@ -382,6 +401,7 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { */ @Test public void testRollbackApexDataDirectories_Ce() throws Exception { + List<String> before = getSnapshotDirectories("/data/misc_ce/0/apexrollback"); pushTestApex(); // Push files to apex data directory @@ -413,6 +433,12 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { assertEquals(TEST_STRING_2, getDevice().pullFileContents(oldFilePath2)); assertNull(getDevice().pullFile(newFilePath3)); assertNull(getDevice().pullFile(newFilePath4)); + + // Verify snapshots are deleted after restoration + List<String> after = getSnapshotDirectories("/data/misc_ce/0/apexrollback"); + // Only check directories newly created during the test + after.removeAll(before); + after.forEach(dir -> assertDirectoryIsEmpty(dir)); } private void pushTestApex() throws Exception { @@ -439,6 +465,28 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { return String.format("/data/misc_ce/%d/apexdata/%s", userId, apexName); } + private List<String> getSnapshotDirectories(String baseDir) { + try { + return getDevice().getFileEntry(baseDir).getChildren(false) + .stream().filter(entry -> entry.getName().matches("\\d+(-prerestore)?")) + .map(entry -> entry.getFullPath()) + .collect(Collectors.toList()); + } catch (Exception e) { + // Return an empty list if any error + return Collections.EMPTY_LIST; + } + } + + private void assertDirectoryIsEmpty(String path) { + try { + IFileEntry file = getDevice().getFileEntry(path); + assertTrue("Not a directory: " + path, file.isDirectory()); + assertTrue("Directory not empty: " + path, file.getChildren(false).isEmpty()); + } catch (DeviceNotAvailableException e) { + fail("Can't access directory: " + path); + } + } + private void crashProcess(String processName, int numberOfCrashes) throws Exception { String pid = ""; String lastPid = "invalid"; |