1#!/usr/bin/python2 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.getter = control_file_getter.DevServerGetter(self._BUILD, 34 self.dev_server) 35 36 37 def testListControlFiles(self): 38 """Should successfully list control files from the dev server.""" 39 self.dev_server.list_control_files( 40 self._BUILD, 41 suite_name='').AndReturn(self._FILES) 42 self.mox.ReplayAll() 43 self.assertEquals(self.getter.get_control_file_list(), self._FILES) 44 self.assertEquals(self.getter._files, self._FILES) 45 46 47 def testListControlFilesFail(self): 48 """Should fail to list control files from the dev server.""" 49 self.dev_server.list_control_files( 50 self._BUILD, 51 suite_name='').AndRaise(self._403) 52 self.mox.ReplayAll() 53 self.assertRaises(error.NoControlFileList, 54 self.getter.get_control_file_list) 55 56 57 def testGetControlFile(self): 58 """Should successfully get a control file from the dev server.""" 59 path = self._FILES[0] 60 self.dev_server.get_control_file(self._BUILD, 61 path).AndReturn(self._CONTENTS) 62 self.mox.ReplayAll() 63 self.assertEquals(self.getter.get_control_file_contents(path), 64 self._CONTENTS) 65 66 67 def testGetSuiteInfo(self): 68 """ 69 Should successfully list control files' path and contents from the 70 dev server. 71 """ 72 file_contents = {f:self._CONTENTS for f in self._FILES} 73 self.dev_server.list_suite_controls( 74 self._BUILD, 75 suite_name='').AndReturn(file_contents) 76 self.mox.ReplayAll() 77 suite_info = self.getter.get_suite_info() 78 for k in suite_info.keys(): 79 self.assertEquals(suite_info[k], file_contents[k]) 80 self.assertEquals(sorted(self.getter._files), sorted(self._FILES)) 81 82 83 def testListSuiteControlisFail(self): 84 """ 85 Should fail to list all control file's contents from the dev server. 86 """ 87 self.dev_server.list_suite_controls( 88 self._BUILD, 89 suite_name='').AndRaise(self._403) 90 self.mox.ReplayAll() 91 self.assertRaises(error.SuiteControlFileException, 92 self.getter.get_suite_info, 93 '') 94 95 96 def testGetControlFileFail(self): 97 """Should fail to get a control file from the dev server.""" 98 path = self._FILES[0] 99 self.dev_server.get_control_file(self._BUILD, path).AndRaise(self._403) 100 self.mox.ReplayAll() 101 self.assertRaises(error.ControlFileNotFound, 102 self.getter.get_control_file_contents, 103 path) 104 105 106 def testGetControlFileByNameCached(self): 107 """\ 108 Should successfully get a cf by name from the dev server, using a cache. 109 """ 110 name = 'one' 111 path = "file/%s/control" % name 112 113 self.getter._files = self._FILES + [path] 114 self.dev_server.get_control_file(self._BUILD, 115 path).AndReturn(self._CONTENTS) 116 self.mox.ReplayAll() 117 self.assertEquals(self.getter.get_control_file_contents_by_name(name), 118 self._CONTENTS) 119 120 121 def testGetControlFileByName(self): 122 """\ 123 Should successfully get a control file from the dev server by name. 124 """ 125 name = 'one' 126 path = "file/%s/control" % name 127 128 files = self._FILES + [path] 129 self.dev_server.list_control_files( 130 self._BUILD, 131 suite_name='').AndReturn(files) 132 self.dev_server.get_control_file(self._BUILD, 133 path).AndReturn(self._CONTENTS) 134 self.mox.ReplayAll() 135 self.assertEquals(self.getter.get_control_file_contents_by_name(name), 136 self._CONTENTS) 137 138 139 def testGetSuiteControlFileByName(self): 140 """\ 141 Should successfully get a suite control file from the devserver by name. 142 """ 143 name = 'control.bvt' 144 path = "file/" + name 145 146 files = self._FILES + [path] 147 self.dev_server.list_control_files( 148 self._BUILD, 149 suite_name='').AndReturn(files) 150 self.dev_server.get_control_file(self._BUILD, 151 path).AndReturn(self._CONTENTS) 152 self.mox.ReplayAll() 153 self.assertEquals(self.getter.get_control_file_contents_by_name(name), 154 self._CONTENTS) 155 156 157 def testGetControlFileByNameFail(self): 158 """Should fail to get a control file from the dev server by name.""" 159 name = 'one' 160 161 self.dev_server.list_control_files( 162 self._BUILD, 163 suite_name='').AndReturn(self._FILES) 164 self.mox.ReplayAll() 165 self.assertRaises(error.ControlFileNotFound, 166 self.getter.get_control_file_contents_by_name, 167 name) 168 169 170if __name__ == '__main__': 171 unittest.main() 172