github.com/rish1988/moby@v25.0.2+incompatible/client/config_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" 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 "github.com/docker/docker/errdefs" 17 "gotest.tools/v3/assert" 18 is "gotest.tools/v3/assert/cmp" 19 ) 20 21 func TestConfigListUnsupported(t *testing.T) { 22 client := &Client{ 23 version: "1.29", 24 client: &http.Client{}, 25 } 26 _, err := client.ConfigList(context.Background(), types.ConfigListOptions{}) 27 assert.Check(t, is.Error(err, `"config list" requires API version 1.30, but the Docker daemon API version is 1.29`)) 28 } 29 30 func TestConfigListError(t *testing.T) { 31 client := &Client{ 32 version: "1.30", 33 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 34 } 35 36 _, err := client.ConfigList(context.Background(), types.ConfigListOptions{}) 37 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 38 } 39 40 func TestConfigList(t *testing.T) { 41 expectedURL := "/v1.30/configs" 42 43 listCases := []struct { 44 options types.ConfigListOptions 45 expectedQueryParams map[string]string 46 }{ 47 { 48 options: types.ConfigListOptions{}, 49 expectedQueryParams: map[string]string{ 50 "filters": "", 51 }, 52 }, 53 { 54 options: types.ConfigListOptions{ 55 Filters: filters.NewArgs( 56 filters.Arg("label", "label1"), 57 filters.Arg("label", "label2"), 58 ), 59 }, 60 expectedQueryParams: map[string]string{ 61 "filters": `{"label":{"label1":true,"label2":true}}`, 62 }, 63 }, 64 } 65 for _, listCase := range listCases { 66 client := &Client{ 67 version: "1.30", 68 client: newMockClient(func(req *http.Request) (*http.Response, error) { 69 if !strings.HasPrefix(req.URL.Path, expectedURL) { 70 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 71 } 72 query := req.URL.Query() 73 for key, expected := range listCase.expectedQueryParams { 74 actual := query.Get(key) 75 if actual != expected { 76 return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual) 77 } 78 } 79 content, err := json.Marshal([]swarm.Config{ 80 { 81 ID: "config_id1", 82 }, 83 { 84 ID: "config_id2", 85 }, 86 }) 87 if err != nil { 88 return nil, err 89 } 90 return &http.Response{ 91 StatusCode: http.StatusOK, 92 Body: io.NopCloser(bytes.NewReader(content)), 93 }, nil 94 }), 95 } 96 97 configs, err := client.ConfigList(context.Background(), listCase.options) 98 if err != nil { 99 t.Fatal(err) 100 } 101 if len(configs) != 2 { 102 t.Fatalf("expected 2 configs, got %v", configs) 103 } 104 } 105 }