1# Copyright 2007 Google Inc.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software Foundation,
15# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16"""Unit tests for automount.py.
17
18We only test what is overridden in the automount subclasses, most
19functionality is in base.py and tested in passwd_test.py since a
20subclass is required to test the abstract class functionality.
21"""
22
23__author__ = 'vasilios@google.com (Vasilios Hoffman)'
24
25import unittest
26
27from nss_cache.maps import automount
28from nss_cache.maps import passwd
29
30
31class TestAutomountMap(unittest.TestCase):
32    """Tests for the AutomountMap class."""
33
34    def __init__(self, obj):
35        """Set some default avalible data for testing."""
36        super(TestAutomountMap, self).__init__(obj)
37        self._good_entry = automount.AutomountMapEntry()
38        self._good_entry.key = 'foo'
39        self._good_entry.options = '-tcp'
40        self._good_entry.location = 'nfsserver:/mah/stuff'
41
42    def testInit(self):
43        """Construct an empty or seeded AutomountMap."""
44        self.assertEqual(automount.AutomountMap,
45                         type(automount.AutomountMap()),
46                         msg='failed to create an empty AutomountMap')
47        amap = automount.AutomountMap([self._good_entry])
48        self.assertEqual(self._good_entry,
49                         amap.PopItem(),
50                         msg='failed to seed AutomountMap with list')
51        self.assertRaises(TypeError, automount.AutomountMap, ['string'])
52
53    def testAdd(self):
54        """Add throws an error for objects it can't verify."""
55        amap = automount.AutomountMap()
56        entry = self._good_entry
57        self.assertTrue(amap.Add(entry), msg='failed to append new entry.')
58
59        self.assertEqual(1, len(amap), msg='unexpected size for Map.')
60
61        ret_entry = amap.PopItem()
62        self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.')
63
64        pentry = passwd.PasswdMapEntry()
65        pentry.name = 'foo'
66        pentry.uid = 10
67        pentry.gid = 10
68        self.assertRaises(TypeError, amap.Add, pentry)
69
70
71class TestAutomountMapEntry(unittest.TestCase):
72    """Tests for the AutomountMapEntry class."""
73
74    def testInit(self):
75        """Construct an empty and seeded AutomountMapEntry."""
76        self.assertTrue(automount.AutomountMapEntry(),
77                        msg='Could not create empty AutomountMapEntry')
78        seed = {'key': 'foo', 'location': '/dev/sda1'}
79        entry = automount.AutomountMapEntry(seed)
80        self.assertTrue(entry.Verify(),
81                        msg='Could not verify seeded AutomountMapEntry')
82        self.assertEqual(entry.key,
83                         'foo',
84                         msg='Entry returned wrong value for name')
85        self.assertEqual(entry.options,
86                         None,
87                         msg='Entry returned wrong value for options')
88        self.assertEqual(entry.location,
89                         '/dev/sda1',
90                         msg='Entry returned wrong value for location')
91
92    def testAttributes(self):
93        """Test that we can get and set all expected attributes."""
94        entry = automount.AutomountMapEntry()
95        entry.key = 'foo'
96        self.assertEqual(entry.key, 'foo', msg='Could not set attribute: key')
97        entry.options = 'noatime'
98        self.assertEqual(entry.options,
99                         'noatime',
100                         msg='Could not set attribute: options')
101        entry.location = '/dev/ipod'
102        self.assertEqual(entry.location,
103                         '/dev/ipod',
104                         msg='Could not set attribute: location')
105
106    def testVerify(self):
107        """Test that the object can verify it's attributes and itself."""
108        entry = automount.AutomountMapEntry()
109
110        # Empty object should bomb
111        self.assertFalse(entry.Verify())
112
113    def testKey(self):
114        """Key() should return the value of the 'key' attribute."""
115        entry = automount.AutomountMapEntry()
116        entry.key = 'foo'
117        self.assertEqual(entry.Key(), entry.key)
118
119
120if __name__ == '__main__':
121    unittest.main()
122