config_inspect_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package client // import "github.com/docker/docker/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/swarm"
  11. "github.com/pkg/errors"
  12. "github.com/stretchr/testify/assert"
  13. "golang.org/x/net/context"
  14. )
  15. func TestConfigInspectNotFound(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  18. }
  19. _, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
  20. if err == nil || !IsErrNotFound(err) {
  21. t.Fatalf("expected a NotFoundError error, got %v", err)
  22. }
  23. }
  24. func TestConfigInspectWithEmptyID(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. return nil, errors.New("should not make request")
  28. }),
  29. }
  30. _, _, err := client.ConfigInspectWithRaw(context.Background(), "")
  31. if !IsErrNotFound(err) {
  32. t.Fatalf("Expected NotFoundError, got %v", err)
  33. }
  34. }
  35. func TestConfigInspectUnsupported(t *testing.T) {
  36. client := &Client{
  37. version: "1.29",
  38. client: &http.Client{},
  39. }
  40. _, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
  41. assert.EqualError(t, err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`)
  42. }
  43. func TestConfigInspectError(t *testing.T) {
  44. client := &Client{
  45. version: "1.30",
  46. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  47. }
  48. _, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
  49. if err == nil || err.Error() != "Error response from daemon: Server error" {
  50. t.Fatalf("expected a Server Error, got %v", err)
  51. }
  52. }
  53. func TestConfigInspectConfigNotFound(t *testing.T) {
  54. client := &Client{
  55. version: "1.30",
  56. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  57. }
  58. _, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
  59. if err == nil || !IsErrNotFound(err) {
  60. t.Fatalf("expected a configNotFoundError error, got %v", err)
  61. }
  62. }
  63. func TestConfigInspect(t *testing.T) {
  64. expectedURL := "/v1.30/configs/config_id"
  65. client := &Client{
  66. version: "1.30",
  67. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  68. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  69. return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
  70. }
  71. content, err := json.Marshal(swarm.Config{
  72. ID: "config_id",
  73. })
  74. if err != nil {
  75. return nil, err
  76. }
  77. return &http.Response{
  78. StatusCode: http.StatusOK,
  79. Body: ioutil.NopCloser(bytes.NewReader(content)),
  80. }, nil
  81. }),
  82. }
  83. configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if configInspect.ID != "config_id" {
  88. t.Fatalf("expected `config_id`, got %s", configInspect.ID)
  89. }
  90. }