[CalendarServer-changes] [3488] CalendarServer/trunk/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Tue Dec 9 10:47:26 PST 2008


Revision: 3488
          http://trac.macosforge.org/projects/calendarserver/changeset/3488
Author:   cdaboo at apple.com
Date:     2008-12-09 10:47:26 -0800 (Tue, 09 Dec 2008)
Log Message:
-----------
Accounting failures should not cause 500 errors.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/accounting.py

Added Paths:
-----------
    CalendarServer/trunk/twistedcaldav/test/test_accounting.py

Modified: CalendarServer/trunk/twistedcaldav/accounting.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/accounting.py	2008-12-09 18:43:27 UTC (rev 3487)
+++ CalendarServer/trunk/twistedcaldav/accounting.py	2008-12-09 18:47:26 UTC (rev 3488)
@@ -80,38 +80,42 @@
     if not accountingEnabled(category, principal):
         return
 
-    #
-    # Obtain the accounting log file name
-    #
-    logRoot = config.AccountingLogRoot
-    logDirectory = os.path.join(
-        logRoot,
-        principal.record.guid[0:2],
-        principal.record.guid[2:4],
-        principal.record.guid,
-        category
-    )
-    logFilename = os.path.join(logDirectory, datetime.datetime.now().isoformat())
-
-    if not os.path.isdir(logDirectory):
-        os.makedirs(logDirectory)
-        logFilename = "%s-01" % (logFilename,)
-    else:
-        index = 1
-        while True:
-            path = "%s-%02d" % (logFilename, index)
-            if not os.path.isfile(path):
-                logFilename = path
-                break
-            if index == 1000:
-                log.error("Too many %s accounting files for %s" % (category, principal))
-                return
-
-    #
-    # Now write out the data to the log file
-    #
-    logFile = open(logFilename, "a")
     try:
-        logFile.write(data)
-    finally:
-        logFile.close()
+        #
+        # Obtain the accounting log file name
+        #
+        logRoot = config.AccountingLogRoot
+        logDirectory = os.path.join(
+            logRoot,
+            principal.record.guid[0:2],
+            principal.record.guid[2:4],
+            principal.record.guid,
+            category
+        )
+        logFilename = os.path.join(logDirectory, datetime.datetime.now().isoformat())
+    
+        if not os.path.isdir(logDirectory):
+            os.makedirs(logDirectory)
+            logFilename = "%s-01" % (logFilename,)
+        else:
+            index = 1
+            while True:
+                path = "%s-%02d" % (logFilename, index)
+                if not os.path.isfile(path):
+                    logFilename = path
+                    break
+                if index == 1000:
+                    log.error("Too many %s accounting files for %s" % (category, principal))
+                    return
+    
+        #
+        # Now write out the data to the log file
+        #
+        logFile = open(logFilename, "a")
+        try:
+            logFile.write(data)
+        finally:
+            logFile.close()
+    except OSError, e:
+        # No failures in accounting should propagate out
+        log.error("Failed to write accounting data due to: %s" % (str(e),))

Added: CalendarServer/trunk/twistedcaldav/test/test_accounting.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_accounting.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/test/test_accounting.py	2008-12-09 18:47:26 UTC (rev 3488)
@@ -0,0 +1,63 @@
+##
+# Copyright (c) 2005-2008 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from twistedcaldav.accounting import emitAccounting
+from twistedcaldav.config import config
+import twistedcaldav.test.util
+
+import os
+import stat
+
+class Accounting (twistedcaldav.test.util.TestCase):
+
+    def setUp(self):
+        
+        super(Accounting, self).setUp()
+        config.AccountingCategories.iTIP = True
+        config.AccountingPrincipals = ["*",]
+        config.AccountingLogRoot = self.mkdtemp("accounting")[0]
+
+    class _Principal(object):
+        
+        class _Record(object):
+            
+            def __init__(self, guid):
+                self.guid = guid
+                
+        def __init__(self, guid):
+            
+            self.record = self._Record(guid)
+
+    def test_permissions_makedirs(self):
+        """
+        Test permissions when creating accounting
+        """
+        
+        # Make log root non-writeable
+        os.chmod(config.AccountingLogRoot, stat.S_IRUSR)
+        
+        emitAccounting("iTIP", self._Principal("1234-5678"), "bogus")
+
+    def test_file_instead_of_directory(self):
+        """
+        Test permissions when creating accounting
+        """
+        
+        # Make log root a file
+        config.AccountingLogRoot = self.mktemp()
+        open(config.AccountingLogRoot, "w").close()
+        
+        emitAccounting("iTIP", self._Principal("1234-5678"), "bogus")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20081209/8c07acf2/attachment.html>


More information about the calendarserver-changes mailing list