Beispiel #1
0
def test_class5_ex2():
    base_path = "../class5/exercises/exercise2"
    cmd_list = ["ansible-playbook", "exercise2.yml", "-f 12"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0

    # Should be idempotent as would have been configured in exercise1.
    for device in [
            "cisco1",
            "cisco2",
            "cisco5",
            "cisco6",
            "arista5",
            "arista6",
            "arista7",
            "arista8",
            "nxos1",
            "nxos2",
            "vmx1",
            "vmx2",
    ]:
        pattern = rf"^{device}.*changed=0 .*$"
        assert re.search(pattern, std_out, flags=re.M)
Beispiel #2
0
def test_class5_ex1():

    base_path = "../class5/exercises/exercise1"
    cmd_list = ["ansible-playbook", "exercise1.yml", "-f 12"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)

    assert "TASK [IOS global config]" in std_out
    assert "TASK [IOS-XE global config]" in std_out
    assert "TASK [EOS global config]" in std_out
    assert "TASK [NX-OS global config]" in std_out
    assert "TASK [Juniper global config]" in std_out
    for device in [
            "cisco1",
            "cisco2",
            "cisco5",
            "cisco6",
            "arista5",
            "arista6",
            "arista7",
            "arista8",
            "nxos1",
            "nxos2",
            "vmx1",
            "vmx2",
    ]:
        assert std_out.count(device) == 7

    assert std_err == ""
    assert return_code == 0
Beispiel #3
0
def test_bonus2_ex5():
    base_path = "../bonus2/exercises/exercise5"
    cmd_list = ["ansible-playbook", "exercise5.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"cisco1.*ok=1.*changed=1.*failed=0 ", std_out)
Beispiel #4
0
def test_class6_ex3():
    """Should be idempotent on the second execution."""
    base_path = "../class6/exercises/exercise3"
    cmd_list = ["ansible-playbook", "exercise3.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    # Execute script again
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"^cisco1\s+:\s+ok=2.*skipped=1.*$", std_out, flags=re.M)
    assert re.search(r"^cisco2\s+:\s+ok=2.*skipped=1.*$", std_out, flags=re.M)
    assert re.search(r"^cisco5\s+:\s+ok=2.*skipped=1.*$", std_out, flags=re.M)
    assert re.search(r"^cisco6\s+:\s+ok=2.*skipped=1.*$", std_out, flags=re.M)
Beispiel #5
0
def test_class8_ex7a():
    """Should fail when no vault password is provided."""
    base_path = "../class8/exercises/exercise7"
    cmd_list = ["ansible-playbook", "exercise7.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert return_code != 0
Beispiel #6
0
def test_class8_ex6_fixed(playbook):
    base_path = "../class8/exercises/exercise6/solutions"
    cmd_list = ["ansible-playbook", playbook]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
Beispiel #7
0
def test_class8_ex6_errors(playbook):
    """Each of these tests should have errors on std_err."""
    base_path = "../class8/exercises/exercise6"
    cmd_list = ["ansible-playbook", playbook]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert return_code != 0
Beispiel #8
0
def test_class6_ex2a_2b(exercise):
    base_path = "../class6/exercises/exercise2"
    cmd_list = ["ansible-playbook", exercise]
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert "localhost                  : ok=2" in std_out
Beispiel #9
0
def test_bonus2_ex1():
    base_path = "../bonus2/exercises/exercise1"
    cmd_list = ["ansible-playbook", "exercise1.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    std_out = std_out.lower()
    assert std_out.count("hello world") == 3
Beispiel #10
0
def test_class5_ex3a():
    base_path = "../class5/exercises/exercise3"
    cmd_list = ["ansible-playbook", "exercise3a.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_out.count("permit") == 20
    assert std_err == ""
    assert return_code == 0
Beispiel #11
0
def test_runner_collateral(test_case):
    path_obj = Path(test_case)
    script = path_obj.name
    script_dir = path_obj.parents[0]
    cmd_list = ["ansible-playbook", script]
    std_out, std_err, return_code = subprocess_runner(cmd_list, script_dir)
    std_err = remove_ansible_warnings(std_err)
    assert return_code == 0
    assert std_err == ""
Beispiel #12
0
def test_class6_ex6():
    base_path = "../class6/exercises/exercise6"
    cmd_list = ["ansible-playbook", "exercise6.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"^cisco5\s+:\s+ok=1 ", std_out, flags=re.M)
    assert re.search(r"^cisco6\s+:\s+ok=1 ", std_out, flags=re.M)
Beispiel #13
0
def test_class8_ex4():
    base_path = "../class8/exercises/exercise4"
    cmd_list = ["ansible-playbook", "exercise4.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    for switch in ("arista5", "arista6", "arista7", "arista8"):
        assert re.search(rf"{switch}.*ok=6.*failed=0", std_out)
Beispiel #14
0
def test_bonus1_ex3():
    base_path = "../bonus1/exercises/exercise3"
    cmd_list = ["ansible-playbook", "exercise3.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    for hostname in ("arista5", ):
        assert re.search(rf"{hostname}.*ok=4.*failed=0 ", std_out)
Beispiel #15
0
def test_class8_ex5():
    base_path = "../class8/exercises/exercise5"
    cmd_list = ["ansible-playbook", "exercise5.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"nxos1.*ok=8", std_out)
    assert re.search(r"nxos2.*ok=7", std_out)
Beispiel #16
0
def test_class8_ex6a():
    """Exercise 6a doesn't actually fail."""
    base_path = "../class8/exercises/exercise6"
    cmd_list = ["ansible-playbook", "exercise6a.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"msg.*show_lldp", std_out)
Beispiel #17
0
def test_class5_ex5():
    base_path = "../class5/exercises/exercise5"
    cmd_list = [
        "ansible-playbook", "exercise5.yml", "-i", "./ansible-hosts.ini"
    ]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_out.count("student1") == 2
    assert std_err == ""
    assert return_code == 0
Beispiel #18
0
def test_class6_ex2c(tags, result):
    base_path = "../class6/exercises/exercise2"
    if tags:
        cmd_list = ["ansible-playbook", "exercise2c.yml", "--tags", tags]
    else:
        cmd_list = ["ansible-playbook", "exercise2c.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list, exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert result in std_out
Beispiel #19
0
def test_class7_ex3b():
    base_path = "../class7/exercises/exercise3"
    cmd_list = ["ansible-playbook", "exercise3b.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"arista5.*ok=6 ", std_out)
    assert re.search(r"arista6.*ok=6 ", std_out)
    assert re.search(r"arista7.*ok=6 ", std_out)
    assert re.search(r"arista8.*ok=6 ", std_out)
Beispiel #20
0
def test_class7_ex1():
    base_path = "../class7/exercises/exercise1"
    cmd_list = ["ansible-playbook", "exercise1.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert "nxos1                      : ok=7" in std_out
    assert "nxos2                      : ok=7" in std_out
    assert re.search(r"nxos1.*failed=0", std_out)
    assert re.search(r"nxos2.*failed=0", std_out)
Beispiel #21
0
def test_class7_ex6():
    base_path = "../class7/exercises/exercise6"
    cmd_list = ["ansible-inventory", "--graph", "-i", "./dyn_inv.py"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert "@arista" in std_out
    assert "@cisco" in std_out
    assert "@juniper" in std_out
    assert "@local" in std_out
    assert "@nxos" in std_out
Beispiel #22
0
def test_class2_ex1b():
    base_path = "../class2/exercises/exercise1"
    cmd_list = ["ansible-playbook", "exercise1b.yml"]

    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    assert std_out.count("10.220.88.32") == 3
    assert '"ansible_host": "arista5.lasthop.io"' in std_out
    assert (
        "arista5                    : ok=5    changed=0    unreachable=0    failed=0    "
        "skipped=0    rescued=0    ignored=0" in std_out)
    std_err = remove_ansible_warnings(std_err)
    assert std_err.strip() == ""
    assert return_code == 0
Beispiel #23
0
def test_class5_ex4():
    base_path = "../class5/exercises/exercise4"
    cmd_list = ["ansible-playbook", "exercise4.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert "TASK [Generate interface configuration]" in std_out
    assert "TASK [Generate BGP configuration]" in std_out
    assert "TASK [Assemble the two parts into one configuration change file]" in std_out
    assert "TASK [Deploy configurations to devices]" in std_out
    assert "BGP router identifier 172.31.101.101, local AS number 22" in std_out
    assert "BGP router identifier 172.31.101.102, local AS number 22" in std_out
    assert std_err == ""
    assert return_code == 0
Beispiel #24
0
def test_class8_ex7b():
    """Should fail when no vault password is provided."""
    base_path = "../class8/exercises/exercise7"
    cmd_list = [
        "ansible-playbook",
        "exercise7.yml",
        "--vault-password-file",
        ".my_vault",
    ]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert return_code == 0
    assert std_err == ""
    for switch in ("arista5", "arista6", "arista7", "arista8"):
        assert re.search(rf"{switch}.*ok=2.*failed=0", std_out)
    assert std_out.count("130.126.24.24") == 4
    assert std_out.count("bogus.com") == 4
Beispiel #25
0
def test_class2_ex1e():
    base_path = "../class2/exercises/exercise1/exercise1e"
    cmd_list = ["ansible-playbook", "exercise1e.yml"]

    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    assert std_out.count("10.220.88.32") == 3
    assert '"ansible_host": "arista5.lasthop.io"' in std_out
    assert '"ansible_network_os": "eos"' in std_out
    assert '"ansible_host": "arista5.lasthop.io"' in std_out
    assert '"desired_eos_version": "4.21.1"'
    assert '"device_hostname": "arista5.lab.io"'
    assert (
        "arista5                    : ok=8    changed=0    unreachable=0    failed=0    "
        "skipped=0    rescued=0    ignored=0" in std_out)

    std_err = remove_ansible_warnings(std_err)
    assert std_err.strip() == ""
    assert return_code == 0
Beispiel #26
0
def test_bonus1_ex1():
    base_path = "../bonus1/exercises/exercise1"
    cmd_list = ["ansible-playbook", "exercise1.yml"]
    std_out, std_err, return_code = subprocess_runner(cmd_list,
                                                      exercise_dir=base_path)
    std_err = remove_ansible_warnings(std_err)
    assert std_err == ""
    assert return_code == 0
    assert re.search(r"arista5.*ok=4.*failed=0 ", std_out)
    for hostname in (
            "arista6",
            "arista7",
            "arista8",
            "cisco1",
            "cisco2",
            "cisco5",
            "cisco6",
            "nxos1",
            "nxos2",
    ):
        assert re.search(rf"{hostname}.*ok=3.*failed=0 ", std_out)