在使用Docker的客户端API测试方法时,出现了错误的类型错误。

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

Wrong type error on an interface intended to test a method using Docker's client API

问题

我正在重构我写的一个程序,以便我可以为其编写适当的测试。我想首先测试的一个方法是使用Docker的客户端API来查看Docker主机上是否存在某个镜像。

为了能够测试这个方法,我创建了一个与client.ImageList签名相匹配的接口:

type ImageLister interface {
    ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
}

我还修改了要测试的方法,使其接受一个ImageLister作为参数,这样我就可以传入一个特定于我的测试的ImageLister实现。

然而,在我的实际代码中,当我将“真实”的Docker客户端传递给要测试的方法时,出现了以下编译错误:

ImageExists:
    *client.Client does not implement ImageLister (wrong type for ImageList method)
        have ImageList("github.com/docker/docker/vendor/golang.org/x/net/context".Context, types.ImageListOptions) ([]types.ImageSummary, error)
        want ImageList("context".Context, types.ImageListOptions) ([]types.ImageSummary, error)

我该如何解决这个问题?或者我的方法是否不好,我应该选择其他方法?

编辑:
以下程序重现了我遇到的问题。

package main

import (
    "context"
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
)

type ImageLister interface {
    ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
}

func main() {
    client, err := client.NewEnvClient()
    defer client.Close()

    ImageExists(context.TODO(), client, "foo")
}

func ImageExists(ctx context.Context, lister ImageLister, image string) (bool, error) {
    return true, nil
}
英文:

I'm refactoring a program I wrote so I can properly write tests for it. One of the first methods I'd like to test is a method that uses Docker's client API to see if a certain image exists on a Docker host.

To be able to test this method, I created an interface that matches client.ImageList's signature:

type ImageLister interface {
	ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
}

I also changed the method to test to take an ImageLister as argument, so I can pass in an ImageLister implementation specific to my tests.

However, in my actual code, where I pass in the "real" Docker client to the method to test, the following compilation error occurs:

> ImageExists:
*client.Client does not implement ImageLister (wrong type for ImageList method)
have ImageList("github.com/docker/docker/vendor/golang.org/x/net/context".Context, types.ImageListOptions) ([]types.ImageSummary, error)
want ImageList("context".Context, types.ImageListOptions) ([]types.ImageSummary, error)

How can I resolve this? Or is my approach bad anyway, and should I go a different route?

edit:
The following program reproduces the issue I'm encountering.

package main

import (
	"context"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/client"
)

type ImageLister interface {
	ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
}

func main() {
	client, err := client.NewEnvClient()
	defer client.Close()

	ImageExists(context.TODO(), client, "foo")
}

func ImageExists(ctx context.Context, lister ImageLister, image string) (bool, error) {
	return true, nil
}

答案1

得分: 2

github.com/docker/docker/ 包已经将其依赖项“vendored”到 github.com/docker/docker/vendor 目录中。尽管位于 vendor/ 目录中的包是通过其正常的导入路径导入的,但类型仍然通过其完整路径进行匹配,以避免在意外导入包多次时出现不兼容性。因此,在包之间共享类型时,两个包都需要从相同的路径导入依赖项。

处理这个问题的方法是正确地将 docker 包进行 vendoring,这意味着将 docker 使用的包移动到顶层的 vendor 目录中。像 govendorglide 这样的工具可以为您完成此操作,目前还有一个官方的依赖管理工具正在开发中。

英文:

The github.com/docker/docker/ package has "vendored" its dependencies in the github.com/docker/docker/vendor directory. While a package in a vendor/ directory is imported by it's normal import path, the types are still matched by their full path to avoid incompatibilities if a package is inadvertently imported multiple times. Because of this, when sharing types between packages, both packages need to import the dependency from the same path.

The way to handle this is to properly vendor the docker package, which means moving the packages used by docker up to the top-level vendor directory. Various tools like govendor or glide can do this for you, and there is work on an "official" tool for dependency management right now too.

答案2

得分: 1

参数类型

接口中声明的方法需要本地类型 context.Context,但实现的方法却期望来自 Docker 的 Context。这两个实体是完全不同的类型,所以 ImageLister 接口不能用 *client.Client 来实现。

命名导入

我认为问题出在这里:

> want ImageList("context".Context...

这意味着你的本地包名为 "context"。由于 Docker 有一个名为 "context" 的包(github.com/docker/docker/vendor/golang.org/x/net/context),这个事实造成了命名的歧义,并可能导致一些问题。

导入声明

> 假设我们编译了一个包,包含了包声明 package math,导出了函数 Sin,并将编译后的包安装在由 "lib/math" 标识的文件中。这个表格说明了在导入该包的文件中,根据不同类型的导入声明如何访问 Sin

// 导入声明                  Sin 的本地名称

import   "lib/math"        // math.Sin
import m "lib/math"        // m.Sin
import . "lib/math"        // Sin

使用命名导入来解决这个歧义:

import (
    ctx "github.com/docker/docker/vendor/golang.org/x/net/context"
)
英文:

Argument types

The method declared in the interface wants local type context.Context but the implemented method expects Context from docker. This two entities are absolutely different types so ImageLister interface is not implemented with *client.Client.

Named imports

I believe the problem is here:

> want ImageList("context".Context...

It means your local package named "context". Since docker has a package "context" (github.com/docker/docker/vendor/golang.org/x/net/context) this fact creates a naming ambiguity and could cause some issues.

Import declarations:

> Assume we have compiled a package containing the package clause package math, which exports function Sin, and installed the compiled package in the file identified by "lib/math". This table illustrates how Sin is accessed in files that import the package after the various types of import declaration.

// Import declaration          Local name of Sin

import   "lib/math"        // math.Sin
import m "lib/math"        // m.Sin
import . "lib/math"        // Sin

Use named imports to resolve this ambiguity:

import (
    ctx "github.com/docker/docker/vendor/golang.org/x/net/context"
)

huangapple
  • 本文由 发表于 2017年2月10日 19:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/42158672.html
匿名

发表评论

匿名网友

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

确定