From 2421f5dfbf662ed9ed945b8b363dd8af5a11401e Mon Sep 17 00:00:00 2001
From: Valerij Maljulin <vmaljuli@redhat.com>
Date: Dec 03 2020 11:26:37 +0000
Subject: Added --scenario argument to CLI


JIRA: RHELWF-1882

---

diff --git a/tests/test_cli.py b/tests/test_cli.py
index 9b29249..c648e1d 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -389,6 +389,40 @@ koji_base_url=https://koji.fedoraproject.org/kojihub
         )
 
 
+def test_create_waiver_product_version_from_koji_build_scenario(tmpdir):
+    with patch('requests.request') as mock_request:
+        mock_rv = Mock()
+        mock_rv.json.return_value = [{
+            "comment": "This is fine",
+            "id": 15,
+            "subject_type": "koji_build",
+            "subject_identifier": "setup-2.8.71-7.el7_4",
+            "scenario": "somescenario",
+            "testcase": "test.testcase",
+            "timestamp": "2017-010-16T17:42:04.209638",
+            "username": "foo",
+            "waived": True
+        }]
+        mock_request.return_value = mock_rv
+        p = tmpdir.join('client.conf')
+        p.write("""
+[waiverdb]
+auth_method=dummy
+api_url=http://localhost:5004/api/v1.0
+koji_base_url=https://koji.fedoraproject.org/kojihub
+        """)
+        runner = CliRunner()
+        args = ['-C', p.strpath, '-s', '{"type": "koji_build", "item": "setup-2.8.71-7.el7_4"}',
+                '-S', 'somescenario', '-t', 'test.testcase', '-c', "This is fine"]
+        result = runner.invoke(waiverdb_cli, args, catch_exceptions=False)
+        mock_request.assert_called()
+        assert result.output == (
+            'Created waiver 15 for result with '
+            'subject type koji_build, identifier setup-2.8.71-7.el7_4 '
+            'and testcase test.testcase, scenario is somescenario\n'
+        )
+
+
 def test_create_waiver_product_version_from_compose(tmpdir):
     with patch('requests.request') as mock_request:
         mock_rv = Mock()
diff --git a/waiverdb/cli.py b/waiverdb/cli.py
index 3612d61..af3d926 100644
--- a/waiverdb/cli.py
+++ b/waiverdb/cli.py
@@ -86,6 +86,8 @@ def check_response(resp, result_ids):
             waiver_id = data['id']
             msg = 'subject type {0}, identifier {1} and testcase {2}'.format(
                 data['subject_type'], data['subject_identifier'], data['testcase'])
+            if data.get('scenario'):
+                msg += f", scenario is {data['scenario']}"
             print_result(waiver_id, msg)
 
 
@@ -122,6 +124,8 @@ def guess_product_version(toparse, koji_build=False):
               help='Specify a config file to use')
 @click.option('--result-id', '-r', multiple=True, type=int,
               help='Specify one or more results to be waived')
+@click.option('--scenario', '-S',
+              help='Specify a scenario for a result to waive')
 @click.option('--subject', '-s', type=OldJSONSubject(),
               help=('Deprecated. Use --subject-identifier and --subject-type instead. '
                     'Subject for a result to waive.'))
@@ -139,7 +143,7 @@ def guess_product_version(toparse, koji_build=False):
               help='A comment explaining why the result is waived')
 @click.option('--username', '-u', default=None,
               help='Username on whose behalf the caller is proxying.')
-def cli(username, comment, waived, product_version, testcase, subject, subject_identifier,
+def cli(username, comment, waived, product_version, testcase, scenario, subject, subject_identifier,
         subject_type, result_id, config_file):
     """
     Creates new waiver against test results.
@@ -151,7 +155,7 @@ def cli(username, comment, waived, product_version, testcase, subject, subject_i
 
     \b
         waiverdb-cli -t dist.rpmdeplint -i qclib-1.3.1-3.fc28 -T koji_build \\
-            -p "fedora-28" -c "This is expected for non-x86 packages"
+            -p "fedora-28" -S "somescenario" -c "This is expected for non-x86 packages"
     """
     config = configparser.ConfigParser()
 
@@ -227,6 +231,8 @@ def cli(username, comment, waived, product_version, testcase, subject, subject_i
             'comment': comment,
             'username': username
         })
+        if scenario:
+            data_list[-1]['scenario'] = scenario
 
     # XXX - TODO - remove this in a future release.  (for backwards compat)
     for result_id in result_ids:

