【问题标题】:Azure Pipelines integration test against Google Cloud SQL DB using cloud proxy使用云代理针对 Google Cloud SQL DB 进行 Azure Pipelines 集成测试
【发布时间】:2018-12-09 14:50:41
【问题描述】:

我正在管理一个 NodeJS 云函数存储库,它执行一些数据库操作,例如“userExists”等,并希望针对我的 Google Cloud SQL 数据库进行集成测试。为了在本地执行此操作,我在特定端口上启动代理应用程序,然后将 Knex 配置为使用此端口。

但是,我还想使用 Azure Pipelines 为此设置 CI/CD。 Google 允许将代理下载为 docker 映像,我已经这样做了。我已经为其提供了服务帐户密钥,并成功启动了它,但是,当我运行我的测试时,它们仍然无法连接到代理。

我的管道 YAML 文件中处理代理和运行测试的部分如下所示:

- script: |
    docker run -d \
    -v 'qa-key.json':/config \
    -p 127.0.0.1:5432:5432 \
    gcr.io/cloudsql-docker/gce-proxy:1.12 /cloud_sql_proxy \
    -instances=$(DBInstanceConnectionName)=tcp:0.0.0.0:5432 -credential_file=/config
  displayName: 'Run GCP Proxy'

- task: Npm@1
  displayName: 'Run Tests'
  inputs:
    command: custom
    customCommand: 'test'

运行测试的test 命令可用。配置 knex 的函数如下所示:

const connectKnex = () => {
    const config = {
        user: 'my_user',
        password: 'my_users_password',
        database: 'my_db',
        port: 5432
    }

    return knex({
        client: 'pg',
        connection: config
    });
}

我认为我的问题是因为运行代理的任务在测试运行后不再执行,但我不知道如何验证/修复这个问题。

【问题讨论】:

    标签: node.js google-cloud-platform yaml azure-pipelines knex.js


    【解决方案1】:

    问题 1:我没有看到 docker 容器的输出

    我直接从google's support page 复制了运行 docker 容器的命令。问题是该命令包含-d 标志(分离),这会导致容器在后台运行并隐藏其输出,从而导致难以调试。删除后,我可以看到代理图像无法找到凭据文件。

    问题 2:找不到凭据文件

    我无法花时间找到解决此问题的适当方法。由于我在 Azure 构建服务器上运行代理映像,因此我无法访问交互式终端来尝试访问映像的文件系统以尝试调试安装。该问题已与下一个问题一起解决。

    问题 3:我正在为服务帐户密钥上传占位符文件,并在构建过程中从秘密变量中填充它,而不是使用 Azure Pipelines 上的安全文件功能

    上传安全文件允许您在构建过程中下载文件,而无需担心下载时文件的存储位置,因为该位置存储在变量中。它是dead simple to use。我只是在代理启动命令中加入了 location 变量,运行流畅。

    问题 4:我需要代理在我运行测试时仍然在运行

    代理是一个长时间运行的进程,因此启动它的管道任务将继续运行并阻止管道其余部分的执行,直到代理停止。但是代理需要在运行测试的同时运行,所以我能想到的唯一解决方案是将它们放在同一个任务中。

    我可以通过运行以下命令来做到这一点

    <proxy launch command> & \
    sleep 20 && \ //wait for the proxy to finish launching
    yarn test && \
    docker stop $(docker ps -a -q) //kill the proxy once tests finish running
    

    最后,下载文件并运行代理和测试的 yaml 看起来像这样:

    - task: DownloadSecureFile@1
      displayName: Download QA Key
      inputs:
        secureFile: 'qa-key.json'
    
    - script: |
        docker run \
        -v $(Agent.TempDirectory):/config \
        -p 127.0.0.1:5432:5432 \
        gcr.io/cloudsql-docker/gce-proxy:1.12 /cloud_sql_proxy \
        -instances=$(DBInstanceConnectionName)=tcp:0.0.0.0:5432 - 
        credential_file=/config/qa-key.json & \ 
        sleep 20 && \
        yarn test && \
        docker stop $(docker ps -a -q)
      displayName: 'Run Tests'
    

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多