File: s3source_test.py

package info (click to toggle)
nsscache 0.49-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,664 kB
  • sloc: python: 8,661; xml: 584; sh: 304; makefile: 19
file content (181 lines) | stat: -rw-r--r-- 6,260 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""An implementation of a mock S3 data source for nsscache."""

__author__ = "alexey.pikin@gmail.com"

import unittest
from io import StringIO

from nss_cache.maps import group
from nss_cache.maps import passwd
from nss_cache.maps import shadow

try:
    from nss_cache.sources import s3source
except Exception as e:
    raise unittest.SkipTest("s3source unable to be imported: {}".format(e))


class TestS3Source(unittest.TestCase):
    def setUp(self):
        """Initialize a basic config dict."""
        super(TestS3Source, self).setUp()
        self.config = {
            "passwd_object": "PASSWD_OBJ",
            "group_object": "GROUP_OBJ",
            "bucket": "TEST_BUCKET",
        }

    def testDefaultConfiguration(self):
        source = s3source.S3FilesSource({})
        self.assertEqual(source.conf["bucket"], s3source.S3FilesSource.BUCKET)
        self.assertEqual(
            source.conf["passwd_object"], s3source.S3FilesSource.PASSWD_OBJECT
        )

    def testOverrideDefaultConfiguration(self):
        source = s3source.S3FilesSource(self.config)
        self.assertEqual(source.conf["bucket"], "TEST_BUCKET")
        self.assertEqual(source.conf["passwd_object"], "PASSWD_OBJ")
        self.assertEqual(source.conf["group_object"], "GROUP_OBJ")


class TestPasswdMapParser(unittest.TestCase):
    def setUp(self):
        """Set some default avalible data for testing."""
        self.good_entry = passwd.PasswdMapEntry()
        self.good_entry.name = "foo"
        self.good_entry.passwd = "x"
        self.good_entry.uid = 10
        self.good_entry.gid = 10
        self.good_entry.gecos = "How Now Brown Cow"
        self.good_entry.dir = "/home/foo"
        self.good_entry.shell = "/bin/bash"
        self.parser = s3source.S3PasswdMapParser()

    def testGetMap(self):
        passwd_map = passwd.PasswdMap()
        cache_info = StringIO(
            """[
                            { "Key": "foo",
                              "Value": {
                               "uid": 10, "gid": 10, "home": "/home/foo",
                               "shell": "/bin/bash", "comment": "How Now Brown Cow",
                               "irrelevant_key":"bacon"
                              }
                            }
                          ]"""
        )
        self.parser.GetMap(cache_info, passwd_map)
        self.assertEqual(self.good_entry, passwd_map.PopItem())

    def testReadEntry(self):
        data = {
            "uid": "10",
            "gid": "10",
            "comment": "How Now Brown Cow",
            "shell": "/bin/bash",
            "home": "/home/foo",
            "passwd": "x",
        }
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(self.good_entry, entry)

    def testDefaultEntryValues(self):
        data = {"uid": "10", "gid": "10"}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(entry.shell, "/bin/bash")
        self.assertEqual(entry.dir, "/home/foo")
        self.assertEqual(entry.gecos, "")
        self.assertEqual(entry.passwd, "x")

    def testInvalidEntry(self):
        data = {"irrelevant_key": "bacon"}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(entry, None)


class TestS3GroupMapParser(unittest.TestCase):
    def setUp(self):
        self.good_entry = group.GroupMapEntry()
        self.good_entry.name = "foo"
        self.good_entry.passwd = "x"
        self.good_entry.gid = 10
        self.good_entry.members = ["foo", "bar"]
        self.parser = s3source.S3GroupMapParser()

    def testGetMap(self):
        group_map = group.GroupMap()
        cache_info = StringIO(
            """[
                            { "Key": "foo",
                              "Value": {
                               "gid": 10,
                               "members": "foo\\nbar",
                               "irrelevant_key": "bacon"
                              }
                            }
                          ]"""
        )
        self.parser.GetMap(cache_info, group_map)
        self.assertEqual(self.good_entry, group_map.PopItem())

    def testReadEntry(self):
        data = {"passwd": "x", "gid": "10", "members": "foo\nbar"}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(self.good_entry, entry)

    def testDefaultPasswd(self):
        data = {"gid": "10", "members": "foo\nbar"}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(self.good_entry, entry)

    def testNoMembers(self):
        data = {"gid": "10", "members": ""}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(entry.members, [""])

    def testInvalidEntry(self):
        data = {"irrelevant_key": "bacon"}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(entry, None)


class TestS3ShadowMapParser(unittest.TestCase):
    def setUp(self):
        self.good_entry = shadow.ShadowMapEntry()
        self.good_entry.name = "foo"
        self.good_entry.passwd = "*"
        self.good_entry.lstchg = 17246
        self.good_entry.min = 0
        self.good_entry.max = 99999
        self.good_entry.warn = 7
        self.parser = s3source.S3ShadowMapParser()

    def testGetMap(self):
        shadow_map = shadow.ShadowMap()
        cache_info = StringIO(
            """[
                            { "Key": "foo",
                              "Value": {
                               "passwd": "*", "lstchg": 17246, "min": 0,
                               "max": 99999, "warn": 7
                              }
                            }
                          ]"""
        )
        self.parser.GetMap(cache_info, shadow_map)
        self.assertEqual(self.good_entry, shadow_map.PopItem())

    def testReadEntry(self):
        data = {"passwd": "*", "lstchg": 17246, "min": 0, "max": 99999, "warn": 7}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(self.good_entry, entry)

    def testDefaultPasswd(self):
        data = {"lstchg": 17246, "min": 0, "max": 99999, "warn": 7}
        entry = self.parser._ReadEntry("foo", data)
        self.assertEqual(self.good_entry, entry)


if __name__ == "__main__":
    unittest.main()