Exemplo n.º 1
0
    def test_uptime(self):
        '''
        test modules.status.uptime function
        '''
        mock_uptime = 'very often'
        mock_run = MagicMock(return_value=mock_uptime)
        with patch.dict(status.__salt__, {'cmd.run': mock_run}):
            self.assertEqual(status.uptime(), mock_uptime)

        mock_uptime = 'very idle'
        mock_run = MagicMock(return_value=mock_uptime)
        with patch.dict(status.__salt__, {'cmd.run': mock_run}):
            with patch('os.path.exists', MagicMock(return_value=True)):
                self.assertEqual(status.uptime(human_readable=False),
                                 mock_uptime.split()[0])

        mock_uptime = ''
        mock_return = 'unexpected format in /proc/uptime'
        mock_run = MagicMock(return_value=mock_uptime)
        with patch.dict(status.__salt__, {'cmd.run': mock_run}):
            with patch('os.path.exists', MagicMock(return_value=True)):
                self.assertEqual(status.uptime(human_readable=False),
                                 mock_return)

        mock_return = 'cannot find /proc/uptime'
        with patch('os.path.exists', MagicMock(return_value=False)):
            self.assertEqual(status.uptime(human_readable=False), mock_return)
Exemplo n.º 2
0
    def test_uptime_macos(self):
        '''
        Test modules.status.uptime function for macOS
        '''
        m = self._set_up_test_uptime()

        kern_boottime = (
            '{{ sec = {0}, usec = {1:0<6} }} Mon Oct 03 03:09:18.23 2016'
            ''.format(*str(m.now - m.ut).split('.')))
        with patch.multiple(salt.utils,
                            is_linux=MagicMock(return_value=False),
                            is_sunos=MagicMock(return_value=False),
                            is_darwin=MagicMock(return_value=True),
                            is_freebsd=MagicMock(return_value=False),
                            is_openbsd=MagicMock(return_value=False),
                            is_netbsd=MagicMock(return_value=False)):
            with patch.dict(
                    status.__salt__, {
                        'cmd.run': MagicMock(return_value="1\n2\n3"),
                        'sysctl.get': MagicMock(return_value=kern_boottime)
                    }):
                with patch('time.time', MagicMock(return_value=m.now)):
                    ret = status.uptime()
                    self.assertDictEqual(ret, m.ret)

            with patch.dict(status.__salt__,
                            {'sysctl.get': MagicMock(return_value='')}):
                with self.assertRaises(CommandExecutionError):
                    status.uptime()
Exemplo n.º 3
0
    def test_uptime_linux(self):
        """
        Test modules.status.uptime function for Linux
        """
        m = self._set_up_test_uptime()

        with patch.multiple(
                salt.utils.platform,
                is_linux=MagicMock(return_value=True),
                is_sunos=MagicMock(return_value=False),
                is_darwin=MagicMock(return_value=False),
                is_freebsd=MagicMock(return_value=False),
                is_openbsd=MagicMock(return_value=False),
                is_netbsd=MagicMock(return_value=False),
        ), patch("salt.utils.path.which",
                 MagicMock(return_value=True)), patch.dict(
                     status.__salt__,
                     {
                         "cmd.run":
                         MagicMock(
                             return_value=os.linesep.join(["1", "2", "3"]))
                     },
                 ), patch("time.time", MagicMock(return_value=m.now)), patch(
                     "os.path.exists", MagicMock(return_value=True)):
            proc_uptime = salt.utils.stringutils.to_str("{} {}".format(
                m.ut, m.idle))

            with patch("salt.utils.files.fopen",
                       mock_open(read_data=proc_uptime)):
                ret = status.uptime()
                self.assertDictEqual(ret, m.ret)
            with patch("os.path.exists", MagicMock(return_value=False)):
                with self.assertRaises(CommandExecutionError):
                    status.uptime()
Exemplo n.º 4
0
    def test_uptime_linux(self):
        '''
        Test modules.status.uptime function for Linux
        '''
        m = self._set_up_test_uptime()

        with patch.multiple(salt.utils,
                            is_linux=MagicMock(return_value=True),
                            is_sunos=MagicMock(return_value=False),
                            is_darwin=MagicMock(return_value=False),
                            is_freebsd=MagicMock(return_value=False),
                            is_openbsd=MagicMock(return_value=False),
                            is_netbsd=MagicMock(return_value=False)):
            with patch.dict(status.__salt__,
                            {'cmd.run': MagicMock(return_value="1\n2\n3")}):
                with patch('time.time', MagicMock(return_value=m.now)):
                    with patch('os.path.exists', MagicMock(return_value=True)):
                        proc_uptime = '{0} {1}'.format(m.ut, m.idle)
                        with patch('salt.utils.fopen',
                                   mock_open(read_data=proc_uptime)):
                            ret = status.uptime()
                            self.assertDictEqual(ret, m.ret)

                    with patch('os.path.exists',
                               MagicMock(return_value=False)):
                        with self.assertRaises(CommandExecutionError):
                            status.uptime()
Exemplo n.º 5
0
 def test_uptime_return_success_not_supported(self):
     '''
     Test modules.status.uptime function for other platforms
     '''
     exc_mock = MagicMock(side_effect=CommandExecutionError)
     with self.assertRaises(CommandExecutionError):
         with patch.dict(status.__salt__, {'cmd.run': exc_mock}):
             status.uptime()
Exemplo n.º 6
0
 def test_uptime_return_success_not_supported(self):
     """
     Test modules.status.uptime function for other platforms
     """
     exc_mock = MagicMock(side_effect=CommandExecutionError)
     with self.assertRaises(CommandExecutionError):
         with patch.dict(status.__salt__, {"cmd.run": exc_mock}):
             status.uptime()
Exemplo n.º 7
0
 def test_uptime_failure(self):
     '''
     Test modules.status.uptime function should raise an exception if /proc/uptime does not exists.
     :return:
     '''
     with patch('os.path.exists', MagicMock(return_value=False)):
         with self.assertRaises(CommandExecutionError):
             status.uptime()
Exemplo n.º 8
0
 def test_uptime_return_success_not_supported(self):
     '''
     Test modules.status.uptime function for other platforms
     '''
     with patch.multiple(salt.utils,
                         is_linux=MagicMock(return_value=False),
                         is_sunos=MagicMock(return_value=False),
                         is_darwin=MagicMock(return_value=False),
                         is_freebsd=MagicMock(return_value=False),
                         is_openbsd=MagicMock(return_value=False),
                         is_netbsd=MagicMock(return_value=False)):
         exc_mock = MagicMock(side_effect=CommandExecutionError)
         with self.assertRaises(CommandExecutionError):
             with patch.dict(status.__salt__, {'cmd.run': exc_mock}):
                 status.uptime()
Exemplo n.º 9
0
 def test_uptime_sunos(self):
     """
     Test modules.status.uptime function for SunOS
     """
     m = self._set_up_test_uptime()
     m2 = self._set_up_test_uptime_sunos()
     with patch.multiple(
             salt.utils.platform,
             is_linux=MagicMock(return_value=False),
             is_sunos=MagicMock(return_value=True),
             is_darwin=MagicMock(return_value=False),
             is_freebsd=MagicMock(return_value=False),
             is_openbsd=MagicMock(return_value=False),
             is_netbsd=MagicMock(return_value=False),
     ), patch("salt.utils.path.which",
              MagicMock(return_value=True)), patch.dict(
                  status.__salt__,
                  {
                      "cmd.run":
                      MagicMock(
                          return_value=os.linesep.join(["1", "2", "3"])),
                      "cmd.run_all":
                      MagicMock(return_value=m2.ret),
                  },
              ), patch("time.time", MagicMock(return_value=m.now)):
         ret = status.uptime()
         self.assertDictEqual(ret, m.ret)
Exemplo n.º 10
0
    def test_uptime_linux(self):
        """
        Test modules.status.uptime function for Linux
        """
        m = self._set_up_test_uptime()

        with patch.dict(status.__salt__, {"cmd.run": MagicMock(return_value="1\n2\n3")}):
            with patch("time.time", MagicMock(return_value=m.now)):
                with patch("os.path.exists", MagicMock(return_value=True)):
                    proc_uptime = "{0} {1}".format(m.ut, m.idle)
                    with patch("salt.utils.fopen", mock_open(read_data=proc_uptime)):
                        ret = status.uptime()
                        self.assertDictEqual(ret, m.ret)

                with patch("os.path.exists", MagicMock(return_value=False)):
                    with self.assertRaises(CommandExecutionError):
                        status.uptime()
Exemplo n.º 11
0
    def test_uptime_macos(self):
        """
        Test modules.status.uptime function for macOS
        """
        m = self._set_up_test_uptime()

        kern_boottime = "{{ sec = {0}, usec = {1:0<6} }} Mon Oct 03 03:09:18.23 2016" "".format(
            *str(m.now - m.ut).split(".")
        )
        with patch.dict(
            status.__salt__,
            {"cmd.run": MagicMock(return_value="1\n2\n3"), "sysctl.get": MagicMock(return_value=kern_boottime)},
        ):
            with patch("time.time", MagicMock(return_value=m.now)):
                ret = status.uptime()
                self.assertDictEqual(ret, m.ret)

        with patch.dict(status.__salt__, {"sysctl.get": MagicMock(return_value="")}):
            with self.assertRaises(CommandExecutionError):
                status.uptime()
Exemplo n.º 12
0
    def test_uptime_sunos(self):
        '''
        Test modules.status.uptime function for SunOS
        '''
        m = self._set_up_test_uptime()
        m2 = self._set_up_test_uptime_sunos()

        with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value="1\n2\n3"),
                                          'cmd.run_all': MagicMock(return_value=m2.ret)}):
            with patch('time.time', MagicMock(return_value=m.now)):
                ret = status.uptime()
                self.assertDictEqual(ret, m.ret)
Exemplo n.º 13
0
    def test_uptime_sunos(self):
        """
        Test modules.status.uptime function for SunOS
        """
        m = self._set_up_test_uptime()
        m2 = self._set_up_test_uptime_sunos()

        with patch.dict(
            status.__salt__,
            {"cmd.run": MagicMock(return_value="1\n2\n3"), "cmd.run_all": MagicMock(return_value=m2.ret)},
        ):
            with patch("time.time", MagicMock(return_value=m.now)):
                ret = status.uptime()
                self.assertDictEqual(ret, m.ret)
Exemplo n.º 14
0
    def test_uptime_macos(self):
        """
        Test modules.status.uptime function for macOS
        """
        m = self._set_up_test_uptime()

        kern_boottime = (
            "{{ sec = {0}, usec = {1:0<6} }} Mon Oct 03 03:09:18.23 2016".format(
                *str(m.now - m.ut).split(".")
            )
        )
        with patch.multiple(
            salt.utils.platform,
            is_linux=MagicMock(return_value=False),
            is_sunos=MagicMock(return_value=False),
            is_darwin=MagicMock(return_value=True),
            is_freebsd=MagicMock(return_value=False),
            is_openbsd=MagicMock(return_value=False),
            is_netbsd=MagicMock(return_value=False),
        ), patch("salt.utils.path.which", MagicMock(return_value=True)), patch.dict(
            status.__salt__,
            {
                "cmd.run": MagicMock(return_value=os.linesep.join(["1", "2", "3"])),
                "sysctl.get": MagicMock(return_value=kern_boottime),
            },
        ), patch(
            "time.time", MagicMock(return_value=m.now)
        ):

            ret = status.uptime()
            self.assertDictEqual(ret, m.ret)

            with patch.dict(
                status.__salt__, {"sysctl.get": MagicMock(return_value="")}
            ):
                with self.assertRaises(CommandExecutionError):
                    status.uptime()
Exemplo n.º 15
0
 def test_uptime_sunos(self):
     '''
     Test modules.status.uptime function for SunOS
     '''
     m = self._set_up_test_uptime()
     m2 = self._set_up_test_uptime_sunos()
     with patch.multiple(salt.utils.platform,
                         is_linux=MagicMock(return_value=False),
                         is_sunos=MagicMock(return_value=True),
                         is_darwin=MagicMock(return_value=False),
                         is_freebsd=MagicMock(return_value=False),
                         is_openbsd=MagicMock(return_value=False),
                         is_netbsd=MagicMock(return_value=False)), \
             patch('salt.utils.path.which', MagicMock(return_value=True)), \
             patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value=os.linesep.join(['1', '2', '3'])),
                                          'cmd.run_all': MagicMock(return_value=m2.ret)}), \
             patch('time.time', MagicMock(return_value=m.now)):
         ret = status.uptime()
         self.assertDictEqual(ret, m.ret)
Exemplo n.º 16
0
    def test_uptime_sunos(self):
        '''
        Test modules.status.uptime function for SunOS
        '''
        m = self._set_up_test_uptime()
        m2 = self._set_up_test_uptime_sunos()
        with patch.multiple(salt.utils,
                            is_linux=MagicMock(return_value=False),
                            is_sunos=MagicMock(return_value=True),
                            is_darwin=MagicMock(return_value=False),
                            is_freebsd=MagicMock(return_value=False),
                            is_openbsd=MagicMock(return_value=False),
                            is_netbsd=MagicMock(return_value=False)):

            with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value="1\n2\n3"),
                                              'cmd.run_all': MagicMock(return_value=m2.ret)}):
                with patch('time.time', MagicMock(return_value=m.now)):
                    ret = status.uptime()
                    self.assertDictEqual(ret, m.ret)
Exemplo n.º 17
0
    def test_uptime(self):
        '''
        Test modules.status.uptime function, new version
        :return:
        '''
        class ProcUptime(object):
            def __init__(self, *args, **kwargs):
                self.data = "773865.18 1003405.46"

            def read(self):
                return self.data

        with patch.dict(status.__salt__, {'cmd.run': MagicMock(return_value="1\n2\n3")}):
            with patch('os.path.exists', MagicMock(return_value=True)):
                with patch('time.time', MagicMock(return_value=1458821523.72)):
                    status.open = ProcUptime
                    u_time = status.uptime()
                    self.assertEqual(u_time['users'], 3)
                    self.assertEqual(u_time['seconds'], 773865)
                    self.assertEqual(u_time['days'], 8)
                    self.assertEqual(u_time['time'], '22:57')
Exemplo n.º 18
0
 def test_uptime_return_success_not_supported(self):
     '''
     Test modules.status.uptime function for other platforms
     '''
     with self.assertRaises(CommandExecutionError):
         status.uptime()