Skip to content

Commit

Permalink
gplazma2-ldap: add embedded jdap server for unit testing
Browse files Browse the repository at this point in the history
Motivation:
current ldap plugin requires running ldap server for unit tests. Worse,
the ldap server must contain a special configuration, which makes testing
not portable. THis change added an embedded ldap server with corresponding
ldif file and updates the test to use it.

Modification:
added dependency on ldap4testing module. Update gplazma plugin to match the
provided ldif file. Updated test to start and stop embedded server during testing.

Result:
more code tested

Acked-by: Gerd Behrmann
Target: master
Require-book: no
Require-notes: no
  • Loading branch information
kofemann committed Sep 11, 2016
1 parent 05f327a commit cc806ed
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 46 deletions.
18 changes: 3 additions & 15 deletions modules/gplazma2-ldap/pom.xml
Expand Up @@ -34,23 +34,11 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-unit</artifactId>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-integ</artifactId>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-core</artifactId>
<version>1.5.5</version>
<groupId>org.dcache</groupId>
<artifactId>ldap4testing</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
@@ -0,0 +1,64 @@
dn: o=dcache,c=org
dc: o=dcache
objectClass: top
objectClass: domain

dn: ou=people,o=dcache,c=org
ou: people
objectClass: top
objectclass: organizationalunit

dn: ou=group,o=dcache,c=org
ou: group
objectClass: top
objectclass: organizationalunit

dn: uid=kermit,ou=people,o=dcache,c=org
objectClass: posixAccount
objectClass: inetorgperson
objectClass: organizationalperson
objectClass: top
uid: kermit
description: This is the description for Kermit the Frog
userPassword: kermitTheFrog
employeeNumber: 1
initials: kTf
givenName: Kermit
cn: Kermit The Frog
sn: Frog
mail: kermit.the.frog@dcache.org
l: Hamburg
st: DE
gidNumber: 1000
homeDirectory: /home/kermit
uidNumber: 1000

dn: uid=bernd,ou=people,o=dcache,c=org
objectClass: posixAccount
objectClass: inetorgperson
objectClass: organizationalperson
objectClass: top
uid: bernd
description: This is the description for Bernd das Brot
userPassword: berndDasBrot
employeeNumber: 2
initials: BdB
givenName: Bernd
cn: Bernd das Brot
sn: Brot
mail: bernd.das.brot@dcache.org
l: Hamburg
st: DE
gidNumber: 1000
homeDirectory: /home/bernd
uidNumber: 1001


dn: cn=actor,ou=group,o=dcache,c=org
objectClass: groupOfUniqueNames
objectClass: posixGroup
objectClass: top
cn: actor
uniqueMember: uid=bernd,ou=people,o=dcache,c=org
uniqueMember: uid=kermit,ou=people,o=dcache,c=org
gidNumber: 1001
Expand Up @@ -13,40 +13,59 @@ import scala.collection.convert.WrapAsJava.setAsJavaSet
import org.dcache.auth.{GroupNamePrincipal, GidPrincipal, UidPrincipal, UserNamePrincipal}
import org.dcache.gplazma.NoSuchPrincipalException
import org.dcache.auth.attributes.{HomeDirectory, RootDirectory}


import org.dcache.ldap4testing.EmbeddedServer;
/**
* Tests for the gPlazma LDAP plugin.
*
* The tests are all ignored by default because they depend on DESY infrastructure.
*/
@RunWith(classOf[JUnitRunner]) @Ignore
class LdapPluginTest extends FlatSpec with Matchers {

val pluginProperties = {
val properties = new Properties
properties.put(Ldap.LDAP_URL, "ldap://wof-dav.desy.de:389/")
properties.put(Ldap.LDAP_ORG, "ou=NIS,o=DESY,c=DE")
properties.put(Ldap.LDAP_USER_FILTER, "(uid=%s)")
properties.put(Ldap.LDAP_PEOPLE_TREE, "People")
properties.put(Ldap.LDAP_GROUP_TREE, "Groups")
properties.put(Ldap.LDAP_USER_HOME, "/root")
properties.put(Ldap.LDAP_USER_ROOT, "/root%homeDirectory%/home")
properties.put(Ldap.LDAP_GROUP_MEMBER, "uniqueMember")
properties
@RunWith(classOf[JUnitRunner])
class LdapPluginTest extends FlatSpec with Matchers with BeforeAndAfter{

var server : EmbeddedServer = _
var ldapPlugin : Ldap = _

before {
val initialLdif = ClassLoader.getSystemResourceAsStream("org/dcache/gplazma/plugins/ldap/init.ldif")
server = new EmbeddedServer(0, initialLdif)
server.start()

val pluginProperties = {
val properties = new Properties
properties.put(Ldap.LDAP_URL, "ldap://localhost:" + server.getSocketAddress().getPort())
properties.put(Ldap.LDAP_ORG, "o=dcache,c=org")
properties.put(Ldap.LDAP_USER_FILTER, "(uid=%s)")
properties.put(Ldap.LDAP_PEOPLE_TREE, "people")
properties.put(Ldap.LDAP_GROUP_TREE, "group")
properties.put(Ldap.LDAP_USER_HOME, "%homeDirectory%")
properties.put(Ldap.LDAP_USER_ROOT, "/")
properties.put(Ldap.LDAP_GROUP_MEMBER, "uniqueMember")

properties.put(Ldap.LDAP_AUTH, "simple")
properties.put(Ldap.LDAP_BINDDN, "uid=kermit,ou=people,o=dcache,c=org")
properties.put(Ldap.LDAP_BINDPW, "kermitTheFrog")

properties
}

ldapPlugin = new Ldap(pluginProperties)

}

def ldapPlugin = new Ldap(pluginProperties)
after {
server.stop()
}

"map(Set[Principal])" should "return matching Uid and Gid Principals for an existent user name" in {
val principals = new util.HashSet[Principal]()
principals add new UserNamePrincipal("testuser")
principals add new UserNamePrincipal("kermit")

ldapPlugin.map(principals)
principals.size should be (3)
principals should contain (new UserNamePrincipal("testuser"))
principals should contain (new UidPrincipal("50999"))
principals should contain (new GidPrincipal("3752", true))
principals.size should be (4)
principals should contain (new UserNamePrincipal("kermit"))
principals should contain (new UidPrincipal("1000"))
principals should contain (new GidPrincipal("1000", true))
principals should contain (new GidPrincipal("1001", false))
}

it should "leave the principals set unchanged for a non existent user name" in {
Expand All @@ -59,7 +78,7 @@ class LdapPluginTest extends FlatSpec with Matchers {
}

"map(UserNamePrincipal)" should "return a UidPrincipal for an existing user name" in {
ldapPlugin.map(new UserNamePrincipal("testuser")) should be (new UidPrincipal("50999"))
ldapPlugin.map(new UserNamePrincipal("kermit")) should be (new UidPrincipal("1000"))
}

it should "throw a NoSuchPrincipalException if a user does not exist" in {
Expand All @@ -70,11 +89,11 @@ class LdapPluginTest extends FlatSpec with Matchers {
}

"reverseMap" should "return a Set containing a UserNamePrincipal for an existing Uid" in {
ldapPlugin.reverseMap(new UidPrincipal("50999")) should contain (new UserNamePrincipal("testuser"))
ldapPlugin.reverseMap(new UidPrincipal("1000")) should contain (new UserNamePrincipal("kermit"))
}

it should "return a serializable Set" in {
val set = ldapPlugin.reverseMap(new UidPrincipal("50999"))
val set = ldapPlugin.reverseMap(new UidPrincipal("1000"))
set.isInstanceOf[java.io.Serializable] should be (true)
}

Expand All @@ -86,23 +105,23 @@ class LdapPluginTest extends FlatSpec with Matchers {
}

it should "return a Set containing a GroupNamePrincipal for an existing Gid" in {
ldapPlugin.reverseMap(new GidPrincipal("3752", true)) should contain (new GroupNamePrincipal("htw-berlin"))
ldapPlugin.reverseMap(new GidPrincipal("1001", true)) should contain (new GroupNamePrincipal("actor"))
}

it should "throw a NoSuchPrincipalException for a non existent Gid" in {

intercept[NoSuchPrincipalException] {
ldapPlugin.reverseMap(new GidPrincipal("51000", true))
ldapPlugin.reverseMap(new GidPrincipal("1002", true))
}
}

"session" should "return the user's home and root directory, and the access rights" in {
val attr = new java.util.HashSet[AnyRef]()
ldapPlugin.session(setAsJavaSet(Set[Principal](new UserNamePrincipal("testuser"))), attr)
ldapPlugin.session(setAsJavaSet(Set[Principal](new UserNamePrincipal("bernd"))), attr)

attr should have size 3
attr should contain (new HomeDirectory("/root"))
attr should contain (new RootDirectory("/root/dcache-cloud/testuser/home"))
attr should have size 2
attr should contain (new HomeDirectory("/home/bernd"))
attr should contain (new RootDirectory("/"))
}

}
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -827,6 +827,11 @@
<artifactId>rados4j</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>org.dcache</groupId>
<artifactId>ldap4testing</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit cc806ed

Please sign in to comment.