1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/client/plugin_inspect_test.go
Emil Davtyan 3e6bbefd26 Produce errors when empty ids are passed into inspect calls.
If a blank nodeID was previously passed in it resulted in a node list
request. The response would then fail to umarshal into a `Node`
type returning a JSON error.

This adds an extra validation to all inspect calls to check that the ID
that is required is provided and if not return an error.

Signed-off-by: Emil Davtyan <emil2k@gmail.com>
2018-02-07 14:05:14 +01:00

67 lines
1.7 KiB
Go

package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
func TestPluginInspectError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, _, err := client.PluginInspectWithRaw(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginInspectWithEmptyID(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("should not make request")
}),
}
_, _, err := client.PluginInspectWithRaw(context.Background(), "")
if !IsErrNotFound(err) {
t.Fatalf("Expected NotFoundError, got %v", err)
}
}
func TestPluginInspect(t *testing.T) {
expectedURL := "/plugins/plugin_name"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
content, err := json.Marshal(types.Plugin{
ID: "plugin_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(content)),
}, nil
}),
}
pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name")
if err != nil {
t.Fatal(err)
}
if pluginInspect.ID != "plugin_id" {
t.Fatalf("expected `plugin_id`, got %s", pluginInspect.ID)
}
}