~bigdata-dev/charms/trusty/apache-hadoop-hdfs-secondary/trunk

« back to all changes in this revision

Viewing changes to remote/test_dist_config.py

  • Committer: Kevin W. Monroe
  • Date: 2015-03-04 03:49:10 UTC
  • mfrom: (20.1.1 rename)
  • Revision ID: kevin.monroe@canonical.com-20150304034910-7gmfctk5jx2o7u9b
updated charmhelpers to relax spec requirements

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import grp
 
4
import pwd
 
5
import unittest
 
6
 
 
7
from charmhelpers.contrib import bigdata
 
8
 
 
9
 
 
10
class TestDistConfig(unittest.TestCase):
 
11
    """
 
12
    Test that the ``dist.yaml`` settings were applied properly, such as users, groups, and dirs.
 
13
 
 
14
    This is done as a remote test on the deployed unit rather than a regular
 
15
    test under ``tests/`` because filling in the ``dist.yaml`` requires Juju
 
16
    context (e.g., config).
 
17
    """
 
18
    @classmethod
 
19
    def setUpClass(cls):
 
20
        cls.hadoop = bigdata.handlers.apache.HadoopBase()
 
21
 
 
22
    def test_groups(self):
 
23
        for name in self.hadoop.groups:
 
24
            try:
 
25
                grp.getgrnam(name)
 
26
            except KeyError:
 
27
                self.fail('Group {} is missing'.format(name))
 
28
 
 
29
    def test_users(self):
 
30
        for username, details in self.hadoop.users.items():
 
31
            try:
 
32
                user = pwd.getpwnam(username)
 
33
            except KeyError:
 
34
                self.fail('User {} is missing'.format(username))
 
35
            for groupname in details['groups']:
 
36
                try:
 
37
                    group = grp.getgrnam(groupname)
 
38
                except KeyError:
 
39
                    self.fail('Group {} referenced by user {} does not exist'.format(
 
40
                        groupname, username))
 
41
                if group.gr_gid != user.pw_gid:
 
42
                    self.assertIn(username, group.gr_mem, 'User {} not in group {}'.format(
 
43
                        username, groupname))
 
44
 
 
45
    def test_dirs(self):
 
46
        for name, details in self.hadoop.managed_dirs.items():
 
47
            dirpath = details['path']
 
48
            self.assertTrue(dirpath.isdir(), 'Dir {} is missing'.format(name))
 
49
            stat = dirpath.stat()
 
50
            owner = pwd.getpwuid(stat.st_uid).pw_name
 
51
            group = grp.getgrgid(stat.st_gid).gr_name
 
52
            perms = stat.st_mode & ~0o40000
 
53
            self.assertEqual(owner, details.get('owner', 'root'),
 
54
                             'Dir {} ({}) has wrong owner: {}'.format(name, dirpath, owner))
 
55
            self.assertEqual(group, details.get('group', 'root'),
 
56
                             'Dir {} ({}) has wrong group: {}'.format(name, dirpath, group))
 
57
            self.assertEqual(perms, details.get('perms', 0o744),
 
58
                             'Dir {} ({}) has wrong perms: 0o{:o}'.format(name, dirpath, perms))
 
59
 
 
60
 
 
61
if __name__ == '__main__':
 
62
    unittest.main()