github.com/hms58/moby@v1.13.1/client/service_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/swarm"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  func TestServiceInspectError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  
    21  	_, _, err := client.ServiceInspectWithRaw(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 TestServiceInspectServiceNotFound(t *testing.T) {
    28  	client := &Client{
    29  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    30  	}
    31  
    32  	_, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown")
    33  	if err == nil || !IsErrServiceNotFound(err) {
    34  		t.Fatalf("expected an serviceNotFoundError error, got %v", err)
    35  	}
    36  }
    37  
    38  func TestServiceInspect(t *testing.T) {
    39  	expectedURL := "/services/service_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  			content, err := json.Marshal(swarm.Service{
    46  				ID: "service_id",
    47  			})
    48  			if err != nil {
    49  				return nil, err
    50  			}
    51  			return &http.Response{
    52  				StatusCode: http.StatusOK,
    53  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    54  			}, nil
    55  		}),
    56  	}
    57  
    58  	serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id")
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if serviceInspect.ID != "service_id" {
    63  		t.Fatalf("expected `service_id`, got %s", serviceInspect.ID)
    64  	}
    65  }