Browse Source

[trac930] modify stats.py
- add more documentations into update_modules, get_statistics_data and
update_statistics_data methods
- modify two methods: "update_modules" and "get_statistics_data" methods raise
StatsError instead of just returning None, when communication between stats
module and config manager is failed or when it can't find specified
statistics data.
- also modify the unittest depending on the changes of these behaviors.

Naoki Kambe 13 years ago
parent
commit
7a31e95e63
2 changed files with 33 additions and 11 deletions
  1. 22 7
      src/bin/stats/stats.py.in
  2. 11 4
      src/bin/stats/tests/b10-stats_test.py

+ 22 - 7
src/bin/stats/stats.py.in

@@ -197,7 +197,10 @@ class Stats:
 
     def update_modules(self):
         """
-        update information of each module
+        updates information of each module. This method gets each
+        module's information from the config manager and sets it into
+        self.modules. If its getting from the config manager fails, it
+        raises StatsError.
         """
         modules = {}
         seq = self.cc_session.group_sendmsg(
@@ -213,12 +216,16 @@ class Stats:
                     if value[mod] and type(value[mod]) is list:
                         spec["statistics"] = value[mod]
                     modules[mod] = isc.config.module_spec.ModuleSpec(spec)
+            else:
+                raise StatsError("Updating module spec fails: " + str(value))
         modules[self.module_name] = self.mccs.get_module_spec()
         self.modules = modules
 
     def get_statistics_data(self, owner=None, name=None):
         """
-        return statistics data which stats module has of each module
+        returns statistics data which stats module has of each
+        module. If it can't find specified statistics data, it raises
+        StatsError.
         """
         self.update_statistics_data()
         if owner and name:
@@ -235,10 +242,18 @@ class Stats:
             pass
         else:
             return self.statistics_data
+        raise StatsError("No statistics data found: "
+                         + "owner: " + str(owner) + ", "
+                         + "name: " + str(name))
 
     def update_statistics_data(self, owner=None, **data):
         """
-        change statistics date of specified module into specified data
+        change statistics date of specified module into specified
+        data. It updates information of each module first, and it
+        updates statistics data. If specified data is invalid for
+        statistics spec of specified owner, it returns a list of error
+        messeges. If there is no error or if neither owner nor data is
+        specified in args, it returns None.
         """
         self.update_modules()
         statistics_data = {}
@@ -297,10 +312,10 @@ class Stats:
         if errors:
             raise StatsError("stats spec file is incorrect: "
                              + ", ".join(errors))
-        ret = self.get_statistics_data(owner, name)
-        if ret is not None:
-            return isc.config.create_answer(0, ret)
-        else:
+        try:
+            return isc.config.create_answer(
+                0, self.get_statistics_data(owner, name))
+        except StatsError:
             return isc.config.create_answer(
                 1, "specified arguments are incorrect: " \
                     + "owner: " + str(owner) + ", name: " + str(name))

+ 11 - 4
src/bin/stats/tests/b10-stats_test.py

@@ -296,6 +296,10 @@ class TestStats(unittest.TestCase):
         my_statistics_data = stats.get_spec_defaults(self.stats.modules['Boss'].get_statistics_spec())
         self.assertTrue('boot_time' in my_statistics_data)
         self.assertEqual(my_statistics_data['boot_time'], "1970-01-01T00:00:00Z")
+        orig_parse_answer = stats.isc.config.ccsession.parse_answer
+        stats.isc.config.ccsession.parse_answer = lambda x: (99, 'error')
+        self.assertRaises(stats.StatsError, self.stats.update_modules)
+        stats.isc.config.ccsession.parse_answer = orig_parse_answer
 
     def test_get_statistics_data(self):
         my_statistics_data = self.stats.get_statistics_data()
@@ -307,7 +311,7 @@ class TestStats(unittest.TestCase):
         self.assertTrue('last_update_time' in my_statistics_data)
         self.assertTrue('timestamp' in my_statistics_data)
         self.assertTrue('lname' in my_statistics_data)
-        self.assertIsNone(self.stats.get_statistics_data(owner='Foo'))
+        self.assertRaises(stats.StatsError, self.stats.get_statistics_data, owner='Foo')
         my_statistics_data = self.stats.get_statistics_data(owner='Stats')
         self.assertTrue('boot_time' in my_statistics_data)
         my_statistics_data = self.stats.get_statistics_data(owner='Stats', name='report_time')
@@ -320,9 +324,12 @@ class TestStats(unittest.TestCase):
         self.assertEqual(my_statistics_data, 0.0)
         my_statistics_data = self.stats.get_statistics_data(owner='Stats', name='lname')
         self.assertEqual(my_statistics_data, '')
-        self.assertIsNone(self.stats.get_statistics_data(owner='Stats', name='Bar'))
-        self.assertIsNone(self.stats.get_statistics_data(owner='Foo', name='Bar'))
-        self.assertEqual(self.stats.get_statistics_data(name='Bar'), None)
+        self.assertRaises(stats.StatsError, self.stats.get_statistics_data,
+                          owner='Stats', name='Bar')
+        self.assertRaises(stats.StatsError, self.stats.get_statistics_data,
+                          owner='Foo', name='Bar')
+        self.assertRaises(stats.StatsError, self.stats.get_statistics_data,
+                          name='Bar')
 
     def test_update_statistics_data(self):
         self.stats.update_statistics_data(owner='Stats', lname='foo@bar')