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 9import httplib 10import logging 11import mox 12import StringIO 13import unittest 14 15import common 16 17from autotest_lib.client.common_lib import error 18from autotest_lib.client.common_lib.cros import dev_server 19from autotest_lib.server.cros.dynamic_suite import control_file_getter 20 21 22class 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 testGetSuiteInfo(self): 71 """ 72 Should successfully list control files' path and contents from the 73 dev server. 74 """ 75 file_contents = {f:self._CONTENTS for f in self._FILES} 76 self.dev_server.list_suite_controls( 77 self._BUILD, 78 suite_name='').AndReturn(file_contents) 79 self.mox.ReplayAll() 80 suite_info = self.getter.get_suite_info() 81 for k in suite_info.keys(): 82 self.assertEquals(suite_info[k], file_contents[k]) 83 self.assertEquals(sorted(self.getter._files), sorted(self._FILES)) 84 85 86 def testListSuiteControlisFail(self): 87 """ 88 Should fail to list all control file's contents from the dev server. 89 """ 90 self.dev_server.list_suite_controls( 91 self._BUILD, 92 suite_name='').AndRaise(self._403) 93 self.mox.ReplayAll() 94 self.assertRaises(error.SuiteControlFileException, 95 self.getter.get_suite_info, 96 '') 97 98 99 def testGetControlFileFail(self): 100 """Should fail to get a control file from the dev server.""" 101 path = self._FILES[0] 102 self.dev_server.get_control_file(self._BUILD, path).AndRaise(self._403) 103 self.mox.ReplayAll() 104 self.assertRaises(error.ControlFileNotFound, 105 self.getter.get_control_file_contents, 106 path) 107 108 109 def testGetControlFileByNameCached(self): 110 """\ 111 Should successfully get a cf by name from the dev server, using a cache. 112 """ 113 name = 'one' 114 path = "file/%s/control" % name 115 116 self.getter._files = self._FILES + [path] 117 self.dev_server.get_control_file(self._BUILD, 118 path).AndReturn(self._CONTENTS) 119 self.mox.ReplayAll() 120 self.assertEquals(self.getter.get_control_file_contents_by_name(name), 121 self._CONTENTS) 122 123 124 def testGetControlFileByName(self): 125 """\ 126 Should successfully get a control file from the dev server by name. 127 """ 128 name = 'one' 129 path = "file/%s/control" % name 130 131 files = self._FILES + [path] 132 self.dev_server.list_control_files( 133 self._BUILD, 134 suite_name='').AndReturn(files) 135 self.dev_server.get_control_file(self._BUILD, 136 path).AndReturn(self._CONTENTS) 137 self.mox.ReplayAll() 138 self.assertEquals(self.getter.get_control_file_contents_by_name(name), 139 self._CONTENTS) 140 141 142 def testGetSuiteControlFileByName(self): 143 """\ 144 Should successfully get a suite control file from the devserver by name. 145 """ 146 name = 'control.bvt' 147 path = "file/" + name 148 149 files = self._FILES + [path] 150 self.dev_server.list_control_files( 151 self._BUILD, 152 suite_name='').AndReturn(files) 153 self.dev_server.get_control_file(self._BUILD, 154 path).AndReturn(self._CONTENTS) 155 self.mox.ReplayAll() 156 self.assertEquals(self.getter.get_control_file_contents_by_name(name), 157 self._CONTENTS) 158 159 160 def testGetControlFileByNameFail(self): 161 """Should fail to get a control file from the dev server by name.""" 162 name = 'one' 163 164 self.dev_server.list_control_files( 165 self._BUILD, 166 suite_name='').AndReturn(self._FILES) 167 self.mox.ReplayAll() 168 self.assertRaises(error.ControlFileNotFound, 169 self.getter.get_control_file_contents_by_name, 170 name) 171 172 173if __name__ == '__main__': 174 unittest.main() 175