Handled endpoint delete with active containers attached to it

Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
Madhu Venugopal 2015-05-06 13:02:40 -07:00
parent db8100743b
commit 570a76384a
3 changed files with 45 additions and 0 deletions

View File

@ -296,6 +296,9 @@ func (ep *endpoint) Leave(containerID string, options ...EndpointOption) error {
func (ep *endpoint) Delete() error {
var err error
if ep.container != nil {
return &ActiveContainerError{name: ep.name, id: string(ep.id)}
}
n := ep.network
n.Lock()

View File

@ -65,6 +65,17 @@ func (uee *UnknownEndpointError) Error() string {
return fmt.Sprintf("unknown endpoint %s id %s", uee.name, uee.id)
}
// ActiveContainerError is returned when an endpoint is deleted which has active
// containers attached to it.
type ActiveContainerError struct {
name string
id string
}
func (ace *ActiveContainerError) Error() string {
return fmt.Sprintf("endpoint with name %s id %s has active containers", ace.name, ace.id)
}
// InvalidContainerIDError is returned when an invalid container id is passed
// in Join/Leave
type InvalidContainerIDError string

View File

@ -635,6 +635,37 @@ func TestEndpointJoinInvalidContainerId(t *testing.T) {
}
}
func TestEndpointDeleteWithActiveContainer(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
ep, err := n.CreateEndpoint("ep1")
if err != nil {
t.Fatal(err)
}
_, err = ep.Join(containerID,
libnetwork.JoinOptionHostname("test"),
libnetwork.JoinOptionDomainname("docker.io"),
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
if err != nil {
t.Fatal(err)
}
err = ep.Delete()
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
if _, ok := err.(*libnetwork.ActiveContainerError); !ok {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
func TestEndpointMultipleJoins(t *testing.T) {
defer netutils.SetupTestNetNS(t)()