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
6import os
7import shutil
8import tempfile
9import unittest
10
11import common
12from autotest_lib.site_utils import lxc
13from autotest_lib.site_utils.lxc import container_bucket
14from autotest_lib.site_utils.lxc import utils as lxc_utils
15
16
17container_path = None
18
19def 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
27def tearDownModule():
28    """Deletes the test directory. """
29    shutil.rmtree(container_path)
30
31
32class DummyClient(object):
33    """Mock client for bucket test"""
34    def get_container(*args, **xargs):
35        return None
36
37
38class 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
60if __name__ == '__main__':
61    unittest.main()
62