github.com/moby/docker@v26.1.3+incompatible/client/network_prune_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/errdefs" 16 "gotest.tools/v3/assert" 17 is "gotest.tools/v3/assert/cmp" 18 ) 19 20 func TestNetworksPruneError(t *testing.T) { 21 client := &Client{ 22 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 23 version: "1.25", 24 } 25 26 _, err := client.NetworksPrune(context.Background(), filters.NewArgs()) 27 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 28 } 29 30 func TestNetworksPrune(t *testing.T) { 31 const expectedURL = "/v1.25/networks/prune" 32 33 listCases := []struct { 34 filters filters.Args 35 expectedQueryParams map[string]string 36 }{ 37 { 38 filters: filters.Args{}, 39 expectedQueryParams: map[string]string{ 40 "until": "", 41 "filter": "", 42 "filters": "", 43 }, 44 }, 45 { 46 filters: filters.NewArgs(filters.Arg("dangling", "true")), 47 expectedQueryParams: map[string]string{ 48 "until": "", 49 "filter": "", 50 "filters": `{"dangling":{"true":true}}`, 51 }, 52 }, 53 { 54 filters: filters.NewArgs(filters.Arg("dangling", "false")), 55 expectedQueryParams: map[string]string{ 56 "until": "", 57 "filter": "", 58 "filters": `{"dangling":{"false":true}}`, 59 }, 60 }, 61 { 62 filters: filters.NewArgs( 63 filters.Arg("dangling", "true"), 64 filters.Arg("label", "label1=foo"), 65 filters.Arg("label", "label2!=bar"), 66 ), 67 expectedQueryParams: map[string]string{ 68 "until": "", 69 "filter": "", 70 "filters": `{"dangling":{"true":true},"label":{"label1=foo":true,"label2!=bar":true}}`, 71 }, 72 }, 73 } 74 for _, listCase := range listCases { 75 client := &Client{ 76 client: newMockClient(func(req *http.Request) (*http.Response, error) { 77 if !strings.HasPrefix(req.URL.Path, expectedURL) { 78 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 79 } 80 query := req.URL.Query() 81 for key, expected := range listCase.expectedQueryParams { 82 actual := query.Get(key) 83 assert.Check(t, is.Equal(expected, actual)) 84 } 85 content, err := json.Marshal(types.NetworksPruneReport{ 86 NetworksDeleted: []string{"network_id1", "network_id2"}, 87 }) 88 if err != nil { 89 return nil, err 90 } 91 return &http.Response{ 92 StatusCode: http.StatusOK, 93 Body: io.NopCloser(bytes.NewReader(content)), 94 }, nil 95 }), 96 version: "1.25", 97 } 98 99 report, err := client.NetworksPrune(context.Background(), listCase.filters) 100 assert.Check(t, err) 101 assert.Check(t, is.Len(report.NetworksDeleted, 2)) 102 } 103 }