blob: 0978752bb88614045ac903905c3cfbdbbb852361 [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 utils
6"""
7
8__author__ = """stutsman@google.com (Ryan Stutsman)"""
9
10import os
11import sys
12import os.path
13import unittest
14
15# Adjust the path so Python can find the autoserv modules
16src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),))
17if src not in sys.path:
18 sys.path.insert(1, src)
19
20import utils
21
22
23
24class UtilsTestCase(unittest.TestCase):
25 def setUp(self):
26 pass
27
28
29 def tearDown(self):
30 pass
31
32
33 def testGetWithOpenFile(self):
34 tmpdir = utils.get_tmp_dir()
35 tmppath = os.path.join(tmpdir, 'testfile')
36 tmpfile = file(tmppath, 'w')
37 print >> tmpfile, 'Test string'
38 tmpfile.close()
39 tmpfile = file(tmppath)
40 newtmppath = utils.get(tmpfile)
41 self.assertEqual(file(newtmppath).read(), 'Test string\n')
42
43
44 def testGetWithHTTP(self):
45 # Yeah, this test is a bad idea, oh well
46 url = 'http://www.kernel.org/pub/linux/kernel/README'
47 tmppath = utils.get(url)
48 f = file(tmppath)
49 f.readline()
50 self.assertTrue('Linux' in f.readline().split())
51
52
53 def testGetWithPath(self):
54 path = utils.get('/proc/cpuinfo')
55 self.assertTrue(file(path).readline().startswith('processor'))
56
57
58 def testGetWithString(self):
59 path = utils.get('/tmp loves rabbits!')
60 self.assertTrue(file(path).readline().startswith('/tmp loves'))
61
62
63 def testGetWithDir(self):
64 tmpdir = utils.get_tmp_dir()
65 origpath = os.path.join(tmpdir, 'testGetWithDir')
66 os.mkdir(origpath)
67 dstpath = utils.get(origpath)
68 self.assertTrue(dstpath.endswith('/'))
69 self.assertTrue(os.path.isdir(dstpath))
70
71
72def suite():
73 return unittest.TestLoader().loadTestsFromTestCase(UtilsTestCase)
74
75if __name__ == '__main__':
76 unittest.TextTestRunner(verbosity=2).run(suite())