2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-06 14:46:37 -04:00
|
|
|
"encoding/json"
|
2021-08-12 06:57:15 -04:00
|
|
|
"errors"
|
2021-08-24 06:10:50 -04:00
|
|
|
"io"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-03-09 14:42:10 -05:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2019-10-12 18:31:53 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkInspect(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2019-10-12 14:41:14 -04:00
|
|
|
if req.Method != http.MethodGet {
|
2021-08-12 06:57:15 -04:00
|
|
|
return nil, errors.New("expected GET method, got " + req.Method)
|
|
|
|
}
|
|
|
|
if req.URL.Path == "/networks/" {
|
|
|
|
return errorMock(http.StatusInternalServerError, "client should not make a request for empty IDs")(req)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.URL.Path, "/networks/unknown") {
|
|
|
|
return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(req.URL.Path, "/networks/test-500-response") {
|
|
|
|
return errorMock(http.StatusInternalServerError, "Server error")(req)
|
|
|
|
}
|
|
|
|
// other test-cases all use "network_id"
|
|
|
|
if !strings.HasPrefix(req.URL.Path, "/networks/network_id") {
|
|
|
|
return nil, errors.New("expected URL '/networks/network_id', got " + req.URL.Path)
|
|
|
|
}
|
|
|
|
if strings.Contains(req.URL.RawQuery, "scope=global") {
|
2022-03-20 11:55:42 -04:00
|
|
|
return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-03-09 14:42:10 -05:00
|
|
|
var (
|
|
|
|
content []byte
|
|
|
|
err error
|
|
|
|
)
|
2017-06-11 14:04:35 -04:00
|
|
|
if strings.Contains(req.URL.RawQuery, "verbose=true") {
|
2017-03-09 14:42:10 -05:00
|
|
|
s := map[string]network.ServiceInfo{
|
|
|
|
"web": {},
|
|
|
|
}
|
|
|
|
content, err = json.Marshal(types.NetworkResource{
|
|
|
|
Name: "mynetwork",
|
|
|
|
Services: s,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
content, err = json.Marshal(types.NetworkResource{
|
|
|
|
Name: "mynetwork",
|
|
|
|
})
|
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
2021-08-12 06:57:15 -04:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
2016-09-06 14:46:37 -04:00
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(content)),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2021-08-12 06:57:15 -04:00
|
|
|
t.Run("empty ID", func(t *testing.T) {
|
|
|
|
// verify that the client does not create a request if the network-ID/name is empty.
|
|
|
|
_, err := client.NetworkInspect(context.Background(), "", types.NetworkInspectOptions{})
|
|
|
|
assert.Check(t, IsErrNotFound(err))
|
|
|
|
})
|
|
|
|
t.Run("no options", func(t *testing.T) {
|
|
|
|
r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, r.Name, "mynetwork")
|
|
|
|
})
|
|
|
|
t.Run("verbose", func(t *testing.T) {
|
|
|
|
r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Verbose: true})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, r.Name, "mynetwork")
|
|
|
|
_, ok := r.Services["web"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected service `web` missing in the verbose output")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("global scope", func(t *testing.T) {
|
|
|
|
_, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Scope: "global"})
|
2022-03-20 11:55:42 -04:00
|
|
|
assert.ErrorContains(t, err, "Error: No such network: network_id")
|
2021-08-12 06:57:15 -04:00
|
|
|
assert.Check(t, IsErrNotFound(err))
|
|
|
|
})
|
|
|
|
t.Run("unknown network", func(t *testing.T) {
|
|
|
|
_, err := client.NetworkInspect(context.Background(), "unknown", types.NetworkInspectOptions{})
|
2022-03-20 11:55:42 -04:00
|
|
|
assert.ErrorContains(t, err, "Error: No such network: unknown")
|
2021-08-12 06:57:15 -04:00
|
|
|
assert.Check(t, IsErrNotFound(err))
|
|
|
|
})
|
|
|
|
t.Run("server error", func(t *testing.T) {
|
|
|
|
// Just testing that an internal server error is converted correctly by the client
|
|
|
|
_, err := client.NetworkInspect(context.Background(), "test-500-response", types.NetworkInspectOptions{})
|
|
|
|
assert.Check(t, errdefs.IsSystem(err))
|
|
|
|
})
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|