| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2021 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 time |
| 10 |
import subprocess |
| 11 |
import pytest |
| 12 |
import logging |
| 13 |
import os |
| 14 |
import json |
| 15 |
|
| 16 |
from lib389 import DEFAULT_SUFFIX |
| 17 |
from lib389.cli_idm.service import list, get, get_dn, create, delete, modify, rename |
| 18 |
from test389.topologies import topology_st |
| 19 |
from lib389.cli_base import FakeArgs |
| 20 |
from lib389.utils import ds_is_older, ensure_str, is_a_dn |
| 21 |
from lib389.idm.services import ServiceAccounts |
| 22 |
from . import check_value_in_log_and_reset |
| 23 |
|
| 24 |
pytestmark = pytest.mark.tier0 |
| 25 |
|
| 26 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 27 |
log = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture(scope="function") |
| 31 |
def create_test_service(topology_st, request): |
| 32 |
service_name = 'test_service' |
| 33 |
services = ServiceAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 34 |
|
| 35 |
log.info('Create test service') |
| 36 |
if services.exists(service_name): |
| 37 |
test_service = services.get(service_name) |
| 38 |
test_service.delete() |
| 39 |
else: |
| 40 |
test_service = services.create_test_service() |
| 41 |
|
| 42 |
def fin(): |
| 43 |
log.info('Delete test service') |
| 44 |
if test_service.exists(): |
| 45 |
test_service.delete() |
| 46 |
|
| 47 |
request.addfinalizer(fin) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.skipif(ds_is_older("2.1.0"), reason="Not implemented") |
| 51 |
def test_dsidm_service_list(topology_st, create_test_service): |
| 52 |
""" Test dsidm service list option |
| 53 |
|
| 54 |
:id: 218aa060-51e1-11ec-8a70-3497f624ea11 |
| 55 |
:setup: Standalone instance |
| 56 |
:steps: |
| 57 |
1. Run dsidm service list option without json |
| 58 |
2. Check the output content is correct |
| 59 |
3. Run dsidm service list option with json |
| 60 |
4. Check the json content is correct |
| 61 |
5. Delete the service |
| 62 |
6. Check the service is not in the list with json |
| 63 |
7. Check the service is not in the list without json |
| 64 |
:expectedresults: |
| 65 |
1. Success |
| 66 |
2. Success |
| 67 |
3. Success |
| 68 |
4. Success |
| 69 |
5. Success |
| 70 |
6. Success |
| 71 |
7. Success |
| 72 |
""" |
| 73 |
|
| 74 |
standalone = topology_st.standalone |
| 75 |
args = FakeArgs() |
| 76 |
args.json = False |
| 77 |
args.full_dn = False |
| 78 |
service_value = 'test_service' |
| 79 |
json_list = ['type', |
| 80 |
'list', |
| 81 |
'items'] |
| 82 |
|
| 83 |
log.info('Empty the log file to prevent false data to check about service') |
| 84 |
topology_st.logcap.flush() |
| 85 |
|
| 86 |
log.info('Test dsidm service list without json') |
| 87 |
list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 88 |
check_value_in_log_and_reset(topology_st, check_value=service_value) |
| 89 |
|
| 90 |
log.info('Test dsidm service list with json') |
| 91 |
args.json = True |
| 92 |
list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 93 |
check_value_in_log_and_reset(topology_st, content_list=json_list, check_value=service_value) |
| 94 |
|
| 95 |
log.info('Test full_dn option with list') |
| 96 |
args.full_dn = True |
| 97 |
list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 98 |
result = topology_st.logcap.get_raw_outputs() |
| 99 |
json_result = json.loads(result[0]) |
| 100 |
assert is_a_dn(json_result['items'][0]) |
| 101 |
args.full_dn = False |
| 102 |
|
| 103 |
log.info('Delete the service') |
| 104 |
services = ServiceAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 105 |
testservice = services.get(service_value) |
| 106 |
testservice.delete() |
| 107 |
|
| 108 |
log.info('Test empty dsidm service list with json') |
| 109 |
topology_st.logcap.flush() |
| 110 |
list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 111 |
check_value_in_log_and_reset(topology_st, content_list=json_list, check_value_not=service_value) |
| 112 |
|
| 113 |
log.info('Test empty dsidm service list without json') |
| 114 |
args.json = False |
| 115 |
list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 116 |
check_value_in_log_and_reset(topology_st, check_value_not=service_value) |
| 117 |
|
| 118 |
|
| 119 |
@pytest.mark.skipif(ds_is_older("2.1.0"), reason="Not implemented") |
| 120 |
def test_dsidm_service_get_rdn(topology_st, create_test_service): |
| 121 |
""" Test dsidm service get option |
| 122 |
|
| 123 |
:id: 294ef774-51e1-11ec-a2c7-3497f624ea11 |
| 124 |
:setup: Standalone instance |
| 125 |
:steps: |
| 126 |
1. Run dsidm get option for created service with json |
| 127 |
2. Check the output content is correct |
| 128 |
3. Run dsidm get option for created service without json |
| 129 |
4. Check the json content is correct |
| 130 |
:expectedresults: |
| 131 |
1. Success |
| 132 |
2. Success |
| 133 |
3. Success |
| 134 |
4. Success |
| 135 |
""" |
| 136 |
|
| 137 |
standalone = topology_st.standalone |
| 138 |
services = ServiceAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 139 |
testservice = services.get('test_service') |
| 140 |
|
| 141 |
service_content = [f'dn: {testservice.dn}', |
| 142 |
f'cn: {testservice.rdn}', |
| 143 |
'description: Test Service', |
| 144 |
'objectClass: top', |
| 145 |
'objectClass: nsAccount', |
| 146 |
'objectClass: nsMemberOf'] |
| 147 |
|
| 148 |
json_content = ['attrs', |
| 149 |
'objectclass', |
| 150 |
'top', |
| 151 |
'nsAccount', |
| 152 |
'nsMemberOf', |
| 153 |
testservice.rdn, |
| 154 |
'cn', |
| 155 |
'description', |
| 156 |
'creatorsname', |
| 157 |
'cn=directory manager', |
| 158 |
'modifiersname', |
| 159 |
'createtimestamp', |
| 160 |
'modifytimestamp', |
| 161 |
'nsuniqueid', |
| 162 |
'parentid', |
| 163 |
'entryid', |
| 164 |
'entrydn', |
| 165 |
testservice.dn] |
| 166 |
|
| 167 |
args = FakeArgs() |
| 168 |
args.json = False |
| 169 |
args.selector = 'test_service' |
| 170 |
|
| 171 |
log.info('Empty the log file to prevent false data to check about service') |
| 172 |
topology_st.logcap.flush() |
| 173 |
|
| 174 |
log.info('Test dsidm service get without json') |
| 175 |
get(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 176 |
check_value_in_log_and_reset(topology_st, content_list=service_content) |
| 177 |
|
| 178 |
log.info('Test dsidm service get with json') |
| 179 |
args.json = True |
| 180 |
get(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 181 |
check_value_in_log_and_reset(topology_st, content_list=json_content) |
| 182 |
|
| 183 |
|
| 184 |
#@pytest.mark.xfail(reason="Will fail because of bz1893667") |
| 185 |
@pytest.mark.skipif(ds_is_older("2.1.0"), reason="Not implemented") |
| 186 |
def test_dsidm_service_get_dn(topology_st, create_test_service): |
| 187 |
""" Test dsidm service get_dn option |
| 188 |
|
| 189 |
:id: 2e4c8f98-51e1-11ec-b472-3497f624ea11 |
| 190 |
:setup: Standalone instance |
| 191 |
:steps: |
| 192 |
1. Run dsidm service get_dn for created service |
| 193 |
2. Check the output content is correct |
| 194 |
:expectedresults: |
| 195 |
1. Success |
| 196 |
2. Success |
| 197 |
""" |
| 198 |
|
| 199 |
service_name = 'test_service' |
| 200 |
standalone = topology_st.standalone |
| 201 |
services = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 202 |
test_service = services.get(service_name) |
| 203 |
args = FakeArgs() |
| 204 |
args.dn = test_service.dn |
| 205 |
args.json = False |
| 206 |
|
| 207 |
log.info('Empty the log file to prevent false data to check about service') |
| 208 |
topology_st.logcap.flush() |
| 209 |
|
| 210 |
log.info('Test dsidm service get_dn without json') |
| 211 |
get_dn(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 212 |
check_value_in_log_and_reset(topology_st, content_list=service_name) |
| 213 |
|
| 214 |
log.info('Test dsidm service get_dn with json') |
| 215 |
args.json = True |
| 216 |
get_dn(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 217 |
check_value_in_log_and_reset(topology_st, content_list=service_name) |
| 218 |
|
| 219 |
|
| 220 |
@pytest.mark.skipif(ds_is_older("2.1.0"), reason="Not implemented") |
| 221 |
def test_dsidm_service_create(topology_st): |
| 222 |
""" Test dsidm service create option |
| 223 |
|
| 224 |
:id: 338efbc6-51e1-11ec-a83a-3497f624ea11 |
| 225 |
:setup: Standalone instance |
| 226 |
:steps: |
| 227 |
1. Run dsidm service create |
| 228 |
2. Check that a message is provided on creation |
| 229 |
3. Check that created service exists |
| 230 |
:expectedresults: |
| 231 |
1. Success |
| 232 |
2. Success |
| 233 |
3. Success |
| 234 |
""" |
| 235 |
|
| 236 |
standalone = topology_st.standalone |
| 237 |
service_name = 'new_service' |
| 238 |
output = f'Successfully created {service_name}' |
| 239 |
|
| 240 |
args = FakeArgs() |
| 241 |
args.cn = service_name |
| 242 |
args.description = service_name |
| 243 |
|
| 244 |
log.info('Test dsidm service create') |
| 245 |
create(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 246 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 247 |
|
| 248 |
log.info('Check that service is present') |
| 249 |
services = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 250 |
new_service = services.get(service_name) |
| 251 |
assert new_service.exists() |
| 252 |
|
| 253 |
log.info('Clean up for next test') |
| 254 |
new_service.delete() |
| 255 |
|
| 256 |
|
| 257 |
@pytest.mark.skipif(ds_is_older("2.1.0"), reason="Not implemented") |
| 258 |
def test_dsidm_service_delete(topology_st, create_test_service): |
| 259 |
""" Test dsidm service delete option |
| 260 |
|
| 261 |
:id: 3b382a96-51e1-11ec-a1c2-3497f624ea11 |
| 262 |
:setup: Standalone instance |
| 263 |
:steps: |
| 264 |
1. Run dsidm service delete on created service |
| 265 |
2. Check that a message is provided on deletion |
| 266 |
3. Check that service does not exist |
| 267 |
:expectedresults: |
| 268 |
1. Success |
| 269 |
2. Success |
| 270 |
3. Success |
| 271 |
""" |
| 272 |
|
| 273 |
standalone = topology_st.standalone |
| 274 |
services = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 275 |
test_service = services.get('test_service') |
| 276 |
output = f'Successfully deleted {test_service.dn}' |
| 277 |
|
| 278 |
args = FakeArgs() |
| 279 |
args.dn = test_service.dn |
| 280 |
|
| 281 |
log.info('Test dsidm service delete') |
| 282 |
delete(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False) |
| 283 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 284 |
|
| 285 |
log.info('Check that service does not exist') |
| 286 |
assert not test_service.exists() |
| 287 |
|
| 288 |
|
| 289 |
@pytest.mark.skipif(ds_is_older("2.1.0"), reason="Not implemented") |
| 290 |
def test_dsidm_service_modify(topology_st, create_test_service): |
| 291 |
""" Test dsidm service modify add, replace, delete option |
| 292 |
|
| 293 |
:id: 4023ef22-51e1-11ec-93c5-3497f624ea11 |
| 294 |
:setup: Standalone instance |
| 295 |
:steps: |
| 296 |
1. Run dsidm service modify replace description value |
| 297 |
2. Run dsidm service modify add seeAlso attribute to service |
| 298 |
3. Run dsidm service modify delete for seeAlso attribute |
| 299 |
:expectedresults: |
| 300 |
1. description value is replaced with new text |
| 301 |
2. seeAlso attribute is present |
| 302 |
3. seeAlso attribute is deleted |
| 303 |
""" |
| 304 |
|
| 305 |
standalone = topology_st.standalone |
| 306 |
services = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 307 |
test_service = services.get('test_service') |
| 308 |
output = f'Successfully modified {test_service.dn}' |
| 309 |
|
| 310 |
args = FakeArgs() |
| 311 |
args.selector = 'test_service' |
| 312 |
args.changes = ['replace:description:Test Service Modified'] |
| 313 |
|
| 314 |
log.info('Test dsidm service modify replace') |
| 315 |
modify(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False) |
| 316 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 317 |
|
| 318 |
log.info('Test dsidm service modify add') |
| 319 |
args.changes = [f'add:seeAlso:ou=services,{DEFAULT_SUFFIX}'] |
| 320 |
modify(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False) |
| 321 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 322 |
assert test_service.present('seeAlso', f'ou=services,{DEFAULT_SUFFIX}') |
| 323 |
|
| 324 |
log.info('Test dsidm service modify delete') |
| 325 |
args.changes = [f'delete:seeAlso:ou=services,{DEFAULT_SUFFIX}'] |
| 326 |
modify(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False) |
| 327 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 328 |
assert not test_service.present('seeAlso', f'ou=services,{DEFAULT_SUFFIX}') |
| 329 |
|
| 330 |
|
| 331 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented") |
| 332 |
def test_dsidm_service_rename_keep_old_rdn(topology_st, create_test_service): |
| 333 |
""" Test dsidm service rename option with keep-old-rdn |
| 334 |
|
| 335 |
:id: 44cc6b08-51e1-11ec-89e7-3497f624ea11 |
| 336 |
:setup: Standalone instance |
| 337 |
:steps: |
| 338 |
1. Run dsidm service rename option with keep-old-rdn |
| 339 |
2. Check the service does have another cn attribute with the old rdn |
| 340 |
3. Check the old service is deleted |
| 341 |
:expectedresults: |
| 342 |
1. Success |
| 343 |
2. Success |
| 344 |
3. Success |
| 345 |
""" |
| 346 |
|
| 347 |
standalone = topology_st.standalone |
| 348 |
services = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 349 |
test_service = services.get('test_service') |
| 350 |
|
| 351 |
args = FakeArgs() |
| 352 |
args.selector = test_service.rdn |
| 353 |
args.new_name = 'my_service' |
| 354 |
args.keep_old_rdn = True |
| 355 |
|
| 356 |
log.info('Test dsidm service rename') |
| 357 |
rename(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 358 |
my_service = services.get(args.new_name) |
| 359 |
output = f'Successfully renamed to {my_service.dn}' |
| 360 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 361 |
|
| 362 |
log.info('my_service should have cn attribute with the old rdn') |
| 363 |
assert my_service.present('cn', 'test_service') |
| 364 |
assert my_service.get_attr_val_utf8('cn') == 'test_service' |
| 365 |
assert my_service.get_attr_val_utf8('description') == 'Test Service' |
| 366 |
|
| 367 |
log.info('Old service dn should not exist') |
| 368 |
assert not test_service.exists() |
| 369 |
|
| 370 |
log.info('Clean up') |
| 371 |
my_service.delete() |
| 372 |
|
| 373 |
|
| 374 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented") |
| 375 |
def test_dsidm_service_rename(topology_st, create_test_service): |
| 376 |
""" Test dsidm service rename option |
| 377 |
|
| 378 |
:id: 4a13ea64-51e1-11ec-b3ff-3497f624ea11 |
| 379 |
:setup: Standalone instance |
| 380 |
:steps: |
| 381 |
1. Run dsidm service rename option on created service |
| 382 |
2. Check the service does not have another cn attribute with the old rdn |
| 383 |
3. Check the old service is deleted |
| 384 |
:expectedresults: |
| 385 |
1. Success |
| 386 |
2. Success |
| 387 |
3. Success |
| 388 |
""" |
| 389 |
|
| 390 |
standalone = topology_st.standalone |
| 391 |
services = ServiceAccounts(standalone, DEFAULT_SUFFIX) |
| 392 |
test_service = services.get('test_service') |
| 393 |
|
| 394 |
args = FakeArgs() |
| 395 |
args.selector = test_service.rdn |
| 396 |
args.new_name = 'my_service' |
| 397 |
args.keep_old_rdn = False |
| 398 |
|
| 399 |
log.info('Test dsidm service rename') |
| 400 |
args.new_name = 'my_service' |
| 401 |
rename(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args) |
| 402 |
my_service = services.get(args.new_name) |
| 403 |
output = f'Successfully renamed to {my_service.dn}' |
| 404 |
check_value_in_log_and_reset(topology_st, check_value=output) |
| 405 |
|
| 406 |
log.info('New service should not have cn attribute with the old rdn') |
| 407 |
assert not my_service.present('cn', 'test_service') |
| 408 |
assert my_service.get_attr_val_utf8('cn') == 'my_service' |
| 409 |
assert my_service.get_attr_val_utf8('description') == 'Test Service' |
| 410 |
|
| 411 |
log.info('Old service dn should not exist.') |
| 412 |
assert not test_service.exists() |
| 413 |
|
| 414 |
log.info('Clean up') |
| 415 |
my_service.delete() |
| 416 |
|
| 417 |
|
| 418 |
if __name__ == '__main__': |
| 419 |
# Run isolated |
| 420 |
# -s for DEBUG mode |
| 421 |
CURRENT_FILE = os.path.realpath(__file__) |
| 422 |
pytest.main("-s %s" % CURRENT_FILE) |