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
}