Merge changes I4c2acb80,Ib4ccd1be

* changes:
  Use AtomicFile
  Assert there is only one data folder created during test (1/n)
This commit is contained in:
JW Wang
2020-10-13 12:30:13 +00:00
committed by Android (Google) Code Review
2 changed files with 22 additions and 3 deletions

View File

@@ -25,6 +25,7 @@ import android.content.rollback.PackageRollbackInfo;
import android.content.rollback.PackageRollbackInfo.RestoreInfo;
import android.content.rollback.RollbackInfo;
import android.os.UserHandle;
import android.util.AtomicFile;
import android.util.Slog;
import android.util.SparseIntArray;
@@ -37,6 +38,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
@@ -66,8 +68,6 @@ class RollbackStore {
//
// * XXX, YYY are the rollbackIds for the corresponding rollbacks.
// * rollback.json contains all relevant metadata for the rollback.
//
// TODO: Use AtomicFile for all the .json files?
private final File mRollbackDataDir;
RollbackStore(File rollbackDataDir) {
@@ -259,6 +259,8 @@ class RollbackStore {
* Saves the given rollback to persistent storage.
*/
static void saveRollback(Rollback rollback) {
FileOutputStream fos = null;
AtomicFile file = new AtomicFile(new File(rollback.getBackupDir(), "rollback.json"));
try {
JSONObject dataJson = new JSONObject();
dataJson.put("info", rollbackInfoToJson(rollback.info));
@@ -272,11 +274,16 @@ class RollbackStore {
dataJson.putOpt(
"extensionVersions", extensionVersionsToJson(rollback.getExtensionVersions()));
PrintWriter pw = new PrintWriter(new File(rollback.getBackupDir(), "rollback.json"));
fos = file.startWrite();
PrintWriter pw = new PrintWriter(fos);
pw.println(dataJson.toString());
pw.close();
file.finishWrite(fos);
} catch (JSONException | IOException e) {
Slog.e(TAG, "Unable to save rollback for: " + rollback.info.getRollbackId(), e);
if (fos != null) {
file.failWrite(fos);
}
}
}

View File

@@ -18,6 +18,8 @@ package com.android.tests.rollback.host;
import static com.android.tests.rollback.host.WatchdogEventLogger.watchdogEventOccurred;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -272,6 +274,8 @@ public class StagedRollbackTest extends BaseHostJUnit4Test {
List<String> after = getSnapshotDirectories("/data/misc_ce/0/rollback");
// Only check directories newly created during the test
after.removeAll(before);
// There should be only one /data/misc_ce/0/rollback/<rollbackId> created during test
assertThat(after).hasSize(1);
after.forEach(dir -> assertDirectoryIsEmpty(dir));
}
@@ -358,6 +362,8 @@ public class StagedRollbackTest extends BaseHostJUnit4Test {
List<String> after = getSnapshotDirectories("/data/misc/apexrollback");
// Only check directories newly created during the test
after.removeAll(before);
// There should be only one /data/misc/apexrollback/<rollbackId> created during test
assertThat(after).hasSize(1);
after.forEach(dir -> assertDirectoryIsEmpty(dir));
}
@@ -405,6 +411,8 @@ public class StagedRollbackTest extends BaseHostJUnit4Test {
List<String> after = getSnapshotDirectories("/data/misc_de/0/apexrollback");
// Only check directories newly created during the test
after.removeAll(before);
// There should be only one /data/misc_de/0/apexrollback/<rollbackId> created during test
assertThat(after).hasSize(1);
after.forEach(dir -> assertDirectoryIsEmpty(dir));
}
@@ -450,6 +458,8 @@ public class StagedRollbackTest extends BaseHostJUnit4Test {
List<String> after = getSnapshotDirectories("/data/misc_ce/0/apexrollback");
// Only check directories newly created during the test
after.removeAll(before);
// There should be only one /data/misc_ce/0/apexrollback/<rollbackId> created during test
assertThat(after).hasSize(1);
after.forEach(dir -> assertDirectoryIsEmpty(dir));
}
@@ -509,6 +519,8 @@ public class StagedRollbackTest extends BaseHostJUnit4Test {
List<String> after = getSnapshotDirectories("/data/misc_ce/0/apexrollback");
// Only check directories newly created during the test
after.removeAll(before);
// There should be only one /data/misc_ce/0/apexrollback/<rollbackId> created during test
assertThat(after).hasSize(1);
// Expire all rollbacks and check CE snapshot directories are deleted
runPhase("testCleanUp");
for (String dir : after) {