Skip to content

Commit

Permalink
Fixes ExtensionUtils.run_command_get_output() (#1604)
Browse files Browse the repository at this point in the history
* Fixes ExtensionUtils.run_command_get_output()

ExtensionUtils.run_command_get_output() now decodes the output of
commands into a utf-8 string.

Fixes VMAccess extension's inability to accept non latin characters as
password.

* Addressed PR comments

* Updated extension version
  • Loading branch information
D1v38om83r authored Oct 6, 2022
1 parent 14835e2 commit 0eb8d51
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: test_encode.py",
"type": "python",
"request": "launch",
"program": "test_encode.py",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}/Utils/test",
"env" : {
"PYTHONPATH": "${workspaceFolder}"
}
}
]
}
10 changes: 5 additions & 5 deletions Utils/extensionutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ def run_command_get_output(cmd, chk_err=True, log_cmd=True):
logger.error('CalledProcessError. Error Code is ' + str(e.returncode))
logger.error('CalledProcessError. Command string was ' + str(cmd))
logger.error(
'CalledProcessError. Command result was ' + (e.output[:-1]).decode('latin-1'))
return e.returncode, e.output.decode('latin-1')
'CalledProcessError. Command result was ' + (e.output[:-1]).decode('utf-8'))
return e.returncode, e.output.decode('utf-8')
except EnvironmentError as e:
if chk_err and log_cmd:
logger.error(
'CalledProcessError. Error message is ' + str(e))
return e.errno, str(e)
# noinspection PyUnboundLocalVariable
return 0, output.decode('latin-1')
return 0, output.decode('utf-8')


def run(cmd, chk_err=True):
Expand Down Expand Up @@ -238,8 +238,8 @@ def run_send_stdin(cmd, cmd_input, chk_err=True, log_cmd=True):
logger.error('CalledProcessError. Error Code is ' + str(me.returncode))
logger.error('CalledProcessError. Command was ' + str(cmd))
logger.error(
'CalledProcessError. Command result was ' + output[0].decode('latin-1'))
return me.returncode, output[0].decode('latin-1')
'CalledProcessError. Command result was ' + output[0].decode('utf-8'))
return me.returncode, output[0].decode('utf-8')


def get_line_starting_with(prefix, filepath):
Expand Down
1 change: 1 addition & 0 deletions Utils/test/non_latin_characters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ü
15 changes: 15 additions & 0 deletions Utils/test/test_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def test_encode(self):
known_non_ascii_character = b"%c" % encoded_contents[2353]
self.assertEqual(known_non_ascii_character, b'\x9d')

class TestRunCommandGetOutput(unittest.TestCase):
def test_output(self):
cmd = ["cat", "non_latin_characters.txt"]
return_code, output_string = eu.run_command_get_output(cmd)
self.assertEqual(0, return_code)
expected_character_byte = b'\xc3\xbc'
expected_character = expected_character_byte.decode("utf-8")
self.assertEqual(expected_character, output_string[0])

def test_stdin(self):
cmd = ['bash', '-c', 'read ; echo $REPLY']
cmd_input = b'\xc3\xbc' # ü character
return_code, output_string = eu.run_send_stdin(cmd, cmd_input)
self.assertEqual(0, return_code)
self.assertEqual(cmd_input.decode('utf-8'), output_string[0])

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion VMAccess/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ExtensionImage xmlns="http://schemas.microsoft.com/windowsazure">
<ProviderNameSpace>Microsoft.OSTCExtensions</ProviderNameSpace>
<Type>VMAccessForLinux</Type>
<Version>1.5.11</Version>
<Version>1.5.12</Version>
<Label>Microsoft Azure VM Access Extension for Linux Virtual Machines</Label>
<HostingResources>VmRole</HostingResources>
<MediaLink></MediaLink>
Expand Down

0 comments on commit 0eb8d51

Please sign in to comment.