Home | History | Annotate | Download | only in dynamic_suite
      1 #!/usr/bin/python
      2 #
      3 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 """Unit tests for server/cros/dynamic_suite/tools.py."""
      8 
      9 import mox
     10 import unittest
     11 
     12 import common
     13 from autotest_lib.server.cros.dynamic_suite.fakes import FakeHost
     14 from autotest_lib.server.cros.dynamic_suite.host_spec import HostSpec
     15 from autotest_lib.server.cros.dynamic_suite import host_spec
     16 from autotest_lib.server.cros.dynamic_suite import tools
     17 from autotest_lib.server import frontend
     18 
     19 
     20 class DynamicSuiteToolsTest(mox.MoxTestBase):
     21     """Unit tests for dynamic_suite tools module methods.
     22 
     23     @var _BOARD: fake board to reimage
     24     """
     25 
     26     _BOARD = 'board'
     27     _DEPENDENCIES = {'test1': ['label1'], 'test2': ['label2']}
     28     _POOL = 'bvt'
     29 
     30     def setUp(self):
     31         super(DynamicSuiteToolsTest, self).setUp()
     32         self.afe = self.mox.CreateMock(frontend.AFE)
     33         self.tko = self.mox.CreateMock(frontend.TKO)
     34         # Having these ordered by complexity is important!
     35         host_spec_list = [HostSpec([self._BOARD, self._POOL])]
     36         for dep_list in self._DEPENDENCIES.itervalues():
     37             host_spec_list.append(
     38                 HostSpec([self._BOARD, self._POOL], dep_list))
     39         self.specs = host_spec.order_by_complexity(host_spec_list)
     40 
     41     def testInjectVars(self):
     42         """Should inject dict of varibles into provided strings."""
     43         def find_all_in(d, s):
     44             """Returns true if all key-value pairs in |d| are printed in |s|
     45             and the dictionary representation is also in |s|."""
     46             for k, v in d.iteritems():
     47                 if isinstance(v, str):
     48                     if "%s='%s'\n" % (k, v) not in s:
     49                         return False
     50                 else:
     51                     if "%s=%r\n" % (k, v) not in s:
     52                         return False
     53             args_dict_str = "%s=%s\n" % ('args_dict', repr(d))
     54             if args_dict_str not in s:
     55                 return False
     56             return True
     57 
     58         v = {'v1': 'one', 'v2': 'two', 'v3': None, 'v4': False, 'v5': 5}
     59         self.assertTrue(find_all_in(v, tools.inject_vars(v, '')))
     60         self.assertTrue(find_all_in(v, tools.inject_vars(v, 'ctrl')))
     61 
     62 
     63     def testIncorrectlyLocked(self):
     64         """Should detect hosts locked by random users."""
     65         host = FakeHost(locked=True, locked_by='some guy')
     66         self.assertTrue(tools.incorrectly_locked(host))
     67 
     68 
     69     def testNotIncorrectlyLocked(self):
     70         """Should accept hosts locked by the infrastructure."""
     71         infra_user = 'an infra user'
     72         self.mox.StubOutWithMock(tools, 'infrastructure_user')
     73         tools.infrastructure_user().AndReturn(infra_user)
     74         self.mox.ReplayAll()
     75         host = FakeHost(locked=True, locked_by=infra_user)
     76         self.assertFalse(tools.incorrectly_locked(host))
     77 
     78 
     79 if __name__ == "__main__":
     80     unittest.main()
     81