登录
首页 >  Golang >  Go问答

在Golang中进行容器测试时,遇到 Azure Devops 管道问题:容器被意外终止?

来源:stackoverflow

时间:2024-02-06 12:09:23 367浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《在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 特有的问题,因为我们发现即使在单个容器中运行的单元测试也会随机失败。

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

终于介绍完啦!小伙伴们,这篇关于《在Golang中进行容器测试时,遇到 Azure Devops 管道问题:容器被意外终止?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>