[SCM] Samba Shared Repository - branch master updated

Andrew Tridgell tridge at samba.org
Wed Oct 19 21:52:03 MDT 2011


The branch, master has been updated
       via  8890f70 s4-test: added test suite for common.py code
       via  4f08a29 s4-dsdb: moved dsdb_Dn() into common.py
       via  43e5592 subunitrun: give more useful help
      from  019f643 Fix a boatload of warnings in the examples.

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 8890f709d9c19b515b1bd0b0232b7eddc8ebd7c4
Author: Andrew Tridgell <tridge at samba.org>
Date:   Thu Oct 20 13:25:22 2011 +1100

    s4-test: added test suite for common.py code
    
    this tests the dsdb_Dn() class
    
    Pair-Programmed-With: Amitay Isaacs <amitay at gmail.com>
    
    Autobuild-User: Andrew Tridgell <tridge at samba.org>
    Autobuild-Date: Thu Oct 20 05:51:28 CEST 2011 on sn-devel-104

commit 4f08a2951a04ba75fdf7b784dcf84059537d7b4c
Author: Andrew Tridgell <tridge at samba.org>
Date:   Thu Oct 20 13:24:45 2011 +1100

    s4-dsdb: moved dsdb_Dn() into common.py
    
    this gives a method for dealing with binary DNs from python
    
    Pair-Programmed-With: Amitay Isaacs <amitay at gmail.com>

commit 43e5592e9af95f2f2522cf8b015a76b14475729d
Author: Andrew Tridgell <tridge at samba.org>
Date:   Thu Oct 20 13:24:04 2011 +1100

    subunitrun: give more useful help
    
    give some examples and more useful description for subunitrun command
    
    Pair-Programmed-With: Amitay Isaacs <amitay at gmail.com>

-----------------------------------------------------------------------

Summary of changes:
 source4/scripting/bin/subunitrun               |   27 ++++++++++++++-
 source4/scripting/python/samba/common.py       |   40 ++++++++++++++++++++++
 source4/scripting/python/samba/dbchecker.py    |   29 ++---------------
 source4/scripting/python/samba/tests/common.py |   42 ++++++++++++++++++++++++
 4 files changed, 110 insertions(+), 28 deletions(-)
 create mode 100644 source4/scripting/python/samba/tests/common.py


Changeset truncated at 500 lines:

diff --git a/source4/scripting/bin/subunitrun b/source4/scripting/bin/subunitrun
index bc7b42c..9c87d2e 100755
--- a/source4/scripting/bin/subunitrun
+++ b/source4/scripting/bin/subunitrun
@@ -31,9 +31,31 @@ import samba.getopt as options
 import samba.tests
 
 
-parser = optparse.OptionParser("subunitrun [options] <tests>")
+usage = 'subunitrun [options] <tests>'
+description = '''
+This runs a Samba python test suite. The tests are typically located in
+source4/scripting/python/samba/tests/*.py
+
+To run the tests from one of those modules, specify the test as
+samba.tests.MODULE. For example, to run the tests in common.py:
+
+   subunitrun samba.tests.common
+
+To list the tests in that module, use:
+
+   subunitrun -l samba.tests.common
+'''
+
+def format_description(formatter):
+    '''hack to prevent textwrap of the description'''
+    return description
+
+parser = optparse.OptionParser(usage=usage, description=description)
+parser.format_description = format_description
 credopts = options.CredentialsOptions(parser)
+sambaopts = options.SambaOptions(parser)
 parser.add_option_group(credopts)
+parser.add_option_group(sambaopts)
 try:
     from subunit.run import TestProgram
 except ImportError:
@@ -45,7 +67,8 @@ else:
 
 opts, args = parser.parse_args()
 
-samba.tests.cmdline_credentials = credopts.get_credentials(samba.tests.env_loadparm())
+lp = sambaopts.get_loadparm()
+samba.tests.cmdline_credentials = credopts.get_credentials(lp)
 if getattr(opts, "listtests", False):
     args.insert(0, "--list")
 
diff --git a/source4/scripting/python/samba/common.py b/source4/scripting/python/samba/common.py
index 5a9fae1..9738966 100644
--- a/source4/scripting/python/samba/common.py
+++ b/source4/scripting/python/samba/common.py
@@ -18,6 +18,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+import ldb, dsdb
+
 def confirm(msg, forced = False, allow_all=False):
     """confirm an action with the user
         :param msg: A string to print to the user
@@ -55,3 +57,41 @@ def normalise_int32(ivalue):
     if int(ivalue) & 0x80000000 and int(ivalue) > 0:
         return str(int(ivalue) - 0x100000000)
     return str(ivalue)
+
+
+class dsdb_Dn(object):
+    '''a class for binary DN'''
+
+    def __init__(self, samdb, dnstring, syntax_oid=None):
+        '''create a dsdb_Dn'''
+        if syntax_oid is None:
+            # auto-detect based on string
+            if dnstring.startswith("B:"):
+                syntax_oid = dsdb.DSDB_SYNTAX_BINARY_DN
+            elif dnstring.startswith("S:"):
+                syntax_oid = dsdb.DSDB_SYNTAX_STRING_DN
+            else:
+                syntax_oid = dsdb.DSDB_SYNTAX_OR_NAME
+        if syntax_oid in [ dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_STRING_DN ]:
+            # it is a binary DN
+            colons = dnstring.split(':')
+            if len(colons) < 4:
+                raise RuntimeError("Invalid DN %s" % dnstring)
+            prefix_len = 4 + len(colons[1]) + int(colons[1])
+            self.prefix = dnstring[0:prefix_len]
+            self.binary = self.prefix[4:-1]
+            self.dnstring = dnstring[prefix_len:]
+        else:
+            self.dnstring = dnstring
+            self.prefix = ''
+            self.binary = ''
+        self.dn = ldb.Dn(samdb, self.dnstring)
+
+    def __str__(self):
+        return self.prefix + str(self.dn.extended_str(mode=1))
+
+    def get_binary_integer(self):
+        '''return binary part of a dsdb_Dn as an integer, or None'''
+        if self.prefix == '':
+            return None
+        return int(self.binary, 16)
diff --git a/source4/scripting/python/samba/dbchecker.py b/source4/scripting/python/samba/dbchecker.py
index 8120307..7d85adf 100644
--- a/source4/scripting/python/samba/dbchecker.py
+++ b/source4/scripting/python/samba/dbchecker.py
@@ -25,30 +25,7 @@ from samba import common
 from samba.dcerpc import misc
 from samba.ndr import ndr_unpack
 from samba.dcerpc import drsblobs
-
-
-class dsdb_DN(object):
-    '''a class to manipulate DN components'''
-
-    def __init__(self, samdb, dnstring, syntax_oid):
-        if syntax_oid in [ dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_STRING_DN ]:
-            colons = dnstring.split(':')
-            if len(colons) < 4:
-                raise Exception("invalid DN prefix")
-            prefix_len = 4 + len(colons[1]) + int(colons[1])
-            self.prefix = dnstring[0:prefix_len]
-            self.dnstring = dnstring[prefix_len:]
-        else:
-            self.dnstring = dnstring
-            self.prefix = ''
-        try:
-            self.dn = ldb.Dn(samdb, self.dnstring)
-        except Exception, msg:
-            print("ERROR: bad DN string '%s'" % self.dnstring)
-            raise
-
-    def __str__(self):
-        return self.prefix + str(self.dn.extended_str(mode=1))
+from samba.common import dsdb_Dn
 
 class dbcheck(object):
     """check a SAM database for errors"""
@@ -196,7 +173,7 @@ class dbcheck(object):
             self.report("Normalised attribute %s" % attrname)
 
     def is_deleted_objects_dn(self, dsdb_dn):
-        '''see if a dsdb_DN is the special Deleted Objects DN'''
+        '''see if a dsdb_Dn is the special Deleted Objects DN'''
         return dsdb_dn.prefix == "B:32:18E2EA80684F11D2B9AA00C04F79F805:"
 
 
@@ -329,7 +306,7 @@ class dbcheck(object):
         '''check a DN attribute for correctness'''
         error_count = 0
         for val in obj[attrname]:
-            dsdb_dn = dsdb_DN(self.samdb, val, syntax_oid)
+            dsdb_dn = dsdb_Dn(self.samdb, val, syntax_oid)
 
             # all DNs should have a GUID component
             guid = dsdb_dn.dn.get_extended_component("GUID")
diff --git a/source4/scripting/python/samba/tests/common.py b/source4/scripting/python/samba/tests/common.py
new file mode 100644
index 0000000..a97f954
--- /dev/null
+++ b/source4/scripting/python/samba/tests/common.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+# Unix SMB/CIFS implementation. Tests for common.py routines
+# Copyright (C) Andrew Tridgell 2011
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+"""Tests for samba.common"""
+
+import samba, os
+import samba.tests
+from samba.common import *
+from samba.samdb import SamDB
+
+
+class CommonTests(samba.tests.TestCase):
+
+    def test_normalise_int32(self):
+        self.assertEquals('17', normalise_int32(17))
+        self.assertEquals('17', normalise_int32('17'))
+        self.assertEquals('-123', normalise_int32('-123'))
+        self.assertEquals('-1294967296', normalise_int32('3000000000'))
+
+    def test_dsdb_Dn(self):
+        sam = samba.Ldb(url='dntest.ldb')
+        dn1 = dsdb_Dn(sam, "DC=foo,DC=bar")
+        dn2 = dsdb_Dn(sam, "B:8:0000000D:<GUID=b3f0ec29-17f4-452a-b002-963e1909d101>;DC=samba,DC=example,DC=com")
+        self.assertEquals(dn2.binary, "0000000D")
+        self.assertEquals(13, dn2.get_binary_integer())
+        os.unlink('dntest.ldb')


-- 
Samba Shared Repository


More information about the samba-cvs mailing list