blob: 3d037ca047629400002eac4f90af6cd281777a06 [file] [log] [blame]
easoncyleeb1ca0ba2020-03-23 16:21:05 +08001/*
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 */
16package com.android.tradefed.suite.checker;
17
18import java.util.ArrayList;
19import java.util.HashSet;
20import java.util.List;
21import java.util.Set;
22
23import com.android.tradefed.config.Option;
24import com.android.tradefed.device.DeviceNotAvailableException;
25import com.android.tradefed.device.ITestDevice;
26import com.android.tradefed.log.LogUtil.CLog;
27import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus;
28
29/**
30 * Check if device has enough disk space for the given partitions.
31 */
32public class DeviceStorageStatusChecker implements ISystemStatusChecker {
33 @Option(
34 name = "minimal-storage-bytes",
35 description = "Number of bytes that should be left free on the device."
36 )
37 private long mMinimalStorage = 50L * 1024 * 1024; // 50MB
38
39 @Option(
40 name = "partition",
41 description = "Partition needed to be checked on the device."
42 )
43 private Set<String> mPartitions = new HashSet<>();
44
45 /** {@inheritDoc} */
46 @Override
47 public StatusCheckerResult preExecutionCheck(ITestDevice device)
48 throws DeviceNotAvailableException {
49 List<String> noEnoughStorage = new ArrayList<>();
50 for (String partition : mPartitions) {
51 long freeSpace = device.getPartitionFreeSpace(partition) * 1024;
52 String message = String.format(
53 "%s bytes left on the partition %s", freeSpace, partition);
54 CLog.i(message);
55 if (freeSpace < mMinimalStorage) {
56 noEnoughStorage.add(message);
57 }
58 }
59 if (noEnoughStorage.isEmpty()) {
60 return new StatusCheckerResult(CheckStatus.SUCCESS);
61 } else {
62 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.FAILED);
63 String errorMessage =
64 String.format(
65 "No enough storage left on the device in pre-execution: %s\n",
66 String.join(",", noEnoughStorage));
67 CLog.e(errorMessage);
68 result.setErrorMessage(errorMessage);
69 return result;
70 }
71 }
72}