]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
'tahoe stats': tolerate empty directories. Closes #693.
authorBrian Warner <warner@lothar.com>
Wed, 15 Jul 2009 07:51:09 +0000 (00:51 -0700)
committerBrian Warner <warner@lothar.com>
Wed, 15 Jul 2009 07:51:09 +0000 (00:51 -0700)
src/allmydata/scripts/tahoe_manifest.py
src/allmydata/test/test_cli.py

index a43b5b9792346f71a439a1bd5b14bb43a9acbc48..180bf7cb153011fd4342a9e1c8ee4f1e5fa9c5e5 100644 (file)
@@ -117,24 +117,25 @@ class StatsGrabber(SlowOperationRunner):
                     print >>stdout, fmt % (k, data[k]), "  ", absize
                 else:
                     print >>stdout, fmt % (k, data[k])
-        print >>stdout, "Size Histogram:"
-        prevmax = None
-        maxlen = max([len(str(maxsize))
-                      for (minsize, maxsize, count)
-                      in data["size-files-histogram"]])
-        maxcountlen = max([len(str(count))
-                           for (minsize, maxsize, count)
-                           in data["size-files-histogram"]])
-        minfmt = "%" + str(maxlen) + "d"
-        maxfmt = "%-" + str(maxlen) + "d"
-        countfmt = "%-" + str(maxcountlen) + "d"
-        linefmt = minfmt + "-" + maxfmt + " : " + countfmt + "    %s"
-        for (minsize, maxsize, count) in data["size-files-histogram"]:
-            if prevmax is not None and minsize != prevmax+1:
-                print >>stdout, " "*(maxlen-1) + "..."
-            prevmax = maxsize
-            print >>stdout, linefmt % (minsize, maxsize, count,
-                                       abbreviate_space_both(maxsize))
+        if data["size-files-histogram"]:
+            print >>stdout, "Size Histogram:"
+            prevmax = None
+            maxlen = max([len(str(maxsize))
+                          for (minsize, maxsize, count)
+                          in data["size-files-histogram"]])
+            maxcountlen = max([len(str(count))
+                               for (minsize, maxsize, count)
+                               in data["size-files-histogram"]])
+            minfmt = "%" + str(maxlen) + "d"
+            maxfmt = "%-" + str(maxlen) + "d"
+            countfmt = "%-" + str(maxcountlen) + "d"
+            linefmt = minfmt + "-" + maxfmt + " : " + countfmt + "    %s"
+            for (minsize, maxsize, count) in data["size-files-histogram"]:
+                if prevmax is not None and minsize != prevmax+1:
+                    print >>stdout, " "*(maxlen-1) + "..."
+                prevmax = maxsize
+                print >>stdout, linefmt % (minsize, maxsize, count,
+                                           abbreviate_space_both(maxsize))
 
 def stats(options):
     return StatsGrabber().run(options)
index 8b46502d9c2a32a40f6c22158503485534e9f059..8efccb2bf23196ad95cfb7420ea3288cea815d6f 100644 (file)
@@ -1268,6 +1268,21 @@ class Check(GridTestMixin, CLITestMixin, unittest.TestCase):
                             in lines, out)
         d.addCallback(_check2)
 
+        d.addCallback(lambda ign: self.do_cli("stats", self.rooturi))
+        def _check_stats((rc, out, err)):
+            self.failUnlessEqual(err, "")
+            self.failUnlessEqual(rc, 0)
+            lines = out.splitlines()
+            self.failUnlessIn(" count-immutable-files: 1", lines)
+            self.failUnlessIn("   count-mutable-files: 1", lines)
+            self.failUnlessIn("   count-literal-files: 1", lines)
+            self.failUnlessIn("     count-directories: 1", lines)
+            self.failUnlessIn("  size-immutable-files: 400", lines)
+            self.failUnlessIn("Size Histogram:", lines)
+            self.failUnlessIn("   4-10   : 1    (10 B, 10 B)", lines)
+            self.failUnlessIn(" 317-1000 : 1    (1000 B, 1000 B)", lines)
+        d.addCallback(_check_stats)
+
         def _clobber_shares(ignored):
             shares = self.find_shares(self.uris["good"])
             self.failUnlessEqual(len(shares), 10)
@@ -1431,3 +1446,31 @@ class Errors(GridTestMixin, CLITestMixin, unittest.TestCase):
         d.addCallback(_check1)
 
         return d
+
+class Stats(GridTestMixin, CLITestMixin, unittest.TestCase):
+    def test_empty_directory(self):
+        self.basedir = "cli/Stats/empty_directory"
+        self.set_up_grid()
+        c0 = self.g.clients[0]
+        self.fileurls = {}
+        d = c0.create_empty_dirnode()
+        def _stash_root(n):
+            self.rootnode = n
+            self.rooturi = n.get_uri()
+        d.addCallback(_stash_root)
+
+        # make sure we can get stats on an empty directory too
+        d.addCallback(lambda ign: self.do_cli("stats", self.rooturi))
+        def _check_stats((rc, out, err)):
+            self.failUnlessEqual(err, "")
+            self.failUnlessEqual(rc, 0)
+            lines = out.splitlines()
+            self.failUnlessIn(" count-immutable-files: 0", lines)
+            self.failUnlessIn("   count-mutable-files: 0", lines)
+            self.failUnlessIn("   count-literal-files: 0", lines)
+            self.failUnlessIn("     count-directories: 1", lines)
+            self.failUnlessIn("  size-immutable-files: 0", lines)
+            self.failIfIn("Size Histogram:", lines)
+        d.addCallback(_check_stats)
+
+        return d