1from test import support
2from test.support import import_helper
3import unittest
4
5
6# Skip test if nis module does not exist.
7nis = import_helper.import_module('nis')
8
9
10class NisTests(unittest.TestCase):
11    def test_maps(self):
12        try:
13            maps = nis.maps()
14        except nis.error as msg:
15            # NIS is probably not active, so this test isn't useful
16            self.skipTest(str(msg))
17        try:
18            # On some systems, this map is only accessible to the
19            # super user
20            maps.remove("passwd.adjunct.byname")
21        except ValueError:
22            pass
23
24        done = 0
25        for nismap in maps:
26            mapping = nis.cat(nismap)
27            for k, v in mapping.items():
28                if not k:
29                    continue
30                if nis.match(k, nismap) != v:
31                    self.fail("NIS match failed for key `%s' in map `%s'" % (k, nismap))
32                else:
33                    # just test the one key, otherwise this test could take a
34                    # very long time
35                    done = 1
36                    break
37            if done:
38                break
39
40if __name__ == '__main__':
41    unittest.main()
42