service_inspect_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/swarm"
  13. "github.com/docker/docker/errdefs"
  14. "github.com/pkg/errors"
  15. )
  16. func TestServiceInspectError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing", types.ServiceInspectOptions{})
  21. if !errdefs.IsSystem(err) {
  22. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  23. }
  24. }
  25. func TestServiceInspectServiceNotFound(t *testing.T) {
  26. client := &Client{
  27. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  28. }
  29. _, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown", types.ServiceInspectOptions{})
  30. if err == nil || !IsErrNotFound(err) {
  31. t.Fatalf("expected a serviceNotFoundError error, got %v", err)
  32. }
  33. }
  34. func TestServiceInspectWithEmptyID(t *testing.T) {
  35. client := &Client{
  36. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  37. return nil, errors.New("should not make request")
  38. }),
  39. }
  40. _, _, err := client.ServiceInspectWithRaw(context.Background(), "", types.ServiceInspectOptions{})
  41. if !IsErrNotFound(err) {
  42. t.Fatalf("Expected NotFoundError, got %v", err)
  43. }
  44. }
  45. func TestServiceInspect(t *testing.T) {
  46. expectedURL := "/services/service_id"
  47. client := &Client{
  48. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  49. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  50. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  51. }
  52. content, err := json.Marshal(swarm.Service{
  53. ID: "service_id",
  54. })
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &http.Response{
  59. StatusCode: http.StatusOK,
  60. Body: io.NopCloser(bytes.NewReader(content)),
  61. }, nil
  62. }),
  63. }
  64. serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id", types.ServiceInspectOptions{})
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if serviceInspect.ID != "service_id" {
  69. t.Fatalf("expected `service_id`, got %s", serviceInspect.ID)
  70. }
  71. }