gopkg.in/docker/docker.v20@v20.10.27/client/network_connect_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/api/types/network" 15 "github.com/docker/docker/errdefs" 16 ) 17 18 func TestNetworkConnectError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 23 err := client.NetworkConnect(context.Background(), "network_id", "container_id", nil) 24 if !errdefs.IsSystem(err) { 25 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 26 } 27 } 28 29 func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) { 30 expectedURL := "/networks/network_id/connect" 31 32 client := &Client{ 33 client: newMockClient(func(req *http.Request) (*http.Response, error) { 34 if !strings.HasPrefix(req.URL.Path, expectedURL) { 35 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 36 } 37 38 if req.Method != http.MethodPost { 39 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 40 } 41 42 var connect types.NetworkConnect 43 if err := json.NewDecoder(req.Body).Decode(&connect); err != nil { 44 return nil, err 45 } 46 47 if connect.Container != "container_id" { 48 return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container) 49 } 50 51 if connect.EndpointConfig != nil { 52 return nil, fmt.Errorf("expected connect.EndpointConfig to be nil, got %v", connect.EndpointConfig) 53 } 54 55 return &http.Response{ 56 StatusCode: http.StatusOK, 57 Body: io.NopCloser(bytes.NewReader([]byte(""))), 58 }, nil 59 }), 60 } 61 62 err := client.NetworkConnect(context.Background(), "network_id", "container_id", nil) 63 if err != nil { 64 t.Fatal(err) 65 } 66 } 67 68 func TestNetworkConnect(t *testing.T) { 69 expectedURL := "/networks/network_id/connect" 70 71 client := &Client{ 72 client: newMockClient(func(req *http.Request) (*http.Response, error) { 73 if !strings.HasPrefix(req.URL.Path, expectedURL) { 74 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 75 } 76 77 if req.Method != http.MethodPost { 78 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 79 } 80 81 var connect types.NetworkConnect 82 if err := json.NewDecoder(req.Body).Decode(&connect); err != nil { 83 return nil, err 84 } 85 86 if connect.Container != "container_id" { 87 return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container) 88 } 89 90 if connect.EndpointConfig == nil { 91 return nil, fmt.Errorf("expected connect.EndpointConfig to be not nil, got %v", connect.EndpointConfig) 92 } 93 94 if connect.EndpointConfig.NetworkID != "NetworkID" { 95 return nil, fmt.Errorf("expected 'NetworkID', got %s", connect.EndpointConfig.NetworkID) 96 } 97 98 return &http.Response{ 99 StatusCode: http.StatusOK, 100 Body: io.NopCloser(bytes.NewReader([]byte(""))), 101 }, nil 102 }), 103 } 104 105 err := client.NetworkConnect(context.Background(), "network_id", "container_id", &network.EndpointSettings{ 106 NetworkID: "NetworkID", 107 }) 108 if err != nil { 109 t.Fatal(err) 110 } 111 }