closes #28678
Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
Update cli/command/service/update_test.go
Fixes test build error:
secretAPIClientMock does not implement "github.com/docker/docker/client".SecretAPIClient (missing SecretUpdate method)
Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
| ... | ... |
@@ -401,6 +401,9 @@ func (s secretAPIClientMock) SecretRemove(ctx context.Context, id string) error |
| 401 | 401 |
func (s secretAPIClientMock) SecretInspectWithRaw(ctx context.Context, name string) (swarm.Secret, []byte, error) {
|
| 402 | 402 |
return swarm.Secret{}, []byte{}, nil
|
| 403 | 403 |
} |
| 404 |
+func (s secretAPIClientMock) SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error {
|
|
| 405 |
+ return nil |
|
| 406 |
+} |
|
| 404 | 407 |
|
| 405 | 408 |
// TestUpdateSecretUpdateInPlace tests the ability to update the "target" of an secret with "docker service update" |
| 406 | 409 |
// by combining "--secret-rm" and "--secret-add" for the same secret. |
| ... | ... |
@@ -166,4 +166,5 @@ type SecretAPIClient interface {
|
| 166 | 166 |
SecretCreate(ctx context.Context, secret swarm.SecretSpec) (types.SecretCreateResponse, error) |
| 167 | 167 |
SecretRemove(ctx context.Context, id string) error |
| 168 | 168 |
SecretInspectWithRaw(ctx context.Context, name string) (swarm.Secret, []byte, error) |
| 169 |
+ SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error |
|
| 169 | 170 |
} |
| 170 | 171 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,19 @@ |
| 0 |
+package client |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net/url" |
|
| 4 |
+ "strconv" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/docker/docker/api/types/swarm" |
|
| 7 |
+ "golang.org/x/net/context" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+// SecretUpdate updates a Secret. Currently, the only part of a secret spec |
|
| 11 |
+// which can be updated is Labels. |
|
| 12 |
+func (cli *Client) SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error {
|
|
| 13 |
+ query := url.Values{}
|
|
| 14 |
+ query.Set("version", strconv.FormatUint(version.Index, 10))
|
|
| 15 |
+ resp, err := cli.post(ctx, "/secrets/"+id+"/update", query, secret, nil) |
|
| 16 |
+ ensureReaderClosed(resp) |
|
| 17 |
+ return err |
|
| 18 |
+} |
| 0 | 19 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,49 @@ |
| 0 |
+package client |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "io/ioutil" |
|
| 6 |
+ "net/http" |
|
| 7 |
+ "strings" |
|
| 8 |
+ "testing" |
|
| 9 |
+ |
|
| 10 |
+ "golang.org/x/net/context" |
|
| 11 |
+ |
|
| 12 |
+ "github.com/docker/docker/api/types/swarm" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+func TestSecretUpdateError(t *testing.T) {
|
|
| 16 |
+ client := &Client{
|
|
| 17 |
+ client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), |
|
| 18 |
+ } |
|
| 19 |
+ |
|
| 20 |
+ err := client.SecretUpdate(context.Background(), "secret_id", swarm.Version{}, swarm.SecretSpec{})
|
|
| 21 |
+ if err == nil || err.Error() != "Error response from daemon: Server error" {
|
|
| 22 |
+ t.Fatalf("expected a Server Error, got %v", err)
|
|
| 23 |
+ } |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+func TestSecretUpdate(t *testing.T) {
|
|
| 27 |
+ expectedURL := "/secrets/secret_id/update" |
|
| 28 |
+ |
|
| 29 |
+ client := &Client{
|
|
| 30 |
+ client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 31 |
+ if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
| 32 |
+ return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
| 33 |
+ } |
|
| 34 |
+ if req.Method != "POST" {
|
|
| 35 |
+ return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
|
| 36 |
+ } |
|
| 37 |
+ return &http.Response{
|
|
| 38 |
+ StatusCode: http.StatusOK, |
|
| 39 |
+ Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
|
|
| 40 |
+ }, nil |
|
| 41 |
+ }), |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ err := client.SecretUpdate(context.Background(), "secret_id", swarm.Version{}, swarm.SecretSpec{})
|
|
| 45 |
+ if err != nil {
|
|
| 46 |
+ t.Fatal(err) |
|
| 47 |
+ } |
|
| 48 |
+} |