From b922b37c3f8f6b12d183da3a9f8331e4c79dab6d Mon Sep 17 00:00:00 2001 From: Tim Vernum Date: Thu, 4 May 2017 20:13:56 +1000 Subject: [PATCH] [TEST] Fixes for randomised failures - role_mapping.native is always present, but contains no entries if the security index is unavailable - file based role mapping does not allow duplicate keys Original commit: elastic/x-pack-elasticsearch@734cf7e2c0997fec126118a7eee0d590d1c3b674 --- .../ldap/AbstractAdLdapRealmTestCase.java | 20 +++++++++++++++++++ .../ldap/MultipleAdRealmTests.java | 7 +++++-- .../security/SecurityFeatureSetTests.java | 5 ++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/integration/ldap/AbstractAdLdapRealmTestCase.java b/plugin/src/test/java/org/elasticsearch/integration/ldap/AbstractAdLdapRealmTestCase.java index e881f2832b2..26750684ee3 100644 --- a/plugin/src/test/java/org/elasticsearch/integration/ldap/AbstractAdLdapRealmTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/integration/ldap/AbstractAdLdapRealmTestCase.java @@ -323,6 +323,26 @@ public abstract class AbstractAdLdapRealmTestCase extends SecurityIntegTestCase } } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + final RoleMappingEntry that = (RoleMappingEntry) o; + return Objects.equals(this.fileContent, that.fileContent) + && Objects.equals(this.nativeContent, that.nativeContent); + } + + @Override + public int hashCode() { + int result = Objects.hashCode(fileContent); + result = 31 * result + Objects.hashCode(nativeContent); + return result; + } } /** diff --git a/plugin/src/test/java/org/elasticsearch/integration/ldap/MultipleAdRealmTests.java b/plugin/src/test/java/org/elasticsearch/integration/ldap/MultipleAdRealmTests.java index dc4eb9b09e5..dd392d18b05 100644 --- a/plugin/src/test/java/org/elasticsearch/integration/ldap/MultipleAdRealmTests.java +++ b/plugin/src/test/java/org/elasticsearch/integration/ldap/MultipleAdRealmTests.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.common.settings.Settings; @@ -40,8 +41,10 @@ public class MultipleAdRealmTests extends AbstractAdLdapRealmTestCase { // It's easier to test 2 realms when using file based role mapping, and for the purposes of // this test, there's no need to test native mappings. - AbstractAdLdapRealmTestCase.roleMappings = new ArrayList<>(realmConfig.selectRoleMappings(() -> true)); - AbstractAdLdapRealmTestCase.roleMappings.addAll(secondaryRealmConfig.selectRoleMappings(() -> true)); + AbstractAdLdapRealmTestCase.roleMappings = Stream.concat( + realmConfig.selectRoleMappings(() -> true).stream(), + secondaryRealmConfig.selectRoleMappings(() -> true).stream() + ).distinct().collect(Collectors.toList()); } @Override diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityFeatureSetTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityFeatureSetTests.java index 64b8aae7528..3d386c4814c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityFeatureSetTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityFeatureSetTests.java @@ -40,6 +40,8 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.core.Is.is; @@ -237,7 +239,8 @@ public class SecurityFeatureSetTests extends ESTestCase { assertThat(source.getValue("role_mapping.native.size"), is(12)); assertThat(source.getValue("role_mapping.native.enabled"), is(10)); } else { - assertThat(((Map) source.getValue("role_mapping")).isEmpty(), is(true)); + final Map roleMapping = source.getValue("role_mapping.native"); + assertThat(roleMapping.entrySet(), emptyIterable()); } // system key