Home | History | Annotate | Download | only in lxc
      1 #!/usr/bin/python
      2 # Copyright 2017 The Chromium OS Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import os
      7 import shutil
      8 import tempfile
      9 import unittest
     10 
     11 import common
     12 from autotest_lib.site_utils import lxc
     13 from autotest_lib.site_utils.lxc import container_bucket
     14 from autotest_lib.site_utils.lxc import utils as lxc_utils
     15 
     16 
     17 container_path = None
     18 
     19 def setUpModule():
     20     """Creates a directory for running the unit tests. """
     21     global container_path
     22     container_path = tempfile.mkdtemp(
     23             dir=lxc.DEFAULT_CONTAINER_PATH,
     24             prefix='container_bucket_unittest_')
     25 
     26 
     27 def tearDownModule():
     28     """Deletes the test directory. """
     29     shutil.rmtree(container_path)
     30 
     31 
     32 class DummyClient(object):
     33     """Mock client for bucket test"""
     34     def get_container(*args, **xargs):
     35         return None
     36 
     37 
     38 class ContainerBucketTests(lxc_utils.LXCTests):
     39     """Unit tests for the ContainerBucket class."""
     40 
     41     def setUp(self):
     42         self.tmpdir = tempfile.mkdtemp()
     43         self.shared_host_path = os.path.realpath(os.path.join(self.tmpdir,
     44                                                               'host'))
     45 
     46     def tearDown(self):
     47         shutil.rmtree(self.tmpdir)
     48 
     49 
     50     def testEmptyPool(self):
     51         """Verifies that the bucket falls back to creating a new container if it
     52         can't get one from the pool."""
     53         id = lxc.ContainerId.create(3)
     54         factory = container_bucket.ContainerBucket()._factory
     55         factory._client = DummyClient()
     56         container = factory.create_container(id)
     57         self.assertIsNotNone(container)
     58 
     59 
     60 if __name__ == '__main__':
     61     unittest.main()
     62