summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Guzik <jakubmguzik@gmail.com>2021-03-24 00:42:42 +0100
committerMatthew Heon <mheon@redhat.com>2021-03-29 13:26:24 -0400
commita01b387e8251bef5fc73f3e8ac72cc5db9ecea9e (patch)
treef7d18146e18813181e2963b429a124f1d110867c
parent183a68a817208821a9fc79a3c9c19202a63dfb44 (diff)
downloadpodman-a01b387e8251bef5fc73f3e8ac72cc5db9ecea9e.tar.gz
podman-a01b387e8251bef5fc73f3e8ac72cc5db9ecea9e.tar.bz2
podman-a01b387e8251bef5fc73f3e8ac72cc5db9ecea9e.zip
Unification of until filter across list/prune endpoints
Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
-rw-r--r--libpod/image/prune.go9
-rw-r--r--libpod/network/netconflist.go2
-rw-r--r--pkg/domain/filters/containers.go2
-rw-r--r--pkg/util/filters.go4
-rw-r--r--pkg/util/filters_test.go38
5 files changed, 42 insertions, 13 deletions
diff --git a/libpod/image/prune.go b/libpod/image/prune.go
index 1ef35ef83..12727901a 100644
--- a/libpod/image/prune.go
+++ b/libpod/image/prune.go
@@ -4,11 +4,9 @@ import (
"context"
"strconv"
"strings"
- "time"
"github.com/containers/podman/v3/libpod/events"
"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/storage"
"github.com/pkg/errors"
@@ -27,15 +25,10 @@ func generatePruneFilterFuncs(filter, filterValue string) (ImageFilter, error) {
}, nil
case "until":
- ts, err := timetype.GetTimestamp(filterValue, time.Now())
+ until, err := util.ComputeUntilTimestamp([]string{filterValue})
if err != nil {
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 {
if !until.IsZero() && i.Created().After((until)) {
return true
diff --git a/libpod/network/netconflist.go b/libpod/network/netconflist.go
index 179620a0d..08816f2bd 100644
--- a/libpod/network/netconflist.go
+++ b/libpod/network/netconflist.go
@@ -262,7 +262,7 @@ func IfPassesPruneFilter(config *config.Config, netconf *libcni.NetworkConfigLis
case "label":
return util.MatchLabelFilters(filterValues, GetNetworkLabels(netconf)), nil
case "until":
- until, err := util.ComputeUntilTimestamp(key, filterValues)
+ until, err := util.ComputeUntilTimestamp(filterValues)
if err != nil {
return false, err
}
diff --git a/pkg/domain/filters/containers.go b/pkg/domain/filters/containers.go
index 9cae01dc0..84cf03764 100644
--- a/pkg/domain/filters/containers.go
+++ b/pkg/domain/filters/containers.go
@@ -165,7 +165,7 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
return false
}, nil
case "until":
- until, err := util.ComputeUntilTimestamp(filter, filterValues)
+ until, err := util.ComputeUntilTimestamp(filterValues)
if err != nil {
return nil, err
}
diff --git a/pkg/util/filters.go b/pkg/util/filters.go
index 90daef402..43bf646f1 100644
--- a/pkg/util/filters.go
+++ b/pkg/util/filters.go
@@ -12,10 +12,10 @@ import (
)
// 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{}
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())
if err != nil {
diff --git a/pkg/util/filters_test.go b/pkg/util/filters_test.go
index 4771f682b..47259013e 100644
--- a/pkg/util/filters_test.go
+++ b/pkg/util/filters_test.go
@@ -1,6 +1,8 @@
package util
-import "testing"
+import (
+ "testing"
+)
func TestMatchLabelFilters(t *testing.T) {
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
+ }
+ })
+ }
+}