使用 testcontainer-go 时遇到问题

标签 go testcontainers

我正在试用 https://github.com/testcontainers/testcontainer-go 页面的示例

package main

import (
    "context"
    "fmt"
    "net/http"
    "testing"

    testcontainer "github.com/testcontainers/testcontainer-go"
)

func TestNginxLatestReturn(t *testing.T) {
    ctx := context.Background()
    req := testcontainer.ContainerRequest{
        Image:        "nginx",
        ExposedPorts: []string{"80/tcp"},
    }
...
}

但是当我将代码放入文件中时 main.go<home>/go/src/my-sample 下并调用go get ,我只是得到这个错误:

#

 github.com/testcontainers/testcontainer-go
../github.com/testcontainers/testcontainer-go/docker.go:116:32: cannot use inspect.NetworkSettings.NetworkSettingsBase.Ports (type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortMap) as type "github.com/docker/go-connections/nat".PortMap in return argument
../github.com/testcontainers/testcontainer-go/docker.go:197:25: multiple-value uuid.NewV4() in single-value context
../github.com/testcontainers/testcontainer-go/docker.go:219:3: cannot use exposedPortSet (type map["github.com/docker/go-connections/nat".Port]struct {}) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortSet in field value
../github.com/testcontainers/testcontainer-go/docker.go:261:3: cannot use exposedPortMap (type map["github.com/docker/go-connections/nat".Port][]"github.com/docker/go-connections/nat".PortBinding) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortMap in field value

我做错了什么?

最佳答案

我也重现了这个问题,但我确实通过以下步骤构建并运行了测试:

  1. $GOPATH 树之外创建一个 my_package 目录。
  2. 创建一个包含以下内容的 mypackage_test.go 文件:

    package main
    
    import (
        "testing"
    
        testcontainer "github.com/testcontainers/testcontainer-go"
    )
    
    func testNginxLatestReturn(t *testing.T) {
        req := testcontainer.ContainerRequest{
            Image:        "nginx",
            ExposedPorts: []string{"80/tcp"},
        }
        t.Log(req)
    }
    
  3. go mod init mypackage

  4. 去测试

关于使用 testcontainer-go 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54007888/

相关文章:

http - 输入 TYPE TEXT 值形式 (enctype =“multipart/form-data” ) 返回 null

eclipse - Eclipse 中的自动完成错误

jquery - 输入值不适用于 parsley.js addAsyncValidator 方法参数

asp.net-core - Testcontainers for .NET 错误自动发现未检测到 Docker 主机配置

java - 使用 testcontainer 作为 Dockerfile 的一部分运行测试

go - 解码不同类型的数组

go - 我可以在生产中使用 master libpq 吗

java - 无法在 Testcontainers 中使用 Chrome 驱动程序使用 Selenium 打开任何 URL

docker - Spring Batch是否可以使用外部应用程序启动容器?

unit-testing - 测试容器中StartupCheck策略和Wait策略的区别