From 73db6c927e0cb3a1b79d9ad46e787c58c2313317 Mon Sep 17 00:00:00 2001
From: Jan Rusnacko <jrusnack@redhat.com>
Date: Wed, 30 Oct 2013 16:02:03 +0100
Subject: [PATCH 2/4] Adding methods to repl module.

This commit introduces multiple methods into repl module.
---
 lib389/dsmodules/repl.py          |   57 +++++++++++++++++++++++++++++
 tests/test_dsmodules/test_repl.py |   71 +++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+), 0 deletions(-)
 create mode 100644 lib389/dsmodules/repl.py
 create mode 100644 tests/test_dsmodules/test_repl.py

diff --git a/lib389/dsmodules/repl.py b/lib389/dsmodules/repl.py
new file mode 100644
index 0000000..f1b8899
--- /dev/null
+++ b/lib389/dsmodules/repl.py
@@ -0,0 +1,57 @@
+
+def add_repl_manager(ds, replication_manager_dn, replication_manager_pw):
+    input = """
+        dn: {dn}
+        objectClass: inetorgperson
+        objectClass: person
+        objectClass: top
+        cn: replication manager
+        sn: RM
+        userPassword: {pw}
+        passwordExpirationTime: 20380119031407Z
+        nsIdleTimeout: 0
+    """.format(dn=replication_manager_dn, pw=replication_manager_pw)
+    ds.ldapadd_r(input)
+    ds.repl_manager = replication_manager_dn
+    ds.repl_manager_pw = replication_manager_pw
+
+def enable_changelog(ds, changelog_location, changelogmaxage="10dc"):
+    input = """
+        dn: cn=changelog5,cn=config
+        objectclass: top
+        objectclass: extensibleObject
+        cn: changelog5
+        nsslapd-changelogdir: {dir}
+        nsslapd-changelogmaxage: {maxage}
+    """.format(dir=changelog_location, maxage=changelogmaxage)
+    ds.ldapadd_r(input)
+
+def disable_changelog(ds):
+    ds.ldapdelete_r("cn=changelog5,cn=config")
+    
+def enable_supplier(ds, suffix, id):
+    raise NotImplementedError
+
+def enable_consumer(ds, suffix, id):
+    raise NotImplementedError
+
+def enable_hub(ds, suffix, id):
+    raise NotImplementedError
+
+def enable_replica(ds, suffix, id, type, flags):
+    raise NotImplementedError
+
+def disable_replica(ds, suffix):
+    raise NotImplementedError
+
+def add_agreement(ds, suffix, consumer, name, 
+    bind_method="SIMPLE",
+    transport_info="LDAP",
+    schedule="0000-2359 0123456"):
+    raise NotImplementedError
+
+def remove_agreement(ds, suffix, name):
+    raise NotImplementedError
+
+def begin_refresh(ds, suffix, name):
+    raise NotImplementedError
diff --git a/tests/test_dsmodules/test_repl.py b/tests/test_dsmodules/test_repl.py
new file mode 100644
index 0000000..676129c
--- /dev/null
+++ b/tests/test_dsmodules/test_repl.py
@@ -0,0 +1,71 @@
+
+import pytest
+
+def test_add_repl_manager(mock_ds):
+    mock_ds.repl.add_repl_manager("cn=replication manager, cn=config", "Secret123")
+    assert mock_ds.dit["cn=replication manager, cn=config"]["userPassword"] == "Secret123"
+    assert mock_ds.dit["cn=replication manager, cn=config"]["nsIdleTimeout"] == "0"
+    assert mock_ds.dit["cn=replication manager, cn=config"]["cn"] == "replication manager"
+    assert mock_ds.repl_manager == "cn=replication manager, cn=config"
+    assert mock_ds.repl_manager_pw == "Secret123"
+
+def test_changelog(mock_ds):
+    # Test enable_changelog with default value for changelogmaxage
+    mock_ds.repl.enable_changelog("/path/to/my/changelog")
+    assert mock_ds.dit["cn=changelog5,cn=config"]["nsslapd-changelogdir"] == "/path/to/my/changelog"
+    assert mock_ds.dit["cn=changelog5,cn=config"]["nsslapd-changelogmaxage"] == "10dc"
+
+    # Test disable_changelog - verify that entry cn=changelog5,cn=config was deleted
+    mock_ds.repl.disable_changelog()
+    with pytest.raises(KeyError):
+        mock_ds.dit["cn=changelog5,cn=config"]
+
+    # Test enable_changelog with custom value for changelogmaxage
+    mock_ds.repl.enable_changelog("/path/to/my/other/changelog", changelogmaxage="11dc")
+    assert mock_ds.dit["cn=changelog5,cn=config"]["nsslapd-changelogdir"] == "/path/to/my/other/changelog"
+    assert mock_ds.dit["cn=changelog5,cn=config"]["nsslapd-changelogmaxage"] == "11dc"
+
+def test_enable_supplier(mock_ds):
+    mock_ds.enable_supplier("dc=example,dc=com", 2)
+
+    # Assert that replica entry has objectclass nsds5replica
+    assert nsds5replica in mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["objectclass"]
+
+    # Verify all important attributes of replica entry
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaid"] == 2
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicatype"] == 3
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5flags"] == 1
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaroot"] == "dc=example,dc=com"
+
+def test_enable_consumer(mock_ds):
+    mock_ds.enable_consumer("dc=example,dc=com", 1)
+    assert nsds5replica in mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["objectclass"]
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaid"] == 1
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicatype"] == 2
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5flags"] == 0
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaroot"] == "dc=example,dc=com"
+
+def test_enable_hub(mock_ds):
+    mock_ds.enable_hub("dc=example,dc=com", 3)
+    assert nsds5replica in mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["objectclass"]
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaid"] == 3
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicatype"] == 2
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5flags"] == 1
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaroot"] == "dc=example,dc=com"
+
+def test_enable_replica(mock_ds):
+    mock_ds.enable_replica("dc=example,dc=com", 2, 3, 1)
+    assert nsds5replica in mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["objectclass"]
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaid"] == 2
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicatype"] == 3
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5flags"] == 1
+    assert mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["nsds5replicaroot"] == "dc=example,dc=com"
+
+
+def test_disable_replica(mock_ds):
+    mock_ds.enable_replica("dc=example,dc=com", 2, 3, 1)
+    assert nsds5replica in mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]["objectclass"]
+
+    mock_ds.disable_replica("dc=example,dc=com")
+    with pytest.raises(KeyError):
+        mock_ds.dit["cn=replica,cn=dc=example,dc=com,cn=mapping tree,cn=config"]
\ No newline at end of file
-- 
1.7.1

