Ejemplo n.º 1
0
def test_purge(sock_dir, job1):
    """
    Test if it purge all the jobs currently scheduled on the minion.
    """
    _schedule_data = {"job1": job1}
    with patch.dict(schedule.__opts__, {"schedule": {}, "sock_dir": sock_dir}):
        mock = MagicMock(return_value=True)
        with patch.dict(schedule.__salt__, {"event.fire": mock}):
            _ret_value = {"complete": True, "schedule": {}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                with patch.object(schedule, "list_",
                                  MagicMock(return_value=_schedule_data)):
                    assert schedule.purge() == {
                        "comment": ["Deleted job: job1 from schedule."],
                        "changes": {
                            "job1": "removed"
                        },
                        "result": True,
                    }

    _schedule_data = {"job1": job1, "job2": job1, "job3": job1}
    comm = [
        "Deleted job: job1 from schedule.",
        "Deleted job: job2 from schedule.",
        "Deleted job: job3 from schedule.",
    ]

    changes = {"job1": "removed", "job2": "removed", "job3": "removed"}

    schedule_config_file = schedule._get_schedule_config_file()
    with patch.dict(schedule.__opts__, {
            "schedule": {
                "job1": "salt"
            },
            "sock_dir": sock_dir
    }):
        with patch("salt.utils.files.fopen",
                   mock_open(read_data="")) as fopen_mock:
            with patch.object(schedule, "list_",
                              MagicMock(return_value=_schedule_data)):
                ret = schedule.purge(offline=True)
                assert any([True for item in comm if item in ret["comment"]])
                assert ret["changes"] == changes
                assert ret["result"]

                _call = call(b"schedule: {}\n")
                write_calls = fopen_mock.filehandles[schedule_config_file][
                    0].write._mock_mock_calls
                assert _call in write_calls
Ejemplo n.º 2
0
def test_delete(sock_dir, job1):
    """
    Test if it delete a job from the minion's schedule.
    """
    with patch.dict(schedule.__opts__, {"schedule": {}, "sock_dir": sock_dir}):
        mock = MagicMock(return_value=True)
        with patch.dict(schedule.__salt__, {"event.fire": mock}):
            _ret_value = {"complete": True, "schedule": {}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                assert schedule.delete("job1") == {
                    "comment": "Job job1 does not exist.",
                    "changes": {},
                    "result": False,
                }

    _schedule_data = {"job1": job1}
    comm = "Deleted Job job1 from schedule."
    changes = {"job1": "removed"}
    schedule_config_file = schedule._get_schedule_config_file()
    with patch.dict(schedule.__opts__, {
            "schedule": {
                "job1": "salt"
            },
            "sock_dir": sock_dir
    }):
        with patch("salt.utils.files.fopen",
                   mock_open(read_data="")) as fopen_mock:
            with patch.object(schedule, "list_",
                              MagicMock(return_value=_schedule_data)):
                assert schedule.delete("job1", offline="True") == {
                    "comment": comm,
                    "changes": changes,
                    "result": True,
                }

                _call = call(b"schedule: {}\n")
                write_calls = fopen_mock.filehandles[schedule_config_file][
                    0].write._mock_mock_calls
                assert _call in write_calls
Ejemplo n.º 3
0
def test_modify(sock_dir, job1):
    """
    Test if modifying job to the schedule.
    """
    current_job1 = {
        "function": "salt",
        "seconds": "3600",
        "maxrunning": 1,
        "name": "job1",
        "enabled": True,
        "jid_include": True,
    }

    new_job1 = {
        "function": "salt",
        "seconds": "60",
        "maxrunning": 1,
        "name": "job1",
        "enabled": True,
        "jid_include": True,
    }

    comm1 = "Modified job: job1 in schedule."
    changes1 = {
        "job1": {
            "new": salt.utils.odict.OrderedDict(new_job1),
            "old": salt.utils.odict.OrderedDict(current_job1),
        }
    }

    new_job4 = {
        "function": "test.version",
        "seconds": "3600",
        "maxrunning": 1,
        "name": "job1",
        "enabled": True,
        "jid_include": True,
    }

    changes4 = {
        "job1": {
            "new": salt.utils.odict.OrderedDict(new_job4),
            "old": salt.utils.odict.OrderedDict(current_job1),
        }
    }

    expected1 = {"comment": comm1, "changes": changes1, "result": True}

    comm2 = ('Error: Unable to use "seconds", "minutes", "hours", '
             'or "days" with "when" option.')
    expected2 = {"comment": comm2, "changes": {}, "result": False}

    comm3 = 'Unable to use "when" and "cron" options together.  Ignoring.'
    expected3 = {"comment": comm3, "changes": {}, "result": False}

    comm4 = "Job: job1 would be modified in schedule."
    expected4 = {"comment": comm4, "changes": changes4, "result": True}

    comm5 = "Job job2 does not exist in schedule."
    expected5 = {"comment": comm5, "changes": {}, "result": False}

    with patch.dict(schedule.__opts__, {
            "schedule": {
                "job1": current_job1
            },
            "sock_dir": sock_dir
    }):
        mock = MagicMock(return_value=True)
        with patch.dict(schedule.__salt__, {"event.fire": mock}):
            _ret_value = {"complete": True, "schedule": {"job1": current_job1}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                ret = schedule.modify("job1", seconds="60")
                assert "job1" in ret["changes"]
                assert "new" in ret["changes"]["job1"]
                assert "old" in ret["changes"]["job1"]

                for key in [
                        "maxrunning",
                        "function",
                        "seconds",
                        "jid_include",
                        "name",
                        "enabled",
                ]:
                    assert (ret["changes"]["job1"]["new"][key] ==
                            expected1["changes"]["job1"]["new"][key])
                    assert (ret["changes"]["job1"]["old"][key] ==
                            expected1["changes"]["job1"]["old"][key])

                assert ret["comment"] == expected1["comment"]
                assert ret["result"] == expected1["result"]

            _ret_value = {"complete": True, "schedule": {"job1": current_job1}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                ret = schedule.modify("job1",
                                      function="test.ping",
                                      seconds=3600,
                                      when="2400")
                assert ret == expected2

            _ret_value = {"complete": True, "schedule": {"job1": current_job1}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                ret = schedule.modify("job1",
                                      function="test.ping",
                                      when="2400",
                                      cron="2")
                assert ret == expected3

            _ret_value = {"complete": True, "schedule": {"job1": current_job1}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                ret = schedule.modify("job1",
                                      function="test.version",
                                      test=True)

                assert "job1" in ret["changes"]
                assert "new" in ret["changes"]["job1"]
                assert "old" in ret["changes"]["job1"]

                for key in [
                        "maxrunning",
                        "function",
                        "jid_include",
                        "name",
                        "enabled",
                ]:
                    assert (ret["changes"]["job1"]["new"][key] ==
                            expected4["changes"]["job1"]["new"][key])
                    assert (ret["changes"]["job1"]["old"][key] ==
                            expected4["changes"]["job1"]["old"][key])

                assert ret["comment"] == expected4["comment"]
                assert ret["result"] == expected4["result"]

            _ret_value = {"complete": True, "schedule": {}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                ret = schedule.modify("job2",
                                      function="test.version",
                                      test=True)
                assert ret == expected5

    _schedule_data = {"job1": job1}
    comm = "Modified job: job1 in schedule."
    changes = {"job1": "removed"}

    changes = {
        "job1": {
            "new":
            OrderedDict([
                ("function", "test.version"),
                ("maxrunning", 1),
                ("name", "job1"),
                ("enabled", True),
                ("jid_include", True),
            ]),
            "old":
            OrderedDict([
                ("function", "test.ping"),
                ("maxrunning", 1),
                ("name", "job1"),
                ("jid_include", True),
                ("enabled", True),
            ]),
        }
    }
    schedule_config_file = schedule._get_schedule_config_file()
    with patch.dict(schedule.__opts__, {
            "schedule": {
                "job1": "salt"
            },
            "sock_dir": sock_dir
    }):
        with patch("salt.utils.files.fopen",
                   mock_open(read_data="")) as fopen_mock:
            with patch.object(schedule, "list_",
                              MagicMock(return_value=_schedule_data)):
                ret = schedule.modify("job1",
                                      function="test.version",
                                      offline="True")
                assert ret["comment"] == comm
                assert ret["result"]
                assert all([
                    True for k, v in ret["changes"]["job1"]["old"].items()
                    if v == changes["job1"]["old"][k]
                ])
                assert all([
                    True for k, v in ret["changes"]["job1"]["new"].items()
                    if v == changes["job1"]["new"][k]
                ])

                _call = call(
                    b"schedule:\n  job1: {enabled: true, function: test.version, jid_include: true, maxrunning: 1,\n    name: job1}\n"
                )
                write_calls = fopen_mock.filehandles[schedule_config_file][
                    0].write._mock_mock_calls
                assert _call in write_calls
Ejemplo n.º 4
0
def test_add(sock_dir):
    """
    Test if it add a job to the schedule.
    """
    comm1 = "Job job1 already exists in schedule."
    comm2 = ('Error: Unable to use "seconds", "minutes", "hours", '
             'or "days" with "when" or "cron" options.')
    comm3 = 'Unable to use "when" and "cron" options together.  Ignoring.'
    comm4 = "Job: job2 would be added to schedule."
    with patch.dict(schedule.__opts__, {
            "schedule": {
                "job1": "salt"
            },
            "sock_dir": sock_dir
    }):
        mock = MagicMock(return_value=True)
        with patch.dict(schedule.__salt__, {"event.fire": mock}):
            _ret_value = {
                "complete": True,
                "schedule": {
                    "job1": {
                        "salt": "salt"
                    }
                }
            }
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                assert schedule.add("job1") == {
                    "comment": comm1,
                    "changes": {},
                    "result": False,
                }

            _ret_value = {"complete": True, "schedule": {}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                assert schedule.add("job2",
                                    function="test.ping",
                                    seconds=3600,
                                    when="2400") == {
                                        "comment": comm2,
                                        "changes": {},
                                        "result": False
                                    }

            _ret_value = {"complete": True, "schedule": {}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                assert schedule.add("job2",
                                    function="test.ping",
                                    when="2400",
                                    cron="2") == {
                                        "comment": comm3,
                                        "changes": {},
                                        "result": False
                                    }
            _ret_value = {"complete": True, "schedule": {}}
            with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                assert schedule.add("job2", function="test.ping",
                                    test=True) == {
                                        "comment": comm4,
                                        "changes": {},
                                        "result": True,
                                    }

    schedule_config_file = schedule._get_schedule_config_file()
    comm1 = "Added job: job3 to schedule."
    changes1 = {"job3": "added"}
    with patch.dict(schedule.__opts__, {
            "schedule": {
                "job1": "salt"
            },
            "sock_dir": sock_dir
    }):
        with patch("os.path.exists", MagicMock(return_value=True)):
            with patch("salt.utils.files.fopen",
                       mock_open(read_data="")) as fopen_mock:
                assert schedule.add("job3",
                                    function="test.ping",
                                    seconds=3600,
                                    offline="True") == {
                                        "comment": comm1,
                                        "changes": changes1,
                                        "result": True
                                    }

                _call = call(
                    b"schedule:\n  job3: {function: test.ping, seconds: 3600, maxrunning: 1, name: job3, enabled: true,\n    jid_include: true}\n"
                )
                write_calls = fopen_mock.filehandles[schedule_config_file][
                    1].write._mock_mock_calls
                assert _call in write_calls