【问题标题】:Github Actions: Connecting to postgres database in diesel-rsGithub Actions:在 diesel-rs 中连接到 postgres 数据库
【发布时间】:2022-12-10 10:17:27
【问题描述】:

我正在尝试在我的 Actix Web 应用程序的 CI 工作流中运行 cargo test。每个测试通过首先连接到默认数据库(“postgres”)然后执行 SQL 查询来创建自己的数据库。

这是当前使用的工作流程,“Test postgres connection”成功运行,但“Cargo test”失败:

on: [push, pull_request]

name: CI

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    container: rust:latest
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Install PSQL
        run: |
          apt update
          apt install -y postgresql-client

      - name: Test postgres connection
        run: psql -h postgres -d postgres -U postgres -c 'SELECT 1;'
        env:
          PGPASSWORD: postgres

      - name: Cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --verbose
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

这是其中一项测试的示例:

struct Context {
    pub psql_user: String,
    pub psql_pw: String,
}

impl Context {
    fn new() -> Self {
        dotenv().ok();
        let psql_user =
            env::var("POSTGRES_USER").expect("POSTGRES_USER must be set for integration tests");
        let psql_pw = env::var("POSTGRES_PASSWORD")
            .expect("POSTGRES_PASSWORD must be set for integration tests");
        let database_url = format!(
            "postgres://{}:{}@localhost:5432/postgres",
            psql_user, psql_pw
        );
        let mut conn = PgConnection::establish(&database_url)
            .expect("Failed to connect to the database 'postgres'"); // This panics

        // ...
    }
}

#[actix_web::test]
async fn test_create_task_req() {
    let ctx = Context::new("create_task_test");

    // ...
}

我假设错误出在我的代码中,因为工作流中的一切都运行良好,直到 cargo test 抛出此错误:

---- test_create_task_req stdout ----
thread 'test_create_task_req' panicked at 'Failed to connect to the database 'postgres': 
BadConnection("could not connect to server: Connection refused
    Is the server running on host \"localhost\" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host \"localhost\" (::1) and accepting
    TCP/IP connections on port 5432?
")', 
tests/tasks_crud_integration.rs:42:14

在本地运行cargo test,没有出现问题。

【问题讨论】:

    标签: postgresql github-actions actix-web rust-diesel


    【解决方案1】:

    经过反复试验,我最终找到了一个可行的解决方案:

    on: [push, pull_request]
    
    name: CI
    
    env:
      CARGO_TERM_COLOR: always
    
    jobs:
        test:
            name: Test
            runs-on: ubuntu-latest
            # Removed the container 'rust:latest'
            services:
              postgres:
                image: postgres # Removed version notation
                env:
                  POSTGRES_PASSWORD: postgres
                ports:
                  - 5432:5432
                options: >-
                  --health-cmd pg_isready
                  --health-interval 10s
                  --health-timeout 5s
                  --health-retries 5
    
            steps:
              - name: Checkout sources
                uses: actions/checkout@v2
    
              - name: Install stable toolchain
                uses: actions-rs/toolchain@v1
                with:
                  profile: minimal
                  toolchain: stable
                  override: true
    
              # Removed 'Install PSQL' step as psql comes preinstalled in the postgres Docker Hub image
    
              - name: Test postgres connection
                run: psql postgres://postgres:postgres@localhost:5432/postgres -c 'SELECT 1;'
    
              - name: Cargo test
                uses: actions-rs/cargo@v1
                with:
                  command: test
                  args: --verbose
                env:
                  POSTGRES_USER: postgres
                  POSTGRES_PASSWORD: postgres
    

    如上所示,看似最关键的变化是删除了生锈容器(无论如何这对工作流程来说都是不必要的)。尽管找到了解决方案,但我仍然不确切地知道 Docker 映像中的什么首先导致了问题。

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 2023-02-14
      • 1970-01-01
      • 2022-08-08
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2021-07-14
      • 2017-11-12
      相关资源
      最近更新 更多