【问题标题】:How do I connect mysql with cypress through ssh tunneling?如何通过 ssh 隧道连接 mysql 和 cypress?
【发布时间】:2022-01-03 19:34:49
【问题描述】:

目前 cypress 支持不使用 ssh 的 mysql 连接,如下链接所示

https://docs.cypress.io/api/commands/task#Allows-a-single-argument-only

但我正在尝试通过 ssh 隧道将 cypress 连接到 mysql。 我正在使用 npm 包mysql-ssh 建立连接。

我可以直接使用node.js 来实现这一点,但我在通过 cypress 实施时遇到了问题。这是我在node.js 中尝试的sn-p。

const mysqlssh = require('mysql-ssh');
const fs = require('fs');

mysqlssh.connect(
    {
        host: 'x.x.x.x',
        user: 'xyz',
        privateKey: fs.readFileSync('filePath')  //this is the ssh filePath
    },
    {
        host: 'HOST_NAME',
        user: 'USER_NAME',
        password: 'xxxx',
        database: 'DB_NAME'
    }
)
.then(client => {
    client.query('select * from TABLE_NAME',  function (err, results, fields) {
        if (err)
        {
            console.log(err)
        }
        console.log(results);
        mysqlssh.close()
    })
})
.catch(err => {
    console.log(err)
})

我想通过cypress/plugins/index.js 文件或直接在cypress/integration 中执行此操作。有没有简单的方法来做到这一点?

【问题讨论】:

    标签: mysql node.js cypress ssh-tunnel


    【解决方案1】:

    我找到了解决方案。这是我的cypress/plugins/index.js 文件代码:

    const dotenvPlugin = require('cypress-dotenv');
    const mysqlssh = require('mysql-ssh');
    const fs = require('fs');
    
    module.exports = (on, config) => {
      //  `config` is the resolved Cypress config
        config = dotenvPlugin(config);
    
        on('task', {
            executeSql (sql, ...args) {
                return new Promise(async (resolve, reject) => {
                    try {
                        let connection = await mysqlssh.connect(  {
                                host: process.env.SSH_HOST,
                                user: process.env.SSH_USER,
                                privateKey: fs.readFileSync(process.env.HOME + '/.ssh/id_rsa_old')
                            },
                            {
                                host: process.env.MYSQL_HOST,
                                user: process.env.MYSQL_USER,
                                password: process.env.MYSQL_PASSWORD,
                                database: process.env.MYSQL_DB
                            });
                        let result = await connection.promise().query(sql, args);
                        mysqlssh.close();
                        resolve(result[0][0]); 
                    } catch (err) {
                        reject(err);
                    }
                });
            }
        })
    
        return config
    }
    

    所以这个连接必须在这个文件中建立 b/c cypress 不与主机提供的节点进程通信。所以我们需要使用 cypress 任务来运行一个 Node 代码。在此处查看文档 - https://docs.cypress.io/api/commands/task#Examples

    在一个测试文件示例中,我是这样使用它的:

    describe('Db Test',  () => {
        it('Query Test', () => {
            cy.task('executeSql', 'SELECT count(id) as cnt FROM table_name').then(result => {
                expect(result.cnt, 'Does not equal to 8').to.equal(2000);
            })
        })
    })
    

    附:附加的cypress-dotenv 包仅用于从 .env 文件加载环境变量。

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 1970-01-01
      • 2012-06-19
      • 2014-03-21
      • 2014-09-17
      • 1970-01-01
      • 2016-07-02
      相关资源
      最近更新 更多