Beispiel #1
0
 def test_prototype_execution(self):
     ioc = GreaseContainer()
     cmd = DaemonProcess(ioc)
     # add search path
     fil = open(ioc.getConfig().greaseConfigFile, 'r')
     data = json.loads(fil.read())
     fil.close()
     fil = open(ioc.getConfig().greaseConfigFile, 'w')
     data['Import']['searchPath'].append('tgt_grease.router.Commands.tests')
     fil.write(json.dumps(data, sort_keys=True, indent=4))
     fil.close()
     Configuration.ReloadConfig()
     # Update Node to run it
     ioc.getCollection('JobServer')\
         .update_one(
         {'_id': ObjectId(ioc.getConfig().NodeIdentity)},
         {
             '$set': {
                 'prototypes': ['TestProtoType']
             }
         }
     )
     # Sleeps are because mongo in Travis is slow sometimes to persist data
     time.sleep(1.5)
     self.assertTrue(cmd.server())
     self.assertTrue(cmd.drain_jobs(ioc.getCollection('JobQueue')))
     # ensure jobs drain out
     time.sleep(1.5)
     self.assertEqual(
         ioc.getCollection('TestProtoType').find({
             'runs': {
                 '$exists': True
             }
         }).count(), 10)
     # clean up
     fil = open(ioc.getConfig().greaseConfigFile, 'r')
     data = json.loads(fil.read())
     fil.close()
     # remove collection
     ioc.getCollection('TestProtoType').drop()
     # pop search path
     trash = data['Import']['searchPath'].pop()
     # close out
     fil = open(ioc.getConfig().greaseConfigFile, 'w')
     fil.write(json.dumps(data, sort_keys=True, indent=4))
     fil.close()
     ioc.getCollection('JobServer') \
         .update_one(
         {'_id': ObjectId(ioc.getConfig().NodeIdentity)},
         {
             '$set': {
                 'prototypes': []
             }
         }
     )
Beispiel #2
0
    def test_scan(self):
        # setup
        configList = [
            {
                "name": "test1",
                "job": "fakeJob",
                "exe_env": "windows",
                "source": "TestSource",
                "logic": {
                    "regex": [
                        {
                            "field": "character",
                            "pattern": ".*skywalker.*"
                        }
                    ]
                }
            }
        ]
        ioc = GreaseContainer()
        ioc.ensureRegistration()
        ioc.getConfig().set('trace', True, 'Logging')
        ioc.getConfig().set('verbose', True, 'Logging')
        fil = open(ioc.getConfig().greaseConfigFile, 'r')
        data = json.loads(fil.read())
        fil.close()
        fil = open(ioc.getConfig().greaseConfigFile, 'w')
        data['Import']['searchPath'].append('tgt_grease.enterprise.Model.tests')
        fil.write(json.dumps(data, sort_keys=True, indent=4))
        fil.close()
        Configuration.ReloadConfig()
        jServer = ioc.getCollection('JobServer')
        jID1 = jServer.insert_one({
                'jobs': 0,
                'os': platform.system().lower(),
                'roles': ["general"],
                'prototypes': ["detect"],
                'active': True,
                'activationTime': datetime.utcnow()
        }).inserted_id
        time.sleep(1)
        jID2 = jServer.insert_one({
                'jobs': 0,
                'os': platform.system().lower(),
                'roles': ["general"],
                'prototypes': ["detect"],
                'active': True,
                'activationTime': datetime.utcnow()
        }).inserted_id

        # Begin Test
        conf = PrototypeConfig(ioc)
        conf.load(reloadConf=True, ConfigurationList=configList)
        scanner = Scan(ioc)
        # Scan Environment
        self.assertTrue(scanner.Parse())
        # Begin ensuring environment is how we expect
        # we assert less or equal because sometimes uuid's are close :p
        self.assertLessEqual(ioc.getCollection('SourceData').find({
            'detectionServer': ObjectId(jID1)
        }).count(), 3)
        self.assertLessEqual(ioc.getCollection('SourceData').find({
            'detectionServer': ObjectId(jID2)
        }).count(), 3)
        self.assertLessEqual(ioc.getCollection('JobServer').find_one({
            '_id': ObjectId(jID1)
        })['jobs'], 3)
        self.assertLessEqual(ioc.getCollection('JobServer').find_one({
            '_id': ObjectId(jID2)
        })['jobs'], 3)

        # clean up
        fil = open(ioc.getConfig().greaseConfigFile, 'r')
        data = json.loads(fil.read())
        fil.close()
        # remove collection
        ioc.getCollection('TestProtoType').drop()
        # remove prototypes
        data['NodeInformation']['ProtoTypes'] = []
        # pop search path
        trash = data['Import']['searchPath'].pop()
        # close out
        fil = open(ioc.getConfig().greaseConfigFile, 'w')
        fil.write(json.dumps(data, sort_keys=True, indent=4))
        fil.close()
        jServer.delete_one({'_id': ObjectId(jID1)})
        jServer.delete_one({'_id': ObjectId(jID2)})
        ioc.getCollection('SourceData').drop()
        ioc.getCollection('Dedup_Sourcing').drop()
        ioc.getConfig().set('trace', False, 'Logging')
        ioc.getConfig().set('verbose', False, 'Logging')
        Configuration.ReloadConfig()