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 | |
Dale Curtis | 8adf789 | 2011-09-08 16:13:36 -0700 | [diff] [blame] | 14 | import common |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 15 | |
Dale Curtis | 8adf789 | 2011-09-08 16:13:36 -0700 | [diff] [blame] | 16 | from autotest_lib.server import utils |
| 17 | from autotest_lib.server import autotest |
| 18 | from autotest_lib.server import hosts |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | class AutotestTestCase(unittest.TestCase): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 22 | def setUp(self): |
| 23 | self.autotest = autotest.Autotest() |
| 24 | |
| 25 | def tearDown(self): |
| 26 | pass |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 27 | |
| 28 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 29 | def testGetAutoDir(self): |
| 30 | class MockInstallHost: |
| 31 | def __init__(self): |
| 32 | self.commands = [] |
| 33 | self.result = "autodir='/stuff/autotest'\n" |
| 34 | |
| 35 | def run(self, command): |
| 36 | if command == "grep autodir= /etc/autotest.conf": |
| 37 | result = hosts.CmdResult() |
| 38 | result.stdout = self.result |
| 39 | return result |
| 40 | else: |
| 41 | self.commands.append(command) |
| 42 | |
| 43 | host = MockInstallHost() |
| 44 | self.assertEqual('/stuff/autotest', |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 45 | autotest.Autotest.get_installed_autodir(host)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 46 | host.result = "autodir=/stuff/autotest\n" |
| 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/auto test"\n' |
| 50 | self.assertEqual('/stuff/auto test', |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 51 | autotest.Autotest.get_installed_autodir(host)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 52 | |
| 53 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 54 | def testInstallFromDir(self): |
| 55 | class MockInstallHost: |
| 56 | def __init__(self): |
| 57 | self.commands = [] |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 58 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 59 | def run(self, command): |
| 60 | if command == "grep autodir= /etc/autotest.conf": |
| 61 | result= hosts.CmdResult() |
| 62 | result.stdout = "autodir=/usr/local/autotest\n" |
| 63 | return result |
| 64 | else: |
| 65 | self.commands.append(command) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 66 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 67 | def send_file(self, src, dst): |
| 68 | self.commands.append("send_file: %s %s" % (src, |
| 69 | dst)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 70 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 71 | host = MockInstallHost() |
| 72 | tmpdir = utils.get_tmp_dir() |
| 73 | self.autotest.get(tmpdir) |
| 74 | self.autotest.install(host) |
| 75 | self.assertEqual(host.commands[0], |
| 76 | 'mkdir -p /usr/local/autotest') |
| 77 | self.assertTrue(host.commands[1].startswith('send_file: /tmp/')) |
| 78 | self.assertTrue(host.commands[1].endswith( |
| 79 | '/ /usr/local/autotest')) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 80 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | |
| 84 | def testInstallFromSVN(self): |
| 85 | class MockInstallHost: |
| 86 | def __init__(self): |
| 87 | self.commands = [] |
| 88 | |
| 89 | def run(self, command): |
| 90 | if command == "grep autodir= /etc/autotest.conf": |
| 91 | result= hosts.CmdResult() |
| 92 | result.stdout = "autodir=/usr/local/autotest\n" |
| 93 | return result |
| 94 | else: |
| 95 | self.commands.append(command) |
| 96 | |
| 97 | host = MockInstallHost() |
| 98 | self.autotest.install(host) |
| 99 | self.assertEqual(host.commands, |
| 100 | ['svn checkout ' |
| 101 | + autotest.AUTOTEST_SVN + ' ' |
| 102 | + "/usr/local/autotest"]) |
| 103 | |
| 104 | |
| 105 | def testFirstInstallFromSVNFails(self): |
| 106 | class MockFirstInstallFailsHost: |
| 107 | def __init__(self): |
| 108 | self.commands = [] |
| 109 | |
| 110 | def run(self, command): |
| 111 | if command == "grep autodir= /etc/autotest.conf": |
| 112 | result= hosts.CmdResult() |
| 113 | result.stdout = "autodir=/usr/local/autotest\n" |
| 114 | return result |
| 115 | else: |
| 116 | self.commands.append(command) |
| 117 | first = ('svn checkout ' + |
| 118 | autotest.AUTOTEST_SVN + ' ' + |
| 119 | "/usr/local/autotest") |
| 120 | if (command == first): |
| 121 | raise autotest.AutoservRunError( |
| 122 | "svn not found") |
| 123 | |
| 124 | host = MockFirstInstallFailsHost() |
| 125 | self.autotest.install(host) |
| 126 | self.assertEqual(host.commands, |
| 127 | ['svn checkout ' + autotest.AUTOTEST_SVN + |
| 128 | ' ' + "/usr/local/autotest", |
| 129 | 'svn checkout ' + autotest.AUTOTEST_HTTP + |
| 130 | ' ' + "/usr/local/autotest"]) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def suite(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 134 | return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 135 | |
| 136 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 137 | unittest.TextTestRunner(verbosity=2).run(suite()) |