Browse Source

Handled endpoint delete with active containers attached to it

Signed-off-by: Madhu Venugopal <madhu@docker.com>
Madhu Venugopal 10 năm trước cách đây
mục cha
commit
570a76384a
3 tập tin đã thay đổi với 45 bổ sung0 xóa
  1. 3 0
      libnetwork/endpoint.go
  2. 11 0
      libnetwork/error.go
  3. 31 0
      libnetwork/libnetwork_test.go

+ 3 - 0
libnetwork/endpoint.go

@@ -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()

+ 11 - 0
libnetwork/error.go

@@ -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

+ 31 - 0
libnetwork/libnetwork_test.go

@@ -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)()