From abeef43a5b8ee5699895f01cbf5a96647112f8b3 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 20 Aug 2012 12:15:29 -0400 Subject: [PATCH] cc-4227: fixed small bugs in upgrade script and finished testing --- .../media-monitor2/tests/test_upgrade2dot2.py | 48 ++++++++++++++++++- python_apps/media-monitor2/upgrade2dot2.py | 4 +- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/python_apps/media-monitor2/tests/test_upgrade2dot2.py b/python_apps/media-monitor2/tests/test_upgrade2dot2.py index bad930478..fc74d8984 100644 --- a/python_apps/media-monitor2/tests/test_upgrade2dot2.py +++ b/python_apps/media-monitor2/tests/test_upgrade2dot2.py @@ -1,5 +1,51 @@ +from configobj import ConfigObj import unittest +import os import upgrade2dot2 + +def create_cfg(cfg): + config = ConfigObj() + config.filename = cfg['path'] + for k,v in cfg['data'].iteritems(): config[k] = v + return config + class TestUpgrade(unittest.TestCase): - pass + + def setUp(self): + self.source = 'ttt1.cfg' + self.dest = 'ttt2.cfg' + + def test_upgrade(self): + cf = { + 'source' : { + 'path' : self.source, + 'data' : { + 'key1' : 'val1', + 'key2' : 'val2', + 'key3' : 5, + 'key4' : 10,}, + }, + 'dest' : { + 'path' : self.dest, + 'data' : { + 'key1' : 'NEW_VAL', + 'key3' : 25, } + } + } + config1, config2 = create_cfg(cf['source']), create_cfg(cf['dest']) + for c in [config1,config2]: c.write() + self.assertTrue( os.path.exists(cf['source']['path']) ) + self.assertTrue( os.path.exists(cf['dest']['path']) ) + # Finished preparing + upgrade2dot2.upgrade({ cf['source']['path'] : cf['dest']['path'] }) + c1, c2 = ConfigObj(cf['source']['path']), ConfigObj(cf['dest']['path']) + self.assertEqual( c2['key2'], 'val2') + self.assertEqual( c2['key4'], '10') + self.assertEqual( c2['key3'], '25') + + def tearDown(self): + for clean in [ self.source, self.dest ]: + os.unlink(clean) + +if __name__ == '__main__': unittest.main() diff --git a/python_apps/media-monitor2/upgrade2dot2.py b/python_apps/media-monitor2/upgrade2dot2.py index 88d910d10..5283c28b1 100644 --- a/python_apps/media-monitor2/upgrade2dot2.py +++ b/python_apps/media-monitor2/upgrade2dot2.py @@ -12,13 +12,13 @@ def upgrade(upgrade_data): Must be ran as sudo. will do upgrade of configuration files by filling in missing values according to upgrade_data """ - for source, destination in upgrade_data: + for source, destination in upgrade_data.iteritems(): if not os.path.exists(source): print("Cannot upgrade '%s'. Skipping this file" % source) continue try: cfg_source, cfg_dest = ConfigObj(source), ConfigObj(destination) - for key, val in cfg_source: + for key, val in cfg_source.iteritems(): if key not in cfg_dest: cfg_dest[key] = val cfg_dest.write() except Exception: