|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# sonar-tools tests |
| 4 | +# Copyright (C) 2024-2025 Olivier Korach |
| 5 | +# mailto:olivier.korach AT gmail DOT com |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 3 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this program; if not, write to the Free Software Foundation, |
| 19 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | +# |
| 21 | + |
| 22 | +"""sonar-audit CLI tests""" |
| 23 | + |
| 24 | +import os |
| 25 | +from collections.abc import Generator |
| 26 | + |
| 27 | +import utilities as tutil |
| 28 | +from sonar import errcodes as e |
| 29 | +import cli.options as opt |
| 30 | +from cli import audit |
| 31 | + |
| 32 | +CMD = f"sonar-audit.py {tutil.SQS_OPTS}" |
| 33 | + |
| 34 | +AUDIT_DISABLED = """ |
| 35 | +audit.globalSettings = no |
| 36 | +audit.projects = false |
| 37 | +audit.qualityGates = no |
| 38 | +audit.qualityProfiles = no |
| 39 | +audit.users = no |
| 40 | +audit.groups = no |
| 41 | +audit.portfolios = no |
| 42 | +audit.applications = no |
| 43 | +audit.logs = no |
| 44 | +audit.plugins = no""" |
| 45 | + |
| 46 | + |
| 47 | +def test_audit_disabled(csv_file: Generator[str]) -> None: |
| 48 | + """Tests that nothing is output when all audits are disabled""" |
| 49 | + with open(".sonar-audit.properties", mode="w", encoding="utf-8") as fd: |
| 50 | + print(AUDIT_DISABLED, file=fd) |
| 51 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {csv_file}") == e.OK |
| 52 | + os.remove(".sonar-audit.properties") |
| 53 | + assert tutil.csv_nbr_lines(csv_file) == 0 |
| 54 | + |
| 55 | + |
| 56 | +def test_audit_stdout() -> None: |
| 57 | + """Tests audit to stdout""" |
| 58 | + assert tutil.run_cmd(audit.main, CMD) == e.OK |
| 59 | + |
| 60 | + |
| 61 | +def test_audit_json(json_file: Generator[str]) -> None: |
| 62 | + """Test audit to json file""" |
| 63 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {json_file}") == e.OK |
| 64 | + |
| 65 | + |
| 66 | +def test_audit_proj_key(csv_file: Generator[str]) -> None: |
| 67 | + """Tests that audit can select only specific project keys""" |
| 68 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {csv_file} --{opt.WHAT} projects --{opt.KEY_REGEXP} {tutil.LIVE_PROJECT}") == e.OK |
| 69 | + |
| 70 | + |
| 71 | +def test_audit_proj_non_existing_key() -> None: |
| 72 | + """Tests that error is raised when the project key regexp does not select any project""" |
| 73 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.WHAT} projects --{opt.KEY_REGEXP} {tutil.LIVE_PROJECT},bad_key") == e.ARGS_ERROR |
| 74 | + |
| 75 | + |
| 76 | +def test_audit_cmd_line_settings(csv_file: Generator[str]) -> None: |
| 77 | + """Verifies that passing audit settings from command line with -D<key>=<value> works""" |
| 78 | + what_to_audit = ["logs", "projects", "portfolios", "applications", "qualityProfiles", "qualityGates", "users", "groups"] |
| 79 | + cli_opt = " ".join([f"-Daudit.{what}=true" for what in what_to_audit]) |
| 80 | + assert tutil.run_cmd(audit.main, f"{CMD} {cli_opt} --{opt.REPORT_FILE} {csv_file}") == e.OK |
| 81 | + assert tutil.csv_nbr_lines(csv_file) > 0 |
| 82 | + |
| 83 | + cli_opt = " ".join([f"-Daudit.{what}=false" for what in what_to_audit + ["globalSettings"]]) |
| 84 | + assert tutil.run_cmd(audit.main, f"{CMD} {cli_opt} --{opt.REPORT_FILE} {csv_file}") == e.OK |
| 85 | + assert tutil.csv_nbr_lines(csv_file) == 0 |
| 86 | + |
| 87 | + |
| 88 | +def test_filter_severity(csv_file: Generator[str]) -> None: |
| 89 | + """Verify that filtering by severities works""" |
| 90 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {csv_file} --{opt.SEVERITIES} MEDIUM,HIGH") == e.OK |
| 91 | + assert tutil.csv_nbr_lines(csv_file) > 0 |
| 92 | + assert tutil.csv_col_is_value(csv_file, "Severity", "MEDIUM", "HIGH") |
| 93 | + |
| 94 | + |
| 95 | +def test_filter_type(json_file: Generator[str]) -> None: |
| 96 | + """Verify that filtering by severities works""" |
| 97 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {json_file} --{opt.TYPES} HOUSEKEEPING,SECURITY") == e.OK |
| 98 | + assert tutil.json_field_in_values(json_file, "type", "HOUSEKEEPING", "SECURITY") |
| 99 | + |
| 100 | + |
| 101 | +def test_filter_problem(csv_file: Generator[str]) -> None: |
| 102 | + """Verify that filtering by problem id works""" |
| 103 | + regexp = "(OBJECT.+|QG.+)" |
| 104 | + assert tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {csv_file} --problems {regexp}") == e.OK |
| 105 | + assert tutil.csv_col_match(csv_file, "Problem", regexp) |
| 106 | + |
| 107 | + |
| 108 | +def test_filter_multiple(csv_file: Generator[str]) -> None: |
| 109 | + """Verify that filtering by problem id works""" |
| 110 | + regexp = "(OBJECT.+|QG.+)" |
| 111 | + assert ( |
| 112 | + tutil.run_cmd(audit.main, f"{CMD} --{opt.REPORT_FILE} {csv_file} --{opt.TYPES} HOUSEKEEPING --{opt.SEVERITIES} MEDIUM --problems {regexp}") |
| 113 | + == e.OK |
| 114 | + ) |
| 115 | + assert tutil.csv_col_is_value(csv_file, "Severity", "MEDIUM") |
| 116 | + assert tutil.csv_col_is_value(csv_file, "Type", "HOUSEKEEPING") |
| 117 | + assert tutil.csv_col_match(csv_file, "Problem", regexp) |
0 commit comments