service_inspect_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/swarm"
  12. "golang.org/x/net/context"
  13. )
  14. func TestServiceInspectError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. _, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing", types.ServiceInspectOptions{})
  19. if err == nil || err.Error() != "Error response from daemon: Server error" {
  20. t.Fatalf("expected a Server Error, got %v", err)
  21. }
  22. }
  23. func TestServiceInspectServiceNotFound(t *testing.T) {
  24. client := &Client{
  25. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  26. }
  27. _, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown", types.ServiceInspectOptions{})
  28. if err == nil || !IsErrServiceNotFound(err) {
  29. t.Fatalf("expected a serviceNotFoundError error, got %v", err)
  30. }
  31. }
  32. func TestServiceInspect(t *testing.T) {
  33. expectedURL := "/services/service_id"
  34. client := &Client{
  35. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  36. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  37. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  38. }
  39. content, err := json.Marshal(swarm.Service{
  40. ID: "service_id",
  41. })
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &http.Response{
  46. StatusCode: http.StatusOK,
  47. Body: ioutil.NopCloser(bytes.NewReader(content)),
  48. }, nil
  49. }),
  50. }
  51. serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id", types.ServiceInspectOptions{})
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if serviceInspect.ID != "service_id" {
  56. t.Fatalf("expected `service_id`, got %s", serviceInspect.ID)
  57. }
  58. }