github.com/moby/docker@v26.1.3+incompatible/client/secret_create_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" 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 "gotest.tools/v3/assert" 17 is "gotest.tools/v3/assert/cmp" 18 ) 19 20 func TestSecretCreateUnsupported(t *testing.T) { 21 client := &Client{ 22 version: "1.24", 23 client: &http.Client{}, 24 } 25 _, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 26 assert.Check(t, is.Error(err, `"secret create" requires API version 1.25, but the Docker daemon API version is 1.24`)) 27 } 28 29 func TestSecretCreateError(t *testing.T) { 30 client := &Client{ 31 version: "1.25", 32 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 33 } 34 _, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 35 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 36 } 37 38 func TestSecretCreate(t *testing.T) { 39 expectedURL := "/v1.25/secrets/create" 40 client := &Client{ 41 version: "1.25", 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 if req.Method != http.MethodPost { 47 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 48 } 49 b, err := json.Marshal(types.SecretCreateResponse{ 50 ID: "test_secret", 51 }) 52 if err != nil { 53 return nil, err 54 } 55 return &http.Response{ 56 StatusCode: http.StatusCreated, 57 Body: io.NopCloser(bytes.NewReader(b)), 58 }, nil 59 }), 60 } 61 62 r, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if r.ID != "test_secret" { 67 t.Fatalf("expected `test_secret`, got %s", r.ID) 68 } 69 }