Add initial version of autoserv



git-svn-id: http://test.kernel.org/svn/autotest/trunk@557 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/server/tests/alltests_suite.py b/server/tests/alltests_suite.py
new file mode 100644
index 0000000..14ffc1d
--- /dev/null
+++ b/server/tests/alltests_suite.py
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+#
+# Copyright 2007 Google Inc. Released under the GPL v2
+
+"""This module provides a means to run all the unittests for autoserv
+"""
+
+__author__ = """stutsman@google.com (Ryan Stutsman)"""
+
+import os, sys
+
+# Adjust the path so Python can find the autoserv modules
+src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),))
+if src not in sys.path:
+	sys.path.insert(1, src)
+
+import unittest
+
+
+import autotest_test
+import utils_test
+
+
+def suite():
+	return unittest.TestSuite([autotest_test.suite(),
+                                   utils_test.suite()])
+
+
+if __name__ == '__main__':
+	unittest.TextTestRunner(verbosity=2).run(suite())
diff --git a/server/tests/autotest_test.py b/server/tests/autotest_test.py
new file mode 100644
index 0000000..46507dd
--- /dev/null
+++ b/server/tests/autotest_test.py
@@ -0,0 +1,139 @@
+#!/usr/bin/python
+#
+# Copyright 2007 Google Inc. Released under the GPL v2
+
+"""This module defines the unittests for the Autotest class
+"""
+
+__author__ = """stutsman@google.com (Ryan Stutsman)"""
+
+import os
+import sys
+import unittest
+
+# Adjust the path so Python can find the autoserv modules
+src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),))
+if src not in sys.path:
+	sys.path.insert(1, src)
+
+import utils
+import autotest
+import hosts
+
+
+class AutotestTestCase(unittest.TestCase):
+	def setUp(self):
+		self.autotest = autotest.Autotest()
+	
+	def tearDown(self):
+		pass
+
+
+	def testGetAutoDir(self):
+		class MockInstallHost:
+			def __init__(self):
+				self.commands = []
+				self.result = "autodir='/stuff/autotest'\n"
+			
+			def run(self, command):
+				if command == "grep autodir= /etc/autotest.conf":
+					result = hosts.CmdResult()
+					result.stdout = self.result
+					return result
+				else:
+					self.commands.append(command)
+		
+		host = MockInstallHost()
+		self.assertEqual('/stuff/autotest',
+				 autotest._get_autodir(host))
+		host.result = "autodir=/stuff/autotest\n"
+		self.assertEqual('/stuff/autotest',
+				 autotest._get_autodir(host))
+		host.result = 'autodir="/stuff/auto test"\n'
+		self.assertEqual('/stuff/auto test',
+				 autotest._get_autodir(host))
+
+
+	def testInstallFromDir(self):
+		class MockInstallHost:
+			def __init__(self):
+				self.commands = []
+			
+			def run(self, command):
+				if command == "grep autodir= /etc/autotest.conf":
+					result= hosts.CmdResult()
+					result.stdout = "autodir=/usr/local/autotest\n"
+					return result
+				else:
+					self.commands.append(command)
+
+			def send_file(self, src, dst):
+				self.commands.append("send_file: %s %s" % (src,
+									   dst))
+				
+		host = MockInstallHost()
+		tmpdir = utils.get_tmp_dir()
+		self.autotest.get_from_file(tmpdir)
+		self.autotest.install(host)
+		self.assertEqual(host.commands,
+				 ['mkdir -p /usr/local/autotest',
+				  'send_file: %s/ %s' % (tmpdir,
+							'/usr/local/autotest')])
+
+		
+
+	
+	def testInstallFromSVN(self):
+		class MockInstallHost:
+			def __init__(self):
+				self.commands = []
+			
+			def run(self, command):
+				if command == "grep autodir= /etc/autotest.conf":
+					result= hosts.CmdResult()
+					result.stdout = "autodir=/usr/local/autotest\n"
+					return result
+				else:
+					self.commands.append(command)
+		
+		host = MockInstallHost()
+		self.autotest.install(host)
+		self.assertEqual(host.commands,
+				 ['svn checkout '
+				  + autotest.AUTOTEST_SVN + ' '
+				  + "/usr/local/autotest"])
+
+	
+	def testFirstInstallFromSVNFails(self):
+		class MockFirstInstallFailsHost:
+			def __init__(self):
+				self.commands = []
+			
+			def run(self, command):
+				if command == "grep autodir= /etc/autotest.conf":
+					result= hosts.CmdResult()
+					result.stdout = "autodir=/usr/local/autotest\n"
+					return result
+				else:
+					self.commands.append(command)
+					first = ('svn checkout ' +
+					    autotest.AUTOTEST_SVN + ' ' +
+					    "/usr/local/autotest")
+					if (command == first):
+						raise autotest.errors.AutoservRunError(
+							"svn not found")
+		
+		host = MockFirstInstallFailsHost()
+		self.autotest.install(host)
+		self.assertEqual(host.commands,
+				 ['svn checkout ' + autotest.AUTOTEST_SVN +
+				  ' ' + "/usr/local/autotest",
+				  'svn checkout ' + autotest.AUTOTEST_HTTP +
+				  ' ' + "/usr/local/autotest"])
+
+
+def suite():
+	return unittest.TestLoader().loadTestsFromTestCase(AutotestTestCase)
+
+if __name__ == '__main__':
+	unittest.TextTestRunner(verbosity=2).run(suite())
diff --git a/server/tests/utils_test.py b/server/tests/utils_test.py
new file mode 100644
index 0000000..0978752
--- /dev/null
+++ b/server/tests/utils_test.py
@@ -0,0 +1,76 @@
+#!/usr/bin/python
+#
+# Copyright 2007 Google Inc. Released under the GPL v2
+
+"""This module defines the unittests for the utils
+"""
+
+__author__ = """stutsman@google.com (Ryan Stutsman)"""
+
+import os
+import sys
+import os.path
+import unittest
+
+# Adjust the path so Python can find the autoserv modules
+src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),))
+if src not in sys.path:
+	sys.path.insert(1, src)
+
+import utils
+
+
+
+class UtilsTestCase(unittest.TestCase):
+	def setUp(self):
+		pass
+
+
+	def tearDown(self):
+		pass
+
+
+	def testGetWithOpenFile(self):
+		tmpdir = utils.get_tmp_dir()
+		tmppath = os.path.join(tmpdir, 'testfile')
+		tmpfile = file(tmppath, 'w')
+		print >> tmpfile, 'Test string'
+		tmpfile.close()
+		tmpfile = file(tmppath)
+		newtmppath = utils.get(tmpfile)
+		self.assertEqual(file(newtmppath).read(), 'Test string\n')
+
+
+	def testGetWithHTTP(self):
+		# Yeah, this test is a bad idea, oh well
+		url = 'http://www.kernel.org/pub/linux/kernel/README'
+		tmppath = utils.get(url)
+		f = file(tmppath)
+		f.readline()
+		self.assertTrue('Linux' in f.readline().split())
+
+
+	def testGetWithPath(self):
+		path = utils.get('/proc/cpuinfo')
+		self.assertTrue(file(path).readline().startswith('processor'))
+
+
+	def testGetWithString(self):
+		path = utils.get('/tmp loves rabbits!')
+		self.assertTrue(file(path).readline().startswith('/tmp loves'))
+
+
+	def testGetWithDir(self):
+		tmpdir = utils.get_tmp_dir()
+		origpath = os.path.join(tmpdir, 'testGetWithDir')
+		os.mkdir(origpath)
+		dstpath = utils.get(origpath)
+		self.assertTrue(dstpath.endswith('/'))
+		self.assertTrue(os.path.isdir(dstpath))
+
+
+def suite():
+	return unittest.TestLoader().loadTestsFromTestCase(UtilsTestCase)
+
+if __name__ == '__main__':
+	unittest.TextTestRunner(verbosity=2).run(suite())