Exemple #1
0
 def test_run_cwd_doesnt_exist_issue_7154(self):
     '''
     cmd.run should fail and raise
     hubblestack.exceptions.CommandExecutionError if the cwd dir does not
     exist
     '''
     cmd = 'echo OHAI'
     cwd = '/path/to/nowhere'
     try:
         cmdmod.run_all(cmd, cwd=cwd)
     except CommandExecutionError:
         pass
     else:
         raise RuntimeError
Exemple #2
0
    def test_run_all_binary_replace(self):
        '''
        Test for failed decoding of binary data, for instance when doing
        something silly like using dd to read from /dev/urandom and write to
        /dev/stdout.
        '''
        # Since we're using unicode_literals, read the random bytes from a file
        rand_bytes_file = os.path.join(FILES, 'random_bytes')
        with hubblestack.utils.files.fopen(rand_bytes_file, 'rb') as fp_:
            stdout_bytes = fp_.read()

        # kitchen-salt uses unix2dos on all the files before copying them over
        # to the vm that will be running the tests. It skips binary files though
        # The file specified in `rand_bytes_file` is detected as binary so the
        # Unix-style line ending remains. This should account for that.
        stdout_bytes = stdout_bytes.rstrip() + os.linesep.encode()

        # stdout with the non-decodable bits replaced with the unicode
        # replacement character U+FFFD.
        stdout_unicode = '\ufffd\x1b\ufffd\ufffd' + os.linesep
        stderr_bytes = os.linesep.encode().join([
            b'1+0 records in', b'1+0 records out',
            b'4 bytes copied, 9.1522e-05 s, 43.7 kB/s'
        ]) + os.linesep.encode()
        stderr_unicode = stderr_bytes.decode()

        proc = MagicMock(return_value=MockTimedProc(stdout=stdout_bytes,
                                                    stderr=stderr_bytes))
        with patch('hubblestack.utils.timed_subprocess.TimedProc', proc):
            ret = cmdmod.run_all(
                'dd if=/dev/urandom of=/dev/stdout bs=4 count=1', rstrip=False)

        self.assertEqual(ret['stdout'], stdout_unicode)
        self.assertEqual(ret['stderr'], stderr_unicode)
Exemple #3
0
    def test_run_all_none(self):
        '''
        Tests cases when proc.stdout or proc.stderr are None. These should be
        caught and replaced with empty strings.
        '''
        proc = MagicMock(return_value=MockTimedProc(stdout=None, stderr=None))
        with patch('hubblestack.utils.timed_subprocess.TimedProc', proc):
            ret = cmdmod.run_all('some command', rstrip=False)

        self.assertEqual(ret['stdout'], '')
        self.assertEqual(ret['stderr'], '')
Exemple #4
0
    def test_run_all_output_encoding(self):
        '''
        Test that specifying the output encoding works as expected
        '''
        stdout = 'Æ'
        stdout_latin1_enc = stdout.encode('latin1')

        proc = MagicMock(return_value=MockTimedProc(stdout=stdout_latin1_enc))

        with patch('hubblestack.utils.timed_subprocess.TimedProc', proc), \
                patch.object(builtins, '__salt_system_encoding__', 'utf-8'):
            ret = cmdmod.run_all('some command', output_encoding='latin1')

        self.assertEqual(ret['stdout'], stdout)
Exemple #5
0
    def test_run_all_unicode(self):
        '''
        Ensure that unicode stdout and stderr are decoded properly
        '''
        stdout_unicode = 'Here is some unicode: спам'
        stderr_unicode = 'Here is some unicode: яйца'
        stdout_bytes = stdout_unicode.encode('utf-8')
        stderr_bytes = stderr_unicode.encode('utf-8')

        proc = MagicMock(return_value=MockTimedProc(stdout=stdout_bytes,
                                                    stderr=stderr_bytes))

        with patch('hubblestack.utils.timed_subprocess.TimedProc', proc), \
                patch.object(builtins, '__salt_system_encoding__', 'utf-8'):
            ret = cmdmod.run_all('some command', rstrip=False)

        self.assertEqual(ret['stdout'], stdout_unicode)
        self.assertEqual(ret['stderr'], stderr_unicode)