github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/service_inspect_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/swarm"
    15  	"github.com/docker/docker/errdefs"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  func TestServiceInspectError(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    22  	}
    23  
    24  	_, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing", types.ServiceInspectOptions{})
    25  	if !errdefs.IsSystem(err) {
    26  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    27  	}
    28  }
    29  
    30  func TestServiceInspectServiceNotFound(t *testing.T) {
    31  	client := &Client{
    32  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    33  	}
    34  
    35  	_, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown", types.ServiceInspectOptions{})
    36  	if err == nil || !IsErrNotFound(err) {
    37  		t.Fatalf("expected a serviceNotFoundError error, got %v", err)
    38  	}
    39  }
    40  
    41  func TestServiceInspectWithEmptyID(t *testing.T) {
    42  	client := &Client{
    43  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    44  			return nil, errors.New("should not make request")
    45  		}),
    46  	}
    47  	_, _, err := client.ServiceInspectWithRaw(context.Background(), "", types.ServiceInspectOptions{})
    48  	if !IsErrNotFound(err) {
    49  		t.Fatalf("Expected NotFoundError, got %v", err)
    50  	}
    51  }
    52  
    53  func TestServiceInspect(t *testing.T) {
    54  	expectedURL := "/services/service_id"
    55  	client := &Client{
    56  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    57  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    58  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    59  			}
    60  			content, err := json.Marshal(swarm.Service{
    61  				ID: "service_id",
    62  			})
    63  			if err != nil {
    64  				return nil, err
    65  			}
    66  			return &http.Response{
    67  				StatusCode: http.StatusOK,
    68  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    69  			}, nil
    70  		}),
    71  	}
    72  
    73  	serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id", types.ServiceInspectOptions{})
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	if serviceInspect.ID != "service_id" {
    78  		t.Fatalf("expected `service_id`, got %s", serviceInspect.ID)
    79  	}
    80  }