1# Copyright 2017 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import os 6 7import common 8from autotest_lib.site_utils import lxc 9from autotest_lib.site_utils.lxc import constants 10from autotest_lib.site_utils.lxc import utils as lxc_utils 11 12 13class FastContainerBucket(lxc.ContainerBucket): 14 """A fast container bucket for testing. 15 16 If a base container image already exists in the default location on the 17 local machine, this container just makes a snapshot of it for testing, 18 rather than re-downloading and installing a fresh base continer. 19 """ 20 def __init__(self, lxc_path, host_path): 21 self.fast_setup = False 22 try: 23 if lxc_utils.path_exists( 24 os.path.join(constants.DEFAULT_CONTAINER_PATH, 25 constants.BASE)): 26 lxc_path = os.path.realpath(lxc_path) 27 if not lxc_utils.path_exists(lxc_path): 28 os.makedirs(lxc_path) 29 30 # Clone the base container (snapshot for speed) to make a base 31 # container for the unit test. 32 base = lxc.Container.createFromExistingDir( 33 constants.DEFAULT_CONTAINER_PATH, constants.BASE) 34 lxc.Container.clone(src=base, 35 new_name=constants.BASE, 36 new_path=lxc_path, 37 snapshot=True, 38 cleanup=False) 39 self.fast_setup = True 40 finally: 41 super(FastContainerBucket, self).__init__(lxc_path, host_path) 42 if self.base_container is not None: 43 self._setup_shared_host_path() 44 45 46 def setup_base(self, *args, **kwargs): 47 """Runs setup_base if fast setup did not work.""" 48 if not self.fast_setup: 49 super(FastContainerBucket, self).setup_base(*args, **kwargs) 50