blob: 46507ddf90ea9c86b2c15b7ce3746c13cd1e2668 [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:
17 sys.path.insert(1, src)
18
19import utils
20import autotest
21import hosts
22
23
24class AutotestTestCase(unittest.TestCase):
25 def setUp(self):
26 self.autotest = autotest.Autotest()
27
28 def tearDown(self):
29 pass
30
31
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',
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))
55
56
57 def testInstallFromDir(self):
58 class MockInstallHost:
59 def __init__(self):
60 self.commands = []
61
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)
69
70 def send_file(self, src, dst):
71 self.commands.append("send_file: %s %s" % (src,
72 dst))
73
74 host = MockInstallHost()
75 tmpdir = utils.get_tmp_dir()
76 self.autotest.get_from_file(tmpdir)
77 self.autotest.install(host)
78 self.assertEqual(host.commands,
79 ['mkdir -p /usr/local/autotest',
80 'send_file: %s/ %s' % (tmpdir,
81 '/usr/local/autotest')])
82
83
84
85
86 def testInstallFromSVN(self):
87 class MockInstallHost:
88 def __init__(self):
89 self.commands = []
90
91 def run(self, command):
92 if command == "grep autodir= /etc/autotest.conf":
93 result= hosts.CmdResult()
94 result.stdout = "autodir=/usr/local/autotest\n"
95 return result
96 else:
97 self.commands.append(command)
98
99 host = MockInstallHost()
100 self.autotest.install(host)
101 self.assertEqual(host.commands,
102 ['svn checkout '
103 + autotest.AUTOTEST_SVN + ' '
104 + "/usr/local/autotest"])
105
106
107 def testFirstInstallFromSVNFails(self):
108 class MockFirstInstallFailsHost:
109 def __init__(self):
110 self.commands = []
111
112 def run(self, command):
113 if command == "grep autodir= /etc/autotest.conf":
114 result= hosts.CmdResult()
115 result.stdout = "autodir=/usr/local/autotest\n"
116 return result
117 else:
118 self.commands.append(command)
119 first = ('svn checkout ' +
120 autotest.AUTOTEST_SVN + ' ' +
121 "/usr/local/autotest")
122 if (command == first):
123 raise autotest.errors.AutoservRunError(
124 "svn not found")
125
126 host = MockFirstInstallFailsHost()
127 self.autotest.install(host)
128 self.assertEqual(host.commands,
129 ['svn checkout ' + autotest.AUTOTEST_SVN +
130 ' ' + "/usr/local/autotest",
131 'svn checkout ' + autotest.AUTOTEST_HTTP +
132 ' ' + "/usr/local/autotest"])
133
134
135def suite():
136 return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase)
137
138if __name__ == '__main__':
139 unittest.TextTestRunner(verbosity=2).run(suite())