From 49bb640bae0aac7367d43bf9f16c7e3ead8a62ab Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 20 Jul 2022 10:49:00 +0100 Subject: [PATCH] accounting: fix panic in core/stats-reset with unknown group - fixes #6327 This also adds tests for the rc commands for stats groups --- fs/accounting/stats_groups.go | 4 ++ fs/accounting/stats_groups_test.go | 85 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/fs/accounting/stats_groups.go b/fs/accounting/stats_groups.go index 28548cfc9..7906df998 100644 --- a/fs/accounting/stats_groups.go +++ b/fs/accounting/stats_groups.go @@ -2,6 +2,7 @@ package accounting import ( "context" + "fmt" "sync" "github.com/rclone/rclone/fs/rc" @@ -190,6 +191,9 @@ func rcResetStats(ctx context.Context, in rc.Params) (rc.Params, error) { if group != "" { stats := groups.get(group) + if stats == nil { + return rc.Params{}, fmt.Errorf("group %q not found", group) + } stats.ResetErrors() stats.ResetCounters() } else { diff --git a/fs/accounting/stats_groups_test.go b/fs/accounting/stats_groups_test.go index c111a08d8..ed04d134e 100644 --- a/fs/accounting/stats_groups_test.go +++ b/fs/accounting/stats_groups_test.go @@ -7,8 +7,10 @@ import ( "testing" "time" + "github.com/rclone/rclone/fs/rc" "github.com/rclone/rclone/fstest/testy" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestStatsGroupOperations(t *testing.T) { @@ -117,6 +119,89 @@ func TestStatsGroupOperations(t *testing.T) { t.Errorf("HeapObjects = %d, expected %d", end.HeapObjects, start.HeapObjects) } }) + + testGroupStatsInfo := NewStatsGroup(ctx, "test-group") + testGroupStatsInfo.Deletes(1) + GlobalStats().Deletes(41) + + t.Run("core/group-list", func(t *testing.T) { + call := rc.Calls.Get("core/group-list") + require.NotNil(t, call) + got, err := call.Fn(ctx, rc.Params{}) + require.NoError(t, err) + require.Equal(t, rc.Params{ + "groups": []string{ + "test-group", + }, + }, got) + }) + + t.Run("core/stats", func(t *testing.T) { + call := rc.Calls.Get("core/stats") + require.NotNil(t, call) + gotNoGroup, err := call.Fn(ctx, rc.Params{}) + require.NoError(t, err) + gotGroup, err := call.Fn(ctx, rc.Params{"group": "test-group"}) + require.NoError(t, err) + assert.Equal(t, int64(42), gotNoGroup["deletes"]) + assert.Equal(t, int64(1), gotGroup["deletes"]) + }) + + t.Run("core/transferred", func(t *testing.T) { + call := rc.Calls.Get("core/transferred") + require.NotNil(t, call) + gotNoGroup, err := call.Fn(ctx, rc.Params{}) + require.NoError(t, err) + gotGroup, err := call.Fn(ctx, rc.Params{"group": "test-group"}) + require.NoError(t, err) + assert.Equal(t, rc.Params{ + "transferred": []TransferSnapshot{}, + }, gotNoGroup) + assert.Equal(t, rc.Params{ + "transferred": []TransferSnapshot{}, + }, gotGroup) + }) + + t.Run("core/stats-reset", func(t *testing.T) { + call := rc.Calls.Get("core/stats-reset") + require.NotNil(t, call) + + assert.Equal(t, int64(41), GlobalStats().deletes) + assert.Equal(t, int64(1), testGroupStatsInfo.deletes) + + _, err := call.Fn(ctx, rc.Params{"group": "test-group"}) + require.NoError(t, err) + + assert.Equal(t, int64(41), GlobalStats().deletes) + assert.Equal(t, int64(0), testGroupStatsInfo.deletes) + + _, err = call.Fn(ctx, rc.Params{}) + require.NoError(t, err) + + assert.Equal(t, int64(0), GlobalStats().deletes) + assert.Equal(t, int64(0), testGroupStatsInfo.deletes) + + _, err = call.Fn(ctx, rc.Params{"group": "not-found"}) + require.ErrorContains(t, err, `group "not-found" not found`) + + }) + + testGroupStatsInfo = NewStatsGroup(ctx, "test-group") + + t.Run("core/stats-delete", func(t *testing.T) { + call := rc.Calls.Get("core/stats-delete") + require.NotNil(t, call) + + assert.Equal(t, []string{"test-group"}, groups.names()) + + _, err := call.Fn(ctx, rc.Params{"group": "test-group"}) + require.NoError(t, err) + + assert.Equal(t, []string{}, groups.names()) + + _, err = call.Fn(ctx, rc.Params{"group": "not-found"}) + require.NoError(t, err) + }) } func percentDiff(start, end uint64) uint64 {