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