blob: 08dc482415d0b33ed3c735c8ddc66879ac2959ee [file] [log] [blame]
mblighdcd57a82007-07-11 23:06:47 +00001#!/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
10import os
11import sys
12import unittest
13
14# Adjust the path so Python can find the autoserv modules
15src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),))
16if src not in sys.path:
jadmanski0afbb632008-06-06 21:10:57 +000017 sys.path.insert(1, src)
mblighdcd57a82007-07-11 23:06:47 +000018
19import utils
20import autotest
21import hosts
22
23
24class AutotestTestCase(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000025 def setUp(self):
26 self.autotest = autotest.Autotest()
27
28 def tearDown(self):
29 pass
mblighdcd57a82007-07-11 23:06:47 +000030
31
jadmanski0afbb632008-06-06 21:10:57 +000032 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',
48 autotest._get_autodir(host))
49 host.result = "autodir=/stuff/autotest\n"
50 self.assertEqual('/stuff/autotest',
51 autotest._get_autodir(host))
52 host.result = 'autodir="/stuff/auto test"\n'
53 self.assertEqual('/stuff/auto test',
54 autotest._get_autodir(host))
mblighdcd57a82007-07-11 23:06:47 +000055
56
jadmanski0afbb632008-06-06 21:10:57 +000057 def testInstallFromDir(self):
58 class MockInstallHost:
59 def __init__(self):
60 self.commands = []
mblighdcd57a82007-07-11 23:06:47 +000061
jadmanski0afbb632008-06-06 21:10:57 +000062 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)
mblighdcd57a82007-07-11 23:06:47 +000069
jadmanski0afbb632008-06-06 21:10:57 +000070 def send_file(self, src, dst):
71 self.commands.append("send_file: %s %s" % (src,
72 dst))
mblighdcd57a82007-07-11 23:06:47 +000073
jadmanski0afbb632008-06-06 21:10:57 +000074 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'))
mblighdcd57a82007-07-11 23:06:47 +000083
jadmanski0afbb632008-06-06 21:10:57 +000084
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"])
mblighdcd57a82007-07-11 23:06:47 +0000134
135
136def suite():
jadmanski0afbb632008-06-06 21:10:57 +0000137 return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase)
mblighdcd57a82007-07-11 23:06:47 +0000138
139if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000140 unittest.TextTestRunner(verbosity=2).run(suite())