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 client/common_lib/cros/control_file_getter.py."""
      8 
      9 import httplib
     10 import logging
     11 import mox
     12 import StringIO
     13 import unittest
     14 
     15 import common
     16 
     17 from autotest_lib.client.common_lib import error
     18 from autotest_lib.client.common_lib.cros import dev_server
     19 from autotest_lib.server.cros.dynamic_suite import control_file_getter
     20 
     21 
     22 class DevServerGetterTest(mox.MoxTestBase):
     23     """Unit tests for control_file_getter.DevServerGetter.
     24 
     25     @var _HOST: fake dev server host address.
     26     """
     27 
     28     _BUILD = 'fake/build'
     29     _FILES = ['a/b/control', 'b/c/control']
     30     _CONTENTS = 'Multi-line\nControl File Contents\n'
     31     _403 = dev_server.DevServerException('HTTP 403 Forbidden!')
     32 
     33     def setUp(self):
     34         super(DevServerGetterTest, self).setUp()
     35         self.dev_server = self.mox.CreateMock(dev_server.ImageServer)
     36         self.getter = control_file_getter.DevServerGetter(self._BUILD,
     37                                                           self.dev_server)
     38 
     39 
     40     def testListControlFiles(self):
     41         """Should successfully list control files from the dev server."""
     42         self.dev_server.list_control_files(
     43                 self._BUILD,
     44                 suite_name='').AndReturn(self._FILES)
     45         self.mox.ReplayAll()
     46         self.assertEquals(self.getter.get_control_file_list(), self._FILES)
     47         self.assertEquals(self.getter._files, self._FILES)
     48 
     49 
     50     def testListControlFilesFail(self):
     51         """Should fail to list control files from the dev server."""
     52         self.dev_server.list_control_files(
     53                 self._BUILD,
     54                 suite_name='').AndRaise(self._403)
     55         self.mox.ReplayAll()
     56         self.assertRaises(error.NoControlFileList,
     57                           self.getter.get_control_file_list)
     58 
     59 
     60     def testGetControlFile(self):
     61         """Should successfully get a control file from the dev server."""
     62         path = self._FILES[0]
     63         self.dev_server.get_control_file(self._BUILD,
     64                                          path).AndReturn(self._CONTENTS)
     65         self.mox.ReplayAll()
     66         self.assertEquals(self.getter.get_control_file_contents(path),
     67                           self._CONTENTS)
     68 
     69 
     70     def testGetControlFileFail(self):
     71         """Should fail to get a control file from the dev server."""
     72         path = self._FILES[0]
     73         self.dev_server.get_control_file(self._BUILD, path).AndRaise(self._403)
     74         self.mox.ReplayAll()
     75         self.assertRaises(error.ControlFileNotFound,
     76                           self.getter.get_control_file_contents,
     77                           path)
     78 
     79 
     80     def testGetControlFileByNameCached(self):
     81         """\
     82         Should successfully get a cf by name from the dev server, using a cache.
     83         """
     84         name = 'one'
     85         path = "file/%s/control" % name
     86 
     87         self.getter._files = self._FILES + [path]
     88         self.dev_server.get_control_file(self._BUILD,
     89                                          path).AndReturn(self._CONTENTS)
     90         self.mox.ReplayAll()
     91         self.assertEquals(self.getter.get_control_file_contents_by_name(name),
     92                           self._CONTENTS)
     93 
     94 
     95     def testGetControlFileByName(self):
     96         """\
     97         Should successfully get a control file from the dev server by name.
     98         """
     99         name = 'one'
    100         path = "file/%s/control" % name
    101 
    102         files = self._FILES + [path]
    103         self.dev_server.list_control_files(
    104                 self._BUILD,
    105                 suite_name='').AndReturn(files)
    106         self.dev_server.get_control_file(self._BUILD,
    107                                          path).AndReturn(self._CONTENTS)
    108         self.mox.ReplayAll()
    109         self.assertEquals(self.getter.get_control_file_contents_by_name(name),
    110                           self._CONTENTS)
    111 
    112 
    113     def testGetSuiteControlFileByName(self):
    114         """\
    115         Should successfully get a suite control file from the devserver by name.
    116         """
    117         name = 'control.bvt'
    118         path = "file/" + name
    119 
    120         files = self._FILES + [path]
    121         self.dev_server.list_control_files(
    122                 self._BUILD,
    123                 suite_name='').AndReturn(files)
    124         self.dev_server.get_control_file(self._BUILD,
    125                                          path).AndReturn(self._CONTENTS)
    126         self.mox.ReplayAll()
    127         self.assertEquals(self.getter.get_control_file_contents_by_name(name),
    128                           self._CONTENTS)
    129 
    130 
    131     def testGetControlFileByNameFail(self):
    132         """Should fail to get a control file from the dev server by name."""
    133         name = 'one'
    134 
    135         self.dev_server.list_control_files(
    136                 self._BUILD,
    137                 suite_name='').AndReturn(self._FILES)
    138         self.mox.ReplayAll()
    139         self.assertRaises(error.ControlFileNotFound,
    140                           self.getter.get_control_file_contents_by_name,
    141                           name)
    142 
    143 
    144 if __name__ == '__main__':
    145     unittest.main()
    146