卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35650本站已运行398

Golang testcontainers,无法使网络工作

golang testcontainers,无法使网络工作

php小编草莓在使用Golang testcontainers时遇到了一个问题,即无法使网络正常工作。Golang testcontainers是一个用于在测试中运行容器的工具,它可以帮助开发人员在测试环境中快速启动和销毁容器。然而,在实际使用中,php小编草莓发现无法在容器中正常使用网络功能,这给测试带来了一定的困扰。接下来,我们将一起探讨这个问题的解决方案。

问题内容

我正在尝试在我的微服务中创建一些测试,我想创建一个网络,将我的数据库测试容器(postgres)和我的微服务测试容器附加到该网络。无论我尝试什么,我都无法让我的微服务连接到数据库。我的微服务是使用 Fiber 和 Gorm 的 golang。我尝试连接到 db.go 配置文件中的数据库,如下所示:

func SetupDB(port string, host string) *gorm.DB {

    dsn := "host=" + host + " user=postgres password=password dbname=prescription port=" + port + " sslmode=disable"

    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

    if err != nil {
        panic("error connecting to database")
    }

    db.AutoMigrate(&model.Prescription{})

    return db
}

这就是我的测试容器的样子:

prescriptionDBContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            Image:        "postgres",
            ExposedPorts: []string{postgresPort.Port()},
            Env: map[string]string{
                "POSTGRES_USER":     "postgres",
                "POSTGRES_PASSWORD": "password",
                "POSTGRES_DB":       "prescription",
            },
            Networks: []string{network.Name},
            NetworkAliases: map[string][]string{
                network.Name: {"db-network"},
            },
            WaitingFor: wait.ForAll(
                wait.ForLog("database system is ready to accept connections"),
                wait.ForListeningPort(postgresPort),
            ),
        },
        Started: true,
    })
prescriptionContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            FromDockerfile: testcontainers.FromDockerfile{Context: "../../../../prescription"},
            Networks:       []string{network.Name},
            NetworkAliases: map[string][]string{
                network.Name: {"db-network"},
            },
            Env: map[string]string{
                "POSTGRES_USER":     "postgres",
                "POSTGRES_PASSWORD": "password",
                "POSTGRES_DB":       "prescription",
                "HOST":              prescriptionDBHost,
                "DB_PORT":           prescriptionDBPort.Port(),
            },
            ExposedPorts: []string{pMicroPort.Port()},
            WaitingFor:   wait.ForListeningPort("8080"),
        },
        Started: true,
    })

也许是因为我根本不了解docker中网络连接过程中发生的事情,但我真的迷失了,当我为HOST和DB_PORT设置环境时(我已经尝试了阳光下的每一种组合),它拒绝连接数据库的微服务

在我尝试过的微服务的测试容器中:

"HOST":              prescriptionDBHost,
"DB_PORT":           prescriptionDBPort.Port(),

prescriptionDBHost 的提取方式为:

prescriptionDBHost, err := prescriptionDBContainer.Name(context.Background())

导致错误消息:

failed to initialize database, got error failed to connect to `host=/stoic_heyrovsky user=postgres database=prescription`: dial error (dial unix /stoic_heyrovsky/.s.PGSQL.53802: connect: no such file or directory)
panic: error connecting to database

然后我尝试从主机名中删除“/”,例如:

"HOST":              strings.Trim(prescriptionDBHost,"/"),
"DB_PORT":           prescriptionDBPort.Port(),

我也尝试过:

"HOST":              "localhost",
"DB_PORT":           prescriptionDBPort.Port(),
"HOST":              "127.0.0.1",
"DB_PORT":           prescriptionDBPort.Port(),
prescriptionDBHost, err := prescriptionDBContainer.ContainerIP(context.Background())

"HOST":              prescriptionDBHost,
"DB_PORT":           prescriptionDBPort.Port(),

这里的最后 4 个示例都会导致某种拨号 TCP 错误,例如:

failed to initialize database, got error failed to connect to `host=localhost user=postgres database=prescription`: dial error (dial tcp [::1]:53921: connect: cannot assign requested address)

我还在 testcontainer 创建数据库容器后进行了调试和停止,然后转到我的微服务并使用 DB_HOST=localhost 和 port= 硬编码连接到该容器,并且成功了,所以我真的迷失了正在发生的事情错误的。我唯一能想到的是,微服务容器在尝试连接数据库之前没有连接到网络?我做了一个 docker 网络检查,我可以看到数据库容器已附加,但微服务从未附加(但也许这只是因为其他原因?)。

解决方法

你可以这样做:

prescriptionDBContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
    ContainerRequest: testcontainers.ContainerRequest{
        Image:        "postgres",
        ExposedPorts: []string{"5432/tcp"},
        Env: map[string]string{
            "POSTGRES_USER":     "postgres",
            "POSTGRES_PASSWORD": "password",
            "POSTGRES_DB":       "prescription",
        },
        Networks:       []string{networkName},
        NetworkAliases: map[string][]string{networkName: []string{"postgres"}},
        WaitingFor: wait.ForAll(
            wait.ForLog("database system is ready to accept connections"),
            wait.ForListeningPort("5432/tcp"),
        ),
    },
    Started: true,
})
if err != nil {
    t.Fatal(err)
}

prescriptionContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
    ContainerRequest: testcontainers.ContainerRequest{
        FromDockerfile: testcontainers.FromDockerfile{Context: "./testapp"},
        ExposedPorts:   []string{"8080/tcp"},
        Networks:       []string{networkName},
        NetworkAliases: map[string][]string{networkName: []string{"blah"}},
        Env: map[string]string{
            "DATABASE_URL": "postgres://postgres:password@postgres:5432/prescription",
        },
        WaitingFor: wait.ForListeningPort("8080/tcp"),
    },
    Started: true,
})

注意NetworkAliases的配置方式;在您的代码中,您将两者都设置为 db-network 但是,我想,这是由于误解。该设置配置了一个可以引用容器的别名(在本例中,我使用 postgres 作为 postgres 容器;这意味着当连接 HOST 时,根据上面示例中使用的 URL,postgres 将是 postgres)。

作为替代方案,您可以使用 port, err :=处方DBContainer.MappedPort(context.Background(), "5432/tcp") 获取主机上公开的端口,然后连接到端口上的 host.docker.internal port.Port().当被测试的应用程序在主机上而不是在容器中运行时,经常使用此方法(但在这种情况下,您将连接到 localhost 并使用从 MappedPort() 返回的报告)。

卓越飞翔博客
上一篇: git ls-remote 成功而 go get 失败
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏