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