mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame^] | 1 | #!/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 | |
| 10 | import os |
| 11 | import sys |
| 12 | import os.path |
| 13 | import unittest |
| 14 | |
| 15 | # Adjust the path so Python can find the autoserv modules |
| 16 | src = os.path.abspath("%s/.." % (os.path.dirname(sys.argv[0]),)) |
| 17 | if src not in sys.path: |
| 18 | sys.path.insert(1, src) |
| 19 | |
| 20 | import utils |
| 21 | |
| 22 | |
| 23 | |
| 24 | class 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 | |
| 72 | def suite(): |
| 73 | return unittest.TestLoader().loadTestsFromTestCase(UtilsTestCase) |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | unittest.TextTestRunner(verbosity=2).run(suite()) |