Skip to content

Commit ead068b

Browse files
committed
add tests
1 parent 33c99d4 commit ead068b

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

packages/core/src/shared/extensions/ssh.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as path from 'path'
88
import * as nls from 'vscode-nls'
99
import fs from '../fs/fs'
1010
import { getLogger } from '../logger'
11-
import { ChildProcess } from '../utilities/processUtils'
11+
import { ChildProcess, ChildProcessResult } from '../utilities/processUtils'
1212
import { ArrayConstructor, NonNullObject } from '../utilities/typeConstructors'
1313
import { Settings } from '../settings'
1414
import { VSCODE_EXTENSION_ID } from '../extensions'
@@ -129,16 +129,16 @@ export async function testSshConnection(
129129
sshPath: string,
130130
user: string,
131131
session: SSM.StartSessionResponse
132-
): Promise<void> {
132+
): Promise<ChildProcessResult | never> {
133133
try {
134134
const env = { SESSION_ID: session.SessionId, STREAM_URL: session.StreamUrl, TOKEN: session.TokenValue }
135-
await new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo connected && exit']).run({
135+
const result = await new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo connected && exit']).run({
136136
spawnOptions: {
137137
env,
138138
},
139139
})
140+
return result
140141
} catch (error) {
141-
getLogger().error('SSH connection test failed: %O', error)
142142
throw new SSHError('SSH connection test failed', { cause: error as Error })
143143
}
144144
}

packages/core/src/test/shared/extensions/ssh.test.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
*/
55
import * as assert from 'assert'
66
import { ChildProcess } from '../../../shared/utilities/processUtils'
7-
import { startSshAgent } from '../../../shared/extensions/ssh'
7+
import { startSshAgent, testSshConnection } from '../../../shared/extensions/ssh'
8+
import { createBoundProcess } from '../../../shared/remoteSession'
9+
import { createExecutableFile, createTestWorkspaceFolder } from '../../testUtil'
10+
import { WorkspaceFolder } from 'vscode'
11+
import path from 'path'
12+
import { SSM } from 'aws-sdk'
13+
import { fs } from '../../../shared/fs/fs'
814

915
describe('SSH Agent', function () {
1016
it('can start the agent on windows', async function () {
@@ -29,3 +35,65 @@ describe('SSH Agent', function () {
2935
assert.strictEqual(await getStatus(), 'Running')
3036
})
3137
})
38+
39+
describe('testSshConnection', function () {
40+
let testWorkspace: WorkspaceFolder
41+
let sshPath: string
42+
43+
before(async function () {
44+
testWorkspace = await createTestWorkspaceFolder()
45+
sshPath = path.join(testWorkspace.uri.fsPath, 'fakeSSH')
46+
})
47+
48+
after(async function () {
49+
await fs.delete(testWorkspace.uri.fsPath, { recursive: true, force: true })
50+
await fs.delete(sshPath, { force: true })
51+
})
52+
53+
it('runs in bound process', async function () {
54+
const envProvider = async () => ({ MY_VAR: 'yes' })
55+
const process = createBoundProcess(envProvider)
56+
const session = {
57+
SessionId: 'testSession',
58+
StreamUrl: 'testUrl',
59+
TokenValue: 'testToken',
60+
} as SSM.StartSessionResponse
61+
62+
await createExecutableFile(sshPath, 'echo "$MY_VAR"')
63+
const r = await testSshConnection(process, 'localhost', sshPath, 'test-user', session)
64+
assert.strictEqual(r.stdout, 'yes')
65+
await createExecutableFile(sshPath, 'echo "$UNDEFINED"')
66+
const r2 = await testSshConnection(process, 'localhost', sshPath, 'test-user', session)
67+
assert.strictEqual(r2.stdout, '')
68+
})
69+
70+
it('injects new session into env', async function () {
71+
const oldSession = {
72+
SessionId: 'testSession1',
73+
StreamUrl: 'testUrl1',
74+
TokenValue: 'testToken1',
75+
} as SSM.StartSessionResponse
76+
const newSession = {
77+
SessionId: 'testSession2',
78+
StreamUrl: 'testUrl2',
79+
TokenValue: 'testToken2',
80+
} as SSM.StartSessionResponse
81+
const envProvider = async () => ({
82+
SESSION_ID: oldSession.SessionId,
83+
STREAM_URL: oldSession.StreamUrl,
84+
TOKEN: oldSession.TokenValue,
85+
})
86+
const process = createBoundProcess(envProvider)
87+
88+
await createExecutableFile(sshPath, 'echo "$SESSION_ID, $STREAM_URL, $TOKEN"')
89+
const r = await testSshConnection(process, 'localhost', sshPath, 'test-user', newSession)
90+
assert.strictEqual(r.stdout, `${newSession.SessionId}, ${newSession.StreamUrl}, ${newSession.TokenValue}`)
91+
})
92+
93+
it('passes proper args to the ssh invoke', async function () {
94+
const process = createBoundProcess(async () => ({}))
95+
await createExecutableFile(sshPath, 'echo "$1,$2"')
96+
const r = await testSshConnection(process, 'localhost', sshPath, 'test-user', {} as SSM.StartSessionResponse)
97+
assert.strictEqual(r.stdout, '-T,test-user@localhost')
98+
})
99+
})

0 commit comments

Comments
 (0)