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

NewNetwork and CreateEndpoint to validate resource name

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2015-05-07 19:59:06 -07:00
parent 5284149bc8
commit 1739626f4d
4 changed files with 29 additions and 1 deletions

View file

@ -128,6 +128,9 @@ func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver)
// NewNetwork creates a new network of the specified network type. The options // NewNetwork creates a new network of the specified network type. The options
// are network specific and modeled in a generic way. // are network specific and modeled in a generic way.
func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) { func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
if name == "" {
return nil, ErrInvalidNetworkName
}
// Check if a driver for the specified network type is available // Check if a driver for the specified network type is available
c.Lock() c.Lock()
d, ok := c.drivers[networkType] d, ok := c.drivers[networkType]

View file

@ -18,6 +18,12 @@ var (
// ErrNoContainer is returned when the endpoint has no container // ErrNoContainer is returned when the endpoint has no container
// attached to it. // attached to it.
ErrNoContainer = errors.New("no container attached to the endpoint") ErrNoContainer = errors.New("no container attached to the endpoint")
// ErrInvalidEndpointName is returned if an invalid endpoint name
// is passed when creating an endpoint
ErrInvalidEndpointName = errors.New("invalid endpoint name")
// ErrInvalidNetworkName is returned if an invalid network name
// is passed when creating a network
ErrInvalidNetworkName = errors.New("invalid network name")
) )
// NetworkTypeError type is returned when the network type string is not // NetworkTypeError type is returned when the network type string is not

View file

@ -276,8 +276,16 @@ func TestDuplicateNetwork(t *testing.T) {
func TestNetworkName(t *testing.T) { func TestNetworkName(t *testing.T) {
defer netutils.SetupTestNetNS(t)() defer netutils.SetupTestNetNS(t)()
networkName := "testnetwork"
_, err := createTestNetwork(bridgeNetType, "", options.Generic{}, options.Generic{})
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if err != libnetwork.ErrInvalidNetworkName {
t.Fatal("Expected to fail with ErrInvalidNetworkName error")
}
networkName := "testnetwork"
n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{}, options.Generic{}) n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{}, options.Generic{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -392,6 +400,14 @@ func TestUnknownEndpoint(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
_, err = network.CreateEndpoint("")
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if err != libnetwork.ErrInvalidEndpointName {
t.Fatal("Expected to fail with ErrInvalidEndpointName error")
}
ep, err := network.CreateEndpoint("testep") ep, err := network.CreateEndpoint("testep")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -132,6 +132,9 @@ func (n *network) Delete() error {
} }
func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) { func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
if name == "" {
return nil, ErrInvalidEndpointName
}
ep := &endpoint{name: name, generic: make(map[string]interface{})} ep := &endpoint{name: name, generic: make(map[string]interface{})}
ep.id = types.UUID(stringid.GenerateRandomID()) ep.id = types.UUID(stringid.GenerateRandomID())
ep.network = n ep.network = n