blob: 033b09128243bd66aea9e4528cb43287c6955816 [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()
mblighc8949b82007-07-23 16:33:58 +000076 self.autotest.get(tmpdir)
mblighdcd57a82007-07-11 23:06:47 +000077 self.autotest.install(host)
mblighc8949b82007-07-23 16:33:58 +000078 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
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):
mbligh03f4fc72007-11-29 20:56:14 +0000124 raise autotest.AutoservRunError(
mblighdcd57a82007-07-11 23:06:47 +0000125 "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"])
134
135
136def suite():
137 return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase)
138
139if __name__ == '__main__':
140 unittest.TextTestRunner(verbosity=2).run(suite())