1 year ago

#354328

test-img

christos.goulas

nodejs tunnel-ssh close server from test script

I have a script that creates an ssh connection and a mongodb connection. In addition I have a test file in order to test the functionality. But from the test file I want, when the ssh connection established, to close the connection. I can do it from the original file but not from the test file. Is it possible to achive it? My code is that:

connection.js

const tunnel = require('tunnel-ssh');
const MongoClient = require('mongodb').MongoClient;

function sshConnection(){
    let config = {//the configuration values};

    let server = tunnel(config, function (error, server) {
        if(error){
            console.log("SSH connection error: " + error);
        }
        else {
            console.log('ssh connection done');
            // server.close(); This code runs If I comment it out
        }
    });

    return server;
};

function mongoConnection(){
    const uri = "mongodb://localhost:27018/"
    const client = new MongoClient(uri, { useNewUrlParser: true });
    client.connect(function(err) {
        if (err){
        console.log('err')
        } else {
        console.log('mongodb connection done')
        }
    });
    
    return client
};

module.exports = {
    sshConnection,
    mongoConnection
}

connection_test.js

const connection = require('../utilities/connection');

describe('testing the mongodb connection', function() {
    xit('establishes an ssh connection', async function() {
        let server = connection.sshConnection();
        server.close(); //This code does not run and the connection is alive
    });

    it('establishes a mongodb connection', function() {
        let client = connection.mongoConnection();
        client.close(); 
    });
});

node.js

mongodb

mocha.js

ssh-tunnel

0 Answers

Your Answer

Accepted video resources