github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/secret_create_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 "github.com/stretchr/testify/assert" 15 "golang.org/x/net/context" 16 ) 17 18 func TestSecretCreateUnsupported(t *testing.T) { 19 client := &Client{ 20 version: "1.24", 21 client: &http.Client{}, 22 } 23 _, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 24 assert.EqualError(t, err, `"secret create" requires API version 1.25, but the Docker daemon API version is 1.24`) 25 } 26 27 func TestSecretCreateError(t *testing.T) { 28 client := &Client{ 29 version: "1.25", 30 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 31 } 32 _, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 33 if err == nil || err.Error() != "Error response from daemon: Server error" { 34 t.Fatalf("expected a Server Error, got %v", err) 35 } 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 != "POST" { 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: ioutil.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 }