最新公告
  • 欢迎您光临码农资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!加入我们
  • Golang 测试容器与 Azure Devops 管道:容器被随机杀死?

    golang 测试容器与 azure devops 管道:容器被随机杀死?

    问题内容

    虽然使用 golang testcontainers 实现和运行我的数据库集成测试在本地非常有效,但我的测试似乎在 azure devops 管道中随机不起作用。

    管道日志显示:

    2023/01/09 16:06:02 (...) error: read tcp 127.0.0.1:52546->127.0.0.1:49161: read: connection reset by peer

    添加容器日志记录、改进等待标准、删除 db 容器配置文件的使用(因此我不必将文件复制到容器中)并禁用 ryuk 后,我想知道还需要做什么,或者如果我我初始化容器不正确。

    对于每个单元测试,测试容器都是这样启动的:

    func setuptestdatabase(ctx context.context) (testcontainers.container, project.repository, error) {
        containerreq := testcontainers.containerrequest{
            skipreaper:   true,
            image:        "postgres:11.18-alpine3.17",
            exposedports: []string{"5432/tcp"},
            cmd:          []string{"postgres", "-c", "fsync=off"},
            env: map[string]string{
                "postgres_db":         "postgre",
                "postgres_password":   "postgres",
                "postgres_user":       "postgres",
                "pguser":              "postgres",
                "postgres_extensions": "uuid-ossp",
            },
        }
    
        containerreq.waitingfor = wait.forall(
            wait.forlisteningport("5432/tcp"),
            wait.forexec([]string{"pg_isready", "-t 10", "-q"}), // postgre docs: https://www.postgresql.org/docs/9.4/app-pg-isready.html
        ).withdeadline(3 * time.minute)
    
        dbcontainer, err := testcontainers.genericcontainer(
            ctx,
            testcontainers.genericcontainerrequest{
                containerrequest: containerreq,
                started:          true,
            })
        if err != nil {
            log.fatalf("database container could not be started. error: %s", err)
            return nil, nil, errors.withstack(err)
        }
    
        err = dbcontainer.startlogproducer(ctx)
        if err != nil {
            log.fatalf("logproducer could not be started. error: %s", err)
            return nil, nil, errors.withstack(err)
        }
        defer dbcontainer.stoplogproducer()
        lc := logconsumer{}
        dbcontainer.followoutput(&lc)
    
        port, err := dbcontainer.mappedport(ctx, "5432")
        if err != nil {
            log.fatalf("mapped port could not be retrieved. error: %s", err)
            return nil, nil, errors.withstack(err)
        }
        host, err := dbcontainer.host(ctx)
        if err != nil {
            log.fatalf("hostname could not be retrieved. error: %s", err)
            return nil, nil, errors.withstack(err)
        }
    
        global.config.postgres.host = host
        global.config.postgres.port = port.port()
        global.config.postgres.user = "postgres"
        global.config.postgres.password = "postgres"
        global.config.postgres.dbname = "postgre"
        global.config.local = true
    
        global.logger = logger.newnull(config.config{
            logging: config.logging{
                level: "debug",
            },
        })
    
        repository, err := new(ctx)
        if err != nil {
            log.fatalf("repository could not be setup. error: %s", err)
            return nil, nil, errors.withstack(err)
        }
    
        return dbcontainer, repository, nil
    }

    …使用 repository 创建存储库,err := new(ctx) 最后使用 migrate 来设置与我们的生产数据库类似的数据库,并使用 gorm 进行数据库连接和处理等。

    单元测试的基本模板是:

    func Test_pg_Has(t *testing.T) {
        ctx := context.TODO()
        dbContainer, repository, err := SetupTestDatabase(ctx)
        if err != nil {
            t.Errorf("error running testcontainers, error: %s", err)
        }
        t.Cleanup(func() {
            if err := dbContainer.Terminate(ctx); err != nil {
                t.Fatalf("failed to terminate container: %s", err)
            }
            time.Sleep(10 * time.Second)
        })
        
        ... TEST_CODE
    }

    对于 azure pipeline,使用库存 azure 代理池,go 版本为“1.18.0 x64”。

    如有任何提示,我们将不胜感激,提前谢谢您。

    正确答案

    作为一个不太令人满意的解决方案,我在创建容器后、设置数据库连接之前添加了 5 秒的睡眠。

    这可能是 Azure DevOps 特有的问题,因为我们发现即使在单个容器中运行的单元测试也会随机失败。

    接受这个作为回应对社区来说也是一种挑战……

    想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
    本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
    如有侵权请发送邮件至1943759704@qq.com删除

    码农资源网 » Golang 测试容器与 Azure Devops 管道:容器被随机杀死?
    • 17会员总数(位)
    • 16020资源总数(个)
    • 1135本周发布(个)
    • 272 今日发布(个)
    • 113稳定运行(天)

    提供最优质的资源集合

    立即查看 了解详情