2015-04-26 03:19:01 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-21 06:58:30 -04:00
|
|
|
"encoding/json"
|
2015-05-22 10:54:23 -04:00
|
|
|
"fmt"
|
2015-04-26 03:19:01 -04:00
|
|
|
"io"
|
2015-06-15 11:42:14 -04:00
|
|
|
"net/http"
|
2015-05-21 06:58:30 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
2015-04-26 03:19:01 -04:00
|
|
|
"testing"
|
2015-05-12 19:39:30 -04:00
|
|
|
|
2015-09-07 13:33:28 -04:00
|
|
|
_ "github.com/docker/libnetwork/testutils"
|
2015-04-26 03:19:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// nopCloser is used to provide a dummy CallFunc for Cmd()
|
|
|
|
type nopCloser struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopCloser) Close() error { return nil }
|
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
setupMockHTTPCallback()
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2015-06-15 11:42:14 -04:00
|
|
|
var callbackFunc func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, http.Header, int, error)
|
2015-07-02 01:00:48 -04:00
|
|
|
var mockNwJSON, mockNwListJSON, mockServiceJSON, mockServiceListJSON, mockSbJSON, mockSbListJSON []byte
|
2015-05-21 06:58:30 -04:00
|
|
|
var mockNwName = "test"
|
2015-05-23 18:03:52 -04:00
|
|
|
var mockNwID = "2a3456789"
|
2015-05-22 10:54:23 -04:00
|
|
|
var mockServiceName = "testSrv"
|
2015-05-23 18:03:52 -04:00
|
|
|
var mockServiceID = "2a3456789"
|
|
|
|
var mockContainerID = "2a3456789"
|
2015-07-02 01:00:48 -04:00
|
|
|
var mockSandboxID = "2b3456789"
|
2015-05-21 06:58:30 -04:00
|
|
|
|
|
|
|
func setupMockHTTPCallback() {
|
|
|
|
var list []networkResource
|
|
|
|
nw := networkResource{Name: mockNwName, ID: mockNwID}
|
|
|
|
mockNwJSON, _ = json.Marshal(nw)
|
|
|
|
list = append(list, nw)
|
|
|
|
mockNwListJSON, _ = json.Marshal(list)
|
2015-05-22 10:54:23 -04:00
|
|
|
|
2015-06-11 11:30:45 -04:00
|
|
|
var srvList []serviceResource
|
|
|
|
ep := serviceResource{Name: mockServiceName, ID: mockServiceID, Network: mockNwName}
|
2015-05-22 10:54:23 -04:00
|
|
|
mockServiceJSON, _ = json.Marshal(ep)
|
|
|
|
srvList = append(srvList, ep)
|
|
|
|
mockServiceListJSON, _ = json.Marshal(srvList)
|
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
var sbxList []SandboxResource
|
|
|
|
sb := SandboxResource{ID: mockSandboxID, ContainerID: mockContainerID}
|
2015-07-02 01:00:48 -04:00
|
|
|
mockSbJSON, _ = json.Marshal(sb)
|
|
|
|
sbxList = append(sbxList, sb)
|
|
|
|
mockSbListJSON, _ = json.Marshal(sbxList)
|
|
|
|
|
2015-06-15 11:42:14 -04:00
|
|
|
dummyHTTPHdr := http.Header{}
|
|
|
|
|
|
|
|
callbackFunc = func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, http.Header, int, error) {
|
2015-05-21 06:58:30 -04:00
|
|
|
var rsp string
|
|
|
|
switch method {
|
|
|
|
case "GET":
|
2015-05-22 10:54:23 -04:00
|
|
|
if strings.Contains(path, fmt.Sprintf("networks?name=%s", mockNwName)) {
|
|
|
|
rsp = string(mockNwListJSON)
|
|
|
|
} else if strings.Contains(path, "networks?name=") {
|
|
|
|
rsp = "[]"
|
|
|
|
} else if strings.Contains(path, fmt.Sprintf("networks?partial-id=%s", mockNwID)) {
|
2015-05-21 06:58:30 -04:00
|
|
|
rsp = string(mockNwListJSON)
|
2015-05-22 10:54:23 -04:00
|
|
|
} else if strings.Contains(path, "networks?partial-id=") {
|
|
|
|
rsp = "[]"
|
2015-05-21 06:58:30 -04:00
|
|
|
} else if strings.HasSuffix(path, "networks") {
|
|
|
|
rsp = string(mockNwListJSON)
|
|
|
|
} else if strings.HasSuffix(path, "networks/"+mockNwID) {
|
|
|
|
rsp = string(mockNwJSON)
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.Contains(path, fmt.Sprintf("services?name=%s", mockServiceName)) {
|
2015-05-22 10:54:23 -04:00
|
|
|
rsp = string(mockServiceListJSON)
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.Contains(path, "services?name=") {
|
2015-05-22 10:54:23 -04:00
|
|
|
rsp = "[]"
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.Contains(path, fmt.Sprintf("services?partial-id=%s", mockServiceID)) {
|
2015-05-22 10:54:23 -04:00
|
|
|
rsp = string(mockServiceListJSON)
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.Contains(path, "services?partial-id=") {
|
2015-05-22 10:54:23 -04:00
|
|
|
rsp = "[]"
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.HasSuffix(path, "services") {
|
2015-05-22 10:54:23 -04:00
|
|
|
rsp = string(mockServiceListJSON)
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.HasSuffix(path, "services/"+mockServiceID) {
|
2015-05-22 10:54:23 -04:00
|
|
|
rsp = string(mockServiceJSON)
|
2015-06-15 05:27:24 -04:00
|
|
|
} else if strings.Contains(path, "containers") {
|
2015-06-15 11:42:14 -04:00
|
|
|
return nopCloser{bytes.NewBufferString("")}, dummyHTTPHdr, 400, fmt.Errorf("Bad Request")
|
2015-07-02 01:00:48 -04:00
|
|
|
} else if strings.Contains(path, fmt.Sprintf("sandboxes?container-id=%s", mockContainerID)) {
|
|
|
|
rsp = string(mockSbListJSON)
|
2015-09-09 20:35:07 -04:00
|
|
|
} else if strings.Contains(path, fmt.Sprintf("sandboxes?partial-container-id=%s", mockContainerID)) {
|
|
|
|
rsp = string(mockSbListJSON)
|
2015-05-21 06:58:30 -04:00
|
|
|
}
|
|
|
|
case "POST":
|
2015-05-23 18:03:52 -04:00
|
|
|
var data []byte
|
2015-05-22 10:54:23 -04:00
|
|
|
if strings.HasSuffix(path, "networks") {
|
2015-05-23 18:03:52 -04:00
|
|
|
data, _ = json.Marshal(mockNwID)
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.HasSuffix(path, "services") {
|
2015-05-23 18:03:52 -04:00
|
|
|
data, _ = json.Marshal(mockServiceID)
|
2015-06-11 11:30:45 -04:00
|
|
|
} else if strings.HasSuffix(path, "backend") {
|
2015-07-02 01:00:48 -04:00
|
|
|
data, _ = json.Marshal(mockSandboxID)
|
2015-05-22 10:54:23 -04:00
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
rsp = string(data)
|
2015-05-21 06:58:30 -04:00
|
|
|
case "PUT":
|
|
|
|
case "DELETE":
|
|
|
|
rsp = ""
|
|
|
|
}
|
2015-06-15 11:42:14 -04:00
|
|
|
return nopCloser{bytes.NewBufferString(rsp)}, dummyHTTPHdr, 200, nil
|
2015-05-21 06:58:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-26 03:19:01 -04:00
|
|
|
func TestClientDummyCommand(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
|
|
|
err := cli.Cmd("docker", "dummy")
|
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Incorrect Command must fail")
|
2015-04-26 03:19:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 10:07:09 -04:00
|
|
|
func TestClientNetworkInvalidCommand(t *testing.T) {
|
2015-04-26 03:19:01 -04:00
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
2015-05-17 10:07:09 -04:00
|
|
|
err := cli.Cmd("docker", "network", "invalid")
|
2015-04-26 03:19:01 -04:00
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Passing invalid commands must fail")
|
2015-04-26 03:19:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNetworkCreate(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
err := cli.Cmd("docker", "network", "create", mockNwName)
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNetworkCreateWithDriver(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
err := cli.Cmd("docker", "network", "create", "-f=dummy", mockNwName)
|
2015-04-26 03:19:01 -04:00
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Passing incorrect flags to the create command must fail")
|
2015-04-26 03:19:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
err = cli.Cmd("docker", "network", "create", "-d=dummy", mockNwName)
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNetworkRm(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
err := cli.Cmd("docker", "network", "rm", mockNwName)
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNetworkLs(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
|
|
|
err := cli.Cmd("docker", "network", "ls")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNetworkInfo(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
|
|
|
|
|
|
|
err := cli.Cmd("docker", "network", "info", mockNwName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientNetworkInfoById(t *testing.T) {
|
|
|
|
var out, errOut bytes.Buffer
|
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
err := cli.Cmd("docker", "network", "info", mockNwID)
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 10:07:09 -04:00
|
|
|
// Docker Flag processing in flag.go uses os.Exit() frequently, even for --help
|
2015-04-26 03:19:01 -04:00
|
|
|
// TODO : Handle the --help test-case in the IT when CLI is available
|
|
|
|
/*
|
2015-05-22 10:54:23 -04:00
|
|
|
func TestClientNetworkServiceCreateHelp(t *testing.T) {
|
2015-04-26 03:19:01 -04:00
|
|
|
var out, errOut bytes.Buffer
|
|
|
|
cFunc := func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, int, error) {
|
|
|
|
return nil, 0, nil
|
|
|
|
}
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
|
|
|
err := cli.Cmd("docker", "network", "create", "--help")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-05-04 05:54:19 -04:00
|
|
|
// Docker flag processing in flag.go uses os.Exit(1) for incorrect parameter case.
|
2015-04-26 03:19:01 -04:00
|
|
|
// TODO : Handle the missing argument case in the IT when CLI is available
|
|
|
|
/*
|
2015-05-22 10:54:23 -04:00
|
|
|
func TestClientNetworkServiceCreateMissingArgument(t *testing.T) {
|
2015-04-26 03:19:01 -04:00
|
|
|
var out, errOut bytes.Buffer
|
|
|
|
cFunc := func(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, int, error) {
|
|
|
|
return nil, 0, nil
|
|
|
|
}
|
2015-05-21 06:58:30 -04:00
|
|
|
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
2015-04-26 03:19:01 -04:00
|
|
|
|
|
|
|
err := cli.Cmd("docker", "network", "create")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|