2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-03-18 19:19:29 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-06-12 13:28:21 -04:00
|
|
|
"bytes"
|
2020-03-18 19:19:29 -04:00
|
|
|
"context"
|
2021-05-11 21:36:15 -04:00
|
|
|
"encoding/json"
|
2020-06-12 13:28:21 -04:00
|
|
|
"fmt"
|
2020-03-18 19:19:29 -04:00
|
|
|
"os"
|
2020-06-12 13:28:21 -04:00
|
|
|
"path"
|
2020-03-18 19:19:29 -04:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usageTestFile struct {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:47:47 -04:00
|
|
|
func TestDataUsageUpdate(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
base := t.TempDir()
|
2020-06-12 13:28:21 -04:00
|
|
|
const bucket = "bucket"
|
2022-01-02 12:15:06 -05:00
|
|
|
files := []usageTestFile{
|
2020-03-18 19:19:29 -04:00
|
|
|
{name: "rootfile", size: 10000},
|
|
|
|
{name: "rootfile2", size: 10000},
|
|
|
|
{name: "dir1/d1file", size: 2000},
|
|
|
|
{name: "dir2/d2file", size: 300},
|
|
|
|
{name: "dir1/dira/dafile", size: 100000},
|
|
|
|
{name: "dir1/dira/dbfile", size: 200000},
|
|
|
|
{name: "dir1/dira/dirasub/dcfile", size: 1000000},
|
|
|
|
{name: "dir1/dira/dirasub/sublevel3/dccccfile", size: 10},
|
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
createUsageTestFiles(t, base, bucket, files)
|
2020-03-18 19:19:29 -04:00
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
getSize := func(item scannerItem) (sizeS sizeSummary, err error) {
|
2020-03-18 19:19:29 -04:00
|
|
|
if item.Typ&os.ModeDir == 0 {
|
2020-12-07 16:47:48 -05:00
|
|
|
var s os.FileInfo
|
|
|
|
s, err = os.Stat(item.Path)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-12-07 16:47:48 -05:00
|
|
|
sizeS.totalSize = s.Size()
|
2021-05-11 21:36:15 -04:00
|
|
|
sizeS.versions++
|
2020-12-07 16:47:48 -05:00
|
|
|
return sizeS, nil
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-03-19 12:47:47 -04:00
|
|
|
|
2023-07-17 12:52:05 -04:00
|
|
|
got, err := scanDataFolder(context.Background(), nil, base, dataUsageCache{Info: dataUsageCacheInfo{Name: bucket}}, getSize, 0)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test dirs
|
2022-01-02 12:15:06 -05:00
|
|
|
want := []struct {
|
2020-03-18 19:19:29 -04:00
|
|
|
path string
|
|
|
|
isNil bool
|
|
|
|
size, objs int
|
|
|
|
flatten bool
|
|
|
|
oSizes sizeHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "/",
|
|
|
|
size: 1322310,
|
|
|
|
flatten: true,
|
|
|
|
objs: 8,
|
|
|
|
oSizes: sizeHistogram{0: 2, 1: 6},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/",
|
|
|
|
size: 20000,
|
|
|
|
objs: 2,
|
|
|
|
oSizes: sizeHistogram{1: 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/dir1",
|
2021-05-11 21:36:15 -04:00
|
|
|
size: 1302010,
|
|
|
|
objs: 5,
|
|
|
|
oSizes: sizeHistogram{0: 1, 1: 4},
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
2021-05-11 21:36:15 -04:00
|
|
|
path: "/dir1/dira",
|
|
|
|
isNil: true,
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/nonexistying",
|
|
|
|
isNil: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range want {
|
2020-06-12 13:28:21 -04:00
|
|
|
p := path.Join(bucket, w.path)
|
|
|
|
t.Run(p, func(t *testing.T) {
|
|
|
|
e := got.find(p)
|
2020-03-18 19:19:29 -04:00
|
|
|
if w.isNil {
|
|
|
|
if e != nil {
|
|
|
|
t.Error("want nil, got", e)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e == nil {
|
|
|
|
t.Fatal("got nil result")
|
|
|
|
}
|
|
|
|
if w.flatten {
|
|
|
|
*e = got.flatten(*e)
|
|
|
|
}
|
|
|
|
if e.Size != int64(w.size) {
|
|
|
|
t.Error("got size", e.Size, "want", w.size)
|
|
|
|
}
|
|
|
|
if e.Objects != uint64(w.objs) {
|
|
|
|
t.Error("got objects", e.Objects, "want", w.objs)
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
if e.Versions != uint64(w.objs) {
|
|
|
|
t.Error("got versions", e.Versions, "want", w.objs)
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
if e.ObjSizes != w.oSizes {
|
|
|
|
t.Error("got histogram", e.ObjSizes, "want", w.oSizes)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
files = []usageTestFile{
|
|
|
|
{
|
|
|
|
name: "newfolder/afile",
|
|
|
|
size: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "newfolder/anotherone",
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "newfolder/anemptyone",
|
|
|
|
size: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir1/fileindir1",
|
|
|
|
size: 20000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir1/dirc/fileindirc",
|
|
|
|
size: 20000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rootfile3",
|
|
|
|
size: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir1/dira/dirasub/fileindira2",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
createUsageTestFiles(t, base, bucket, files)
|
|
|
|
err = os.RemoveAll(filepath.Join(base, bucket, "dir1/dira/dirasub/dcfile"))
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Changed dir must be picked up in this many cycles.
|
|
|
|
for i := 0; i < dataUsageUpdateDirCycles; i++ {
|
2023-07-17 12:52:05 -04:00
|
|
|
got, err = scanDataFolder(context.Background(), nil, base, got, getSize, 0)
|
2021-08-25 11:25:26 -04:00
|
|
|
got.Info.NextCycle++
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
want = []struct {
|
|
|
|
path string
|
|
|
|
isNil bool
|
|
|
|
size, objs int
|
|
|
|
flatten bool
|
|
|
|
oSizes sizeHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "/",
|
|
|
|
size: 363515,
|
|
|
|
flatten: true,
|
|
|
|
objs: 14,
|
|
|
|
oSizes: sizeHistogram{0: 7, 1: 7},
|
|
|
|
},
|
|
|
|
{
|
2021-05-11 21:36:15 -04:00
|
|
|
path: "/dir1",
|
|
|
|
size: 342210,
|
|
|
|
objs: 7,
|
|
|
|
flatten: false,
|
|
|
|
oSizes: sizeHistogram{0: 2, 1: 5},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/newfolder",
|
|
|
|
size: 5,
|
|
|
|
objs: 3,
|
|
|
|
oSizes: sizeHistogram{0: 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/nonexistying",
|
|
|
|
isNil: true,
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range want {
|
2020-06-12 13:28:21 -04:00
|
|
|
p := path.Join(bucket, w.path)
|
|
|
|
t.Run(p, func(t *testing.T) {
|
|
|
|
e := got.find(p)
|
2020-03-18 19:19:29 -04:00
|
|
|
if w.isNil {
|
|
|
|
if e != nil {
|
|
|
|
t.Error("want nil, got", e)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e == nil {
|
|
|
|
t.Fatal("got nil result")
|
|
|
|
}
|
|
|
|
if w.flatten {
|
|
|
|
*e = got.flatten(*e)
|
|
|
|
}
|
|
|
|
if e.Size != int64(w.size) {
|
|
|
|
t.Error("got size", e.Size, "want", w.size)
|
|
|
|
}
|
|
|
|
if e.Objects != uint64(w.objs) {
|
|
|
|
t.Error("got objects", e.Objects, "want", w.objs)
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
if e.Versions != uint64(w.objs) {
|
|
|
|
t.Error("got versions", e.Versions, "want", w.objs)
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
if e.ObjSizes != w.oSizes {
|
|
|
|
t.Error("got histogram", e.ObjSizes, "want", w.oSizes)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:47:47 -04:00
|
|
|
func TestDataUsageUpdatePrefix(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
base := t.TempDir()
|
2021-05-11 21:36:15 -04:00
|
|
|
scannerSleeper.Update(0, 0)
|
2022-01-02 12:15:06 -05:00
|
|
|
files := []usageTestFile{
|
2020-03-18 19:19:29 -04:00
|
|
|
{name: "bucket/rootfile", size: 10000},
|
|
|
|
{name: "bucket/rootfile2", size: 10000},
|
|
|
|
{name: "bucket/dir1/d1file", size: 2000},
|
|
|
|
{name: "bucket/dir2/d2file", size: 300},
|
|
|
|
{name: "bucket/dir1/dira/dafile", size: 100000},
|
|
|
|
{name: "bucket/dir1/dira/dbfile", size: 200000},
|
|
|
|
{name: "bucket/dir1/dira/dirasub/dcfile", size: 1000000},
|
|
|
|
{name: "bucket/dir1/dira/dirasub/sublevel3/dccccfile", size: 10},
|
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
createUsageTestFiles(t, base, "", files)
|
2021-05-11 21:36:15 -04:00
|
|
|
const foldersBelow = 3
|
|
|
|
const filesBelowT = dataScannerCompactLeastObject / 2
|
|
|
|
const filesAboveT = dataScannerCompactAtFolders + 1
|
|
|
|
const expectSize = foldersBelow*filesBelowT + filesAboveT
|
|
|
|
|
|
|
|
generateUsageTestFiles(t, base, "bucket/dirwithalot", foldersBelow, filesBelowT, 1)
|
|
|
|
generateUsageTestFiles(t, base, "bucket/dirwithevenmore", filesAboveT, 1, 1)
|
2020-03-18 19:19:29 -04:00
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
getSize := func(item scannerItem) (sizeS sizeSummary, err error) {
|
2020-03-18 19:19:29 -04:00
|
|
|
if item.Typ&os.ModeDir == 0 {
|
2020-12-07 16:47:48 -05:00
|
|
|
var s os.FileInfo
|
|
|
|
s, err = os.Stat(item.Path)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-12-07 16:47:48 -05:00
|
|
|
sizeS.totalSize = s.Size()
|
2021-05-11 21:36:15 -04:00
|
|
|
sizeS.versions++
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2023-07-17 12:52:05 -04:00
|
|
|
got, err := scanDataFolder(context.Background(), nil, base, dataUsageCache{Info: dataUsageCacheInfo{Name: "bucket"}}, getSize, 0)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-24 16:47:01 -04:00
|
|
|
if got.root() == nil {
|
|
|
|
t.Log("cached folders:")
|
|
|
|
for folder := range got.Cache {
|
|
|
|
t.Log("folder:", folder)
|
|
|
|
}
|
|
|
|
t.Fatal("got nil root.")
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
|
|
|
|
// Test dirs
|
2022-01-02 12:15:06 -05:00
|
|
|
want := []struct {
|
2020-03-18 19:19:29 -04:00
|
|
|
path string
|
|
|
|
isNil bool
|
|
|
|
size, objs int
|
|
|
|
oSizes sizeHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "flat",
|
2021-05-11 21:36:15 -04:00
|
|
|
size: 1322310 + expectSize,
|
|
|
|
objs: 8 + expectSize,
|
|
|
|
oSizes: sizeHistogram{0: 2 + expectSize, 1: 6},
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "bucket/",
|
|
|
|
size: 20000,
|
|
|
|
objs: 2,
|
|
|
|
oSizes: sizeHistogram{1: 2},
|
|
|
|
},
|
|
|
|
{
|
2021-05-11 21:36:15 -04:00
|
|
|
// Gets compacted...
|
2020-03-18 19:19:29 -04:00
|
|
|
path: "bucket/dir1",
|
2021-05-11 21:36:15 -04:00
|
|
|
size: 1302010,
|
|
|
|
objs: 5,
|
|
|
|
oSizes: sizeHistogram{0: 1, 1: 4},
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
2021-05-11 21:36:15 -04:00
|
|
|
// Gets compacted at this level...
|
|
|
|
path: "bucket/dirwithalot/0",
|
|
|
|
size: filesBelowT,
|
|
|
|
objs: filesBelowT,
|
|
|
|
oSizes: sizeHistogram{0: filesBelowT},
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
2021-05-11 21:36:15 -04:00
|
|
|
// Gets compacted at this level (below obj threshold)...
|
|
|
|
path: "bucket/dirwithalot/0",
|
|
|
|
size: filesBelowT,
|
|
|
|
objs: filesBelowT,
|
|
|
|
oSizes: sizeHistogram{0: filesBelowT},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Gets compacted at this level...
|
|
|
|
path: "bucket/dirwithevenmore",
|
|
|
|
size: filesAboveT,
|
|
|
|
objs: filesAboveT,
|
|
|
|
oSizes: sizeHistogram{0: filesAboveT},
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "bucket/nonexistying",
|
|
|
|
isNil: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range want {
|
|
|
|
t.Run(w.path, func(t *testing.T) {
|
|
|
|
e := got.find(w.path)
|
|
|
|
if w.path == "flat" {
|
|
|
|
f := got.flatten(*got.root())
|
|
|
|
e = &f
|
|
|
|
}
|
|
|
|
if w.isNil {
|
|
|
|
if e != nil {
|
|
|
|
t.Error("want nil, got", e)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e == nil {
|
|
|
|
t.Fatal("got nil result")
|
|
|
|
}
|
|
|
|
if e.Size != int64(w.size) {
|
|
|
|
t.Error("got size", e.Size, "want", w.size)
|
|
|
|
}
|
|
|
|
if e.Objects != uint64(w.objs) {
|
|
|
|
t.Error("got objects", e.Objects, "want", w.objs)
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
if e.Versions != uint64(w.objs) {
|
|
|
|
t.Error("got versions", e.Versions, "want", w.objs)
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
if e.ObjSizes != w.oSizes {
|
|
|
|
t.Error("got histogram", e.ObjSizes, "want", w.oSizes)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
files = []usageTestFile{
|
|
|
|
{
|
|
|
|
name: "bucket/newfolder/afile",
|
|
|
|
size: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/newfolder/anotherone",
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/newfolder/anemptyone",
|
|
|
|
size: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/dir1/fileindir1",
|
|
|
|
size: 20000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/dir1/dirc/fileindirc",
|
|
|
|
size: 20000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/rootfile3",
|
|
|
|
size: 1000,
|
|
|
|
},
|
2021-05-11 21:36:15 -04:00
|
|
|
{
|
|
|
|
name: "bucket/dir1/dira/dirasub/fileindira2",
|
|
|
|
size: 200,
|
|
|
|
},
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
createUsageTestFiles(t, base, "", files)
|
2021-05-11 21:36:15 -04:00
|
|
|
err = os.RemoveAll(filepath.Join(base, "bucket/dir1/dira/dirasub/dcfile"))
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
// Changed dir must be picked up in this many cycles.
|
|
|
|
for i := 0; i < dataUsageUpdateDirCycles; i++ {
|
2023-07-17 12:52:05 -04:00
|
|
|
got, err = scanDataFolder(context.Background(), nil, base, got, getSize, 0)
|
2021-08-25 11:25:26 -04:00
|
|
|
got.Info.NextCycle++
|
2021-05-11 21:36:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
|
|
|
|
want = []struct {
|
|
|
|
path string
|
|
|
|
isNil bool
|
|
|
|
size, objs int
|
|
|
|
oSizes sizeHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "flat",
|
2021-05-11 21:36:15 -04:00
|
|
|
size: 363515 + expectSize,
|
|
|
|
objs: 14 + expectSize,
|
|
|
|
oSizes: sizeHistogram{0: 7 + expectSize, 1: 7},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "bucket/dir1",
|
|
|
|
size: 342210,
|
|
|
|
objs: 7,
|
|
|
|
oSizes: sizeHistogram{0: 2, 1: 5},
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "bucket/",
|
|
|
|
size: 21000,
|
|
|
|
objs: 3,
|
|
|
|
oSizes: sizeHistogram{0: 1, 1: 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "bucket/newfolder",
|
|
|
|
size: 5,
|
|
|
|
objs: 3,
|
|
|
|
oSizes: sizeHistogram{0: 3},
|
|
|
|
},
|
|
|
|
{
|
2021-05-11 21:36:15 -04:00
|
|
|
// Compacted into bucket/dir1
|
|
|
|
path: "bucket/dir1/dira",
|
|
|
|
isNil: true,
|
2020-03-18 19:19:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "bucket/nonexistying",
|
|
|
|
isNil: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range want {
|
|
|
|
t.Run(w.path, func(t *testing.T) {
|
|
|
|
e := got.find(w.path)
|
|
|
|
if w.path == "flat" {
|
|
|
|
f := got.flatten(*got.root())
|
|
|
|
e = &f
|
|
|
|
}
|
|
|
|
if w.isNil {
|
|
|
|
if e != nil {
|
|
|
|
t.Error("want nil, got", e)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e == nil {
|
2021-05-11 21:36:15 -04:00
|
|
|
t.Error("got nil result")
|
2020-03-18 19:19:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if e.Size != int64(w.size) {
|
|
|
|
t.Error("got size", e.Size, "want", w.size)
|
|
|
|
}
|
|
|
|
if e.Objects != uint64(w.objs) {
|
|
|
|
t.Error("got objects", e.Objects, "want", w.objs)
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
if e.Versions != uint64(w.objs) {
|
|
|
|
t.Error("got versions", e.Versions, "want", w.objs)
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
if e.ObjSizes != w.oSizes {
|
|
|
|
t.Error("got histogram", e.ObjSizes, "want", w.oSizes)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
func createUsageTestFiles(t *testing.T, base, bucket string, files []usageTestFile) {
|
2020-03-18 19:19:29 -04:00
|
|
|
for _, f := range files {
|
2020-06-12 13:28:21 -04:00
|
|
|
err := os.MkdirAll(filepath.Dir(filepath.Join(base, bucket, f.name)), os.ModePerm)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-09-19 14:05:16 -04:00
|
|
|
err = os.WriteFile(filepath.Join(base, bucket, f.name), make([]byte, f.size), os.ModePerm)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
// generateUsageTestFiles create nFolders * nFiles files of size bytes each.
|
|
|
|
func generateUsageTestFiles(t *testing.T, base, bucket string, nFolders, nFiles, size int) {
|
|
|
|
pl := make([]byte, size)
|
|
|
|
for i := 0; i < nFolders; i++ {
|
|
|
|
name := filepath.Join(base, bucket, fmt.Sprint(i), "0.txt")
|
|
|
|
err := os.MkdirAll(filepath.Dir(name), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for j := 0; j < nFiles; j++ {
|
|
|
|
name := filepath.Join(base, bucket, fmt.Sprint(i), fmt.Sprint(j)+".txt")
|
2022-09-19 14:05:16 -04:00
|
|
|
err = os.WriteFile(name, pl, os.ModePerm)
|
2021-05-11 21:36:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:47:47 -04:00
|
|
|
func TestDataUsageCacheSerialize(t *testing.T) {
|
2022-07-25 15:37:26 -04:00
|
|
|
base := t.TempDir()
|
2020-06-12 13:28:21 -04:00
|
|
|
const bucket = "abucket"
|
2022-01-02 12:15:06 -05:00
|
|
|
files := []usageTestFile{
|
2020-03-18 19:19:29 -04:00
|
|
|
{name: "rootfile", size: 10000},
|
|
|
|
{name: "rootfile2", size: 10000},
|
|
|
|
{name: "dir1/d1file", size: 2000},
|
|
|
|
{name: "dir2/d2file", size: 300},
|
2020-06-12 13:28:21 -04:00
|
|
|
{name: "dir2/d2file2", size: 300},
|
|
|
|
{name: "dir2/d2file3/", size: 300},
|
|
|
|
{name: "dir2/d2file4/", size: 300},
|
|
|
|
{name: "dir2/d2file5", size: 300},
|
2020-03-18 19:19:29 -04:00
|
|
|
{name: "dir1/dira/dafile", size: 100000},
|
|
|
|
{name: "dir1/dira/dbfile", size: 200000},
|
|
|
|
{name: "dir1/dira/dirasub/dcfile", size: 1000000},
|
|
|
|
{name: "dir1/dira/dirasub/sublevel3/dccccfile", size: 10},
|
2020-06-12 13:28:21 -04:00
|
|
|
{name: "dir1/dira/dirasub/sublevel3/dccccfile20", size: 20},
|
|
|
|
{name: "dir1/dira/dirasub/sublevel3/dccccfile30", size: 30},
|
|
|
|
{name: "dir1/dira/dirasub/sublevel3/dccccfile40", size: 40},
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
createUsageTestFiles(t, base, bucket, files)
|
2020-03-18 19:19:29 -04:00
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
getSize := func(item scannerItem) (sizeS sizeSummary, err error) {
|
2020-03-18 19:19:29 -04:00
|
|
|
if item.Typ&os.ModeDir == 0 {
|
2020-12-07 16:47:48 -05:00
|
|
|
var s os.FileInfo
|
|
|
|
s, err = os.Stat(item.Path)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
sizeS.versions++
|
2020-12-07 16:47:48 -05:00
|
|
|
sizeS.totalSize = s.Size()
|
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-12-07 16:47:48 -05:00
|
|
|
return
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2023-07-17 12:52:05 -04:00
|
|
|
want, err := scanDataFolder(context.Background(), nil, base, dataUsageCache{Info: dataUsageCacheInfo{Name: bucket}}, getSize, 0)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
e := want.find("abucket/dir2")
|
2021-09-18 16:31:35 -04:00
|
|
|
e.ReplicationStats = &replicationAllStats{
|
|
|
|
Targets: map[string]replicationStats{
|
|
|
|
"arn": {
|
|
|
|
PendingSize: 1,
|
|
|
|
ReplicatedSize: 2,
|
|
|
|
FailedSize: 3,
|
|
|
|
FailedCount: 5,
|
|
|
|
PendingCount: 6,
|
|
|
|
},
|
|
|
|
},
|
2021-05-11 21:36:15 -04:00
|
|
|
}
|
|
|
|
want.replace("abucket/dir2", "", *e)
|
2020-12-10 16:03:22 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = want.serializeTo(&buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log("serialized size:", buf.Len(), "bytes")
|
2020-03-18 19:19:29 -04:00
|
|
|
var got dataUsageCache
|
2020-12-10 16:03:22 -05:00
|
|
|
err = got.deserialize(&buf)
|
2020-03-18 19:19:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got.Info.LastUpdate.IsZero() {
|
|
|
|
t.Error("lastupdate not set")
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:47:47 -04:00
|
|
|
if !want.Info.LastUpdate.Equal(got.Info.LastUpdate) {
|
2020-06-12 13:28:21 -04:00
|
|
|
t.Fatalf("deserialize LastUpdate mismatch\nwant: %+v\ngot: %+v", want, got)
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
if len(want.Cache) != len(got.Cache) {
|
|
|
|
t.Errorf("deserialize mismatch length\nwant: %+v\ngot: %+v", len(want.Cache), len(got.Cache))
|
|
|
|
}
|
|
|
|
for wkey, wval := range want.Cache {
|
|
|
|
gotv := got.Cache[wkey]
|
2021-05-11 21:36:15 -04:00
|
|
|
if !equalAsJSON(gotv, wval) {
|
|
|
|
t.Errorf("deserialize mismatch, key %v\nwant: %#v\ngot: %#v", wkey, wval, gotv)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
// equalAsJSON returns whether the values are equal when encoded as JSON.
|
|
|
|
func equalAsJSON(a, b interface{}) bool {
|
|
|
|
aj, err := json.Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bj, err := json.Marshal(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bytes.Equal(aj, bj)
|
2020-03-18 19:19:29 -04:00
|
|
|
}
|