GoLang postgres testcontainer 将 BindMounts 转换为 Mounts

huangapple go评论141阅读模式
英文:

GoLang postgres testcontainer convert BindMounts to Mounts

问题

我刚刚将测试容器库从github.com/testcontainers/testcontainers-go v0.12.0升级到github.com/testcontainers/testcontainers-go v0.13.0。之前我创建请求的方式如下:

ContainerRequest: testcontainers.ContainerRequest{
    Image:          mountebankImage,
    Name:           uuid.New().String(),
    ExposedPorts:   []string{mountebankExposedPort},
    BindMounts:     map[string]string{"/mountebank": path.Join(c.rootDir, "/test/stubs/mountebank")},
    Entrypoint:     []string{"mb", "start", "--configfile", "/mountebank/imposters.ejs"},
    Networks:       []string{c.network.Name},
}

在最新版本的测试容器库中,不再支持BindMounts(链接1)。我尝试在我的初始化脚本中替换它,但是找不到替代方法。

BindMounts:     map[string]string{"/mountebank": path.Join(c.rootDir, "/test/stubs/mountebank")},

这是请求体的一部分。我尝试了testcontainers.ContainerMounts{}等方法,但没有成功。

我有什么遗漏吗?

英文:

I have just upgraded the test container lib from github.com/testcontainers/testcontainers-go v0.12.0 to github.com/testcontainers/testcontainers-go v0.13.0
previously this is the way I was creating a request

	ContainerRequest: testcontainers.ContainerRequest{
			Image:          mountebankImage,
			Name:           uuid.New().String(),
			ExposedPorts:   []string{mountebankExposedPort},
			BindMounts:     map[string]string{"/mountebank": path.Join(c.rootDir, "/test/stubs/mountebank")},
			Entrypoint:     []string{"mb", "start", "--configfile", "/mountebank/imposters.ejs"},
			Networks:       []string{c.network.Name},
   

In the recent version of the test container library, BindMounts(not supported anymore link) got replaced by Mounts.
Tried replacing the same in my init script however not able to find it.

BindMounts:     map[string]string{"/mountebank": path.Join(c.rootDir, "/test/stubs/mountebank")},

its a part of request body. Tried with testcontainers.ContainerMounts{}etc.

Am I missing something?

答案1

得分: 4

ContainerRequest 对象包含一个 ContainerMount 对象的列表,这些对象记录了

> 源通常是 GenericBindMountSourceGenericVolumeMountSource

GenericBindMountSource 只是命名了一个主机路径。如果需要高级选项,也可以使用 DockerBindMountSource

因此,您应该能够将 BindMounts: 参数替换为 Mounts:

ContainerRequest: testcontainers.ContainerRequest{
        Mounts: testcontainers.Mounts(testcontainers.ContainerMount{
                Source: testcontainers.GenericBindMountSource{
                        HostPath: path.Join(c.rootDir, "/test/stubs/mountebank"),
                },
                Target: testcontainers.ContainerMountTarget("/mountebank"),
        }),
        ...
},
英文:

The ContainerRequest object contains a list of ContainerMount objects, which document that

> Source is typically either a GenericBindMountSource or a GenericVolumeMountSource

GenericBindMountSource just names a host path. You could also use a DockerBindMountSource if you needed advanced options.

So you should be able to replace that BindMounts: parameter with Mounts:

ContainerRequest: testcontainers.ContainerRequest{
        Mounts: testcontainers.Mounts(testcontainers.ContainerMount{
                Source: testcontainers.GenericBindMountSource{
                        HostPath: path.Join(c.rootDir, "/test/stubs/mountebank"),
                },
                Target: testcontainers.ContainerMountTarget("/mountebank"),
        }),
        ...
},

huangapple
  • 本文由 发表于 2022年5月10日 15:02:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/72181947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定