Passed
Pull Request — master (#4216)
by Matěj
02:33 queued 10s
created

test_ansible.test_when_update()   B

Complexity

Conditions 1

Size

Total Lines 45
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nop 0
dl 0
loc 45
rs 8.9919
c 0
b 0
f 0
1
import os
2
import re
3
import textwrap
4
from collections import OrderedDict
5
6
import pytest
7
8
import ssg.ansible
9
from ssg.constants import min_ansible_version
10
11
12
def strings_equal_except_whitespaces(left, right):
13
    return re.sub(r'\s', '', left) == re.sub(r'\s', '', right)
14
15
16
def test_add_minimum_version():
17
    good_snippet = """
18
    # a comment
19
     - hosts: all
20
       vars:
21
       tasks:
22
    """
23
    good_snippet = textwrap.dedent(good_snippet)
24
25
    bad_snippet = """
26
    # a comment
27
     - hosts: all
28
       pre_tasks:
29
         - name: something_else
30
           assert:
31
             that: "2 > 3"
32
             msg: "two is less than three!"
33
       vars:
34
       tasks:
35
    """
36
    bad_snippet = textwrap.dedent(bad_snippet)
37
38
    unknown_snippet = """
39
    I don't think this is YAML
40
    """
41
42
    processed_snippet = """
43
    # a comment
44
     - hosts: all
45
       pre_tasks:
46
         - name: Verify Ansible meets SCAP-Security-Guide version requirements.
47
           assert:
48
             that: "ansible_version.full is version_compare('{min_version}', '>=')"
49
             msg: >
50
               "You must update Ansible to at least version {min_version} to use this role."
51
52
       vars:
53
       tasks:
54
    """.format(min_version=min_ansible_version)
55
    processed_snippet = textwrap.dedent(processed_snippet)
56
57
    output = ssg.ansible.add_minimum_version(good_snippet)
58
    assert strings_equal_except_whitespaces(output, processed_snippet)
59
60
    with pytest.raises(ValueError):
61
        ssg.ansible.add_minimum_version(bad_snippet)
62
63
    assert ssg.ansible.add_minimum_version(unknown_snippet) == unknown_snippet
64
65
    assert ssg.ansible.add_minimum_version(processed_snippet) == processed_snippet
66