Пример #1
0
class ManagedDomain:
    def __init__(self):
        self.cr = ConfigReader()
        self.sh = StateHandler()

    def readConfig(self, confFiles=[], confContent=''):
        self.cr.setFilenames(confFiles)
        self.cr.setContentList(confContent)
        self.cr.open()
        if 'cdm' not in self.cr.cp:
            self.cr.cp['cdm'] = {}
        self.cr.interprete(self.sh)
        self.sh.registerConfig(self.cr.config['cdm'])

    def run(self, confFile=None, forcePhase='next', confContent=''):
        if confFile is not None or 0 < len(confContent):
            self.readConfig(confFile, confContent)
        self.sh.load()
        self.sh.delete()  # --next starts with prepare, if failed
        self.sh.resetOpStateRecursive()
        currentPhase = getCurrentPhase(self.sh, forcePhase)
        log.info('Running phase: {}'.format(currentPhase))
        runPhase(self.cr, self.sh, currentPhase)
        nextphase = getNextPhase(currentPhase)
        self.sh.registerResult({'nextphase': nextphase})
        self.sh.save()
 def testSubstateSaveLoad(self):
     sh = StateHandler()
     ssh1 = sh.getSubstate("mysubstate_1")
     ssh2 = sh.getSubstate("mysubstate_2")
     sssh21 = ssh2.getSubstate("mysubstate_21")
     sssh22 = ssh2.getSubstate("mysubstate_22")
     sssh22.setOpStateDone()
     sssh21.setOpStateWaiting()
     ssh2.setOpStateRunning()
     sh.config = {'DEFAULT': {'statedir': '/tmp/test_cdmstatehandler'}}
     sh.save()
     ssh1.setOpStateDone()
     sh.save('/tmp/test_cdmstatehandler_2/state2.json')
     shLoad1 = StateHandler()
     shLoad1.load('/tmp/test_cdmstatehandler_2/state2.json')
     with self.subTest("set substates - load direkt"):
         self.assertEqual({'opstate': 'uninitialized', 'result': {},
                           'config': {'DEFAULT': {'statedir':
                                      '/tmp/test_cdmstatehandler'}},\
         'substate': {'mysubstate_1': {'opstate': 'done', 'result': {}, 'config': {}, 'substate': {}},\
                      'mysubstate_2': {'opstate': 'running', 'result': {}, 'config': {}, \
                                       'substate': {'mysubstate_21': {'opstate': 'waiting', 'result': {}, 'config': {}, 'substate': {}}, \
                          'mysubstate_22': {'opstate': 'done', 'result': {}, 'config': {}, 'substate': {}}}}}}, shLoad1.toDict())
     shLoad2 = StateHandler()
     shLoad2.config = {'DEFAULT': {'statedir': '/tmp/test_cdmstatehandler'}}
     shLoad2.load()
     with self.subTest("set substates - load via config"):
         self.assertEqual({'opstate': 'uninitialized', 'result': {},
                           'config': {'DEFAULT': {'statedir':
                                                  '/tmp/test_cdmstatehandler'}},\
         'substate': {'mysubstate_1': {'opstate': 'uninitialized', 'result': {}, 'config': {}, 'substate': {}},\
                      'mysubstate_2': {'opstate': 'running', 'result': {}, 'config': {}, \
                                       'substate': {'mysubstate_21': {'opstate': 'waiting', 'result': {}, 'config': {}, 'substate': {}}, \
                          'mysubstate_22': {'opstate': 'done', 'result': {}, 'config': {}, 'substate': {}}}}}}, shLoad2.toDict())
     with self.subTest("delete via config - before"):
         self.assertTrue(
             os.path.isfile('/tmp/test_cdmstatehandler/state.json'))
     sh.delete()
     with self.subTest("delete via config - after"):
         self.assertFalse(
             os.path.isfile('/tmp/test_cdmstatehandler/state.json'))
     with self.subTest("delete direct - before"):
         self.assertTrue(
             os.path.isfile('/tmp/test_cdmstatehandler_2/state2.json'))
     sh.delete('/tmp/test_cdmstatehandler_2/state2.json')
     with self.subTest("delete direct - after"):
         self.assertFalse(
             os.path.isfile('/tmp/test_cdmstatehandler_2/state2.json'))