From 94627699de39fd1c73730cf505e19329153f031e Mon Sep 17 00:00:00 2001 From: Soisik Froger Date: Tue, 4 Jan 2022 09:17:18 +0100 Subject: [PATCH] Fix #12 Do not delete entry if get script returns an error --- .../AbstractExecutableLdifService.java | 8 +- .../executable/Ldap2ExecutableSyncTest.java | 10 ++ src/test/resources/etc/lsc.xml | 131 ++++++++++++++++++ .../executable/get-bad-return-code.sh | 24 ++++ 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100755 src/test/resources/org/lsc/plugins/connectors/executable/get-bad-return-code.sh diff --git a/src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java b/src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java index 8b7526d..98c77a2 100755 --- a/src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java +++ b/src/main/java/org/lsc/plugins/connectors/executable/AbstractExecutableLdifService.java @@ -97,9 +97,13 @@ public int execute(String[] runtime, String[] env, String input) { return execute(runtime, env, input, datas); } - public String executeWithReturn(String[] runtime, String[] env, String input) { + public String executeWithReturn(String[] runtime, String[] env, String input) throws LscServiceException { StringBuffer datas = new StringBuffer(); - execute(runtime, env, input, datas); + int exitValue = execute(runtime, env, input, datas); + if (exitValue != 0) { + String message = "Process returned a non zero exit code, exit code=" + exitValue; + throw new LscServiceException(message, new RuntimeException(message)); + } return datas.toString(); } diff --git a/src/test/java/org/lsc/plugins/connectors/executable/Ldap2ExecutableSyncTest.java b/src/test/java/org/lsc/plugins/connectors/executable/Ldap2ExecutableSyncTest.java index 402504a..7226579 100644 --- a/src/test/java/org/lsc/plugins/connectors/executable/Ldap2ExecutableSyncTest.java +++ b/src/test/java/org/lsc/plugins/connectors/executable/Ldap2ExecutableSyncTest.java @@ -317,6 +317,16 @@ public final void testCleanLdap2Ldap() throws Exception { assertFalse(dstJndiServices.exists(DN_DELETE_DST)); } + public final void testFailOnBadExitCode() throws Exception { + // Clean should fail + SimpleSynchronize sync = new SimpleSynchronize(); + List cleanType = new ArrayList(); + cleanType.add("failOnErrorTestTask"); + sync.setThreads(1); + boolean ret = sync.launch(new ArrayList(), new ArrayList(), cleanType); + assertFalse(ret); + + } private void launchSyncCleanTask(String taskName, boolean doSync, boolean doClean) throws Exception { // initialize required stuff diff --git a/src/test/resources/etc/lsc.xml b/src/test/resources/etc/lsc.xml index 198841b..e63b332 100644 --- a/src/test/resources/etc/lsc.xml +++ b/src/test/resources/etc/lsc.xml @@ -29,6 +29,12 @@ com.sun.jndi.ldap.LdapCtxFactory false + + executable + fake + fake + fake + @@ -159,5 +165,130 @@ + + failOnErrorTestTask + org.lsc.beans.SimpleBean + + failOnErrorTestTask-src-service + + + failOnErrorTestTask-src-service-exec + + src/test/resources/org/lsc/plugins/connectors/executable/list.sh + src/test/resources/org/lsc/plugins/connectors/executable/get-bad-return-code.sh + + LDAP_BIND_DNcn=Directory Manager + LDAP_BIND_PWsecret + LDAP_URLldap://localhost:33389 + LDAP_BASEou=ldap2ldap2TestTaskSrc,ou=Test Data,dc=lsc-project,dc=org + LDAP_SCOPEsub + + + + + failOnErrorTestTask-dst-service + + + failOnErrorTestTask-dst-service-exec + + ou=ldap2ldap2TestTaskDst,ou=Test Data,dc=lsc-project,dc=org + + sn + + + description + cn + sn + userPassword + objectClass + uid + mail + telephoneNumber + seeAlso + + (objectClass=person) + (&(objectClass=person)(sn={sn})) + src/test/resources/org/lsc/plugins/connectors/executable/add.sh + src/test/resources/org/lsc/plugins/connectors/executable/update.sh + src/test/resources/org/lsc/plugins/connectors/executable/delete.sh + src/test/resources/org/lsc/plugins/connectors/executable/rename.sh + + LDAP_BIND_DNcn=Directory Manager + LDAP_BIND_PWsecret + LDAP_URLldap://localhost:33389 + + + + + js:"cn=" + srcBean.getDatasetFirstValueById("cn") + ",ou=ldap2ldap2TestTaskDst,ou=Test Data,dc=lsc-project,dc=org" + , + KEEP + + telephoneNumber + MERGE + + "123456" + "789987" + + + "000000" + "11111" + + + + objectClass + KEEP + + "inetOrgPerson" + "organizationalPerson" + "person" + "top" + + + + initials + FORCE + + "cn=oops" + + + + default + FORCE + + + description + MERGE + + srcBean.getDatasetFirstValueById("userPassword") + + + + seeAlso + FORCE + + + userPassword + FORCE + + + "secret" + srcBean.getDatasetFirstValueById("cn") + + + + + mail + FORCE + + + + "ok@domain.net" + + + + diff --git a/src/test/resources/org/lsc/plugins/connectors/executable/get-bad-return-code.sh b/src/test/resources/org/lsc/plugins/connectors/executable/get-bad-return-code.sh new file mode 100755 index 0000000..c8130be --- /dev/null +++ b/src/test/resources/org/lsc/plugins/connectors/executable/get-bad-return-code.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +line="" +read line +text="$line" +filter="(&(`echo "$line" | sed -e "s/^\([a-zA-Z0-9]\+\): \(.*\)$/\1=\2/g"`)" + +while test "$line" != "" +do + read line + text="$text +$line" + if test "$line" != ""; then + filter="$filter(`echo "$line" | sed -e "s/^\([a-zA-Z0-9]\+\): \(.*\)$/\1=\2/g"`)" + fi +done + +filter="$filter)" + +echo "Getting user information for id=$1" 1>&2 +ldapsearch -x -LLL -H "$LDAP_URL" -D "$LDAP_BIND_DN" -w "$LDAP_BIND_PW" -b "$LDAP_BASE" -s "$LDAP_SCOPE" "$filter" \ + | sed -e "s/^\(dn: .*\),dc=lsc-project, *dc=org$/\1/g" + +exit 1 \ No newline at end of file