~ubuntu-branches/debian/experimental/389-ds-base/experimental

« back to all changes in this revision

Viewing changes to dirsrvtests/tickets/ticket47815_test.py

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2014-07-08 15:50:11 UTC
  • mfrom: (0.2.2)
  • Revision ID: package-import@ubuntu.com-20140708155011-r66lvtioamqwaype
Tags: 1.3.2.19-1
* New upstream release.
* admin_scripts.diff: Updated to fix more bashisms.
* watch: Update the url.
* Install failedbinds.py and logregex.py scripts.
* init: Use status from init-functions.
* control: Update my email.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
import time
 
4
import ldap
 
5
import logging
 
6
import socket
 
7
import pytest
 
8
from lib389 import DirSrv, Entry, tools
 
9
from lib389.tools import DirSrvTools
 
10
from lib389._constants import *
 
11
from lib389.properties import *
 
12
from constants import *
 
13
 
 
14
log = logging.getLogger(__name__)
 
15
 
 
16
installation_prefix = None
 
17
 
 
18
 
 
19
class TopologyStandalone(object):
 
20
    def __init__(self, standalone):
 
21
        standalone.open()
 
22
        self.standalone = standalone
 
23
 
 
24
 
 
25
pytest.fixture(scope="module")
 
26
def topology(request):
 
27
    '''
 
28
        This fixture is used to standalone topology for the 'module'.
 
29
        At the beginning, It may exists a standalone instance.
 
30
        It may also exists a backup for the standalone instance.
 
31
 
 
32
        Principle:
 
33
            If standalone instance exists:
 
34
                restart it
 
35
            If backup of standalone exists:
 
36
                create/rebind to standalone
 
37
 
 
38
                restore standalone instance from backup
 
39
            else:
 
40
                Cleanup everything
 
41
                    remove instance
 
42
                    remove backup
 
43
                Create instance
 
44
                Create backup
 
45
    '''
 
46
    global installation_prefix
 
47
 
 
48
    if installation_prefix:
 
49
        args_instance[SER_DEPLOYED_DIR] = installation_prefix
 
50
 
 
51
    standalone = DirSrv(verbose=False)
 
52
 
 
53
    # Args for the standalone instance
 
54
    args_instance[SER_HOST] = HOST_STANDALONE
 
55
    args_instance[SER_PORT] = PORT_STANDALONE
 
56
    args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
 
57
    args_standalone = args_instance.copy()
 
58
    standalone.allocate(args_standalone)
 
59
 
 
60
    # Get the status of the backups
 
61
    backup_standalone = standalone.checkBackupFS()
 
62
 
 
63
    # Get the status of the instance and restart it if it exists
 
64
    instance_standalone = standalone.exists()
 
65
    if instance_standalone:
 
66
        # assuming the instance is already stopped, just wait 5 sec max
 
67
        standalone.stop(timeout=5)
 
68
        standalone.start(timeout=10)
 
69
 
 
70
    if backup_standalone:
 
71
        # The backup exist, assuming it is correct
 
72
        # we just re-init the instance with it
 
73
        if not instance_standalone:
 
74
            standalone.create()
 
75
            # Used to retrieve configuration information (dbdir, confdir...)
 
76
            standalone.open()
 
77
 
 
78
        # restore standalone instance from backup
 
79
        standalone.stop(timeout=10)
 
80
        standalone.restoreFS(backup_standalone)
 
81
        standalone.start(timeout=10)
 
82
 
 
83
    else:
 
84
        # We should be here only in two conditions
 
85
        #      - This is the first time a test involve standalone instance
 
86
        #      - Something weird happened (instance/backup destroyed)
 
87
        #        so we discard everything and recreate all
 
88
 
 
89
        # Remove the backup. So even if we have a specific backup file
 
90
        # (e.g backup_standalone) we clear backup that an instance may have created
 
91
        if backup_standalone:
 
92
            standalone.clearBackupFS()
 
93
 
 
94
        # Remove the instance
 
95
        if instance_standalone:
 
96
            standalone.delete()
 
97
 
 
98
        # Create the instance
 
99
        standalone.create()
 
100
 
 
101
        # Used to retrieve configuration information (dbdir, confdir...)
 
102
        standalone.open()
 
103
 
 
104
        # Time to create the backups
 
105
        standalone.stop(timeout=10)
 
106
        standalone.backupfile = standalone.backupFS()
 
107
        standalone.start(timeout=10)
 
108
 
 
109
    #
 
110
    # Here we have standalone instance up and running
 
111
    # Either coming from a backup recovery
 
112
    # or from a fresh (re)init
 
113
    # Time to return the topology
 
114
    return TopologyStandalone(standalone)
 
115
 
 
116
 
 
117
def test_ticket47815(topology):
 
118
    """
 
119
        Test betxn plugins reject an invalid option, and make sure that the rejected entry
 
120
        is not in the entry cache.
 
121
 
 
122
        Enable memberOf, automember, and retrocl plugins
 
123
        Add the automember config entry
 
124
        Add the automember group
 
125
        Add a user that will be rejected by a betxn plugin - result error 53
 
126
        Attempt the same add again, and it should result in another error 53 (not error 68)
 
127
    """
 
128
    result = 0
 
129
    result2 = 0
 
130
 
 
131
    log.info('Testing Ticket 47815 - Add entries that should be rejected by the betxn plugins, and are not left in the entry cache')
 
132
 
 
133
    # Enabled the plugins
 
134
    topology.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
 
135
    topology.standalone.plugins.enable(name=PLUGIN_AUTOMEMBER)
 
136
    topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
 
137
 
 
138
    # configure automember config entry
 
139
    log.info('Adding automember config')
 
140
    try:
 
141
        topology.standalone.add_s(Entry(('cn=group cfg,cn=Auto Membership Plugin,cn=plugins,cn=config', {
 
142
                                     'objectclass': 'top autoMemberDefinition'.split(),
 
143
                                     'autoMemberScope': 'dc=example,dc=com',
 
144
                                     'autoMemberFilter': 'cn=user',
 
145
                                     'autoMemberDefaultGroup': 'cn=group,dc=example,dc=com',
 
146
                                     'autoMemberGroupingAttr': 'member:dn',
 
147
                                     'cn': 'group cfg'})))
 
148
    except:
 
149
        log.error('Failed to add automember config')
 
150
        exit(1)
 
151
 
 
152
    topology.standalone.stop(timeout=120)
 
153
    time.sleep(1)
 
154
    topology.standalone.start(timeout=120)
 
155
    time.sleep(3)
 
156
 
 
157
    # need to reopen a connection toward the instance
 
158
    topology.standalone.open()
 
159
 
 
160
    # add automember group
 
161
    log.info('Adding automember group')
 
162
    try:
 
163
        topology.standalone.add_s(Entry(('cn=group,dc=example,dc=com', {
 
164
                                  'objectclass': 'top groupOfNames'.split(),
 
165
                                  'cn': 'group'})))
 
166
    except:
 
167
        log.error('Failed to add automember group')
 
168
        exit(1)
 
169
 
 
170
    # add user that should result in an error 53
 
171
    log.info('Adding invalid entry')
 
172
 
 
173
    try:
 
174
        topology.standalone.add_s(Entry(('cn=user,dc=example,dc=com', {
 
175
                                  'objectclass': 'top person'.split(),
 
176
                                  'sn': 'user',
 
177
                                  'cn': 'user'})))
 
178
    except ldap.UNWILLING_TO_PERFORM:
 
179
        log.debug('Adding invalid entry failed as expected')
 
180
        result = 53
 
181
    except ldap.LDAPError, e:
 
182
        log.error('Unexpected result ' + e.message['desc'])
 
183
        assert False
 
184
    if result == 0:
 
185
        log.error('Add operation unexpectedly succeeded')
 
186
        assert False
 
187
 
 
188
    # Attempt to add user again, should result in error 53 again
 
189
    try:
 
190
        topology.standalone.add_s(Entry(('cn=user,dc=example,dc=com', {
 
191
                                  'objectclass': 'top person'.split(),
 
192
                                  'sn': 'user',
 
193
                                  'cn': 'user'})))
 
194
    except ldap.UNWILLING_TO_PERFORM:
 
195
        log.debug('2nd add of invalid entry failed as expected')
 
196
        result2 = 53
 
197
    except ldap.LDAPError, e:
 
198
        log.error('Unexpected result ' + e.message['desc'])
 
199
        assert False
 
200
    if result2 == 0:
 
201
        log.error('2nd Add operation unexpectedly succeeded')
 
202
        assert False
 
203
 
 
204
    # If we got here we passed!
 
205
    log.info('Ticket47815 Test - Passed')
 
206
 
 
207
 
 
208
def test_ticket47815_final(topology):
 
209
    topology.standalone.stop(timeout=10)
 
210
 
 
211
 
 
212
def run_isolated():
 
213
    '''
 
214
        run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
 
215
        To run isolated without py.test, you need to
 
216
            - edit this file and comment '@pytest.fixture' line before 'topology' function.
 
217
            - set the installation prefix
 
218
            - run this program
 
219
    '''
 
220
    global installation_prefix
 
221
    installation_prefix = None
 
222
 
 
223
    topo = topology(True)
 
224
    test_ticket47815(topo)
 
225
 
 
226
if __name__ == '__main__':
 
227
    run_isolated()