1 /* 2 * Copyright (C) 2020 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.rollback.host; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 23 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 24 25 import org.junit.After; 26 import org.junit.Before; 27 import org.junit.Ignore; 28 import org.junit.Rule; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.concurrent.TimeUnit; 33 34 /** 35 * Runs the network rollback tests. 36 */ 37 @RunWith(DeviceJUnit4ClassRunner.class) 38 public class NetworkStagedRollbackTest extends BaseHostJUnit4Test { 39 /** 40 * Runs the given phase of a test by calling into the device. 41 * Throws an exception if the test phase fails. 42 * <p> 43 * For example, <code>runPhase("testApkOnlyEnableRollback");</code> 44 */ runPhase(String phase)45 private void runPhase(String phase) throws Exception { 46 assertTrue(runDeviceTests("com.android.tests.rollback", 47 "com.android.tests.rollback.NetworkStagedRollbackTest", 48 phase)); 49 } 50 51 private static final String REASON_EXPLICIT_HEALTH_CHECK = "REASON_EXPLICIT_HEALTH_CHECK"; 52 53 private static final String ROLLBACK_INITIATE = "ROLLBACK_INITIATE"; 54 private static final String ROLLBACK_BOOT_TRIGGERED = "ROLLBACK_BOOT_TRIGGERED"; 55 private static final String ROLLBACK_SUCCESS = "ROLLBACK_SUCCESS"; 56 57 private WatchdogEventLogger mLogger = new WatchdogEventLogger(); 58 59 @Rule 60 public AbandonSessionsRule mHostTestRule = new AbandonSessionsRule(this); 61 62 @Before setUp()63 public void setUp() throws Exception { 64 runPhase("cleanUp"); 65 mLogger.start(getDevice()); 66 } 67 68 @After tearDown()69 public void tearDown() throws Exception { 70 mLogger.stop(); 71 runPhase("cleanUp"); 72 } 73 74 /** 75 * Tests failed network health check triggers watchdog staged rollbacks. 76 */ 77 @Ignore("b/159569441") 78 @Test testNetworkFailedRollback()79 public void testNetworkFailedRollback() throws Exception { 80 try { 81 // Disconnect internet so we can test network health triggered rollbacks 82 getDevice().executeShellCommand("svc wifi disable"); 83 getDevice().executeShellCommand("svc data disable"); 84 85 runPhase("testNetworkFailedRollback_Phase1"); 86 // Reboot device to activate staged package 87 getDevice().reboot(); 88 89 // Verify rollback was enabled 90 runPhase("testNetworkFailedRollback_Phase2"); 91 // Wait for reboot to happen 92 assertTrue(getDevice().waitForDeviceNotAvailable(TimeUnit.MINUTES.toMillis(5))); 93 // Wait for reboot to complete and device to become available 94 getDevice().waitForDeviceAvailable(); 95 // Verify rollback was executed after health check deadline 96 runPhase("testNetworkFailedRollback_Phase3"); 97 98 assertTrue(mLogger.watchdogEventOccurred(ROLLBACK_INITIATE, null, 99 REASON_EXPLICIT_HEALTH_CHECK, null)); 100 assertTrue(mLogger.watchdogEventOccurred(ROLLBACK_BOOT_TRIGGERED, null, 101 null, null)); 102 assertTrue(mLogger.watchdogEventOccurred(ROLLBACK_SUCCESS, null, null, null)); 103 } finally { 104 // Reconnect internet again so we won't break tests which assume internet available 105 getDevice().executeShellCommand("svc wifi enable"); 106 getDevice().executeShellCommand("svc data enable"); 107 } 108 } 109 110 /** 111 * Tests passed network health check does not trigger watchdog staged rollbacks. 112 */ 113 @Test testNetworkPassedDoesNotRollback()114 public void testNetworkPassedDoesNotRollback() throws Exception { 115 runPhase("testNetworkPassedDoesNotRollback_Phase1"); 116 // Reboot device to activate staged package 117 getDevice().reboot(); 118 119 // Verify rollback was enabled 120 runPhase("testNetworkPassedDoesNotRollback_Phase2"); 121 122 // Connect to internet so network health check passes 123 getDevice().executeShellCommand("svc wifi enable"); 124 getDevice().executeShellCommand("svc data enable"); 125 126 // Wait for device available because emulator device may restart after turning 127 // on mobile data 128 getDevice().waitForDeviceAvailable(); 129 130 // Verify rollback was not executed after health check deadline 131 runPhase("testNetworkPassedDoesNotRollback_Phase3"); 132 133 assertEquals(mLogger.watchdogEventOccurred(null, null, 134 REASON_EXPLICIT_HEALTH_CHECK, null), false); 135 } 136 } 137