68 lines
2.2 KiB
JavaScript
68 lines
2.2 KiB
JavaScript
import Ssh from '../src/ssh'
|
|
import to from 'await-to-js'
|
|
import { expect } from 'chai'
|
|
import { it } from 'mocha'
|
|
import logger from '@uci-utils/logger'
|
|
|
|
describe('SSH Class Testing ',async ()=> {
|
|
|
|
let remote
|
|
let log
|
|
before(async () => {
|
|
log = logger({ name: 'remote-code', test:'/test/ssh.test.js', class:'Ssh', file:'src/ssh.js', id: 'testing' })
|
|
remote = new Ssh()
|
|
await remote.configure('./test/config/ssh') // will pick up config file format
|
|
log.info(`making connection to ${remote.opts.host}`)
|
|
let [err] = await to(remote.connect())
|
|
if (err) {
|
|
log.info('unable to connect aborting test', err)
|
|
throw new Error (err)
|
|
}
|
|
log.info('ready for testing')
|
|
})
|
|
|
|
after(async () => {
|
|
remote.close()
|
|
})
|
|
|
|
it('simple connect and execute "cd /opt && pwd" on remote' , async function () {
|
|
let [err,res] = await to(remote.exec('cd /opt && pwd'))
|
|
if (err) {
|
|
log.info('error running command aborting test', err)
|
|
return
|
|
}
|
|
log.info({remoteHost:remote.opts.host, reply:res.reply.toString().trim(), err:err, cmd:res.command, msg:`remote command completed ${res.command}`})
|
|
expect(res.reply.toString().trim(), 'test failed').to.equal('/opt')
|
|
})
|
|
|
|
it('connect via an "interactive shell" and cat the test.txt file in home directory"' , async function () {
|
|
let number = Date.now()
|
|
log.info({number:number, msg:'putting number into remote test.text file'})
|
|
// log.info('putting number into remote test.text file')
|
|
await to(remote.shell(`echo ${number} > test.txt`))
|
|
let [err,res] = await to(remote.shell('cat test.txt'))
|
|
if (err) {
|
|
log.info('error running shell command aborting test', err)
|
|
throw new Error(err)
|
|
}
|
|
let passed = false
|
|
if (res.reply.indexOf(number > 0)) passed=true
|
|
log.info({remoteHost:remote.opts.host, reply:res.reply.toString().trim(), err:err, cmd:res.command, msg:`remote command completed ${res.command}`})
|
|
expect(passed, 'test failed').to.equal(true)
|
|
})
|
|
|
|
})
|
|
|
|
// function hooks(remote) {
|
|
//
|
|
|
|
// // beforeEach(async() => {
|
|
// // await someasyncfunctiontodobeforeeachtest()
|
|
// // })
|
|
//
|
|
// // after(async() => {
|
|
// // await someasyncfunctiontodoaftereeachtest()
|
|
// // })
|
|
//
|
|
// }
|