github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/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"
    13  	"github.com/docker/docker/api/types/swarm"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  func TestServiceInspectError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	_, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing", types.ServiceInspectOptions{})
    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 TestServiceInspectServiceNotFound(t *testing.T) {
    29  	client := &Client{
    30  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    31  	}
    32  
    33  	_, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown", types.ServiceInspectOptions{})
    34  	if err == nil || !IsErrNotFound(err) {
    35  		t.Fatalf("expected a serviceNotFoundError error, got %v", err)
    36  	}
    37  }
    38  
    39  func TestServiceInspect(t *testing.T) {
    40  	expectedURL := "/services/service_id"
    41  	client := &Client{
    42  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    43  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    44  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    45  			}
    46  			content, err := json.Marshal(swarm.Service{
    47  				ID: "service_id",
    48  			})
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  			return &http.Response{
    53  				StatusCode: http.StatusOK,
    54  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
    55  			}, nil
    56  		}),
    57  	}
    58  
    59  	serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id", types.ServiceInspectOptions{})
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if serviceInspect.ID != "service_id" {
    64  		t.Fatalf("expected `service_id`, got %s", serviceInspect.ID)
    65  	}
    66  }