|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +''' |
| 3 | + :codeauthor: :email:`Rupesh Tare <rupesht@saltstack.com>` |
| 4 | +''' |
| 5 | + |
| 6 | +# Import Salt Testing Libs |
| 7 | +from salttesting import TestCase, skipIf |
| 8 | +from salttesting.mock import ( |
| 9 | + MagicMock, |
| 10 | + patch, |
| 11 | + NO_MOCK, |
| 12 | + NO_MOCK_REASON |
| 13 | +) |
| 14 | + |
| 15 | +# Import Salt Libs |
| 16 | +from salt.modules import at |
| 17 | + |
| 18 | +# Import salt libs |
| 19 | +import salt.utils |
| 20 | + |
| 21 | +# Globals |
| 22 | +at.__grains__ = {} |
| 23 | + |
| 24 | + |
| 25 | +@skipIf(NO_MOCK, NO_MOCK_REASON) |
| 26 | +class AtTestCase(TestCase): |
| 27 | + ''' |
| 28 | + TestCase for the salt.modules.at module |
| 29 | + ''' |
| 30 | + |
| 31 | + atq_output = {'jobs': [{'date': '2014-12-11', 'job': 101, 'queue': 'A', |
| 32 | + 'tag': '', 'time': '19:48:47', 'user': 'B'}]} |
| 33 | + |
| 34 | + @patch('salt.modules.at._cmd', MagicMock(return_value=None)) |
| 35 | + def test_atq_not_available(self): |
| 36 | + ''' |
| 37 | + Tests the at.atq not available for any type of os_family. |
| 38 | + ''' |
| 39 | + with patch.dict(at.__grains__, {'os_family': 'RedHat'}): |
| 40 | + self.assertEqual(at.atq(), '\'at.atq\' is not available.') |
| 41 | + |
| 42 | + with patch.dict(at.__grains__, {'os_family': ''}): |
| 43 | + self.assertEqual(at.atq(), '\'at.atq\' is not available.') |
| 44 | + |
| 45 | + @patch('salt.modules.at._cmd', MagicMock(return_value='')) |
| 46 | + def test_atq_no_jobs_available(self): |
| 47 | + ''' |
| 48 | + Tests the no jobs available for any type of os_family. |
| 49 | + ''' |
| 50 | + with patch.dict(at.__grains__, {'os_family': 'RedHat'}): |
| 51 | + self.assertDictEqual(at.atq(), {'jobs': []}) |
| 52 | + |
| 53 | + with patch.dict(at.__grains__, {'os_family': ''}): |
| 54 | + self.assertDictEqual(at.atq(), {'jobs': []}) |
| 55 | + |
| 56 | + @patch('salt.modules.at._cmd') |
| 57 | + def test_atq_list(self, salt_modules_at__cmd_mock): |
| 58 | + ''' |
| 59 | + Tests the list all queued and running jobs. |
| 60 | + ''' |
| 61 | + salt_modules_at__cmd_mock.return_value = '101\tThu Dec 11 \ |
| 62 | + 19:48:47 2014 A B' |
| 63 | + with patch.dict(at.__grains__, {'os_family': '', 'os': ''}): |
| 64 | + self.assertDictEqual(at.atq(), {'jobs': [{'date': '2014-12-11', |
| 65 | + 'job': 101, |
| 66 | + 'queue': 'A', |
| 67 | + 'tag': '', |
| 68 | + 'time': '19:48:00', |
| 69 | + 'user': 'B'}]}) |
| 70 | + |
| 71 | + salt_modules_at__cmd_mock.return_value = '101\t2014-12-11 \ |
| 72 | + 19:48:47 A B' |
| 73 | + with patch.dict(at.__grains__, {'os_family': 'RedHat', 'os': ''}): |
| 74 | + self.assertDictEqual(at.atq(), {'jobs': [{'date': '2014-12-11', |
| 75 | + 'job': 101, |
| 76 | + 'queue': 'A', |
| 77 | + 'tag': '', |
| 78 | + 'time': '19:48:47', |
| 79 | + 'user': 'B'}]}) |
| 80 | + |
| 81 | + salt_modules_at__cmd_mock.return_value = 'SALT: Dec 11, \ |
| 82 | + 2014 19:48 A 101 B' |
| 83 | + with patch.dict(at.__grains__, {'os_family': '', 'os': 'OpenBSD'}): |
| 84 | + self.assertDictEqual(at.atq(), {'jobs': [{'date': '2014-12-11', |
| 85 | + 'job': '101', |
| 86 | + 'queue': 'B', |
| 87 | + 'tag': '', |
| 88 | + 'time': '19:48:00', |
| 89 | + 'user': 'A'}]}) |
| 90 | + |
| 91 | + @patch('salt.modules.at.atq', MagicMock(return_value=atq_output)) |
| 92 | + def test_atrm(self): |
| 93 | + """ |
| 94 | + Tests for remove jobs from the queue. |
| 95 | + """ |
| 96 | + with patch.object(salt.utils, 'which', return_value=None): |
| 97 | + self.assertEqual(at.atrm(), "'at.atrm' is not available.") |
| 98 | + |
| 99 | + with patch.object(salt.utils, 'which', return_value=True): |
| 100 | + self.assertDictEqual(at.atrm(), {'jobs': {'removed': [], |
| 101 | + 'tag': None}}) |
| 102 | + |
| 103 | + with patch.object(at, '_cmd', return_value=True): |
| 104 | + with patch.object(salt.utils, 'which', return_value=True): |
| 105 | + self.assertDictEqual(at.atrm('all'), |
| 106 | + {'jobs': {'removed': ['101'], |
| 107 | + 'tag': None}}) |
| 108 | + |
| 109 | + with patch.object(at, '_cmd', return_value=True): |
| 110 | + with patch.object(salt.utils, 'which', return_value=True): |
| 111 | + self.assertDictEqual(at.atrm(101), |
| 112 | + {'jobs': {'removed': ['101'], |
| 113 | + 'tag': None}}) |
| 114 | + |
| 115 | + with patch.object(at, '_cmd', return_value=None): |
| 116 | + self.assertEqual(at.atrm(101), '\'at.atrm\' is not available.') |
| 117 | + |
| 118 | + @patch('salt.modules.at.atq', MagicMock(return_value=atq_output)) |
| 119 | + def test_jobcheck(self): |
| 120 | + """ |
| 121 | + Tests for check the job from queue. |
| 122 | + """ |
| 123 | + self.assertDictEqual(at.jobcheck(), |
| 124 | + {'error': 'You have given a condition'}) |
| 125 | + |
| 126 | + self.assertDictEqual(at.jobcheck(runas='foo'), |
| 127 | + {'note': 'No match jobs or time format error', |
| 128 | + 'jobs': []}) |
| 129 | + |
| 130 | + self.assertDictEqual(at.jobcheck(runas='B', tag='', hour=19, minute=48, |
| 131 | + day=11, month=12, Year=2014), |
| 132 | + {'jobs': [{'date': '2014-12-11', |
| 133 | + 'job': 101, |
| 134 | + 'queue': 'A', |
| 135 | + 'tag': '', |
| 136 | + 'time': '19:48:47', |
| 137 | + 'user': 'B'}]}) |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + from integration import run_tests |
| 141 | + run_tests(AtTestCase, needs_daemon=False) |
0 commit comments