mirror of https://github.com/containers/podman.git
Unification of until filter across list/prune endpoints
Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
This commit is contained in:
parent
5eab1b0742
commit
914218c1e8
|
@ -3,11 +3,9 @@ package image
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/containers/podman/v3/libpod/events"
|
"github.com/containers/podman/v3/libpod/events"
|
||||||
"github.com/containers/podman/v3/pkg/domain/entities/reports"
|
"github.com/containers/podman/v3/pkg/domain/entities/reports"
|
||||||
"github.com/containers/podman/v3/pkg/timetype"
|
|
||||||
"github.com/containers/podman/v3/pkg/util"
|
"github.com/containers/podman/v3/pkg/util"
|
||||||
"github.com/containers/storage"
|
"github.com/containers/storage"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -26,15 +24,10 @@ func generatePruneFilterFuncs(filter, filterValue string) (ImageFilter, error) {
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
case "until":
|
case "until":
|
||||||
ts, err := timetype.GetTimestamp(filterValue, time.Now())
|
until, err := util.ComputeUntilTimestamp([]string{filterValue})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
seconds, nanoseconds, err := timetype.ParseTimestamps(ts, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
until := time.Unix(seconds, nanoseconds)
|
|
||||||
return func(i *Image) bool {
|
return func(i *Image) bool {
|
||||||
if !until.IsZero() && i.Created().After((until)) {
|
if !until.IsZero() && i.Created().After((until)) {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -262,7 +262,7 @@ func IfPassesPruneFilter(config *config.Config, netconf *libcni.NetworkConfigLis
|
||||||
case "label":
|
case "label":
|
||||||
return util.MatchLabelFilters(filterValues, GetNetworkLabels(netconf)), nil
|
return util.MatchLabelFilters(filterValues, GetNetworkLabels(netconf)), nil
|
||||||
case "until":
|
case "until":
|
||||||
until, err := util.ComputeUntilTimestamp(key, filterValues)
|
until, err := util.ComputeUntilTimestamp(filterValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,7 +165,7 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
|
||||||
return false
|
return false
|
||||||
}, nil
|
}, nil
|
||||||
case "until":
|
case "until":
|
||||||
until, err := util.ComputeUntilTimestamp(filter, filterValues)
|
until, err := util.ComputeUntilTimestamp(filterValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,10 +12,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ComputeUntilTimestamp extracts until timestamp from filters
|
// ComputeUntilTimestamp extracts until timestamp from filters
|
||||||
func ComputeUntilTimestamp(filter string, filterValues []string) (time.Time, error) {
|
func ComputeUntilTimestamp(filterValues []string) (time.Time, error) {
|
||||||
invalid := time.Time{}
|
invalid := time.Time{}
|
||||||
if len(filterValues) != 1 {
|
if len(filterValues) != 1 {
|
||||||
return invalid, errors.Errorf("specify exactly one timestamp for %s", filter)
|
return invalid, errors.Errorf("specify exactly one timestamp for until")
|
||||||
}
|
}
|
||||||
ts, err := timetype.GetTimestamp(filterValues[0], time.Now())
|
ts, err := timetype.GetTimestamp(filterValues[0], time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestMatchLabelFilters(t *testing.T) {
|
func TestMatchLabelFilters(t *testing.T) {
|
||||||
testLabels := map[string]string{
|
testLabels := map[string]string{
|
||||||
|
@ -75,3 +77,37 @@ func TestMatchLabelFilters(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestComputeUntilTimestamp(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Return error when more values in list",
|
||||||
|
args: []string{"5h", "6s"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Return error when invalid time",
|
||||||
|
args: []string{"invalidTime"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Do not return error when correct time format supplied",
|
||||||
|
args: []string{"44m"},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
tt := tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
_, err := ComputeUntilTimestamp(tt.args)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("ComputeUntilTimestamp() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue