autotest: Remove existing autoserv dirs on startup
While server tests, when run normally, will clean up their temporary
installation and some large output directories, the same is not true
when a user uses ctrl-c to force-close the test.
This change modifies server tests to remove existing /tmp/autoserv-* and
/tmp/sysinfo/autoserv-* directories before creating new ones, such that
existing left-over directories will not cause a DUT's /tmp filesystem to
run out of space.
BUG=chromium:863601
TEST=Ran server tests to completion, ran a modified server test with
a syntax error, and interrupted running server tests with SIGINT.
The first two cases remain unchanged, as the test framework
performed cleanup as expected. Ensured that the third test case was
actually triggering a cleanup.
TEST=Used get_tmp_dir to create multiple temp dirs, including a set of
nested temporary directories. Then alternated between printing
self.tmp_dirs and calling delete_all_tmp_dirs with various parent
directories to ensure the expected behavior was occuring both on
the host and with regards to the contents of self.tmp_dirs.
Change-Id: I82a1619d4c8976547792f3cac84b6ed41148b484
Reviewed-on: https://chromium-review.googlesource.com/1147500
Commit-Ready: Alex Khouderchah <[email protected]>
Tested-by: Alex Khouderchah <[email protected]>
Reviewed-by: Richard Barnette <[email protected]>
diff --git a/server/hosts/remote.py b/server/hosts/remote.py
index 81a4d0f..ea3ae06 100644
--- a/server/hosts/remote.py
+++ b/server/hosts/remote.py
@@ -1,6 +1,7 @@
"""This class defines the Remote host class."""
import os, logging, urllib, time
+import re
from autotest_lib.client.common_lib import error
from autotest_lib.server import utils
from autotest_lib.server.hosts import base_classes
@@ -28,6 +29,7 @@
_DETECTABLE_LABELS = []
VAR_LOG_MESSAGES_COPY_PATH = "/var/tmp/messages.autotest_start"
+ TMP_DIR_TEMPLATE = 'autoserv-XXXXXX'
def _initialize(self, hostname, autodir=None, *args, **dargs):
@@ -257,7 +259,7 @@
it.
"""
self.run("mkdir -p %s" % parent)
- template = os.path.join(parent, 'autoserv-XXXXXX')
+ template = os.path.join(parent, self.TMP_DIR_TEMPLATE)
dir_name = self.run("mktemp -d %s" % template).stdout.rstrip()
self.tmp_dirs.append(dir_name)
return dir_name
@@ -302,6 +304,31 @@
self.tmp_dirs.remove(tmpdir)
+ def delete_all_tmp_dirs(self, parent='/tmp'):
+ """
+ Delete all directories in parent that were created by get_tmp_dir
+
+ Note that this may involve deleting directories created by calls to
+ get_tmp_dir on a different RemoteHost instance than the one running this
+ method. Only perform this operation when certain that this will not
+ cause unexpected behavior.
+ """
+ # follow mktemp's behavior of only expanding 3 or more consecutive Xs
+ base_template = re.sub('XXXX*', '*', self.TMP_DIR_TEMPLATE)
+ # distinguish between non-wildcard asterisks in parent directory name
+ # and wildcards inserted from the template
+ base = '*'.join(map(lambda x: '"%s"' % utils.sh_escape(x),
+ base_template.split('*')))
+ path = '"%s' % os.path.join(utils.sh_escape(parent), base[1:])
+ self.run('rm -rf %s' % path, ignore_status=True)
+ # remove deleted directories from tmp_dirs
+ regex = os.path.join(parent, re.sub('(XXXX*)',
+ lambda match: '[a-zA-Z0-9]{%d}' % len(match.group(1)),
+ self.TMP_DIR_TEMPLATE))
+ regex += '(/|$)' # remove if matches, or is within a dir that matches
+ self.tmp_dirs = filter(lambda x: not re.match(regex, x), self.tmp_dirs)
+
+
def check_uptime(self):
"""
Check that uptime is available and monotonically increasing.
diff --git a/server/test.py b/server/test.py
index f1b1873..99d155f 100644
--- a/server/test.py
+++ b/server/test.py
@@ -84,6 +84,9 @@
class _sysinfo_logger(object):
+ AUTOTEST_PARENT_DIR = '/tmp/sysinfo'
+ OUTPUT_PARENT_DIR = '/tmp'
+
def __init__(self, job):
self.job = job
self.pickle = None
@@ -109,10 +112,14 @@
if not self.host.is_client_install_supported:
return self.host, None, None
try:
- tmp_dir = self.host.get_tmp_dir(parent="/tmp/sysinfo")
+ # Remove existing autoserv-* directories before creating more
+ self.host.delete_all_tmp_dirs(self.AUTOTEST_PARENT_DIR)
+ self.host.delete_all_tmp_dirs(self.OUTPUT_PARENT_DIR)
+
+ tmp_dir = self.host.get_tmp_dir(self.AUTOTEST_PARENT_DIR)
self.autotest = autotest.Autotest(self.host)
self.autotest.install(autodir=tmp_dir)
- self.outputdir = self.host.get_tmp_dir()
+ self.outputdir = self.host.get_tmp_dir(self.OUTPUT_PARENT_DIR)
except:
# if installation fails roll back the host
try: