douji1877 2017-02-10 11:48
浏览 177
已采纳

打算使用Docker客户端API测试方法的接口上的类型错误

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
}
  • 写回答

2条回答 默认 最新

  • doumiebiao6827 2017-02-10 17:51
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝