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