Пример #1
0
class TestNegateWithUndo:
    @pytest.fixture(autouse=True)
    def setUpClass(self, options_negate_with_undo):
        self.os = "comware5"
        self.running_config = "test_for_undo\nundo test_for_redo"
        self.generated_config = "undo test_for_undo\ntest_for_redo"
        self.remediation = "undo test_for_undo\ntest_for_redo"
        self.host = Host("example1.rtr", self.os, options_negate_with_undo)

    def test_merge(self):
        self.host.load_running_config(self.running_config)
        self.host.load_generated_config(self.generated_config)
        self.host.remediation_config()
        assert self.remediation == str(self.host.remediation_config())
Пример #2
0
class TestHost:
    @pytest.fixture(autouse=True)
    def setup(self, options_ios):
        self.host = Host("example.rtr", "ios", options_ios)

    def test_load_config_from(self, running_config, generated_config):
        self.host.load_running_config(running_config)
        self.host.load_generated_config(generated_config)

        assert len(self.host.generated_config) > 0
        assert len(self.host.running_config) > 0

    def test_load_remediation(self, running_config, generated_config):
        self.host.load_running_config(running_config)
        self.host.load_generated_config(generated_config)
        self.host.remediation_config()

        assert len(self.host.remediation_config().children) > 0

    def test_load_tags(self, tags_ios):
        self.host.load_tags(tags_ios)
        assert len(self.host.hconfig_tags) > 0

    def test_filter_remediation(self, running_config, generated_config,
                                tags_ios):
        self.host.load_running_config(running_config)
        self.host.load_generated_config(generated_config)
        self.host.load_tags(tags_ios)

        rem1 = self.host.remediation_config_filtered_text(set(), set())
        rem2 = self.host.remediation_config_filtered_text({"safe"}, set())

        assert rem1 != rem2
def main():
    module = AnsibleModule(
        argument_spec=dict(
            hostname=dict(required=True, type="str"),
            generated_config=dict(required=False, type="str"),
            generated_config_string=dict(required=False, type="str"),
            running_config=dict(required=False, type="str"),
            running_config_string=dict(required=False, type="str"),
            remediation_config=dict(required=False, type="str"),
            os_role=dict(required=True, type="str"),
            options_file=dict(required=False, type="str"),
            tags_file=dict(required=False, type="str"),
            include_tags=dict(required=False, type="list"),
            exclude_tags=dict(required=False, type="list"),
        ),
        required_one_of=[
            ["generated_config", "generated_config_string"],
            ["running_config", "running_config_string"],
        ],
        mutually_exclusive=[
            ["generated_config", "generated_config_string"],
            ["running_config", "running_config_string"],
        ],
        supports_check_mode=False,
    )
    changed = False
    hostname = module.params["hostname"]
    remediation_config = module.params["remediation_config"]
    os_role = module.params["os_role"]
    operating_system = os_role.strip("os_")
    include_tags = (module.params["include_tags"]
                    if module.params["include_tags"] else None)
    exclude_tags = (module.params["exclude_tags"]
                    if module.params["exclude_tags"] else None)
    hier_options = _load_hier("options", os_role=os_role, module=module)
    hier_tags = _load_hier("tags", os_role=os_role, module=module)

    host = Host(hostname, operating_system, hier_options)
    if module.params["running_config"]:
        host.load_running_config_from_file(
            file=module.params["running_config"])
    else:
        host.load_running_config(
            config_text=module.params["running_config_string"])

    if module.params["generated_config"]:
        host.load_generated_config_from_file(
            file=module.params["generated_config"])
    else:
        host.load_generated_config(
            config_text=module.params["generated_config_string"])

    host.load_tags(hier_tags)
    host.remediation_config()
    remediation_obj = host.remediation_config_filtered_text(
        include_tags=include_tags, exclude_tags=exclude_tags)

    remediation_config_string = "".join([line for line in remediation_obj])

    if remediation_config is not None:
        md5_original = None
        if os.path.isfile(remediation_config):
            md5_original = hashlib.md5(
                open(remediation_config).read().encode("utf-8")).hexdigest()

        with open(remediation_config, "w") as tmp:
            tmp.write(remediation_config_string)

        md5_new = hashlib.md5(
            open(remediation_config).read().encode("utf-8")).hexdigest()

        if len({md5_new, md5_original}) > 1:
            changed = True
    else:
        if remediation_config_string:
            changed = True

    module.exit_json(changed=changed, response=remediation_config_string)