Skip to content

Commit

Permalink
Testing adding a ERROR message in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kimettog committed Feb 8, 2024
1 parent 2afb0e3 commit f9bc320
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 8 deletions.
135 changes: 135 additions & 0 deletions dirsrvtests/tests/suites/import/import_warning_error_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# --- BEGIN COPYRIGHT BLOCK ---
# Copyright (C) 2024 Red Hat, Inc.
# All rights reserved.
#
# License: GPL (version 3 or any later version).
# See LICENSE for details.
# --- END COPYRIGHT BLOCK ---
#

import pytest

from lib389.utils import *
from lib389.topologies import topology_st
from lib389.cli_conf.backend import *
from lib389.cli_base import FakeArgs
from lib389._constants import DEFAULT_SUFFIX

pytestmark = pytest.mark.tier1


DEBUGGING = os.getenv("DEBUGGING", default=False)
if DEBUGGING:
logging.getLogger(__name__).setLevel(logging.DEBUG)
else:
logging.getLogger(__name__).setLevel(logging.INFO)
log = logging.getLogger(__name__)


def create_example_ldif(topology_st):
ldif_dir = topology_st.standalone.get_ldif_dir()
line1 = """version: 1
# entry-id: 1
dn: dc=example,dc=com
dn: dc=example,dc=com
nsUniqueId: e5c4172a-97aa11eb-aaa8e47e-b1e12808
nsUniqueId: e5c4172a-97aa11eb-aaa8e47e-b1e12808
objectClass: top
objectClass: domain
dc: example
description: dc=example,dc=com
creatorsName: cn=Directory Manager
modifiersName: cn=Directory Manager
createTimestamp: 20210407140942Z
modifyTimestamp: 20210407140942Z
aci: (targetattr="dc || description || objectClass")(targetfilter="(objectClas
s=domain)")(version 3.0; acl "Enable anyone domain read"; allow (read, search
, compare)(userdn="ldap:///anyone");)
# entry-id: 3
dn: uid=demo,ou=People,dc=example,dc=com
dn: uid=demo,ou=People,dc=example,dc=com
objectClass: person
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: posixAccount
objectClass: top
uidNumber: 1119
gidNumber: 1000
#nsUniqueId: e5c4172a-97aa11eb-aaa8e47e-b1e12808
nsUniqueId: 9a0e6603-a1cb11eb-aa2daeeb-95660ab0
creatorsName:
modifiersName: cn=directory manager
createTimestamp: 20210420112927Z
modifyTimestamp: 20210420113016Z
passwordGraceUserTime: 0
cn: demo
homeDirectory: /home/demo
uid: demo
sn: demo
"""
with open(f'{ldif_dir}/warning_parent.ldif', 'w') as out:
out.write(f'{line1}')
os.chmod(out.name, 0o777)
out.close()
import_ldif1 = ldif_dir + '/warning_parent.ldif'
return import_ldif1

def test_import_warning(topology_st):
"""Import ldif file with duplicate DNs Unique ID entries to generate a warning message
:id: 80b0d0c8-8498-11ee-b0fa-e40d365eed59
:setup: Standalone Instance
:steps:
1. Create LDIF file with Duplicate DN entries
2. Import the LDIF file with backend import
3. Check the topology logs
4. Check errors log for Duplicate DN entry message
5. Check errors log for Duplicate Unique ID entry message
:expectedresults:
1. Success
2. Success
3. Result message should contain warning code
4. Errors log should contain Duplicate DN entry message
5. Errors log should contain Duplicate Unique ID entry message
"""

standalone = topology_st.standalone
message = 'The import task has finished successfully, with warning code 8, check the logs for more detail'

args = FakeArgs()
args.be_name = 'userRoot'
args.ldifs = [create_example_ldif(topology_st)]
args.chunks_size = None
args.encrypted = False
args.gen_uniq_id = None
args.only_core = False
args.include_suffixes = 'dc=example,dc=com'
args.exclude_suffixes = None
args.timeout = 0
dn_test = "dc=example,dc=com"
dn_people_test = "uid=demo,ou=People,dc=example,dc=com"
uid_test = "e5c4172a-97aa11eb-aaa8e47e-b1e12808"
exp_msg_dup_dn = f"Entry has multiple dns \"{dn_test}\" and \"{dn_test}\" (second ignored)\n"
exp_msg_dup_uid = f"Entry has multiple uniqueids {uid_test} and {uid_test} (second ignored)\n"
exp_msg_dup_ou_dn =f"Entry has multiple dns \"{dn_people_test}\" and \"{dn_people_test}\" (second ignored)\n"
log.info('Import the LDIF file')
backend_import(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)

log.info('Check logs for a warning message')
assert topology_st.logcap.contains(message)
str2_dup_err_warn_log_lst = ([warn_log for warn_log in (standalone.ds_error_log.readlines()) if "str2entry_dupcheck" in warn_log])
stripped_err_warn_lst = set ([s[69:] for s in str2_dup_err_warn_log_lst])
_lst = [exp_msg_dup_dn,exp_msg_dup_uid,exp_msg_dup_ou_dn]
for msg in _lst:
log.info(f"Checking for {msg} warning in Logs")
assert msg in stripped_err_warn_lst,(f"Test Failed - Unable to find {msg}.")
log.info(f"Found warning {msg}")

if __name__ == '__main__':
# Run isolated
# -s for DEBUG mode
CURRENT_FILE = os.path.realpath(__file__)
pytest.main("-s %s" % CURRENT_FILE)
28 changes: 20 additions & 8 deletions ldap/servers/slapd/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
slapi_log_err(SLAPI_LOG_WARNING, "str2entry_dupcheck",
"Entry (%s), ignoring invalid line \"%s\"...\n",
rawdn ? (char *)rawdn : "", s);
slapi_log_err("This is line 809");

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (GCC)

macro "slapi_log_err" requires 3 arguments, but only 1 given

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (GCC)

'slapi_log_err' undeclared (first use in this function); did you mean 'slapi_log_error'?

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (GCC Strict)

macro "slapi_log_err" requires 3 arguments, but only 1 given

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (GCC Strict)

'slapi_log_err' undeclared (first use in this function); did you mean 'slapi_log_error'?

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (Clang -Weverything)

too few arguments provided to function-like macro invocation

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (Clang -Weverything)

use of undeclared identifier 'slapi_log_err'; did you mean 'slapi_log_error'?

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (Clang)

too few arguments provided to function-like macro invocation

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (Clang)

use of undeclared identifier 'slapi_log_err'; did you mean 'slapi_log_error'?

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (GCC Static Analyzer)

macro "slapi_log_err" requires 3 arguments, but only 1 given

Check failure on line 809 in ldap/servers/slapd/entry.c

View workflow job for this annotation

GitHub Actions / compile (GCC Static Analyzer)

'slapi_log_err' undeclared (first use in this function); did you mean 'slapi_log_error'?
continue;
}
type = bvtype.bv_val;
Expand Down Expand Up @@ -841,8 +842,9 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
} else {
normdn = slapi_create_dn_string("%s", rawdn);
if (NULL == normdn) {
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck",
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"Invalid DN: %s\n", (char *)rawdn);
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 847");
slapi_entry_free(e);
if (freeval)
slapi_ch_free_string(&bvvalue.bv_val);
Expand All @@ -865,7 +867,8 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
} else {
normdn = slapi_create_dn_string("%s", rawdn);
if (NULL == normdn) {
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck",
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 870");
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"Invalid DN: %s\n", (char *)rawdn);
slapi_entry_free(e);
if (freeval)
Expand All @@ -887,7 +890,8 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
if (strcasecmp(type, "dn") == 0) {
if (slapi_entry_get_dn_const(e) != NULL) {
char ebuf[BUFSIZ];
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck"
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 983");
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck",
"Entry has multiple dns \"%s\" and \"%s\" (second ignored)\n",
(char *)slapi_entry_get_dn_const(e),
escape_string(valuecharptr, ebuf));
Expand All @@ -897,8 +901,9 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
continue;
}
normdn = slapi_create_dn_string("%s", valuecharptr);
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 904");
if (NULL == normdn) {
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck",
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"Invalid DN: %s\n", valuecharptr);
slapi_entry_free(e);
e = NULL;
Expand Down Expand Up @@ -935,7 +940,8 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
/* retrieve uniqueid */
if ((bvtype.bv_len == SLAPI_ATTR_UNIQUEID_LENGTH) && (PL_strcasecmp(type, SLAPI_ATTR_UNIQUEID) == 0)) {
if (e->e_uniqueid != NULL) {
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck"
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 943");
slapi_log_err(SLAPI_LOG_WARNING, "str2entry_dupcheck",
"Entry has multiple uniqueids %s and %s (second ignored)\n",
e->e_uniqueid, valuecharptr, 0);
} else {
Expand Down Expand Up @@ -1047,8 +1053,9 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
if (strict) {
/* check that the dn is formatted correctly */
rc = slapi_dn_syntax_check(NULL, valuecharptr, 1);
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 1056");
if (rc) { /* syntax check failed */
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck"
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"strict: Invalid DN value: %s: %s\n",
type, valuecharptr);
slapi_entry_free(e);
Expand Down Expand Up @@ -1105,6 +1112,7 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
csn_free(&attributedeletioncsn);
} else {
/* Failure adding to value tree */
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 1115");
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"Unexpected failure %d adding value\n", rc);
slapi_value_free(&value); /* value not consumed - free it */
Expand All @@ -1119,6 +1127,7 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein

/* All done with parsing. Now create the entry. */
/* check to make sure there was a dn: line */
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 1130");
if (slapi_entry_get_dn_const(e) == NULL) {
if (!(SLAPI_STR2ENTRY_INCLUDE_VERSION_STR & flags))
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck", "Entry has no dn\n");
Expand All @@ -1138,12 +1147,14 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
*/
for (i = 0; i < nattrs; i++) {
sa = &attrs[i];
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 1150");
if (sa->sa_numdups > 0) {
if (sa->sa_numdups > 1) {
slapi_log_err(SLAPI_LOG_WARNING, "str2entry_dupcheck", "%d duplicate values for attribute "
"type %s detected in entry %s. Extra values ignored.\n",
sa->sa_numdups, sa->sa_type, slapi_entry_get_dn_const(e));
} else {
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 1157");
slapi_log_err(SLAPI_LOG_WARNING, "str2entry_dupcheck", "Duplicate value for attribute "
"type %s detected in entry %s. Extra value ignored.\n",
sa->sa_type, slapi_entry_get_dn_const(e));
Expand Down Expand Up @@ -1196,8 +1207,9 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
/* If this is a tombstone, it requires a special treatment for rdn. */
if (e->e_flags & SLAPI_ENTRY_FLAG_TOMBSTONE) {
/* tombstone */
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck","This is line 1210");
if (_entry_set_tombstone_rdn(e, slapi_entry_get_dn_const(e))) {
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck",
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"tombstone entry has badly formatted dn: %s\n",
slapi_entry_get_dn_const(e));
slapi_entry_free(e);
Expand All @@ -1209,7 +1221,7 @@ str2entry_dupcheck(const char *rawdn, const char *s, int flags, int read_statein
/* Add the RDN values, if asked, and if not already present */
if (flags & SLAPI_STR2ENTRY_ADDRDNVALS) {
if (slapi_entry_add_rdn_values(e) != LDAP_SUCCESS) {
slapi_log_err(SLAPI_LOG_TRACE, "str2entry_dupcheck",
slapi_log_err(SLAPI_LOG_ERR, "str2entry_dupcheck",
"Entry has badly formatted dn\n");
slapi_entry_free(e);
e = NULL;
Expand Down

0 comments on commit f9bc320

Please sign in to comment.