def test_metadata_set_success(self):
     """verify successful setting of key,val pair"""
     json_str = """{"backup-today": "2019-01-02", "latest-complete": "2019-01-02-012345"}"""
     metadata = MetaData(self.log, self.comms, self.settings, json_str)
     metadata.set('backup-today', "2019-12-12")
     self.assertEqual(metadata.get("backup-today"), "2019-12-12")
     self.assertEqual(self.log.getVal('info'), '')
    def test_metadata_set_exception(self):
        """verify set checks for invalid keys"""
        json_str = ""
        metadata = MetaData(self.log, self.comms, self.settings, json_str)
        self.assertDictEqual(metadata.meta, {
            'backup-today': '',
            'latest-complete': ''
        })
        with self.assertRaises(CrashPlanError) as cpe:
            metadata.set("test", "t")

        self.assertIsInstance(cpe.exception, CrashPlanError)
        self.assertEqual(repr(cpe.exception.value),
                         "'(set) Invalid meta data key - test'")

        #self.assertEqual(self.log.getVal('info').split('|')[0], 'MetaData added missing expected key latest-complete.')
        #self.assertEqual(self.log.getVal('info').split('|')[1], 'MetaData added missing expected key backup-today.')
        self.assertEqual(self.log.getVal('info'), '')
    def finishUp(self):
        """write .metadata file"""
        if self.backup_successful:
            meta2 = MetaData(self.log, self.comms, self.settings, "")
            meta2.set("latest-complete", TimeDate.datedir())
            meta2.set("backup-today", TimeDate.today())

            try:
                meta2.writeMetaData()

                # move WORKING to Latest Complete Date
                src = os.path.join(self.settings('backup-destination'),
                                   self.settings('local-hostname'), "WORKING")
                dest = os.path.join(self.settings('backup-destination'),
                                    self.settings('local-hostname'),
                                    TimeDate.datedir())
                self.comms.remoteCommand(f"mv {src} {dest}")
            except CrashPlanError as exc:
                print(exc)
                self.log.error(exc)