使用 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/

相关文章:

用于测试的 Elasticsearch 和 Testcontainers "None of the configured nodes are available"

java - 在 SpringBoot 中使用 Testcontainers 进行 Spring Data Elasticsearch 集成测试

java - testcontainer启动后,如何在集成测试中覆盖application.properties中定义的端口?

docker - 使用系统权限在 oracle 测试容器上运行 init 脚本

go - 如何检测是否调用了重定向

git - 从 Kubernetes secret 获取凭据时的 go-git 基本身份验证问题

go - 为什么我可以省略函数的参数名称?

java - 具有 sftp 图像的测试容器在 "nc not found"或 "Address Not Available"上抛出错误

go - 如何从 Go 1.18 中的单个方法返回两种不同的具体类型?

go - 为什么下面这个 panic 例子是 golang 的类型错误?