github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/secret_list_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/ioutil" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/api/types/filters" 15 "github.com/docker/docker/api/types/swarm" 16 "gotest.tools/assert" 17 is "gotest.tools/assert/cmp" 18 ) 19 20 func TestSecretListUnsupported(t *testing.T) { 21 client := &Client{ 22 version: "1.24", 23 client: &http.Client{}, 24 } 25 _, err := client.SecretList(context.Background(), types.SecretListOptions{}) 26 assert.Check(t, is.Error(err, `"secret list" requires API version 1.25, but the Docker daemon API version is 1.24`)) 27 } 28 29 func TestSecretListError(t *testing.T) { 30 client := &Client{ 31 version: "1.25", 32 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 33 } 34 35 _, err := client.SecretList(context.Background(), types.SecretListOptions{}) 36 if err == nil || err.Error() != "Error response from daemon: Server error" { 37 t.Fatalf("expected a Server Error, got %v", err) 38 } 39 } 40 41 func TestSecretList(t *testing.T) { 42 expectedURL := "/v1.25/secrets" 43 44 filters := filters.NewArgs() 45 filters.Add("label", "label1") 46 filters.Add("label", "label2") 47 48 listCases := []struct { 49 options types.SecretListOptions 50 expectedQueryParams map[string]string 51 }{ 52 { 53 options: types.SecretListOptions{}, 54 expectedQueryParams: map[string]string{ 55 "filters": "", 56 }, 57 }, 58 { 59 options: types.SecretListOptions{ 60 Filters: filters, 61 }, 62 expectedQueryParams: map[string]string{ 63 "filters": `{"label":{"label1":true,"label2":true}}`, 64 }, 65 }, 66 } 67 for _, listCase := range listCases { 68 client := &Client{ 69 version: "1.25", 70 client: newMockClient(func(req *http.Request) (*http.Response, error) { 71 if !strings.HasPrefix(req.URL.Path, expectedURL) { 72 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 73 } 74 query := req.URL.Query() 75 for key, expected := range listCase.expectedQueryParams { 76 actual := query.Get(key) 77 if actual != expected { 78 return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual) 79 } 80 } 81 content, err := json.Marshal([]swarm.Secret{ 82 { 83 ID: "secret_id1", 84 }, 85 { 86 ID: "secret_id2", 87 }, 88 }) 89 if err != nil { 90 return nil, err 91 } 92 return &http.Response{ 93 StatusCode: http.StatusOK, 94 Body: ioutil.NopCloser(bytes.NewReader(content)), 95 }, nil 96 }), 97 } 98 99 secrets, err := client.SecretList(context.Background(), listCase.options) 100 if err != nil { 101 t.Fatal(err) 102 } 103 if len(secrets) != 2 { 104 t.Fatalf("expected 2 secrets, got %v", secrets) 105 } 106 } 107 }