mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
| 5 | """This module defines the unittests for the Autotest class |
| 6 | """ |
| 7 | |
| 8 | __author__ = """stutsman@google.com (Ryan Stutsman)""" |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | import unittest |
| 13 | |
| 14 | # Adjust the path so Python can find the autoserv modules |
| 15 | src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),)) |
| 16 | if src not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 17 | sys.path.insert(1, src) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 18 | |
| 19 | import utils |
| 20 | import autotest |
| 21 | import hosts |
| 22 | |
| 23 | |
| 24 | class AutotestTestCase(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 25 | def setUp(self): |
| 26 | self.autotest = autotest.Autotest() |
| 27 | |
| 28 | def tearDown(self): |
| 29 | pass |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 30 | |
| 31 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 32 | def testGetAutoDir(self): |
| 33 | class MockInstallHost: |
| 34 | def __init__(self): |
| 35 | self.commands = [] |
| 36 | self.result = "autodir='/stuff/autotest'\n" |
| 37 | |
| 38 | def run(self, command): |
| 39 | if command == "grep autodir= /etc/autotest.conf": |
| 40 | result = hosts.CmdResult() |
| 41 | result.stdout = self.result |
| 42 | return result |
| 43 | else: |
| 44 | self.commands.append(command) |
| 45 | |
| 46 | host = MockInstallHost() |
| 47 | self.assertEqual('/stuff/autotest', |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame^] | 48 | autotest.Autotest.get_installed_autodir(host)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 49 | host.result = "autodir=/stuff/autotest\n" |
| 50 | self.assertEqual('/stuff/autotest', |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame^] | 51 | autotest.Autotest.get_installed_autodir(host)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 52 | host.result = 'autodir="/stuff/auto test"\n' |
| 53 | self.assertEqual('/stuff/auto test', |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame^] | 54 | autotest.Autotest.get_installed_autodir(host)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 55 | |
| 56 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 57 | def testInstallFromDir(self): |
| 58 | class MockInstallHost: |
| 59 | def __init__(self): |
| 60 | self.commands = [] |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 61 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 62 | def run(self, command): |
| 63 | if command == "grep autodir= /etc/autotest.conf": |
| 64 | result= hosts.CmdResult() |
| 65 | result.stdout = "autodir=/usr/local/autotest\n" |
| 66 | return result |
| 67 | else: |
| 68 | self.commands.append(command) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 69 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 70 | def send_file(self, src, dst): |
| 71 | self.commands.append("send_file: %s %s" % (src, |
| 72 | dst)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 73 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 74 | host = MockInstallHost() |
| 75 | tmpdir = utils.get_tmp_dir() |
| 76 | self.autotest.get(tmpdir) |
| 77 | self.autotest.install(host) |
| 78 | self.assertEqual(host.commands[0], |
| 79 | 'mkdir -p /usr/local/autotest') |
| 80 | self.assertTrue(host.commands[1].startswith('send_file: /tmp/')) |
| 81 | self.assertTrue(host.commands[1].endswith( |
| 82 | '/ /usr/local/autotest')) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 83 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | |
| 87 | def testInstallFromSVN(self): |
| 88 | class MockInstallHost: |
| 89 | def __init__(self): |
| 90 | self.commands = [] |
| 91 | |
| 92 | def run(self, command): |
| 93 | if command == "grep autodir= /etc/autotest.conf": |
| 94 | result= hosts.CmdResult() |
| 95 | result.stdout = "autodir=/usr/local/autotest\n" |
| 96 | return result |
| 97 | else: |
| 98 | self.commands.append(command) |
| 99 | |
| 100 | host = MockInstallHost() |
| 101 | self.autotest.install(host) |
| 102 | self.assertEqual(host.commands, |
| 103 | ['svn checkout ' |
| 104 | + autotest.AUTOTEST_SVN + ' ' |
| 105 | + "/usr/local/autotest"]) |
| 106 | |
| 107 | |
| 108 | def testFirstInstallFromSVNFails(self): |
| 109 | class MockFirstInstallFailsHost: |
| 110 | def __init__(self): |
| 111 | self.commands = [] |
| 112 | |
| 113 | def run(self, command): |
| 114 | if command == "grep autodir= /etc/autotest.conf": |
| 115 | result= hosts.CmdResult() |
| 116 | result.stdout = "autodir=/usr/local/autotest\n" |
| 117 | return result |
| 118 | else: |
| 119 | self.commands.append(command) |
| 120 | first = ('svn checkout ' + |
| 121 | autotest.AUTOTEST_SVN + ' ' + |
| 122 | "/usr/local/autotest") |
| 123 | if (command == first): |
| 124 | raise autotest.AutoservRunError( |
| 125 | "svn not found") |
| 126 | |
| 127 | host = MockFirstInstallFailsHost() |
| 128 | self.autotest.install(host) |
| 129 | self.assertEqual(host.commands, |
| 130 | ['svn checkout ' + autotest.AUTOTEST_SVN + |
| 131 | ' ' + "/usr/local/autotest", |
| 132 | 'svn checkout ' + autotest.AUTOTEST_HTTP + |
| 133 | ' ' + "/usr/local/autotest"]) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def suite(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 137 | return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 138 | |
| 139 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 140 | unittest.TextTestRunner(verbosity=2).run(suite()) |