Repository: rucio/rucio

Issue:
Test reaper console script
Motivation
----------

The reaper console script `rucio-reaper` is not tested in the testsuite.

Modification
------------
- Add test for the reaper console script.
- Install the environnement with `python setup.py develop` in the docker env to have the generated console scripts available in the docker.
- Extend the reaper argparse method and the reaper tests to validate the argparse main method and console script.






Discussion:
Before you go through the effort, we should decide if we want to move all the bin/ scripts to console scripts, or everything back to bin/
Is there any major added benefit of the console_scripts? We had one issue where a dependency conflict was only affecting the console_scripts, not the bin/ scripts. So we should get this consistent in one way or the other.

Resolving patch:
diff --git a/lib/rucio/clis/daemons/reaper/reaper.py b/lib/rucio/clis/daemons/reaper/reaper.py
--- a/lib/rucio/clis/daemons/reaper/reaper.py
+++ b/lib/rucio/clis/daemons/reaper/reaper.py
@@ -1,4 +1,4 @@
-# Copyright 2012-2018 CERN for the benefit of the ATLAS collaboration.
+# Copyright 2012-2019 CERN for the benefit of the ATLAS collaboration.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
 # limitations under the License.
 #
 # Authors:
-# - Vincent Garonne, <vgaronne@gmail.com>, 2012-2018
+# - Vincent Garonne, <vgaronne@gmail.com>, 2012-2019
 # - Wen Guan, <wguan.icedew@gmail.com>, 2014
 # - Hannes Hansen, <hannes.jakob.hansen@cern.ch>, 2018
 
@@ -23,6 +23,7 @@
 
 import argparse
 import signal
+import sys
 
 from rucio.daemons.reaper.reaper import run, stop
 
@@ -72,11 +73,19 @@ def get_parser():
     return parser
 
 
-def main():
+def main(argv=None):
+    """
+    The main reaper method called by the command.
 
+    :param argv: Command-line arguments. Default to  sys.argv if not set.
+    """
     signal.signal(signal.SIGTERM, stop)
+
+    if argv is None:
+        argv = sys.argv[1:]
+
     parser = get_parser()
-    args = parser.parse_args()
+    args = parser.parse_args(argv)
     try:
         run(total_workers=args.total_workers, chunk_size=args.chunk_size, greedy=args.greedy,
             once=args.run_once, scheme=args.scheme, rses=args.rses, threads_per_worker=args.threads_per_worker,


Test patch:
diff --git a/lib/rucio/tests/test_reaper.py b/lib/rucio/tests/test_reaper.py
--- a/lib/rucio/tests/test_reaper.py
+++ b/lib/rucio/tests/test_reaper.py
@@ -1,4 +1,4 @@
-# Copyright 2013-2018 CERN for the benefit of the ATLAS collaboration.
+# Copyright 2013-2019 CERN for the benefit of the ATLAS collaboration.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -13,21 +13,23 @@
 # limitations under the License.
 #
 # Authors:
-# - Vincent Garonne <vgaronne@gmail.com>, 2013-2018
+# - Vincent Garonne <vgaronne@gmail.com>, 2013-2019
 # - Martin Barisits <martin.barisits@cern.ch>, 2016
 # - Joaquin Bogado <jbogado@linti.unlp.edu.ar>, 2018
+import nose.tools
 
-from rucio.common.utils import generate_uuid
+from rucio.common.utils import execute, generate_uuid
 from rucio.core import rse as rse_core
 from rucio.core import replica as replica_core
-from rucio.daemons.reaper.reaper import reaper
+
+from rucio.clis.daemons.reaper.reaper import main
 
 
 def test_reaper():
     """ REAPER (DAEMON): Test the reaper daemon."""
     nb_files = 30
     file_size = 2147483648  # 2G
-    for i in range(nb_files):
+    for _ in range(nb_files):
         replica_core.add_replica(rse='MOCK', scope='data13_hip',
                                  name='lfn' + generate_uuid(), bytes=file_size,
                                  account='root', adler32=None, md5=None)
@@ -36,6 +38,10 @@ def test_reaper():
     rse_core.set_rse_limits(rse='MOCK', name='MinFreeSpace', value=10737418240)
     rse_core.set_rse_limits(rse='MOCK', name='MaxBeingDeletedFiles', value=10)
 
-    rses = [rse_core.get_rse('MOCK'), ]
-    reaper(once=True, rses=rses)
-    reaper(once=True, rses=rses)
+    argv = ['--run-once', '--rses', 'MOCK']
+    main(argv)
+    # Test the rucio-reaper console script
+    cmd = 'rucio-reaper ' + ' '.join(argv)
+    exitcode, out, err = execute(cmd)
+    print(cmd, out, err)
+    nose.tools.assert_equal(exitcode, 0)