github.com/vvnotw/moby@v1.13.1/client/network_inspect_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestNetworkInspectError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, err := client.NetworkInspect(context.Background(), "nothing")
    22  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    23  		t.Fatalf("expected a Server Error, got %v", err)
    24  	}
    25  }
    26  
    27  func TestNetworkInspectContainerNotFound(t *testing.T) {
    28  	client := &Client{
    29  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    30  	}
    31  
    32  	_, err := client.NetworkInspect(context.Background(), "unknown")
    33  	if err == nil || !IsErrNetworkNotFound(err) {
    34  		t.Fatalf("expected a containerNotFound error, got %v", err)
    35  	}
    36  }
    37  
    38  func TestNetworkInspect(t *testing.T) {
    39  	expectedURL := "/networks/network_id"
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    43  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    44  			}
    45  			if req.Method != "GET" {
    46  				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
    47  			}
    48  
    49  			content, err := json.Marshal(types.NetworkResource{
    50  				Name: "mynetwork",
    51  			})
    52  			if err != nil {
    53  				return nil, err
    54  			}
    55  			return &http.Response{
    56  				StatusCode: http.StatusOK,
    57  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    58  			}, nil
    59  		}),
    60  	}
    61  
    62  	r, err := client.NetworkInspect(context.Background(), "network_id")
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	if r.Name != "mynetwork" {
    67  		t.Fatalf("expected `mynetwork`, got %s", r.Name)
    68  	}
    69  }