blob: 6e9df7be2efc281b7fccdd42d60f6e0120253259 [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
Dale Curtis8adf7892011-09-08 16:13:36 -070014import common
mblighdcd57a82007-07-11 23:06:47 +000015
Dale Curtis8adf7892011-09-08 16:13:36 -070016from autotest_lib.server import utils
17from autotest_lib.server import autotest
18from autotest_lib.server import hosts
mblighdcd57a82007-07-11 23:06:47 +000019
20
21class AutotestTestCase(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000022 def setUp(self):
23 self.autotest = autotest.Autotest()
24
25 def tearDown(self):
26 pass
mblighdcd57a82007-07-11 23:06:47 +000027
28
jadmanski0afbb632008-06-06 21:10:57 +000029 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',
showardad812bf2009-10-20 23:49:56 +000045 autotest.Autotest.get_installed_autodir(host))
jadmanski0afbb632008-06-06 21:10:57 +000046 host.result = "autodir=/stuff/autotest\n"
47 self.assertEqual('/stuff/autotest',
showardad812bf2009-10-20 23:49:56 +000048 autotest.Autotest.get_installed_autodir(host))
jadmanski0afbb632008-06-06 21:10:57 +000049 host.result = 'autodir="/stuff/auto test"\n'
50 self.assertEqual('/stuff/auto test',
showardad812bf2009-10-20 23:49:56 +000051 autotest.Autotest.get_installed_autodir(host))
mblighdcd57a82007-07-11 23:06:47 +000052
53
jadmanski0afbb632008-06-06 21:10:57 +000054 def testInstallFromDir(self):
55 class MockInstallHost:
56 def __init__(self):
57 self.commands = []
mblighdcd57a82007-07-11 23:06:47 +000058
jadmanski0afbb632008-06-06 21:10:57 +000059 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)
mblighdcd57a82007-07-11 23:06:47 +000066
jadmanski0afbb632008-06-06 21:10:57 +000067 def send_file(self, src, dst):
68 self.commands.append("send_file: %s %s" % (src,
69 dst))
mblighdcd57a82007-07-11 23:06:47 +000070
jadmanski0afbb632008-06-06 21:10:57 +000071 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'))
mblighdcd57a82007-07-11 23:06:47 +000080
jadmanski0afbb632008-06-06 21:10:57 +000081
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"])
mblighdcd57a82007-07-11 23:06:47 +0000131
132
133def suite():
jadmanski0afbb632008-06-06 21:10:57 +0000134 return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase)
mblighdcd57a82007-07-11 23:06:47 +0000135
136if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000137 unittest.TextTestRunner(verbosity=2).run(suite())