Example #1
0
            def mock_delivery(cmd):
                project_id = f"snpseq{random.randint(0, 10**10):010d}"
                dds_output = f"""Current user: bio
    Project created with id: {project_id}
    User forskare was associated with Project {project_id} as Owner=True. An e-mail notification has not been sent.
    Invitation sent to [email protected]. The user should have a valid account to be added to a
    project"""
                log.debug(f"Mock is called with {cmd}")
                shell = False
                if any(cmd[0].endswith(delivery_prgm)
                       for delivery_prgm in ['dds', 'moverinfo', 'to_outbox']):
                    new_cmd = ['sleep', str(self.mock_duration)]

                    if cmd[0].endswith('dds') and 'project' in cmd:
                        new_cmd += ['&&', 'echo', f'"{dds_output}"']
                        new_cmd = " ".join(new_cmd)
                        shell = True
                else:
                    new_cmd = cmd

                log.debug(f"Running mocked {new_cmd}")
                p = Subprocess(new_cmd,
                               stdout=PIPE,
                               stderr=PIPE,
                               stdin=PIPE,
                               shell=shell)
                return Execution(pid=p.pid, process_obj=p)
Example #2
0
 def run(self, cmd):
     if self.throw:
         raise Exception("Test the exception handling...")
     mock_process = mock.MagicMock()
     execution = Execution(pid=random.randint(1, 1000),
                           process_obj=mock_process)
     return execution
Example #3
0
    def setUp(self):
        self.staging_order1 = StagingOrder(id=1,
                                           source='/test/this',
                                           staging_target='/foo',
                                           status=StagingStatus.pending)

        self.mock_general_project_repo = mock.MagicMock()

        stdout_mimicing_rsync = """
            Number of files: 1 (reg: 1)
            Number of created files: 0
            Number of deleted files: 0
            Number of regular files transferred: 1
            Total file size: 207,707,566 bytes
            Total transferred file size: 207,707,566 bytes
            Literal data: 207,707,566 bytes
            Matched data: 0 bytes
            File list size: 0
            File list generation time: 0.001 seconds
            File list transfer time: 0.000 seconds
            Total bytes sent: 207,758,378
            Total bytes received: 35

            sent 207,758,378 bytes  received 35 bytes  138,505,608.67 bytes/sec
            total size is 207,707,566  speedup is 1.00
        """

        mock_process = mock.MagicMock()
        mock_execution = Execution(pid=random.randint(1, 1000),
                                   process_obj=mock_process)

        self.mock_external_runner_service = mock.create_autospec(
            ExternalProgramService)
        self.mock_external_runner_service.run.return_value = mock_execution

        @coroutine
        def wait_as_coroutine(x):
            return ExecutionResult(stdout=stdout_mimicing_rsync,
                                   stderr="",
                                   status_code=0)

        self.mock_external_runner_service.wait_for_execution = wait_as_coroutine
        mock_staging_repo = mock.MagicMock()
        mock_staging_repo.get_staging_order_by_id.return_value = self.staging_order1
        mock_staging_repo.create_staging_order.return_value = self.staging_order1

        self.mock_runfolder_repo = mock.MagicMock()

        mock_db_session_factory = mock.MagicMock()

        self.staging_service = StagingService(
            staging_dir="/tmp",
            external_program_service=self.mock_external_runner_service,
            staging_repo=mock_staging_repo,
            runfolder_repo=self.mock_runfolder_repo,
            session_factory=mock_db_session_factory,
            project_dir_repo=self.mock_general_project_repo)
        self.staging_service.io_loop_factory = MockIOLoop
        super(TestStagingService, self).setUp()
Example #4
0
 def run(cmd):
     """
     Run a process and do not wait for it to finish
     :param cmd: the command to run as a list, i.e. ['ls','-l', '/']
     :return: A instance of Execution
     """
     p = Subprocess(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
     return Execution(pid=p.pid, process_obj=p)
    def setUp(self):

        example_mover_stdout = """TestCase_31-ngi2016001-1484739218"""

        example_moverinfo_stdout = """Delivered: Jan 19 00:23:31 [1484781811UTC]"""

        self.mock_mover_runner = create_autospec(ExternalProgramService)
        mock_process = MagicMock()
        mock_execution = Execution(pid=random.randint(1, 1000),
                                   process_obj=mock_process)
        self.mock_mover_runner.run.return_value = mock_execution

        @coroutine
        def wait_as_coroutine(x):
            return ExecutionResult(stdout=example_mover_stdout,
                                   stderr="",
                                   status_code=0)

        self.mock_mover_runner.wait_for_execution = wait_as_coroutine

        self.mock_moverinfo_runner = create_autospec(ExternalProgramService)

        @coroutine
        def mover_info_wait_as_coroutine(x):
            return ExecutionResult(stdout=example_moverinfo_stdout,
                                   stderr="",
                                   status_code=0)

        self.mock_moverinfo_runner.run_and_wait = MagicMock(
            wraps=mover_info_wait_as_coroutine)

        self.mock_staging_service = MagicMock()
        self.mock_delivery_repo = MagicMock()

        self.delivery_order = DeliveryOrder(id=1,
                                            delivery_source="/foo",
                                            delivery_project="TestProj")

        self.mock_delivery_repo.create_delivery_order.return_value = self.delivery_order
        self.mock_delivery_repo.get_delivery_order_by_id.return_value = self.delivery_order

        self.mock_session_factory = MagicMock()
        self.mock_path_to_mover = "/foo/bar/"
        self.mover_delivery_service = MoverDeliveryService(
            external_program_service=None,
            staging_service=self.mock_staging_service,
            delivery_repo=self.mock_delivery_repo,
            session_factory=self.mock_session_factory,
            path_to_mover=self.mock_path_to_mover)

        # Inject separate external runner instances for the tests, since they need to return
        # different information
        self.mover_delivery_service.mover_external_program_service = self.mock_mover_runner
        self.mover_delivery_service.moverinfo_external_program_service = self.mock_moverinfo_runner

        super(TestMoverDeliveryService, self).setUp()
    def setUp(self):

        example_dds_project_ls_stdout = """[
  {
    "Last updated": "Thu, 03 Mar 2022 11:46:31 CET",
    "PI": "Dungeon master",
    "Project ID": "snpseq00001",
    "Size": 26956752654,
    "Status": "In Progress",
    "Title": "Bullywug anatomy"
  },
  {
    "Last updated": "Thu, 03 Mar 2022 10:34:05 CET",
    "PI": "matas618",
    "Project ID": "snpseq00002",
    "Size": 0,
    "Status": "In Progress",
    "Title": "Site admins project"
  }
]
        """


        self.mock_mover_runner = create_autospec(ExternalProgramService)
        mock_process = MagicMock()
        mock_execution = Execution(pid=random.randint(1, 1000), process_obj=mock_process)
        self.mock_mover_runner.run.return_value = mock_execution

        @coroutine
        def wait_as_coroutine(x):
            return ExecutionResult(stdout=example_dds_project_ls_stdout, stderr="", status_code=0)

        self.mock_mover_runner.wait_for_execution = wait_as_coroutine


        self.mock_staging_service = MagicMock()
        self.mock_delivery_repo = MagicMock()
        self.mock_dds_project_repo = MagicMock()

        self.delivery_order = DeliveryOrder(
                id=1,
                delivery_source="/staging/dir/bar",
                delivery_project="snpseq00001",
                )

        self.mock_delivery_repo.create_delivery_order.return_value = self.delivery_order
        self.mock_delivery_repo.get_delivery_order_by_id.return_value = self.delivery_order

        self.mock_session_factory = MagicMock()
        self.mock_dds_config = {
                'log_path': '/foo/bar/log',
                }
        self.dds_service = DDSService(
                external_program_service=ExternalProgramService(),
                staging_service=self.mock_staging_service,
                staging_dir='/foo/bar/staging_dir',
                delivery_repo=self.mock_delivery_repo,
                dds_project_repo=self.mock_dds_project_repo,
                session_factory=self.mock_session_factory,
                dds_conf=self.mock_dds_config
                )

        # Inject separate external runner instances for the tests, since they need to return
        # different information
        self.dds_service.mover_external_program_service = self.mock_mover_runner

        super(TestDDSService, self).setUp()