Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hweinstock committed Nov 18, 2024
1 parent 33c99d4 commit ead068b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/shared/extensions/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path'
import * as nls from 'vscode-nls'
import fs from '../fs/fs'
import { getLogger } from '../logger'
import { ChildProcess } from '../utilities/processUtils'
import { ChildProcess, ChildProcessResult } from '../utilities/processUtils'
import { ArrayConstructor, NonNullObject } from '../utilities/typeConstructors'
import { Settings } from '../settings'
import { VSCODE_EXTENSION_ID } from '../extensions'
Expand Down Expand Up @@ -129,16 +129,16 @@ export async function testSshConnection(
sshPath: string,
user: string,
session: SSM.StartSessionResponse
): Promise<void> {
): Promise<ChildProcessResult | never> {
try {
const env = { SESSION_ID: session.SessionId, STREAM_URL: session.StreamUrl, TOKEN: session.TokenValue }
await new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo connected && exit']).run({
const result = await new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo connected && exit']).run({
spawnOptions: {
env,
},
})
return result
} catch (error) {
getLogger().error('SSH connection test failed: %O', error)
throw new SSHError('SSH connection test failed', { cause: error as Error })
}
}
Expand Down
70 changes: 69 additions & 1 deletion packages/core/src/test/shared/extensions/ssh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
*/
import * as assert from 'assert'
import { ChildProcess } from '../../../shared/utilities/processUtils'
import { startSshAgent } from '../../../shared/extensions/ssh'
import { startSshAgent, testSshConnection } from '../../../shared/extensions/ssh'
import { createBoundProcess } from '../../../shared/remoteSession'
import { createExecutableFile, createTestWorkspaceFolder } from '../../testUtil'
import { WorkspaceFolder } from 'vscode'
import path from 'path'
import { SSM } from 'aws-sdk'
import { fs } from '../../../shared/fs/fs'

describe('SSH Agent', function () {
it('can start the agent on windows', async function () {
Expand All @@ -29,3 +35,65 @@ describe('SSH Agent', function () {
assert.strictEqual(await getStatus(), 'Running')
})
})

describe('testSshConnection', function () {
let testWorkspace: WorkspaceFolder
let sshPath: string

before(async function () {
testWorkspace = await createTestWorkspaceFolder()
sshPath = path.join(testWorkspace.uri.fsPath, 'fakeSSH')
})

after(async function () {
await fs.delete(testWorkspace.uri.fsPath, { recursive: true, force: true })
await fs.delete(sshPath, { force: true })
})

it('runs in bound process', async function () {
const envProvider = async () => ({ MY_VAR: 'yes' })
const process = createBoundProcess(envProvider)
const session = {
SessionId: 'testSession',
StreamUrl: 'testUrl',
TokenValue: 'testToken',
} as SSM.StartSessionResponse

await createExecutableFile(sshPath, 'echo "$MY_VAR"')
const r = await testSshConnection(process, 'localhost', sshPath, 'test-user', session)
assert.strictEqual(r.stdout, 'yes')
await createExecutableFile(sshPath, 'echo "$UNDEFINED"')
const r2 = await testSshConnection(process, 'localhost', sshPath, 'test-user', session)
assert.strictEqual(r2.stdout, '')
})

it('injects new session into env', async function () {
const oldSession = {
SessionId: 'testSession1',
StreamUrl: 'testUrl1',
TokenValue: 'testToken1',
} as SSM.StartSessionResponse
const newSession = {
SessionId: 'testSession2',
StreamUrl: 'testUrl2',
TokenValue: 'testToken2',
} as SSM.StartSessionResponse
const envProvider = async () => ({
SESSION_ID: oldSession.SessionId,
STREAM_URL: oldSession.StreamUrl,
TOKEN: oldSession.TokenValue,
})
const process = createBoundProcess(envProvider)

await createExecutableFile(sshPath, 'echo "$SESSION_ID, $STREAM_URL, $TOKEN"')
const r = await testSshConnection(process, 'localhost', sshPath, 'test-user', newSession)
assert.strictEqual(r.stdout, `${newSession.SessionId}, ${newSession.StreamUrl}, ${newSession.TokenValue}`)
})

it('passes proper args to the ssh invoke', async function () {
const process = createBoundProcess(async () => ({}))
await createExecutableFile(sshPath, 'echo "$1,$2"')
const r = await testSshConnection(process, 'localhost', sshPath, 'test-user', {} as SSM.StartSessionResponse)
assert.strictEqual(r.stdout, '-T,test-user@localhost')
})
})

0 comments on commit ead068b

Please sign in to comment.