• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.tradefed.device.cloud;
17 
18 import com.android.ddmlib.IDevice;
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.device.IDeviceMonitor;
21 import com.android.tradefed.device.IDeviceStateMonitor;
22 import com.android.tradefed.device.TestDevice;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.result.error.DeviceErrorIdentifier;
25 import com.android.tradefed.util.CommandResult;
26 import com.android.tradefed.util.CommandStatus;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 /**
32  * Representation of the device running inside a remote Cuttlefish VM. It will alter the local
33  * device {@link TestDevice} behavior in some cases to take advantage of the setup.
34  */
35 public class NestedRemoteDevice extends TestDevice {
36 
37     // TODO: Improve the way we associate nested device with their user
38     private static final Map<String, String> IP_TO_USER = new HashMap<>();
39 
40     static {
41         IP_TO_USER.put("0.0.0.0:6520", "1");
42         IP_TO_USER.put("0.0.0.0:6521", "2");
43         IP_TO_USER.put("0.0.0.0:6522", "3");
44         IP_TO_USER.put("0.0.0.0:6523", "4");
45         IP_TO_USER.put("0.0.0.0:6524", "5");
46         IP_TO_USER.put("0.0.0.0:6525", "6");
47         IP_TO_USER.put("0.0.0.0:6526", "7");
48         IP_TO_USER.put("0.0.0.0:6527", "8");
49         IP_TO_USER.put("0.0.0.0:6528", "9");
50         IP_TO_USER.put("0.0.0.0:6529", "10");
51         IP_TO_USER.put("0.0.0.0:6530", "11");
52         IP_TO_USER.put("0.0.0.0:6531", "12");
53         IP_TO_USER.put("0.0.0.0:6532", "13");
54         IP_TO_USER.put("0.0.0.0:6533", "14");
55         IP_TO_USER.put("0.0.0.0:6534", "15");
56     }
57 
58     /**
59      * Creates a {@link NestedRemoteDevice}.
60      *
61      * @param device the associated {@link IDevice}
62      * @param stateMonitor the {@link IDeviceStateMonitor} mechanism to use
63      * @param allocationMonitor the {@link IDeviceMonitor} to inform of allocation state changes.
64      */
NestedRemoteDevice( IDevice device, IDeviceStateMonitor stateMonitor, IDeviceMonitor allocationMonitor)65     public NestedRemoteDevice(
66             IDevice device, IDeviceStateMonitor stateMonitor, IDeviceMonitor allocationMonitor) {
67         super(device, stateMonitor, allocationMonitor);
68         // TODO: Use IDevice directly
69         if (stateMonitor instanceof NestedDeviceStateMonitor) {
70             ((NestedDeviceStateMonitor) stateMonitor).setDevice(this);
71         }
72     }
73 
74     /** Teardown and restore the virtual device so testing can proceed. */
resetVirtualDevice()75     public final boolean resetVirtualDevice() throws DeviceNotAvailableException {
76         String usernameId = IP_TO_USER.get(getSerialNumber());
77         if (usernameId == null) {
78             throw new DeviceNotAvailableException(
79                     String.format("Cannot reset %s, no mapping in IP_TO_USER", getSerialNumber()),
80                     getSerialNumber(),
81                     DeviceErrorIdentifier.DEVICE_FAILED_TO_RESET);
82         }
83         String powerwashCommand = String.format("powerwash_cvd -instance_num %s", usernameId);
84         long timeout = Math.max(300000L, this.getOptions().getGceCmdTimeout());
85         CommandResult powerwashRes = getRunUtil().runTimedCmd(timeout, powerwashCommand.split(" "));
86         if (!CommandStatus.SUCCESS.equals(powerwashRes.getStatus())) {
87             CLog.e("%s", powerwashRes.getStderr());
88             // Log 'adb devices' to confirm device is gone
89             CommandResult printAdbDevices = getRunUtil().runTimedCmd(60000L, "adb", "devices");
90             CLog.e("%s\n%s", printAdbDevices.getStdout(), printAdbDevices.getStderr());
91             return false;
92         }
93         // Wait for the device to start for real.
94         getRunUtil().sleep(5000);
95         waitForDeviceAvailable();
96         resetContentProviderSetup();
97         return true;
98     }
99 }
100