2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
2017-06-11 14:04:35 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-06 14:46:37 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkInspectError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-11 14:04:35 -04:00
|
|
|
_, err := client.NetworkInspect(context.Background(), "nothing", types.NetworkInspectOptions{})
|
2016-09-06 14:46:37 -04:00
|
|
|
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
|
|
|
t.Fatalf("expected a Server Error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkInspectContainerNotFound(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-11 14:04:35 -04:00
|
|
|
_, err := client.NetworkInspect(context.Background(), "unknown", types.NetworkInspectOptions{})
|
2016-09-06 14:46:37 -04:00
|
|
|
if err == nil || !IsErrNetworkNotFound(err) {
|
2016-11-17 02:50:38 -05:00
|
|
|
t.Fatalf("expected a networkNotFound error, got %v", err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkInspect(t *testing.T) {
|
|
|
|
expectedURL := "/networks/network_id"
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
|
|
|
|
}
|
|
|
|
|
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, "scope=global") {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-06-11 14:04:35 -04:00
|
|
|
r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{})
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Name != "mynetwork" {
|
|
|
|
t.Fatalf("expected `mynetwork`, got %s", r.Name)
|
|
|
|
}
|
2017-03-09 14:42:10 -05:00
|
|
|
|
2017-06-11 14:04:35 -04:00
|
|
|
r, err = client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Verbose: true})
|
2017-03-09 14:42:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Name != "mynetwork" {
|
|
|
|
t.Fatalf("expected `mynetwork`, got %s", r.Name)
|
|
|
|
}
|
|
|
|
_, ok := r.Services["web"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected service `web` missing in the verbose output")
|
|
|
|
}
|
2017-06-11 14:04:35 -04:00
|
|
|
|
|
|
|
_, err = client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Scope: "global"})
|
|
|
|
assert.EqualError(t, err, "Error: No such network: network_id")
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|