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
10import os
11import sys
12import unittest
13
14import common
15
16from autotest_lib.server import utils
17from autotest_lib.server import autotest
18from autotest_lib.server import hosts
19
20
21class AutotestTestCase(unittest.TestCase):
22    def setUp(self):
23        self.autotest = autotest.Autotest()
24
25    def tearDown(self):
26        pass
27
28
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',
45                         autotest.Autotest.get_installed_autodir(host))
46        host.result = "autodir=/stuff/autotest\n"
47        self.assertEqual('/stuff/autotest',
48                         autotest.Autotest.get_installed_autodir(host))
49        host.result = 'autodir="/stuff/auto test"\n'
50        self.assertEqual('/stuff/auto test',
51                         autotest.Autotest.get_installed_autodir(host))
52
53
54    def testInstallFromDir(self):
55        class MockInstallHost:
56            def __init__(self):
57                self.commands = []
58
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)
66
67            def send_file(self, src, dst):
68                self.commands.append("send_file: %s %s" % (src,
69                                                           dst))
70
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'))
80
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"])
131
132
133def suite():
134    return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase)
135
136if __name__ == '__main__':
137    unittest.TextTestRunner(verbosity=2).run(suite())
138