2022-08-24 15:01:07 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2022-09-23 06:12:04 +00:00
|
|
|
from neofs_testlib.shell.interfaces import CommandOptions, InteractiveInput
|
|
|
|
from neofs_testlib.shell.local_shell import LocalShell
|
|
|
|
|
2022-08-24 11:41:11 +00:00
|
|
|
from tests.helpers import format_error_details, get_output_lines
|
2022-08-24 15:01:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestLocalShellInteractive(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.shell = LocalShell()
|
|
|
|
|
|
|
|
def test_command_with_one_prompt(self):
|
|
|
|
script = "password = input('Password: '); print(password)"
|
|
|
|
|
|
|
|
inputs = [InteractiveInput(prompt_pattern="Password", input="test")]
|
|
|
|
result = self.shell.exec(
|
2022-08-24 11:41:11 +00:00
|
|
|
f'python3 -c "{script}"', CommandOptions(interactive_inputs=inputs)
|
2022-08-24 15:01:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(0, result.return_code)
|
2022-08-24 11:41:11 +00:00
|
|
|
self.assertEqual(["Password: test", "test"], get_output_lines(result))
|
2022-08-24 15:01:07 +00:00
|
|
|
self.assertEqual("", result.stderr)
|
|
|
|
|
|
|
|
def test_command_with_several_prompts(self):
|
|
|
|
script = (
|
|
|
|
"input1 = input('Input1: '); print(input1); "
|
|
|
|
"input2 = input('Input2: '); print(input2)"
|
|
|
|
)
|
|
|
|
inputs = [
|
|
|
|
InteractiveInput(prompt_pattern="Input1", input="test1"),
|
|
|
|
InteractiveInput(prompt_pattern="Input2", input="test2"),
|
|
|
|
]
|
|
|
|
|
|
|
|
result = self.shell.exec(
|
2022-08-24 11:41:11 +00:00
|
|
|
f'python3 -c "{script}"', CommandOptions(interactive_inputs=inputs)
|
2022-08-24 15:01:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(0, result.return_code)
|
2022-08-24 11:41:11 +00:00
|
|
|
self.assertEqual(
|
|
|
|
["Input1: test1", "test1", "Input2: test2", "test2"], get_output_lines(result)
|
|
|
|
)
|
2022-08-24 15:01:07 +00:00
|
|
|
self.assertEqual("", result.stderr)
|
|
|
|
|
|
|
|
def test_failed_command_with_check(self):
|
|
|
|
script = "invalid script"
|
|
|
|
inputs = [InteractiveInput(prompt_pattern=".*", input="test")]
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError) as exc:
|
2022-08-24 11:41:11 +00:00
|
|
|
self.shell.exec(f'python3 -c "{script}"', CommandOptions(interactive_inputs=inputs))
|
2022-08-24 15:01:07 +00:00
|
|
|
|
|
|
|
error = format_error_details(exc.exception)
|
|
|
|
self.assertIn("Error", error)
|
|
|
|
# TODO: it would be nice to have return code as well
|
|
|
|
# self.assertIn("return code: 1", error)
|
|
|
|
|
|
|
|
def test_failed_command_without_check(self):
|
|
|
|
script = "invalid script"
|
|
|
|
inputs = [InteractiveInput(prompt_pattern=".*", input="test")]
|
|
|
|
|
|
|
|
result = self.shell.exec(
|
2022-08-24 11:41:11 +00:00
|
|
|
f'python3 -c "{script}"',
|
2022-08-24 15:01:07 +00:00
|
|
|
CommandOptions(interactive_inputs=inputs, check=False),
|
|
|
|
)
|
|
|
|
self.assertEqual(1, result.return_code)
|
|
|
|
|
|
|
|
def test_non_existing_binary(self):
|
|
|
|
inputs = [InteractiveInput(prompt_pattern=".*", input="test")]
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError) as exc:
|
|
|
|
self.shell.exec("not-a-command", CommandOptions(interactive_inputs=inputs))
|
|
|
|
|
|
|
|
error = format_error_details(exc.exception)
|
2022-12-08 07:42:45 +00:00
|
|
|
self.assertIn("The command was not found", error)
|
2022-08-24 11:41:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestLocalShellNonInteractive(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.shell = LocalShell()
|
|
|
|
|
|
|
|
def test_successful_command(self):
|
|
|
|
script = "print('test')"
|
2022-08-24 15:01:07 +00:00
|
|
|
|
2022-08-24 11:41:11 +00:00
|
|
|
result = self.shell.exec(f'python3 -c "{script}"')
|
|
|
|
|
|
|
|
self.assertEqual(0, result.return_code)
|
|
|
|
self.assertEqual("test", result.stdout.strip())
|
|
|
|
self.assertEqual("", result.stderr)
|
|
|
|
|
|
|
|
def test_invalid_command_with_check(self):
|
|
|
|
script = "invalid script"
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError) as exc:
|
|
|
|
self.shell.exec(f'python3 -c "{script}"')
|
|
|
|
|
|
|
|
error = format_error_details(exc.exception)
|
|
|
|
self.assertIn("Error", error)
|
|
|
|
self.assertIn("return code: 1", error)
|
|
|
|
|
|
|
|
def test_invalid_command_without_check(self):
|
|
|
|
script = "invalid script"
|
|
|
|
|
|
|
|
result = self.shell.exec(f'python3 -c "{script}"', CommandOptions(check=False))
|
|
|
|
|
|
|
|
self.assertEqual(1, result.return_code)
|
|
|
|
self.assertIn("Error", result.stdout)
|
|
|
|
|
|
|
|
def test_non_existing_binary(self):
|
|
|
|
with self.assertRaises(RuntimeError) as exc:
|
|
|
|
self.shell.exec("not-a-command")
|
|
|
|
|
|
|
|
error = format_error_details(exc.exception)
|
|
|
|
self.assertIn("return code: 127", error)
|