aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2017-10-22 04:26:29 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-10-22 04:26:29 +0000
commit96ffd7f6ea857cfd67624d8a26e243005de41d82 (patch)
treebd558463e4806c25d380f9b8926a138456a73a7c
parent57121e69f0d2139f73c3b0649078d1a4fc9612b2 (diff)
parent90512d398cab5cbca6fc5ff74f4b38ed953b52de (diff)
downloadcontrib-96ffd7f6ea857cfd67624d8a26e243005de41d82.tar.gz
Merge "Add misc target preparers plus testing for contrib"
-rw-r--r--src/com/android/tradefed/targetprep/FillStorageTargetPreparer.java62
-rw-r--r--src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparer.java35
-rw-r--r--src/com/android/tradefed/targetprep/RootTargetPreparer.java32
-rw-r--r--src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparer.java87
-rw-r--r--src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparer.java68
-rw-r--r--src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparer.java59
-rw-r--r--tests/src/com/android/tradefed/ContribUnitTests.java49
-rw-r--r--tests/src/com/android/tradefed/targetprep/FillStorageTargetPreparerTest.java80
-rw-r--r--tests/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparerTest.java29
-rw-r--r--tests/src/com/android/tradefed/targetprep/RootTargetPreparerTest.java63
-rw-r--r--tests/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparerTest.java81
-rw-r--r--tests/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparerTest.java103
-rw-r--r--tests/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparerTest.java94
13 files changed, 838 insertions, 4 deletions
diff --git a/src/com/android/tradefed/targetprep/FillStorageTargetPreparer.java b/src/com/android/tradefed/targetprep/FillStorageTargetPreparer.java
new file mode 100644
index 0000000..e28faf3
--- /dev/null
+++ b/src/com/android/tradefed/targetprep/FillStorageTargetPreparer.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.LogUtil.CLog;
+
+@OptionClass(alias = "fill-storage")
+public class FillStorageTargetPreparer implements ITargetCleaner {
+
+ private static final String FILL_STORAGE_FILENAME = "/data/bigfile";
+
+ @Option(name = "free-bytes",
+ description = "Number of bytes that should be left free on the device.")
+ private long mFreeBytesRequested;
+
+ private long getDataFreeSpace(ITestDevice device) throws DeviceNotAvailableException {
+ String output = device.executeShellCommand("df /data");
+ String[] lines = output.split("\n");
+ String[] splitLines = lines[1].split("\\s+");
+ return Long.parseLong(splitLines[3]) * 1024L;
+ }
+
+ @Override
+ public void setUp(ITestDevice device, IBuildInfo buildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ long freeSpace = getDataFreeSpace(device);
+ if (freeSpace > mFreeBytesRequested) {
+ long bytesToWrite = freeSpace - mFreeBytesRequested;
+ device.executeShellCommand(
+ String.format("fallocate -l %d %s", bytesToWrite, FILL_STORAGE_FILENAME));
+ CLog.i("Wrote %d bytes to %s", bytesToWrite, FILL_STORAGE_FILENAME);
+ } else {
+ CLog.i("Not enough free space (%d bytes requested free, %d bytes actually free)",
+ mFreeBytesRequested, freeSpace);
+ }
+ }
+
+ @Override
+ public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
+ throws DeviceNotAvailableException {
+ device.executeShellCommand("rm -f " + FILL_STORAGE_FILENAME);
+ }
+}
diff --git a/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparer.java b/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparer.java
index e4bb872..e8cf053 100644
--- a/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparer.java
+++ b/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparer.java
@@ -20,11 +20,12 @@ import com.android.tradefed.config.Option;
import com.android.tradefed.config.OptionClass;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.util.IRunUtil;
import com.android.tradefed.util.RunUtil;
@OptionClass(alias = "restart-system-server")
-public class RestartSystemServerTargetPreparer implements ITargetPreparer {
+public class RestartSystemServerTargetPreparer implements ITargetCleaner {
@Option(name = "poll-interval-millis",
description = "Time interval to poll if system server has restarted")
private long mPollIntervalMillis = 3000L;
@@ -32,6 +33,12 @@ public class RestartSystemServerTargetPreparer implements ITargetPreparer {
description = "Max number of tries to poll")
private int mMaxTries = 10;
+ @Option(name = "restart-before")
+ private boolean mRestartBefore = true;
+
+ @Option(name = "restart-after")
+ private boolean mRestartAfter = false;
+
private IRunUtil mRunUtil;
public RestartSystemServerTargetPreparer() {
@@ -42,9 +49,7 @@ public class RestartSystemServerTargetPreparer implements ITargetPreparer {
this.mRunUtil = runUtil;
}
- @Override
- public void setUp(ITestDevice device, IBuildInfo buildInfo)
- throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ private boolean restartSystemServer(ITestDevice device) throws DeviceNotAvailableException {
device.executeShellCommand("setprop sys.boot_completed 0");
String pid = device.executeShellCommand("pidof system_server");
device.executeShellCommand("kill " + pid);
@@ -56,9 +61,31 @@ public class RestartSystemServerTargetPreparer implements ITargetPreparer {
}
mRunUtil.sleep(mPollIntervalMillis);
}
+ return success;
+ }
+
+ @Override
+ public void setUp(ITestDevice device, IBuildInfo buildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ if (!mRestartBefore) {
+ return;
+ }
+ boolean success = restartSystemServer(device);
if (!success) {
throw new TargetSetupError("Gave up on waiting for system server to restart",
device.getDeviceDescriptor());
}
}
+
+ @Override
+ public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
+ throws DeviceNotAvailableException {
+ if (!mRestartAfter) {
+ return;
+ }
+ boolean success = restartSystemServer(device);
+ if (!success) {
+ CLog.i("Failed to restart system server on tearDown.");
+ }
+ }
}
diff --git a/src/com/android/tradefed/targetprep/RootTargetPreparer.java b/src/com/android/tradefed/targetprep/RootTargetPreparer.java
new file mode 100644
index 0000000..0035376
--- /dev/null
+++ b/src/com/android/tradefed/targetprep/RootTargetPreparer.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+
+@OptionClass(alias = "root")
+public class RootTargetPreparer implements ITargetPreparer {
+ @Override
+ public void setUp(ITestDevice device, IBuildInfo buildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ if (!device.enableAdbRoot()) {
+ throw new TargetSetupError("Failed to adb root device", device.getDeviceDescriptor());
+ }
+ }
+}
diff --git a/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparer.java b/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparer.java
new file mode 100644
index 0000000..922efa7
--- /dev/null
+++ b/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparer.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+
+import java.util.concurrent.TimeUnit;
+
+// Remembers the time at the beginning and resets it (plus time elapsed) on cleanup.
+// Can also optionally set the time at the beginning.
+@OptionClass(alias = "save-restore-time")
+public class SaveAndRestoreTimeTargetPreparer implements ITargetCleaner {
+
+ @Option(name = "set-time",
+ description = "Whether or not to set the time")
+ private boolean mSetTime = false;
+
+ @Option(name = "time",
+ description = "Time to set")
+ private long mTimeToSet;
+
+ public interface ITimeGetter {
+ long getTime();
+ }
+
+ public static class RealTimeGetter implements ITimeGetter {
+ @Override
+ public long getTime() {
+ return System.nanoTime();
+ }
+ }
+
+ private long mStartNanoTime, mDeviceStartTimeMillis;
+ private ITimeGetter mTimeGetter;
+
+ public SaveAndRestoreTimeTargetPreparer(ITimeGetter timeGetter) {
+ this.mTimeGetter = timeGetter;
+ }
+
+ public SaveAndRestoreTimeTargetPreparer() {
+ this(new RealTimeGetter());
+ }
+
+ private long getDeviceTime(ITestDevice device) throws DeviceNotAvailableException {
+ return Long.parseLong(device.executeShellCommand("date +\"%s\"").trim());
+ }
+
+ private void setDeviceTime(ITestDevice device, long time) throws DeviceNotAvailableException {
+ device.executeShellCommand("date @" + time);
+ }
+
+ @Override
+ public void setUp(ITestDevice device, IBuildInfo buildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ mStartNanoTime = mTimeGetter.getTime();
+ mDeviceStartTimeMillis = getDeviceTime(device);
+ if (mSetTime) {
+ setDeviceTime(device, mTimeToSet);
+ }
+ }
+
+ @Override
+ public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
+ throws DeviceNotAvailableException {
+ long elapsedNanos = mTimeGetter.getTime() - mStartNanoTime;
+ long newTime = mDeviceStartTimeMillis + TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
+ setDeviceTime(device, newTime);
+ }
+}
diff --git a/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparer.java b/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparer.java
new file mode 100644
index 0000000..5637876
--- /dev/null
+++ b/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparer.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@OptionClass(alias = "set-restore-sys-prop")
+public class SetAndRestoreSystemPropertyTargetPreparer implements ITargetCleaner {
+
+ @Option(name = "set-property",
+ description = "Property to set then restore on cleanup")
+ private Map<String, String> mProperties = new HashMap<>();
+
+ private Map<String, String> mOldProperties = new HashMap<>();
+
+ private void setProperty(ITestDevice device, String prop, String newValue)
+ throws DeviceNotAvailableException {
+ device.executeShellCommand(String.format("setprop \"%s\" \"%s\"", prop, newValue));
+ }
+
+ private String getProperty(ITestDevice device, String prop)
+ throws DeviceNotAvailableException {
+ return device.executeShellCommand(String.format("getprop \"%s\"", prop)).trim();
+ }
+
+ @Override
+ public void setUp(ITestDevice device, IBuildInfo buildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ for (String prop : mProperties.keySet()) {
+ String oldValue = getProperty(device, prop);
+ mOldProperties.put(prop, oldValue);
+ String newValue = mProperties.get(prop);
+ setProperty(device, prop, newValue);
+ if (!getProperty(device, prop).equals(newValue)) {
+ throw new TargetSetupError("Failed to set property " + prop,
+ device.getDeviceDescriptor());
+ }
+ }
+ }
+
+ @Override
+ public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
+ throws DeviceNotAvailableException {
+ for (Map.Entry<String, String> entry : mOldProperties.entrySet()) {
+ setProperty(device, entry.getKey(), entry.getValue());
+ }
+ }
+}
diff --git a/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparer.java b/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparer.java
new file mode 100644
index 0000000..53cb3ba
--- /dev/null
+++ b/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparer.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+
+import java.io.File;
+
+@OptionClass(alias = "write-restore-file")
+public class WriteAndRestoreFileTargetPreparer implements ITargetCleaner {
+ @Option(name = "file-name",
+ description = "Name of file to write")
+ private String mFileName;
+
+ @Option(name = "contents",
+ description = "Contents to write to file")
+ private String mContents;
+
+ private File mOldContents;
+
+ @Override
+ public void setUp(ITestDevice device, IBuildInfo buildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ if (device.doesFileExist(mFileName)) {
+ mOldContents = device.pullFile(mFileName);
+ }
+ if (!device.pushString(mContents, mFileName)) {
+ throw new TargetSetupError("Failed to push string to file",
+ device.getDeviceDescriptor());
+ }
+ }
+
+ @Override
+ public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
+ throws DeviceNotAvailableException {
+ if (mOldContents == null) {
+ device.executeShellCommand(String.format("rm -f %s", mFileName));
+ } else {
+ device.pushFile(mOldContents, mFileName);
+ }
+ }
+}
diff --git a/tests/src/com/android/tradefed/ContribUnitTests.java b/tests/src/com/android/tradefed/ContribUnitTests.java
new file mode 100644
index 0000000..6e098d7
--- /dev/null
+++ b/tests/src/com/android/tradefed/ContribUnitTests.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2010 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.tradefed;
+
+import com.android.tradefed.targetprep.FillStorageTargetPreparerTest;
+import com.android.tradefed.targetprep.RebootTargetPreparerTest;
+import com.android.tradefed.targetprep.RestartSystemServerTargetPreparerTest;
+import com.android.tradefed.targetprep.RootTargetPreparerTest;
+import com.android.tradefed.targetprep.SaveAndRestoreTimeTargetPreparerTest;
+import com.android.tradefed.targetprep.SetAndRestoreSystemPropertyTargetPreparerTest;
+import com.android.tradefed.targetprep.WriteAndRestoreFileTargetPreparerTest;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+/**
+ * A test suite for all Trade Federation unit tests running under Junit4.
+ *
+ * <p>All tests listed here should be self-contained, and should not require any external
+ * dependencies.
+ */
+@RunWith(Suite.class)
+@SuiteClasses({
+ // targetprep
+ FillStorageTargetPreparerTest.class,
+ RebootTargetPreparerTest.class,
+ RestartSystemServerTargetPreparerTest.class,
+ RootTargetPreparerTest.class,
+ SaveAndRestoreTimeTargetPreparerTest.class,
+ SetAndRestoreSystemPropertyTargetPreparerTest.class,
+ WriteAndRestoreFileTargetPreparerTest.class,
+})
+public class ContribUnitTests {
+ // empty of purpose
+}
diff --git a/tests/src/com/android/tradefed/targetprep/FillStorageTargetPreparerTest.java b/tests/src/com/android/tradefed/targetprep/FillStorageTargetPreparerTest.java
new file mode 100644
index 0000000..69d7fc3
--- /dev/null
+++ b/tests/src/com/android/tradefed/targetprep/FillStorageTargetPreparerTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.OptionSetter;
+import com.android.tradefed.device.ITestDevice;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Unit Tests for {@link FillStorageTargetPreparer}.
+ */
+@RunWith(JUnit4.class)
+public class FillStorageTargetPreparerTest {
+ private FillStorageTargetPreparer mFillStorageTargetPreparer;
+ private ITestDevice mMockDevice;
+ private IBuildInfo mMockBuildInfo;
+
+ @Before
+ public void setUp() {
+ mFillStorageTargetPreparer = new FillStorageTargetPreparer();
+ mMockDevice = EasyMock.createMock(ITestDevice.class);
+ mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
+ }
+
+ @Test
+ public void testSetUpWriteFile() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mFillStorageTargetPreparer);
+ optionSetter.setOptionValue("free-bytes", "50");
+ EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())).andReturn(
+ "a\n0 0 0 75 0\n").once();
+ EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())).andReturn(
+ null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mFillStorageTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testSetUpSkip() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mFillStorageTargetPreparer);
+ optionSetter.setOptionValue("free-bytes", "50000");
+ EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())).andReturn(
+ "a\n0 0 0 25 0\n").once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mFillStorageTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testTearDown() throws Exception {
+ EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())).andReturn(
+ null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mFillStorageTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+}
diff --git a/tests/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparerTest.java b/tests/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparerTest.java
index c76f109..e3383d5 100644
--- a/tests/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparerTest.java
+++ b/tests/src/com/android/tradefed/targetprep/RestartSystemServerTargetPreparerTest.java
@@ -114,4 +114,33 @@ public class RestartSystemServerTargetPreparerTest {
EasyMock.verify(mMockDevice, mMockBuildInfo);
}
+ @Test
+ public void testTearDown_restart() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mRestartSystemServerTargetPreparer);
+ optionSetter.setOptionValue("restart-before", "false");
+ optionSetter.setOptionValue("restart-after", "true");
+
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop sys.boot_completed 0")).andReturn(
+ null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("pidof system_server")).andReturn(
+ "123").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("kill 123")).andReturn(null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop sys.boot_completed")).andReturn(
+ "1").once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mRestartSystemServerTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockDevice, mMockBuildInfo);
+ }
+
+ @Test
+ public void testNone() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mRestartSystemServerTargetPreparer);
+ optionSetter.setOptionValue("restart-before", "false");
+ optionSetter.setOptionValue("restart-after", "false");
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mRestartSystemServerTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ EasyMock.verify(mMockDevice, mMockBuildInfo);
+ }
}
diff --git a/tests/src/com/android/tradefed/targetprep/RootTargetPreparerTest.java b/tests/src/com/android/tradefed/targetprep/RootTargetPreparerTest.java
new file mode 100644
index 0000000..b483545
--- /dev/null
+++ b/tests/src/com/android/tradefed/targetprep/RootTargetPreparerTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.ITestDevice;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Unit Tests for {@link RootTargetPreparer}.
+ */
+@RunWith(JUnit4.class)
+public class RootTargetPreparerTest {
+
+ private RootTargetPreparer mRootTargetPreparer;
+ private ITestDevice mMockDevice;
+ private IBuildInfo mMockBuildInfo;
+
+ @Before
+ public void setUp() {
+ mRootTargetPreparer = new RootTargetPreparer();
+ mMockDevice = EasyMock.createMock(ITestDevice.class);
+ mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
+ }
+
+ @Test
+ public void testSetUpSuccess() throws Exception {
+ EasyMock.expect(mMockDevice.enableAdbRoot()).andReturn(true).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mRootTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ EasyMock.verify(mMockDevice, mMockBuildInfo);
+ }
+
+ @Test(expected = TargetSetupError.class)
+ public void testSetUpFail() throws Exception {
+ EasyMock.expect(mMockDevice.enableAdbRoot()).andReturn(false).once();
+ EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mRootTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ EasyMock.verify(mMockDevice, mMockBuildInfo);
+ }
+
+}
diff --git a/tests/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparerTest.java b/tests/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparerTest.java
new file mode 100644
index 0000000..b5b1bfd
--- /dev/null
+++ b/tests/src/com/android/tradefed/targetprep/SaveAndRestoreTimeTargetPreparerTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.OptionSetter;
+import com.android.tradefed.device.ITestDevice;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Unit Tests for {@link FillStorageTargetPreparer}.
+ */
+@RunWith(JUnit4.class)
+public class SaveAndRestoreTimeTargetPreparerTest {
+ private SaveAndRestoreTimeTargetPreparer mSaveAndRestoreTimeTargetPreparer;
+ private SaveAndRestoreTimeTargetPreparer.ITimeGetter mMockTimeGetter;
+ private ITestDevice mMockDevice;
+ private IBuildInfo mMockBuildInfo;
+
+ @Before
+ public void setUp() {
+ mMockDevice = EasyMock.createMock(ITestDevice.class);
+ mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
+ mMockTimeGetter = EasyMock.createMock(SaveAndRestoreTimeTargetPreparer.ITimeGetter.class);
+ mSaveAndRestoreTimeTargetPreparer = new SaveAndRestoreTimeTargetPreparer(mMockTimeGetter);
+ }
+
+ @Test
+ public void testSaveTime() throws Exception {
+ EasyMock.expect(mMockTimeGetter.getTime()).andReturn(TimeUnit.MILLISECONDS.toNanos(2)).once();
+ EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())).andReturn(
+ "123\n").once();
+ EasyMock.expect(mMockTimeGetter.getTime()).andReturn(TimeUnit.MILLISECONDS.toNanos(7)).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("date @128")).andReturn(
+ null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo, mMockTimeGetter);
+
+ mSaveAndRestoreTimeTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mSaveAndRestoreTimeTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testSaveTime_setTimeAtBeginning() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mSaveAndRestoreTimeTargetPreparer);
+ optionSetter.setOptionValue("set-time", "true");
+ optionSetter.setOptionValue("time", "555");
+ EasyMock.expect(mMockTimeGetter.getTime()).andReturn(TimeUnit.MILLISECONDS.toNanos(2)).once();
+ EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())).andReturn(
+ "123\n").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("date @555")).andReturn(null).once();
+ EasyMock.expect(mMockTimeGetter.getTime()).andReturn(TimeUnit.MILLISECONDS.toNanos(7)).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("date @128")).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo, mMockTimeGetter);
+
+ mSaveAndRestoreTimeTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mSaveAndRestoreTimeTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+}
diff --git a/tests/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparerTest.java b/tests/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparerTest.java
new file mode 100644
index 0000000..d574edd
--- /dev/null
+++ b/tests/src/com/android/tradefed/targetprep/SetAndRestoreSystemPropertyTargetPreparerTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.OptionSetter;
+import com.android.tradefed.device.ITestDevice;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Unit Tests for {@link FillStorageTargetPreparer}.
+ */
+@RunWith(JUnit4.class)
+public class SetAndRestoreSystemPropertyTargetPreparerTest {
+ private SetAndRestoreSystemPropertyTargetPreparer mSetAndRestoreSystemPropertyTargetPreparer;
+ private ITestDevice mMockDevice;
+ private IBuildInfo mMockBuildInfo;
+
+ @Before
+ public void setUp() {
+ mSetAndRestoreSystemPropertyTargetPreparer = new SetAndRestoreSystemPropertyTargetPreparer();
+ mMockDevice = EasyMock.createMock(ITestDevice.class);
+ mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
+ }
+
+ @Test(expected = TargetSetupError.class)
+ public void testOneProp_fail() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mSetAndRestoreSystemPropertyTargetPreparer);
+ optionSetter.setOptionValue("set-property", "a", "new");
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"a\"")).andReturn("old").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"a\" \"new\"")).andReturn(null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"a\"")).andReturn("old").once();
+ EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mSetAndRestoreSystemPropertyTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testZeroProps() throws Exception {
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mSetAndRestoreSystemPropertyTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mSetAndRestoreSystemPropertyTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testOneProp() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mSetAndRestoreSystemPropertyTargetPreparer);
+ optionSetter.setOptionValue("set-property", "a", "new");
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"a\"")).andReturn("old").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"a\" \"new\"")).andReturn(null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"a\"")).andReturn("new").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"a\" \"old\"")).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mSetAndRestoreSystemPropertyTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mSetAndRestoreSystemPropertyTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testTwoProps() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mSetAndRestoreSystemPropertyTargetPreparer);
+ optionSetter.setOptionValue("set-property", "a", "new");
+ optionSetter.setOptionValue("set-property", "b", "newb");
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"a\"")).andReturn("old").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"a\" \"new\"")).andReturn(null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"a\"")).andReturn("new").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"b\"")).andReturn("oldb").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"b\" \"newb\"")).andReturn(null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("getprop \"b\"")).andReturn("newb").once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"a\" \"old\"")).andReturn(null).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("setprop \"b\" \"oldb\"")).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mSetAndRestoreSystemPropertyTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mSetAndRestoreSystemPropertyTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+}
diff --git a/tests/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparerTest.java b/tests/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparerTest.java
new file mode 100644
index 0000000..c0fb972
--- /dev/null
+++ b/tests/src/com/android/tradefed/targetprep/WriteAndRestoreFileTargetPreparerTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2017 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.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.OptionSetter;
+import com.android.tradefed.device.ITestDevice;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.File;
+
+/**
+ * Unit Tests for {@link WriteAndRestoreFileTargetPreparer}.
+ */
+@RunWith(JUnit4.class)
+public class WriteAndRestoreFileTargetPreparerTest {
+ private WriteAndRestoreFileTargetPreparer mWriteAndRestoreFileTargetPreparer;
+ private ITestDevice mMockDevice;
+ private IBuildInfo mMockBuildInfo;
+
+ @Before
+ public void setUp() {
+ mWriteAndRestoreFileTargetPreparer = new WriteAndRestoreFileTargetPreparer();
+ mMockDevice = EasyMock.createMock(ITestDevice.class);
+ mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
+ }
+
+ @Test(expected = TargetSetupError.class)
+ public void testFail() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mWriteAndRestoreFileTargetPreparer);
+ optionSetter.setOptionValue("file-name", "file");
+ optionSetter.setOptionValue("contents", "hi");
+ EasyMock.expect(mMockDevice.doesFileExist("file")).andReturn(false).once();
+ EasyMock.expect(mMockDevice.pushString("hi", "file")).andReturn(false).once();
+ EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mWriteAndRestoreFileTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mWriteAndRestoreFileTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testDoesntExist() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mWriteAndRestoreFileTargetPreparer);
+ optionSetter.setOptionValue("file-name", "file");
+ optionSetter.setOptionValue("contents", "hi");
+ EasyMock.expect(mMockDevice.doesFileExist("file")).andReturn(false).once();
+ EasyMock.expect(mMockDevice.pushString("hi", "file")).andReturn(true).once();
+ EasyMock.expect(mMockDevice.executeShellCommand("rm -f file")).andReturn(null).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mWriteAndRestoreFileTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mWriteAndRestoreFileTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+ @Test
+ public void testAlreadyExists() throws Exception {
+ OptionSetter optionSetter = new OptionSetter(mWriteAndRestoreFileTargetPreparer);
+ File file = new File("a");
+ optionSetter.setOptionValue("file-name", "file");
+ optionSetter.setOptionValue("contents", "hi");
+ EasyMock.expect(mMockDevice.doesFileExist("file")).andReturn(true).once();
+ EasyMock.expect(mMockDevice.pullFile("file")).andReturn(file).once();
+ EasyMock.expect(mMockDevice.pushString("hi", "file")).andReturn(true).once();
+ EasyMock.expect(mMockDevice.pushFile(file, "file")).andReturn(true).once();
+ EasyMock.replay(mMockDevice, mMockBuildInfo);
+
+ mWriteAndRestoreFileTargetPreparer.setUp(mMockDevice, mMockBuildInfo);
+ mWriteAndRestoreFileTargetPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
+ EasyMock.verify(mMockBuildInfo, mMockDevice);
+ }
+
+}