Golang 测试容器与 Azure Devops 管道:容器被随机杀死?

2024年 2月 5日 29.1k 0

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 管道:容器被随机杀死?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论