File: test_playbook_builder.py

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (66 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytest

import os
import ssg.playbook_builder
import yaml
import shutil


DATADIR = os.path.join(
    os.path.dirname(__file__),
    "test_playbook_builder_data"
)

build_config_yaml = os.path.join(DATADIR, "build_config.yml")
product_yaml = os.path.join(DATADIR, "product.yml")
input_dir = os.path.join(DATADIR, "fixes")
output_dir = os.path.join(DATADIR, "playbooks")
resolved_rules_dir = os.path.join(DATADIR, "rules")
resolved_profiles_dir = os.path.join(DATADIR, "profiles")
rule = "selinux_state"
profile = "ospp"

real_output_filepath = os.path.join(output_dir, profile, rule + ".yml")
expected_output_filepath = os.path.join(DATADIR, "selinux_state.yml")


def test_build_rule_playbook():
    playbook_builder = ssg.playbook_builder.PlaybookBuilder(
        product_yaml, input_dir, output_dir, resolved_rules_dir,
        resolved_profiles_dir, build_config_yaml
    )
    playbook_builder.build(profile, rule)

    assert os.path.exists(real_output_filepath)

    with open(real_output_filepath, "r") as real_output:
        real_output_yaml = yaml.load(real_output, Loader=yaml.Loader)
    with open(expected_output_filepath, "r") as expected_output:
        expected_output_yaml = yaml.load(expected_output, Loader=yaml.Loader)

    real_play = real_output_yaml.pop()
    expected_play = expected_output_yaml.pop()
    assert real_play["become"] == expected_play["become"]
    assert real_play["hosts"] == expected_play["hosts"]
    assert real_play["name"] == expected_play["name"]
    real_vars = real_play["vars"]
    expected_vars = expected_play["vars"]
    assert len(real_vars) == len(expected_vars)
    assert real_vars["var_selinux_state"] == expected_vars["var_selinux_state"]
    real_tags = real_play["tags"]
    expected_tags = expected_play["tags"]
    assert len(real_tags) == len(expected_tags)
    for tag in expected_tags:
        assert tag in real_tags
    for t1, t2 in zip(real_tags, expected_tags):
        assert t1 == t2
    real_tasks = real_play["tasks"]
    expected_tasks = expected_play["tasks"]
    assert len(real_tasks) == len(expected_tasks)
    real_task = real_tasks.pop()
    expected_task = expected_tasks.pop()
    assert real_task["name"] == expected_task["name"]
    assert len(real_task.keys()) == len(expected_task.keys())
    shutil.rmtree(output_dir)