Debian Codegraph @

dirsrvtests/tests/suites/clu/dsrc_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2023 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9 import logging
10 import pytest
11 import os
12 from os.path import expanduser
13 from lib389.cli_base import FakeArgs
14 from lib389.cli_ctl.dsrc import create_dsrc, modify_dsrc, delete_dsrc, display_dsrc, replmon_dsrc
15 from lib389._constants import DEFAULT_SUFFIX, DN_DM
16 from test389.topologies import topology_st as topo
17
18 log = logging.getLogger(__name__)
19
20
21 def get_fake_args():
22 # Setup our args
23 args = FakeArgs()
24 args.basedn = DEFAULT_SUFFIX
25 args.groups_rdn = None
26 args.people_rdn = None
27 args.binddn = DN_DM
28 args.json = None
29 args.uri = None
30 args.saslmech = None
31 args.tls_cacertdir = None
32 args.tls_cert = None
33 args.tls_key = None
34 args.tls_reqcert = None
35 args.starttls = None
36 args.cancel_starttls = None
37 args.pwdfile = None
38 args.do_it = True
39 args.add_conn = None
40 args.del_conn = None
41 args.add_alias = None
42 args.del_alias = None
43
44 return args
45
46
47 @pytest.fixture(scope="function")
48 def setup(topo, request):
49 """Preserve any existing .dsrc file"""
50
51 dsrc_file = f'{expanduser("~")}/.dsrc'
52 backup_file = dsrc_file + ".original"
53 if os.path.exists(dsrc_file):
54 os.rename(dsrc_file, backup_file)
55
56 def fin():
57 if os.path.exists(backup_file):
58 os.rename(backup_file, dsrc_file)
59
60 request.addfinalizer(fin)
61
62
63 def test_dsrc(topo, setup):
64 """Test "dsctl dsrc" command
65
66 :id: 0610de6c-e167-4761-bdab-3e677b2d44bb
67 :setup: Standalone Instance
68 :steps:
69 1. Test creation works
70 2. Test creating duplicate section
71 3. Test adding an additional inst config works
72 4. Test removing an instance works
73 5. Test modify works
74 6. Test delete works
75 7. Test display fails when no file is present
76
77 :expectedresults:
78 1. Success
79 2. Success
80 3. Success
81 4. Success
82 5. Success
83 6. Success
84 7. Success
85 """
86
87 inst = topo.standalone
88 serverid = inst.serverid
89 second_inst_name = "Second"
90 second_inst_basedn = "o=second"
91 different_suffix = "o=different"
92
93 # Setup our args
94 args = get_fake_args()
95
96 # Create a dsrc configuration entry
97 create_dsrc(inst, log, args)
98 display_dsrc(inst, topo.logcap.log, args)
99 assert topo.logcap.contains("basedn = " + args.basedn)
100 assert topo.logcap.contains("binddn = " + args.binddn)
101 assert topo.logcap.contains("[" + serverid + "]")
102 topo.logcap.flush()
103
104 # Attempt to add duplicate instance section
105 with pytest.raises(ValueError):
106 create_dsrc(inst, log, args)
107
108 # Test adding a second instance works correctly
109 inst.serverid = second_inst_name
110 args.basedn = second_inst_basedn
111 create_dsrc(inst, log, args)
112 display_dsrc(inst, topo.logcap.log, args)
113 assert topo.logcap.contains("basedn = " + args.basedn)
114 assert topo.logcap.contains("[" + second_inst_name + "]")
115 topo.logcap.flush()
116
117 # Delete second instance
118 delete_dsrc(inst, log, args)
119 inst.serverid = serverid # Restore original instance name
120 display_dsrc(inst, topo.logcap.log, args)
121 assert not topo.logcap.contains("[" + second_inst_name + "]")
122 assert not topo.logcap.contains("basedn = " + args.basedn)
123 # Make sure first instance config is still present
124 assert topo.logcap.contains("[" + serverid + "]")
125 assert topo.logcap.contains("binddn = " + args.binddn)
126 topo.logcap.flush()
127
128 # Modify the config
129 args.basedn = different_suffix
130 modify_dsrc(inst, log, args)
131 display_dsrc(inst, topo.logcap.log, args)
132 assert topo.logcap.contains(different_suffix)
133 topo.logcap.flush()
134
135 # Remove an arg from the config
136 args.basedn = ""
137 modify_dsrc(inst, log, args)
138 display_dsrc(inst, topo.logcap.log, args)
139 assert not topo.logcap.contains(different_suffix)
140 topo.logcap.flush()
141
142 # Remove the last entry, which should delete the file
143 delete_dsrc(inst, log, args)
144 dsrc_file = f'{expanduser("~")}/.dsrc'
145 assert not os.path.exists(dsrc_file)
146
147 # Make sure display fails
148 with pytest.raises(ValueError):
149 display_dsrc(inst, log, args)
150
151
152 def test_dsrc_repl_mon(topo, setup):
153 """Test "dsctl dsrc repl-mon" command, add & remove creds and aliases
154
155 :id: 33007d01-f11c-456b-bb16-fcd7920c9fc8
156 :setup: Standalone Instance
157 :steps:
158 1. Add connection
159 2. Add same connection - should fail
160 3. Delete connection
161 4. Delete same connection - should fail
162 5. Add alias
163 6. Add same alias - should fail
164 7. Delete alias
165 8. Delete same alias again 0 should fail
166
167 :expectedresults:
168 1. Success
169 2. Success
170 3. Success
171 4. Success
172 5. Success
173 6. Success
174 7. Success
175 8. Success
176 """
177
178 inst = topo.standalone
179 args = get_fake_args()
180 create_dsrc(inst, log, args)
181
182 # Add replica connection
183 assert not topo.logcap.contains("repl-monitor-connections")
184 repl_conn = "replica_1:localhost:5555:cn=directory manager:password"
185 args.add_conn = [repl_conn,]
186 replmon_dsrc(inst, log, args)
187 display_dsrc(inst, topo.logcap.log, args)
188 assert topo.logcap.contains("repl-monitor-connections")
189 assert topo.logcap.contains("replica_1 = localhost:5555:cn=directory manager:password")
190 topo.logcap.flush()
191 args.add_conn = None
192
193 # Add duplicate replica connection
194 args.add_conn = [repl_conn, ]
195 try:
196 replmon_dsrc(inst, log, args)
197 assert False
198 except ValueError:
199 pass
200 args.add_conn = None
201
202 # Delete replica connection
203 args.del_conn = ["replica_1"]
204 replmon_dsrc(inst, log, args)
205 display_dsrc(inst, topo.logcap.log, args)
206 assert not topo.logcap.contains("replica_1 = localhost:5555:cn=directory manager:password")
207 assert not topo.logcap.contains("repl-monitor-connections")
208 topo.logcap.flush()
209 args.del_conn = None
210
211 # Delete replica connection (already deleted)
212 args.del_conn = ["replica_1"]
213 try:
214 replmon_dsrc(inst, log, args)
215 assert False
216 except ValueError:
217 pass
218 args.del_conn = None
219
220 # Add Alias
221 assert not topo.logcap.contains("repl-monitor-aliases")
222 repl_alias = "my_alias:localhost:4444"
223 args.add_alias = [repl_alias,]
224 replmon_dsrc(inst, log, args)
225 display_dsrc(inst, topo.logcap.log, args)
226 assert topo.logcap.contains("repl-monitor-aliases")
227 assert topo.logcap.contains("my_alias = localhost:4444")
228 topo.logcap.flush()
229 args.add_alias = None
230
231 # Add Duplicate Alias
232 args.add_alias = [repl_alias,]
233 try:
234 replmon_dsrc(inst, log, args)
235 assert False
236 except ValueError:
237 pass
238 args.add_alias = None
239
240 # Delete Alias
241 args.del_alias = ["my_alias",]
242 replmon_dsrc(inst, log, args)
243 display_dsrc(inst, topo.logcap.log, args)
244 assert not topo.logcap.contains("my_alias = localhost:4444")
245 assert not topo.logcap.contains("repl-monitor-aliases")
246 topo.logcap.flush()
247 args.del_alias = None
248
249 # Delete alias (already deleted)
250 args.del_alias = ["my_alias", ]
251 try:
252 replmon_dsrc(inst, log, args)
253 assert False
254 except ValueError:
255 pass
256 args.del_alias = None
257
258
259 if __name__ == '__main__':
260 # Run isolated
261 # -s for DEBUG mode
262 CURRENT_FILE = os.path.realpath(__file__)
263 pytest.main(["-s", CURRENT_FILE])