1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Add Service hierarchy to rest api

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2015-06-09 16:38:11 -07:00
parent 2d2a702bc1
commit 0912ecfc05
4 changed files with 728 additions and 20 deletions

View file

@ -91,16 +91,25 @@ func (h *httpHandler) initRouter() {
{"/networks/" + nwID + "/endpoints", []string{"partial-id", epPID}, procGetEndpoints},
{"/networks/" + nwID + "/endpoints", nil, procGetEndpoints},
{"/networks/" + nwID + "/endpoints/" + epID, nil, procGetEndpoint},
{"/services", []string{"network", nwName}, procGetServices},
{"/services", []string{"name", epName}, procGetServices},
{"/services", []string{"partial-id", epPID}, procGetServices},
{"/services", nil, procGetServices},
{"/services/" + epID, nil, procGetService},
},
"POST": {
{"/networks", nil, procCreateNetwork},
{"/networks/" + nwID + "/endpoints", nil, procCreateEndpoint},
{"/networks/" + nwID + "/endpoints/" + epID + "/containers", nil, procJoinEndpoint},
{"/services", nil, procPublishService},
{"/services/" + epID + "/backend", nil, procAttachBackend},
},
"DELETE": {
{"/networks/" + nwID, nil, procDeleteNetwork},
{"/networks/" + nwID + "/endpoints/" + epID, nil, procDeleteEndpoint},
{"/networks/" + nwID + "/endpoints/" + epID + "/containers/" + cnID, nil, procLeaveEndpoint},
{"/services/" + epID, nil, procUnpublishService},
{"/services/" + epID + "/backend/" + cnID, nil, procDetachBackend},
},
}
@ -355,7 +364,7 @@ func procGetEndpoints(c libnetwork.NetworkController, vars map[string]string, bo
list = append(list, buildEndpointResource(ep))
}
} else if queryByPid {
// Return all the prefix-matching networks
// Return all the prefix-matching endpoints
l := func(ep libnetwork.Endpoint) bool {
if strings.HasPrefix(ep.ID(), shortID) {
list = append(list, buildEndpointResource(ep))
@ -448,6 +457,153 @@ func procDeleteEndpoint(c libnetwork.NetworkController, vars map[string]string,
return nil, &successResponse
}
/******************
Service interface
*******************/
func procGetServices(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
// Look for query filters and validate
nwName, filterByNwName := vars[urlNwName]
svName, queryBySvName := vars[urlEpName]
shortID, queryBySvPID := vars[urlEpPID]
if filterByNwName && queryBySvName || filterByNwName && queryBySvPID || queryBySvName && queryBySvPID {
return nil, &badQueryResponse
}
var list []*endpointResource
switch {
case filterByNwName:
// return all service present on the specified network
nw, errRsp := findNetwork(c, nwName, byName)
if !errRsp.isOK() {
return list, &successResponse
}
for _, ep := range nw.Endpoints() {
epr := buildEndpointResource(ep)
list = append(list, epr)
}
case queryBySvName:
// Look in each network for the service with the specified name
l := func(ep libnetwork.Endpoint) bool {
if ep.Name() == svName {
list = append(list, buildEndpointResource(ep))
return true
}
return false
}
for _, nw := range c.Networks() {
nw.WalkEndpoints(l)
}
case queryBySvPID:
// Return all the prefix-matching services
l := func(ep libnetwork.Endpoint) bool {
if strings.HasPrefix(ep.ID(), shortID) {
list = append(list, buildEndpointResource(ep))
}
return false
}
for _, nw := range c.Networks() {
nw.WalkEndpoints(l)
}
default:
for _, nw := range c.Networks() {
for _, ep := range nw.Endpoints() {
epr := buildEndpointResource(ep)
list = append(list, epr)
}
}
}
return list, &successResponse
}
func procGetService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
epT, epBy := detectEndpointTarget(vars)
sv, errRsp := findService(c, epT, epBy)
if !errRsp.isOK() {
return nil, endpointToService(errRsp)
}
return buildEndpointResource(sv), &successResponse
}
func procPublishService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
var sp servicePublish
err := json.Unmarshal(body, &sp)
if err != nil {
return "", &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
}
n, errRsp := findNetwork(c, sp.Network, byName)
if !errRsp.isOK() {
return "", errRsp
}
var setFctList []libnetwork.EndpointOption
if sp.ExposedPorts != nil {
setFctList = append(setFctList, libnetwork.CreateOptionExposedPorts(sp.ExposedPorts))
}
if sp.PortMapping != nil {
setFctList = append(setFctList, libnetwork.CreateOptionPortMapping(sp.PortMapping))
}
ep, err := n.CreateEndpoint(sp.Name, setFctList...)
if err != nil {
return "", endpointToService(convertNetworkError(err))
}
return ep.ID(), &createdResponse
}
func procUnpublishService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
epT, epBy := detectEndpointTarget(vars)
sv, errRsp := findService(c, epT, epBy)
if !errRsp.isOK() {
return nil, errRsp
}
err := sv.Delete()
if err != nil {
return nil, endpointToService(convertNetworkError(err))
}
return nil, &successResponse
}
func procAttachBackend(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
var bk endpointJoin
err := json.Unmarshal(body, &bk)
if err != nil {
return nil, &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
}
epT, epBy := detectEndpointTarget(vars)
sv, errRsp := findService(c, epT, epBy)
if !errRsp.isOK() {
return nil, errRsp
}
err = sv.Join(bk.ContainerID, bk.parseOptions()...)
if err != nil {
return nil, convertNetworkError(err)
}
return sv.Info().SandboxKey(), &successResponse
}
func procDetachBackend(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
epT, epBy := detectEndpointTarget(vars)
sv, errRsp := findService(c, epT, epBy)
if !errRsp.isOK() {
return nil, errRsp
}
err := sv.Leave(vars[urlCnID])
if err != nil {
return nil, convertNetworkError(err)
}
return nil, &successResponse
}
/***********
Utilities
************/
@ -492,7 +648,7 @@ func findNetwork(c libnetwork.NetworkController, s string, by int) (libnetwork.N
panic(fmt.Sprintf("unexpected selector for network search: %d", by))
}
if err != nil {
if _, ok := err.(libnetwork.ErrNoSuchNetwork); ok {
if _, ok := err.(types.NotFoundError); ok {
return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
}
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
@ -518,7 +674,7 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy))
}
if err != nil {
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); ok {
if _, ok := err.(types.NotFoundError); ok {
return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
}
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
@ -526,6 +682,34 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
return ep, &successResponse
}
func findService(c libnetwork.NetworkController, svs string, svBy int) (libnetwork.Endpoint, *responseStatus) {
for _, nw := range c.Networks() {
var (
ep libnetwork.Endpoint
err error
)
switch svBy {
case byID:
ep, err = nw.EndpointByID(svs)
case byName:
ep, err = nw.EndpointByName(svs)
default:
panic(fmt.Sprintf("unexpected selector for service search: %d", svBy))
}
if err == nil {
return ep, &successResponse
} else if _, ok := err.(types.NotFoundError); !ok {
return nil, convertNetworkError(err)
}
}
return nil, &responseStatus{Status: "Service not found", StatusCode: http.StatusNotFound}
}
func endpointToService(rsp *responseStatus) *responseStatus {
rsp.Status = strings.Replace(rsp.Status, "endpoint", "service", -1)
return rsp
}
func convertNetworkError(err error) *responseStatus {
var code int
switch err.(type) {

View file

@ -81,7 +81,14 @@ func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkControll
t.Fatal(err)
}
nw, err := c.NewNetwork(bridgeNetType, network, nil)
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": network,
"AllowNonDefaultBridge": true,
},
}
netGeneric := libnetwork.NetworkOptionGeneric(netOption)
nw, err := c.NewNetwork(bridgeNetType, network, netGeneric)
if err != nil {
t.Fatal(err)
}
@ -507,6 +514,447 @@ func TestGetNetworksAndEndpoints(t *testing.T) {
}
}
func TestProcGetServices(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, err := libnetwork.New("")
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}
// Create 2 networks
netName1 := "production"
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": netName1,
"AllowNonDefaultBridge": true,
},
}
nw1, err := c.NewNetwork(bridgeNetType, netName1, libnetwork.NetworkOptionGeneric(netOption))
if err != nil {
t.Fatal(err)
}
netName2 := "work-dev"
netOption = options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": netName2,
"AllowNonDefaultBridge": true,
},
}
nw2, err := c.NewNetwork(bridgeNetType, netName2, libnetwork.NetworkOptionGeneric(netOption))
if err != nil {
t.Fatal(err)
}
vars := make(map[string]string)
li, errRsp := procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list := i2eL(li)
if len(list) != 0 {
t.Fatalf("Unexpected services in response: %v", list)
}
// Add a couple of services on one network and one on the other network
ep11, err := nw1.CreateEndpoint("db-prod")
if err != nil {
t.Fatal(err)
}
ep12, err := nw1.CreateEndpoint("web-prod")
if err != nil {
t.Fatal(err)
}
ep21, err := nw2.CreateEndpoint("db-dev")
if err != nil {
t.Fatal(err)
}
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 3 {
t.Fatalf("Unexpected services in response: %v", list)
}
// Filter by network
vars[urlNwName] = netName1
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 2 {
t.Fatalf("Unexpected services in response: %v", list)
}
vars[urlNwName] = netName2
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 1 {
t.Fatalf("Unexpected services in response: %v", list)
}
vars[urlNwName] = "unknown-network"
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 0 {
t.Fatalf("Unexpected services in response: %v", list)
}
// Query by name
delete(vars, urlNwName)
vars[urlEpName] = "db-prod"
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 1 {
t.Fatalf("Unexpected services in response: %v", list)
}
vars[urlEpName] = "no-service"
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 0 {
t.Fatalf("Unexpected services in response: %v", list)
}
// Query by id or partial id
delete(vars, urlEpName)
vars[urlEpPID] = ep12.ID()
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 1 {
t.Fatalf("Unexpected services in response: %v", list)
}
if list[0].ID != ep12.ID() {
t.Fatalf("Unexpected element in response: %v", list)
}
vars[urlEpPID] = "non-id"
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 0 {
t.Fatalf("Unexpected services in response: %v", list)
}
delete(vars, urlEpPID)
err = ep11.Delete()
if err != nil {
t.Fatal(err)
}
err = ep12.Delete()
if err != nil {
t.Fatal(err)
}
err = ep21.Delete()
if err != nil {
t.Fatal(err)
}
li, errRsp = procGetServices(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
list = i2eL(li)
if len(list) != 0 {
t.Fatalf("Unexpected services in response: %v", list)
}
}
func TestProcGetService(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, nw := createTestNetwork(t, "network")
ep1, err := nw.CreateEndpoint("db")
if err != nil {
t.Fatal(err)
}
ep2, err := nw.CreateEndpoint("web")
if err != nil {
t.Fatal(err)
}
vars := map[string]string{urlEpID: ""}
_, errRsp := procGetService(c, vars, nil)
if errRsp.isOK() {
t.Fatalf("Expected failure, but suceeded")
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d, but got: %d", http.StatusBadRequest, errRsp.StatusCode)
}
vars[urlEpID] = "unknown-service-id"
_, errRsp = procGetService(c, vars, nil)
if errRsp.isOK() {
t.Fatalf("Expected failure, but suceeded")
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d, but got: %d. (%v)", http.StatusNotFound, errRsp.StatusCode, errRsp)
}
vars[urlEpID] = ep1.ID()
si, errRsp := procGetService(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
sv := i2e(si)
if sv.ID != ep1.ID() {
t.Fatalf("Unexpected service resource returned: %v", sv)
}
vars[urlEpID] = ep2.ID()
si, errRsp = procGetService(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
sv = i2e(si)
if sv.ID != ep2.ID() {
t.Fatalf("Unexpected service resource returned: %v", sv)
}
}
func TestProcPublishUnpublishService(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, _ := createTestNetwork(t, "network")
vars := make(map[string]string)
vbad, err := json.Marshal("bad service create data")
if err != nil {
t.Fatal(err)
}
_, errRsp := procPublishService(c, vars, vbad)
if errRsp == &createdResponse {
t.Fatalf("Expected to fail but succeeded")
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
b, err := json.Marshal(servicePublish{Name: ""})
if err != nil {
t.Fatal(err)
}
_, errRsp = procPublishService(c, vars, b)
if errRsp == &createdResponse {
t.Fatalf("Expected to fail but succeeded")
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
b, err = json.Marshal(servicePublish{Name: "db"})
if err != nil {
t.Fatal(err)
}
_, errRsp = procPublishService(c, vars, b)
if errRsp == &createdResponse {
t.Fatalf("Expected to fail but succeeded")
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
b, err = json.Marshal(servicePublish{Name: "db", Network: "unknown-network"})
if err != nil {
t.Fatal(err)
}
_, errRsp = procPublishService(c, vars, b)
if errRsp == &createdResponse {
t.Fatalf("Expected to fail but succeeded")
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
}
b, err = json.Marshal(servicePublish{Name: "", Network: "network"})
if err != nil {
t.Fatal(err)
}
_, errRsp = procPublishService(c, vars, b)
if errRsp == &createdResponse {
t.Fatalf("Expected to fail but succeeded")
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
b, err = json.Marshal(servicePublish{Name: "db", Network: "network"})
if err != nil {
t.Fatal(err)
}
_, errRsp = procPublishService(c, vars, b)
if errRsp != &createdResponse {
t.Fatalf("Unexpected failure: %v", errRsp)
}
sp := servicePublish{
Name: "web",
Network: "network",
ExposedPorts: []types.TransportPort{
types.TransportPort{Proto: types.TCP, Port: uint16(6000)},
types.TransportPort{Proto: types.UDP, Port: uint16(500)},
types.TransportPort{Proto: types.TCP, Port: uint16(700)},
},
PortMapping: []types.PortBinding{
types.PortBinding{Proto: types.TCP, Port: uint16(1230), HostPort: uint16(37000)},
types.PortBinding{Proto: types.UDP, Port: uint16(1200), HostPort: uint16(36000)},
types.PortBinding{Proto: types.TCP, Port: uint16(1120), HostPort: uint16(35000)},
},
}
b, err = json.Marshal(sp)
if err != nil {
t.Fatal(err)
}
si, errRsp := procPublishService(c, vars, b)
if errRsp != &createdResponse {
t.Fatalf("Unexpected failure: %v", errRsp)
}
sid := i2s(si)
vars[urlEpID] = ""
_, errRsp = procUnpublishService(c, vars, nil)
if errRsp.isOK() {
t.Fatalf("Expected failure but succeeded")
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
vars[urlEpID] = "unknown-service-id"
_, errRsp = procUnpublishService(c, vars, nil)
if errRsp.isOK() {
t.Fatalf("Expected failure but succeeded")
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
}
vars[urlEpID] = sid
_, errRsp = procUnpublishService(c, vars, nil)
if !errRsp.isOK() {
t.Fatalf("Unexpected failure: %v", errRsp)
}
_, errRsp = procGetService(c, vars, nil)
if errRsp.isOK() {
t.Fatalf("Expected failure, but suceeded")
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d, but got: %d. (%v)", http.StatusNotFound, errRsp.StatusCode, errRsp)
}
}
func TestAttachDetachBackend(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, nw := createTestNetwork(t, "network")
ep1, err := nw.CreateEndpoint("db")
if err != nil {
t.Fatal(err)
}
vars := make(map[string]string)
vbad, err := json.Marshal("bad data")
if err != nil {
t.Fatal(err)
}
_, errRsp := procAttachBackend(c, vars, vbad)
if errRsp == &successResponse {
t.Fatalf("Expected failure, got: %v", errRsp)
}
vars[urlEpName] = "endpoint"
bad, err := json.Marshal(endpointJoin{})
if err != nil {
t.Fatal(err)
}
_, errRsp = procAttachBackend(c, vars, bad)
if errRsp == &successResponse {
t.Fatalf("Expected failure, got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
}
vars[urlEpName] = "db"
_, errRsp = procAttachBackend(c, vars, bad)
if errRsp == &successResponse {
t.Fatalf("Expected failure, got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
cid := "abcdefghi"
jl := endpointJoin{ContainerID: cid}
jlb, err := json.Marshal(jl)
if err != nil {
t.Fatal(err)
}
_, errRsp = procAttachBackend(c, vars, jlb)
if errRsp != &successResponse {
t.Fatalf("Unexpected failure, got: %v", errRsp)
}
vars[urlEpName] = "endpoint"
_, errRsp = procDetachBackend(c, vars, nil)
if errRsp == &successResponse {
t.Fatalf("Expected failure, got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
}
vars[urlEpName] = "db"
_, errRsp = procDetachBackend(c, vars, nil)
if errRsp == &successResponse {
t.Fatalf("Expected failure, got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
vars[urlCnID] = cid
_, errRsp = procDetachBackend(c, vars, nil)
if errRsp != &successResponse {
t.Fatalf("Unexpected failure, got: %v", errRsp)
}
err = ep1.Delete()
if err != nil {
t.Fatal(err)
}
}
func TestDetectGetNetworksInvalidQueryComposition(t *testing.T) {
c, err := libnetwork.New("")
if err != nil {
@ -532,15 +980,29 @@ func TestDetectGetEndpointsInvalidQueryComposition(t *testing.T) {
}
}
func TestDetectGetServicesInvalidQueryComposition(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, _ := createTestNetwork(t, "network")
vars := map[string]string{urlNwName: "network", urlEpName: "x", urlEpPID: "y"}
_, errRsp := procGetServices(c, vars, nil)
if errRsp.StatusCode != http.StatusBadRequest {
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
}
}
func TestFindNetworkUtilPanic(t *testing.T) {
defer checkPanic(t)
findNetwork(nil, "", -1)
}
func TestFindNetworkUtil(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, nw := createTestNetwork(t, "network")
nid := nw.ID()
defer checkPanic(t)
findNetwork(c, "", -1)
_, errRsp := findNetwork(c, "", byName)
if errRsp == &successResponse {
t.Fatalf("Expected to fail but succeeded")
@ -577,7 +1039,9 @@ func TestFindNetworkUtil(t *testing.T) {
t.Fatalf("Incorrect libnetwork.Network resource. It has different name: %v", n)
}
n.Delete()
if err := n.Delete(); err != nil {
t.Fatalf("Failed to delete the network: %s", err.Error())
}
_, errRsp = findNetwork(c, nid, byID)
if errRsp == &successResponse {
@ -878,6 +1342,21 @@ func TestJoinLeave(t *testing.T) {
}
}
func TestFindEndpointUtilPanic(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
defer checkPanic(t)
c, nw := createTestNetwork(t, "network")
nid := nw.ID()
findEndpoint(c, nid, "", byID, -1)
}
func TestFindServiceUtilPanic(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
defer checkPanic(t)
c, _ := createTestNetwork(t, "network")
findService(c, "random_service", -1)
}
func TestFindEndpointUtil(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
@ -890,9 +1369,6 @@ func TestFindEndpointUtil(t *testing.T) {
}
eid := ep.ID()
defer checkPanic(t)
findEndpoint(c, nid, "", byID, -1)
_, errRsp := findEndpoint(c, nid, "", byID, byName)
if errRsp == &successResponse {
t.Fatalf("Expected failure, but got: %v", errRsp)
@ -906,7 +1382,7 @@ func TestFindEndpointUtil(t *testing.T) {
t.Fatalf("Unexepected failure: %v", errRsp)
}
ep1, errRsp := findEndpoint(c, "second", "secondEp", byName, byName)
ep1, errRsp := findEndpoint(c, "network", "secondEp", byName, byName)
if errRsp != &successResponse {
t.Fatalf("Unexepected failure: %v", errRsp)
}
@ -916,12 +1392,22 @@ func TestFindEndpointUtil(t *testing.T) {
t.Fatalf("Unexepected failure: %v", errRsp)
}
ep3, errRsp := findEndpoint(c, "second", eid, byName, byID)
ep3, errRsp := findEndpoint(c, "network", eid, byName, byID)
if errRsp != &successResponse {
t.Fatalf("Unexepected failure: %v", errRsp)
}
if ep0 != ep1 || ep0 != ep2 || ep0 != ep3 {
ep4, errRsp := findService(c, "secondEp", byName)
if errRsp != &successResponse {
t.Fatalf("Unexepected failure: %v", errRsp)
}
ep5, errRsp := findService(c, eid, byID)
if errRsp != &successResponse {
t.Fatalf("Unexepected failure: %v", errRsp)
}
if ep0 != ep1 || ep0 != ep2 || ep0 != ep3 || ep0 != ep4 || ep0 != ep5 {
t.Fatalf("Diffenrent queries returned different endpoints")
}
@ -935,7 +1421,7 @@ func TestFindEndpointUtil(t *testing.T) {
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
}
_, errRsp = findEndpoint(c, "second", "secondEp", byName, byName)
_, errRsp = findEndpoint(c, "network", "secondEp", byName, byName)
if errRsp == &successResponse {
t.Fatalf("Expected failure, but got: %v", errRsp)
}
@ -951,13 +1437,43 @@ func TestFindEndpointUtil(t *testing.T) {
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
}
_, errRsp = findEndpoint(c, "second", eid, byName, byID)
_, errRsp = findEndpoint(c, "network", eid, byName, byID)
if errRsp == &successResponse {
t.Fatalf("Expected failure, but got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
}
_, errRsp = findService(c, "secondEp", byName)
if errRsp == &successResponse {
t.Fatalf("Expected failure, but got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
}
_, errRsp = findService(c, eid, byID)
if errRsp == &successResponse {
t.Fatalf("Expected failure, but got: %v", errRsp)
}
if errRsp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
}
}
func TestEndpointToService(t *testing.T) {
r := &responseStatus{Status: "this is one endpoint", StatusCode: http.StatusOK}
r = endpointToService(r)
if r.Status != "this is one service" {
t.Fatalf("endpointToService returned unexpected status string: %s", r.Status)
}
r = &responseStatus{Status: "this is one network", StatusCode: http.StatusOK}
r = endpointToService(r)
if r.Status != "this is one network" {
t.Fatalf("endpointToService returned unexpected status string: %s", r.Status)
}
}
func checkPanic(t *testing.T) {

View file

@ -52,6 +52,14 @@ type endpointJoin struct {
UseDefaultSandbox bool `json:"use_default_sandbox"`
}
// servicePublish represents the body of the "publish service" http request message
type servicePublish struct {
Name string `json:"name"`
Network string `json:"network"`
ExposedPorts []types.TransportPort `json:"exposed_ports"`
PortMapping []types.PortBinding `json:"port_mapping"`
}
// EndpointExtraHost represents the extra host object
type endpointExtraHost struct {
Name string `json:"name"`

View file

@ -11,8 +11,8 @@ func (nsn ErrNoSuchNetwork) Error() string {
return fmt.Sprintf("network %s not found", string(nsn))
}
// BadRequest denotes the type of this error
func (nsn ErrNoSuchNetwork) BadRequest() {}
// NotFound denotes the type of this error
func (nsn ErrNoSuchNetwork) NotFound() {}
// ErrNoSuchEndpoint is returned when a endpoint query finds no result
type ErrNoSuchEndpoint string
@ -21,8 +21,8 @@ func (nse ErrNoSuchEndpoint) Error() string {
return fmt.Sprintf("endpoint %s not found", string(nse))
}
// BadRequest denotes the type of this error
func (nse ErrNoSuchEndpoint) BadRequest() {}
// NotFound denotes the type of this error
func (nse ErrNoSuchEndpoint) NotFound() {}
// ErrInvalidNetworkDriver is returned if an invalid driver
// name is passed.