2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
2021-08-12 10:57:15 +00:00
|
|
|
"errors"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-03-09 19:42:10 +00:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2019-10-12 22:31:53 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkInspect(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodGet {
|
2021-08-12 10:57:15 +00: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 15:55:42 +00:00
|
|
|
return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2017-03-09 19:42:10 +00:00
|
|
|
var (
|
|
|
|
content []byte
|
|
|
|
err error
|
|
|
|
)
|
2017-06-11 18:04:35 +00:00
|
|
|
if strings.Contains(req.URL.RawQuery, "verbose=true") {
|
2017-03-09 19:42:10 +00: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 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
2021-08-12 10:57:15 +00:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
2016-09-06 18:46:37 +00:00
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(content)),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2021-08-12 10:57:15 +00: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 15:55:42 +00:00
|
|
|
assert.ErrorContains(t, err, "Error: No such network: network_id")
|
2021-08-12 10:57:15 +00: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 15:55:42 +00:00
|
|
|
assert.ErrorContains(t, err, "Error: No such network: unknown")
|
2021-08-12 10:57:15 +00: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 18:46:37 +00:00
|
|
|
}
|