44 */
55import * as assert from 'assert'
66import { 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
915describe ( '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