def test_add(self): ''' 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): self.assertDictEqual(schedule.add('job1'), {'comment': comm1, 'result': False}) _ret_value = {'complete': True, 'schedule': {}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual(schedule.add('job2', function='test.ping', seconds=3600, when='2400'), {'comment': comm2, 'result': False}) _ret_value = {'complete': True, 'schedule': {}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual(schedule.add('job2', function='test.ping', when='2400', cron='2'), {'comment': comm3, 'result': False}) _ret_value = {'complete': True, 'schedule': {}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual(schedule.add('job2', function='test.ping', test=True), {'comment': comm4, 'result': True})
def test_add(self): ''' 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': self.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): self.assertDictEqual(schedule.add('job1'), { 'comment': comm1, 'result': False }) _ret_value = {'complete': True, 'schedule': {}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual( schedule.add('job2', function='test.ping', seconds=3600, when='2400'), { 'comment': comm2, 'result': False }) _ret_value = {'complete': True, 'schedule': {}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual( schedule.add('job2', function='test.ping', when='2400', cron='2'), { 'comment': comm3, 'result': False }) _ret_value = {'complete': True, 'schedule': {}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual( schedule.add('job2', function='test.ping', test=True), { 'comment': comm4, 'result': True })
def test_add(self): """ 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": self.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): self.assertDictEqual(schedule.add("job1"), { "comment": comm1, "result": False }) _ret_value = {"complete": True, "schedule": {}} with patch.object(SaltEvent, "get_event", return_value=_ret_value): self.assertDictEqual( schedule.add("job2", function="test.ping", seconds=3600, when="2400"), { "comment": comm2, "result": False }, ) _ret_value = {"complete": True, "schedule": {}} with patch.object(SaltEvent, "get_event", return_value=_ret_value): self.assertDictEqual( schedule.add("job2", function="test.ping", when="2400", cron="2"), { "comment": comm3, "result": False }, ) _ret_value = {"complete": True, "schedule": {}} with patch.object(SaltEvent, "get_event", return_value=_ret_value): self.assertDictEqual( schedule.add("job2", function="test.ping", test=True), { "comment": comm4, "result": True }, )
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