From 052061c25bfb0d141da033a69d321100035a9382 Mon Sep 17 00:00:00 2001 From: Vojtech Polasek Date: Thu, 8 Aug 2024 11:08:48 +0200 Subject: [PATCH] apply suggestions provided by Codeclimate extract setting of test scenario variables to a separate function split long strings to multiple lines --- shared/templates/sshd_lineinfile/template.py | 28 +++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/shared/templates/sshd_lineinfile/template.py b/shared/templates/sshd_lineinfile/template.py index 82685617f569..eac4b0b87865 100644 --- a/shared/templates/sshd_lineinfile/template.py +++ b/shared/templates/sshd_lineinfile/template.py @@ -1,12 +1,32 @@ from ssg.utils import parse_template_boolean_value +def set_variables_for_test_scenarios(data): + if data["datatype"] == "int": + if not data.get("value"): + data["wrong_value"] = 123456 + data["correct_value"] = 0 + else: + data["wrong_value"] = str(int(data["value"]) + 1) + data["correct_value"] = str(data["value"]) + elif data["datatype"] == "string": + if not data.get("value"): + data["wrong_value"] = "wrong_value" + data["correct_value"] = "correct_value" + else: + data["wrong_value"] = "wrong_value" + data["correct_value"] = str(data["value"]) + + return data + def preprocess(data, lang): if data.get("value") is not None and data.get("xccdf_variable") is not None: - errmsg = "The template definition of {0} specifies both value and xccdf_variable. This is forbidden.".format(data["_rule_id"]) + errmsg = ("The template definition of {0} specifies both value and xccdf_variable." + "This is forbidden.".format(data["_rule_id"])) raise ValueError(errmsg) if data["datatype"] not in ["string", "int"]: - errmsg = "The template instance of the rule {0} contains invalid datatype. It must be either 'string' or 'int'".format(data["_rule_id"]) + errmsg = ("The template instance of the rule {0} contains invalid datatype." + "It must be either 'string' or 'int'".format(data["_rule_id"])) raise ValueError(errmsg) data["missing_parameter_pass"] = parse_template_boolean_value( data, parameter="missing_parameter_pass", default_value=False) @@ -18,7 +38,6 @@ def preprocess(data, lang): else: data["config_basename"] = "00-complianceascode-hardening.conf" - # set variables used in test scenarios if data["datatype"] == "int": if not data.get("value"): data["wrong_value"] = 123456 @@ -34,4 +53,5 @@ def preprocess(data, lang): data["wrong_value"] = "wrong_value" data["correct_value"] = str(data["value"]) - return data + return set_variables_for_test_scenarios(data) +