update local code for updated modules
Some tests had to be skipped as there's some issues to address, and some of the result-types cannot be mocked / stubbed. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@ -3,26 +3,25 @@ package idresolver
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"github.com/moby/moby/client"
|
||||
)
|
||||
|
||||
type fakeClient struct {
|
||||
client.Client
|
||||
nodeInspectFunc func(string) (swarm.Node, []byte, error)
|
||||
serviceInspectFunc func(string) (swarm.Service, []byte, error)
|
||||
nodeInspectFunc func(string) (client.NodeInspectResult, error)
|
||||
serviceInspectFunc func(string) (client.ServiceInspectResult, error)
|
||||
}
|
||||
|
||||
func (cli *fakeClient) NodeInspectWithRaw(_ context.Context, nodeID string) (swarm.Node, []byte, error) {
|
||||
func (cli *fakeClient) NodeInspect(_ context.Context, nodeID string, _ client.NodeInspectOptions) (client.NodeInspectResult, error) {
|
||||
if cli.nodeInspectFunc != nil {
|
||||
return cli.nodeInspectFunc(nodeID)
|
||||
}
|
||||
return swarm.Node{}, []byte{}, nil
|
||||
return client.NodeInspectResult{}, nil
|
||||
}
|
||||
|
||||
func (cli *fakeClient) ServiceInspectWithRaw(_ context.Context, serviceID string, _ client.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
||||
func (cli *fakeClient) ServiceInspect(_ context.Context, serviceID string, _ client.ServiceInspectOptions) (client.ServiceInspectResult, error) {
|
||||
if cli.serviceInspectFunc != nil {
|
||||
return cli.serviceInspectFunc(serviceID)
|
||||
}
|
||||
return swarm.Service{}, []byte{}, nil
|
||||
return client.ServiceInspectResult{}, nil
|
||||
}
|
||||
|
||||
@ -30,25 +30,25 @@ func New(apiClient client.APIClient, noResolve bool) *IDResolver {
|
||||
func (r *IDResolver) get(ctx context.Context, t any, id string) (string, error) {
|
||||
switch t.(type) {
|
||||
case swarm.Node:
|
||||
node, _, err := r.client.NodeInspectWithRaw(ctx, id)
|
||||
res, err := r.client.NodeInspect(ctx, id, client.NodeInspectOptions{})
|
||||
if err != nil {
|
||||
// TODO(thaJeztah): should error-handling be more specific, or is it ok to ignore any error?
|
||||
return id, nil //nolint:nilerr // ignore nil-error being returned, as this is a best-effort.
|
||||
}
|
||||
if node.Spec.Annotations.Name != "" {
|
||||
return node.Spec.Annotations.Name, nil
|
||||
if res.Node.Spec.Annotations.Name != "" {
|
||||
return res.Node.Spec.Annotations.Name, nil
|
||||
}
|
||||
if node.Description.Hostname != "" {
|
||||
return node.Description.Hostname, nil
|
||||
if res.Node.Description.Hostname != "" {
|
||||
return res.Node.Description.Hostname, nil
|
||||
}
|
||||
return id, nil
|
||||
case swarm.Service:
|
||||
service, _, err := r.client.ServiceInspectWithRaw(ctx, id, client.ServiceInspectOptions{})
|
||||
res, err := r.client.ServiceInspect(ctx, id, client.ServiceInspectOptions{})
|
||||
if err != nil {
|
||||
// TODO(thaJeztah): should error-handling be more specific, or is it ok to ignore any error?
|
||||
return id, nil //nolint:nilerr // ignore nil-error being returned, as this is a best-effort.
|
||||
}
|
||||
return service.Spec.Annotations.Name, nil
|
||||
return res.Service.Spec.Annotations.Name, nil
|
||||
default:
|
||||
return "", errors.New("unsupported type")
|
||||
}
|
||||
|
||||
@ -7,14 +7,15 @@ import (
|
||||
|
||||
"github.com/docker/cli/internal/test/builders"
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"github.com/moby/moby/client"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
||||
func TestResolveError(t *testing.T) {
|
||||
apiClient := &fakeClient{
|
||||
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
|
||||
return swarm.Node{}, []byte{}, errors.New("error inspecting node")
|
||||
nodeInspectFunc: func(nodeID string) (client.NodeInspectResult, error) {
|
||||
return client.NodeInspectResult{}, errors.New("error inspecting node")
|
||||
},
|
||||
}
|
||||
|
||||
@ -27,13 +28,13 @@ func TestResolveError(t *testing.T) {
|
||||
func TestResolveWithNoResolveOption(t *testing.T) {
|
||||
resolved := false
|
||||
apiClient := &fakeClient{
|
||||
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
|
||||
nodeInspectFunc: func(nodeID string) (client.NodeInspectResult, error) {
|
||||
resolved = true
|
||||
return swarm.Node{}, []byte{}, nil
|
||||
return client.NodeInspectResult{}, nil
|
||||
},
|
||||
serviceInspectFunc: func(serviceID string) (swarm.Service, []byte, error) {
|
||||
serviceInspectFunc: func(serviceID string) (client.ServiceInspectResult, error) {
|
||||
resolved = true
|
||||
return swarm.Service{}, []byte{}, nil
|
||||
return client.ServiceInspectResult{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
@ -48,9 +49,11 @@ func TestResolveWithNoResolveOption(t *testing.T) {
|
||||
func TestResolveWithCache(t *testing.T) {
|
||||
inspectCounter := 0
|
||||
apiClient := &fakeClient{
|
||||
nodeInspectFunc: func(nodeID string) (swarm.Node, []byte, error) {
|
||||
nodeInspectFunc: func(string) (client.NodeInspectResult, error) {
|
||||
inspectCounter++
|
||||
return *builders.Node(builders.NodeName("node-foo")), []byte{}, nil
|
||||
return client.NodeInspectResult{
|
||||
Node: *builders.Node(builders.NodeName("node-foo")),
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
@ -69,27 +72,31 @@ func TestResolveWithCache(t *testing.T) {
|
||||
func TestResolveNode(t *testing.T) {
|
||||
testCases := []struct {
|
||||
nodeID string
|
||||
nodeInspectFunc func(string) (swarm.Node, []byte, error)
|
||||
nodeInspectFunc func(string) (client.NodeInspectResult, error)
|
||||
expectedID string
|
||||
}{
|
||||
{
|
||||
nodeID: "nodeID",
|
||||
nodeInspectFunc: func(string) (swarm.Node, []byte, error) {
|
||||
return swarm.Node{}, []byte{}, errors.New("error inspecting node")
|
||||
nodeInspectFunc: func(string) (client.NodeInspectResult, error) {
|
||||
return client.NodeInspectResult{}, errors.New("error inspecting node")
|
||||
},
|
||||
expectedID: "nodeID",
|
||||
},
|
||||
{
|
||||
nodeID: "nodeID",
|
||||
nodeInspectFunc: func(string) (swarm.Node, []byte, error) {
|
||||
return *builders.Node(builders.NodeName("node-foo")), []byte{}, nil
|
||||
nodeInspectFunc: func(string) (client.NodeInspectResult, error) {
|
||||
return client.NodeInspectResult{
|
||||
Node: *builders.Node(builders.NodeName("node-foo")),
|
||||
}, nil
|
||||
},
|
||||
expectedID: "node-foo",
|
||||
},
|
||||
{
|
||||
nodeID: "nodeID",
|
||||
nodeInspectFunc: func(string) (swarm.Node, []byte, error) {
|
||||
return *builders.Node(builders.NodeName(""), builders.Hostname("node-hostname")), []byte{}, nil
|
||||
nodeInspectFunc: func(string) (client.NodeInspectResult, error) {
|
||||
return client.NodeInspectResult{
|
||||
Node: *builders.Node(builders.NodeName(""), builders.Hostname("node-hostname")),
|
||||
}, nil
|
||||
},
|
||||
expectedID: "node-hostname",
|
||||
},
|
||||
@ -111,20 +118,22 @@ func TestResolveNode(t *testing.T) {
|
||||
func TestResolveService(t *testing.T) {
|
||||
testCases := []struct {
|
||||
serviceID string
|
||||
serviceInspectFunc func(string) (swarm.Service, []byte, error)
|
||||
serviceInspectFunc func(string) (client.ServiceInspectResult, error)
|
||||
expectedID string
|
||||
}{
|
||||
{
|
||||
serviceID: "serviceID",
|
||||
serviceInspectFunc: func(string) (swarm.Service, []byte, error) {
|
||||
return swarm.Service{}, []byte{}, errors.New("error inspecting service")
|
||||
serviceInspectFunc: func(string) (client.ServiceInspectResult, error) {
|
||||
return client.ServiceInspectResult{}, errors.New("error inspecting service")
|
||||
},
|
||||
expectedID: "serviceID",
|
||||
},
|
||||
{
|
||||
serviceID: "serviceID",
|
||||
serviceInspectFunc: func(string) (swarm.Service, []byte, error) {
|
||||
return *builders.Service(builders.ServiceName("service-foo")), []byte{}, nil
|
||||
serviceInspectFunc: func(string) (client.ServiceInspectResult, error) {
|
||||
return client.ServiceInspectResult{
|
||||
Service: *builders.Service(builders.ServiceName("service-foo")),
|
||||
}, nil
|
||||
},
|
||||
expectedID: "service-foo",
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user