|
| 1 | +import aws.sdk.kotlin.runtime.auth.credentials.executeCommand |
| 2 | +import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProviderException |
| 3 | +import aws.smithy.kotlin.runtime.util.OsFamily |
| 4 | +import aws.smithy.kotlin.runtime.util.PlatformProvider |
| 5 | +import kotlinx.coroutines.TimeoutCancellationException |
| 6 | +import kotlinx.coroutines.test.runTest |
| 7 | +import kotlin.test.Test |
| 8 | +import kotlin.test.assertEquals |
| 9 | +import kotlin.test.assertFailsWith |
| 10 | + |
| 11 | +class ExecuteCommandTest { |
| 12 | + val provider = PlatformProvider.System |
| 13 | + |
| 14 | + val platformNewline = when (provider.osInfo().family) { |
| 15 | + OsFamily.Windows -> "\r\n" |
| 16 | + else -> "\n" |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + fun testExecuteCommand() = runTest { |
| 21 | + val command = "echo Hello, World!" |
| 22 | + |
| 23 | + val (exitCode, output) = executeCommand( |
| 24 | + command = command, |
| 25 | + platformProvider = provider, |
| 26 | + maxOutputLengthBytes = 1024L, |
| 27 | + timeoutMillis = 1000, |
| 28 | + ) |
| 29 | + |
| 30 | + assertEquals(0, exitCode) |
| 31 | + assertEquals("Hello, World!$platformNewline", output) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun testExecutionTimedOut() = runTest { |
| 36 | + assertFailsWith<TimeoutCancellationException> { |
| 37 | + executeCommand( |
| 38 | + command = "this won't be executed", |
| 39 | + platformProvider = provider, |
| 40 | + maxOutputLengthBytes = 1024L, |
| 41 | + timeoutMillis = 0, |
| 42 | + ) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun testTooManyBytes() = runTest { |
| 48 | + val command = "echo ${"Hello! ".repeat(500)}" |
| 49 | + |
| 50 | + val ex = assertFailsWith<CredentialsProviderException> { |
| 51 | + executeCommand( |
| 52 | + command = command, |
| 53 | + platformProvider = provider, |
| 54 | + maxOutputLengthBytes = 1024L, |
| 55 | + timeoutMillis = 1000, |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + assertEquals("Process output exceeded limit of 1024 bytes", ex.message) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun testCommandHasQuotes() = runTest { |
| 64 | + val command = when (provider.osInfo().family) { |
| 65 | + OsFamily.Windows -> """echo "Hello, in quotes!"""" |
| 66 | + else -> """echo \"Hello, in quotes!\"""" |
| 67 | + } |
| 68 | + |
| 69 | + val (exitCode, output) = executeCommand( |
| 70 | + command = command, |
| 71 | + platformProvider = provider, |
| 72 | + maxOutputLengthBytes = 1024L, |
| 73 | + timeoutMillis = 1000, |
| 74 | + ) |
| 75 | + |
| 76 | + assertEquals(0, exitCode) |
| 77 | + assertEquals(""""Hello, in quotes!"$platformNewline""", output) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun testMultipleArgumentCommand() = runTest { |
| 82 | + val command = when (provider.osInfo().family) { |
| 83 | + OsFamily.Windows -> "powershell -Command \"& {Write-Host 'Arg1'; Write-Host 'Arg2'; Write-Host 'Arg3'}\"" |
| 84 | + else -> "printf '%s\\n%s\\n%s\\n' 'Arg1' 'Arg2' 'Arg3'" |
| 85 | + } |
| 86 | + |
| 87 | + val (exitCode, output) = executeCommand( |
| 88 | + command = command, |
| 89 | + platformProvider = provider, |
| 90 | + maxOutputLengthBytes = 1024L, |
| 91 | + timeoutMillis = 1000, |
| 92 | + ) |
| 93 | + |
| 94 | + assertEquals(0, exitCode) |
| 95 | + assertEquals("Arg1\nArg2\nArg3\n", output) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testErrorReturnsStderr() = runTest { |
| 100 | + val errorCommand = when (provider.osInfo().family) { |
| 101 | + OsFamily.Windows -> "echo Error message 1>&2 & exit /b 13" |
| 102 | + else -> "echo 'Error message' >&2; exit 13" |
| 103 | + } |
| 104 | + |
| 105 | + // Windows command output has an extra space at the end |
| 106 | + // Can't wrap it in quotes because Windows just echoes them back |
| 107 | + // Can't use `<nul set /p` because that doesn't terminate with CRLF |
| 108 | + val expectedOutput = when (provider.osInfo().family) { |
| 109 | + OsFamily.Windows -> "Error message " |
| 110 | + else -> "Error message" |
| 111 | + } |
| 112 | + |
| 113 | + val (exitCode, output) = executeCommand( |
| 114 | + command = errorCommand, |
| 115 | + platformProvider = provider, |
| 116 | + maxOutputLengthBytes = 1024L, |
| 117 | + timeoutMillis = 1000, |
| 118 | + ) |
| 119 | + |
| 120 | + assertEquals(13, exitCode) |
| 121 | + assertEquals("$expectedOutput$platformNewline", output) |
| 122 | + } |
| 123 | +} |
0 commit comments