Esempio n. 1
0
 def test_cmd_serialization(self):
     import time
     cmd = CommandRunInformation(**{
         'workspace': 'fakews',
         'itime': time.time(),
         'command': "ping",
         'params': "127.0.0.1"})
     cmdserialized = self.mapper.serialize(cmd)
     # if serialization fails, returns None
     self.assertNotEqual(
         cmdserialized,
         None,
         "Serialized cmd shouldn't be None")
     # we check the cmd attributes
     self.assertEquals(
         cmdserialized.get("_id"),
         cmd.getID(),
         "Serialized ID is not the same as cmd ID")
     self.assertEquals(
         cmdserialized.get("command"),
         cmd.__getattribute__("command"),
         "Serialized name is not the same as cmd command")
     self.assertEquals(
         cmdserialized.get("params"),
         cmd.__getattribute__("params"),
         "Serialized name is not the same as cmd params")
Esempio n. 2
0
    def processCommandInput(self, pid, cmd, pwd):
        """
        This method tries to find a plugin to parse the command sent
        by the terminal (identiefied by the process id).
        """
        if pid not in self.plugin_sets:
            self.createPluginSet(pid)

        plugin = self._get_plugins_by_input(cmd, self.plugin_sets[pid])

        if plugin:
            modified_cmd_string = plugin.processCommandString("", pwd, cmd)
            if not self._is_command_malformed(cmd, modified_cmd_string):

                cmd_info = CommandRunInformation(
                    **{'workspace': model.api.getActiveWorkspace().name,
                        'itime': time.time(),
                        'import_source': 'shell',
                        'command': cmd.split()[0],
                        'params': ' '.join(cmd.split()[1:])})
                cmd_info.setID(self._mapper_manager.save(cmd_info))

                self._active_plugins[pid] = plugin, cmd_info

                return plugin.id, modified_cmd_string

        return None, None
Esempio n. 3
0
    def processReport(self, plugin, filepath, ws_name=None):

        if not ws_name:
            ws_name = model.api.getActiveWorkspace().name

        cmd_info = CommandRunInformation(
            **{'workspace': ws_name,
               'itime': time.time(),
               'import_source': 'report',
               'command': plugin,
               'params': filepath,
            })

        self._mapper_manager.createMappers(ws_name)
        command_id = self._mapper_manager.save(cmd_info)
        cmd_info.setID(command_id)

        if plugin in self._plugins:
            logger.info('Processing report with plugin {0}'.format(plugin))
            self._plugins[plugin].workspace = ws_name
            with open(filepath, 'rb') as output:
                self.processOutput(self._plugins[plugin], output.read(), cmd_info, True)
            return command_id

        # Plugin to process this report not found, update duration of plugin process
        cmd_info.duration = time.time() - cmd_info.itime
        self._mapper_manager.update(cmd_info)
        return False
Esempio n. 4
0
    def processReport(self, plugin, filepath):

        cmd_info = CommandRunInformation(
            **{'workspace': model.api.getActiveWorkspace().name,
                'itime': time.time(),
                'command': 'Import %s:' % plugin,
                'params': filepath})
        self._mapper_manager.save(cmd_info)

        if plugin in self._plugins:
            self.processOutput(self._plugins[plugin], filepath, True)
            cmd_info.duration = time.time() - cmd_info.itime
            self._mapper_manager.update(cmd_info)
            return True
        return False
Esempio n. 5
0
    def processReport(self, plugin, filepath):

        cmd_info = CommandRunInformation(
            **{'workspace': model.api.getActiveWorkspace().name,
                'itime': time.time(),
                'command': 'Import %s:' % plugin,
                'params': filepath})
        self._mapper_manager.save(cmd_info)

        if plugin in self._plugins:
            self.processOutput(self._plugins[plugin], filepath, True)
            cmd_info.duration = time.time() - cmd_info.itime
            self._mapper_manager.save(cmd_info)
            return True
        return False
Esempio n. 6
0
    def processCommandInput(self, command_string):

        plugin = self._get_plugins_by_input(command_string)

        if plugin:
            modified_cmd_string = plugin.processCommandString(
                "", "", command_string)
            if not self._is_command_malformed(command_string,
                                              modified_cmd_string):

                cmd_info = CommandRunInformation(
                    **{
                        'workspace': model.api.getActiveWorkspace().name,
                        'itime': time(),
                        'command': command_string.split()[0],
                        'params': ' '.join(command_string.split()[1:])
                    })
                self._mapper_manager.save(cmd_info)

                self._active_plugins[command_string] = plugin, cmd_info

                output_file_path = None
                if plugin.has_custom_output():
                    output_file_path = plugin.get_custom_file_path()
                return True, modified_cmd_string, output_file_path

        return False, None, None
Esempio n. 7
0
    def setUp(self):
        self._model_controller = controller.ModelController(mock())
        self.cm = mock(CommandManager)
        when(self.cm).saveCommand().thenReturn(True)
        self._plugin_controller = PluginController("test", {}, self.cm)

        class PluginTest(PluginBase):
            def __init__(self):
                PluginBase.__init__(self)
                self.id = "Test"
                self.name = "Test"

            def parseOutputString(self, output, debug=False):
                pass

        self.workspace = mock(Workspace)
        when(self.workspace).getContainee().thenReturn(ModelObjectContainer())
        self._model_controller.setWorkspace(self.workspace)

        self.plugin = PluginTest()
        api.setUpAPIs(self._model_controller)

        self._plugin_controller.setActivePlugin(self.plugin)
        self.cmdinfo = CommandRunInformation(
                **{'workspace': 'test',
                    'itime': time(),
                    'command': 'test',
                    'params': 'test'})
Esempio n. 8
0
    def test_cmd_creation(self):
        import time
        cmd = CommandRunInformation(
            **{
                'workspace': 'fakews',
                'itime': time.time(),
                'command': "ping",
                'params': "127.0.0.1"
            })

        self.mapper.save(cmd)
        c = self.mapper.find(cmd.getID())
        self.assertEquals(c, cmd,
                          "Cmd retrieved should be the same as persisted")
        self.assertEquals(
            c.getID(), cmd.getID(),
            "Cmd retrieved's Id should be the same as persisted's Id")
Esempio n. 9
0
    def test_cmd_creation(self):
        import time
        cmd = CommandRunInformation(**{
            'workspace': 'fakews',
            'itime': time.time(),
            'command': "ping",
            'params': "127.0.0.1"})

        self.mapper.save(cmd)
        c = self.mapper.find(cmd.getID())
        self.assertEquals(
            c,
            cmd,
            "Cmd retrieved should be the same as persisted")
        self.assertEquals(
            c.getID(),
            cmd.getID(),
            "Cmd retrieved's Id should be the same as persisted's Id")
Esempio n. 10
0
    def test_cmd_create_and_delete(self):
        import time
        cmd = CommandRunInformation(
            **{
                'workspace': 'fakews',
                'itime': time.time(),
                'command': "ping",
                'params': "127.0.0.1"
            })

        self.mapper.save(cmd)
        cmd_id = cmd.getID()

        self.assertNotEquals(self.mapper.load(cmd_id), None,
                             "Command should be saved")

        self.mapper.delete(cmd_id)

        self.assertEquals(self.mapper.find(cmd_id), None,
                          "Command shouldn't exist anymore")
Esempio n. 11
0
    def test_cmd_create_and_delete(self):
        import time
        cmd = CommandRunInformation(**{
            'workspace': 'fakews',
            'itime': time.time(),
            'command': "ping",
            'params': "127.0.0.1"})

        self.mapper.save(cmd)
        cmd_id = cmd.getID()

        self.assertNotEquals(
            self.mapper.load(cmd_id),
            None,
            "Command should be saved")

        self.mapper.delete(cmd_id)

        self.assertEquals(
            self.mapper.find(cmd_id),
            None,
            "Command shouldn't exist anymore")
Esempio n. 12
0
    def processCommandInput(self, prompt, username, current_path,
                            command_string, interactive):
        """
        Receives the prompt that the current session has, the actual command_string that
        the user typed and if the command is interactive. If it is interactive the
        plugin controller does not choose a new active plugin but use the one the
        is already set (if none is set it raises an exeception).

        If always returns an string. It could be modified by the active plugin or, if
        there is none available, it will return the original command_string.
        """

        if interactive:
            return None
        else:
            self._disable_active_plugin()

        choosen_plugin = self._get_plugins_by_input(command_string)
        if choosen_plugin is None:
            model.api.devlog(
                "There is no active plugin to handle current command/user input"
            )
            return None
        self._active_plugin = choosen_plugin

        modified_cmd_string = self._active_plugin.processCommandString(
            username, current_path, command_string)

        if self._is_command_malformed(command_string, modified_cmd_string):
            return None
        else:
            cmd_info = CommandRunInformation(
                **{
                    'workspace': model.api.getActiveWorkspace().name,
                    'itime': time(),
                    'command': command_string.split()[0],
                    'params': ' '.join(command_string.split()[1:])
                })
            workspace = model.api.getActiveWorkspace()
            self._command_manager.saveCommand(cmd_info, workspace)

            self.last_command_information = cmd_info

            return modified_cmd_string if isinstance(modified_cmd_string,
                                                     basestring) else None
Esempio n. 13
0
    def test_valid_command_creation(self):
        information = self.getDefaultCommandInfo()

        command_info = CommandRunInformation(**information)
        self.assertIsNotNone(command_info, "Command wrongly created")

        self.assertEquals(command_info.command, information['command'], \
                "Field %s not instantiated" % information['command'])

        self.assertEquals(command_info.parameters, information['parameters'], \
                "Field %s not instantiated" % information['parameters'])

        self.assertEquals(command_info.itime, information['itime'], \
                "Field %s not instantiated" % information['itime'])

        self.assertEquals(command_info.duration, information['duration'], \
                "Field %s not instantiated" % information['duration'])

        self.assertEquals(command_info.workspace, information['workspace'], \
                "Field %s not instantiated" % information['workspace'])
Esempio n. 14
0
    def test_save_command_in_fs(self):
        """ Tests if command is saved in couch """
        wm = WorkspaceManager(mock(ModelController), mock(PluginController))

        command_info = self.getDefaultCommandInfo()
        command_info['workspace'] = 'new_workspace'
        exec_command = CommandRunInformation(**command_info)

        workspace = wm.createWorkspace(exec_command.workspace,
                                       workspaceClass=WorkspaceOnFS)
        wm.setActiveWorkspace(workspace)

        cm = CommandManager()

        res = cm.saveCommand(exec_command, workspace)

        self._manager = PersistenceManagerFactory.getInstance()
        # saved_doc = self._manager.getDocument(exec_command.workspace, res['id'])

        # After all
        wm.removeWorkspace(command_info['workspace'])
Esempio n. 15
0
    def test_save_command_in_couch(self):
        """ Tests if command is saved in couch """
        cm = CommandManager()

        exec_command = CommandRunInformation(**self.getDefaultCommandInfo())

        wm = WorkspaceManager(mock(ModelController), mock(PluginController))
        workspace = wm.createWorkspace(exec_command.workspace,
                                       workspaceClass=WorkspaceOnCouch)

        res = cm.saveCommand(exec_command, workspace)

        self._manager = PersistenceManagerFactory.getInstance()
        saved_doc = self._manager.getDocument(exec_command.workspace,
                                              res['id'])

        self.assertEquals(exec_command.command, saved_doc['command'],
                          'Saved command diffier')
        self.assertEquals(exec_command.parameters, saved_doc['parameters'],
                          'Saved command diffier')
        self.assertEquals(exec_command.itime, saved_doc['itime'],
                          'Saved command diffier')
        self.assertEquals(exec_command.duration, saved_doc['duration'],
                          'Saved command diffier')