fork/gdu
git clone https://git.julia.blue/fork/gdu
5ff0be6
subvolume
1package report 2 3import ( 4"bytes" 5"errors" 6"testing" 7 8"github.com/dundee/gdu/v5/pkg/analyze" 9"github.com/dundee/gdu/v5/pkg/fs" 10 log"github.com/sirupsen/logrus" 11 12"github.com/stretchr/testify/assert" 13) 14 15func init () { 16log .SetLevel (log .WarnLevel ) 17} 18 19func TestReadAnalysis (t * testing.T ) { 20buff := bytes .NewBuffer ([]byte (` 21[1,2,{"progname":"gdu","progver":"development","timestamp":1626806293}, 22[{"name":"/home/xxx","asize":47000000,"dsize":64000000,"items":6,"mtime":1629333600}, 23{"name":"gdu.json","asize":33805233,"dsize":33808384}, 24{"name":"sock","notreg":true}, 25[{"name":"app","asize":10022,"dsize":20480,"items":4}, 26{"name":"app.go","asize":4638,"dsize":8192}, 27{"name":"app_linux_test.go","asize":1410,"dsize":4096}, 28{"name":"app_linux_test2.go","ino":1234,"hlnkc":true,"asize":1410,"dsize":4096}, 29{"name":"app_test.go","asize":4974,"dsize":8192}], 30{"name":"main.go","asize":3205,"dsize":4096,"mtime":1629333600}]] 31` )) 32 33dir ,err := ReadAnalysis (buff ) 34 35assert .Nil (t ,err ) 36assert .Equal (t ,"xxx" ,dir .GetName ()) 37assert .Equal (t ,"/home/xxx" ,dir .GetPath ()) 38assert .Equal (t ,int64 (47000000 ),dir .GetSize ()) 39assert .Equal (t ,int64 (64000000 ),dir .GetUsage ()) 40assert .Equal (t ,int64 (6 ),dir .GetItemCount ()) 41assert .Equal (t ,int64 (10022 ),dir .Files [2 ].GetSize ()) 42assert .Equal (t ,int64 (20480 ),dir .Files [2 ].GetUsage ()) 43assert .Equal (t ,int64 (4 ),dir .Files [2 ].GetItemCount ()) 44alt2 := dir .Files [2 ].(* analyze.Dir ).Files [2 ].(* analyze.File ) 45assert .Equal (t ,"app_linux_test2.go" ,alt2 .Name ) 46assert .Equal (t ,uint64 (1234 ),alt2 .Mli ) 47assert .Equal (t ,'H' ,alt2 .Flag ) 48} 49 50func TestReadAnalysisPreservesTruncatedDirectoryStats (t * testing.T ) { 51input := bytes .NewBufferString (` 52[1,2,{"progname":"gdu","progver":"development","timestamp":0}, 53[{"name":"/root","asize":4096,"dsize":4096,"items":3}, 54[{"name":"summary","asize":4096,"dsize":4096,"items":2}]]] 55` ) 56 57dir ,err := ReadAnalysis (input ) 58assert .NoError (t ,err ) 59dir .UpdateStats (make (fs.HardLinkedItems ,10 )) 60 61assert .Equal (t ,int64 (4096 ),dir .GetSize ()) 62assert .Equal (t ,int64 (4096 ),dir .GetUsage ()) 63assert .Equal (t ,int64 (3 ),dir .GetItemCount ()) 64summary := dir .Files [0 ].(* analyze.Dir ) 65assert .Equal (t ,int64 (4096 ),summary .GetSize ()) 66assert .Equal (t ,int64 (4096 ),summary .GetUsage ()) 67assert .Equal (t ,int64 (2 ),summary .GetItemCount ()) 68dir .UpdateStatsWithFileFiltering (make (fs.HardLinkedItems ,10 )) 69assert .Equal (t ,int64 (4096 ),dir .GetSize ()) 70assert .Equal (t ,int64 (4096 ),dir .GetUsage ()) 71assert .Equal (t ,int64 (3 ),dir .GetItemCount ()) 72} 73 74func TestReadAnalysisRecomputesEmptyDirStats (t * testing.T ) { 75input := bytes .NewBufferString (` 76[1,2,{"progname":"gdu","progver":"development","timestamp":0}, 77[{"name":"/empty","asize":512,"dsize":0,"items":1}]] 78` ) 79 80dir ,err := ReadAnalysis (input ) 81assert .NoError (t ,err ) 82dir .UpdateStats (make (fs.HardLinkedItems ,10 )) 83 84assert .Equal (t ,int64 (512 ),dir .GetSize ()) 85assert .Equal (t ,int64 (0 ),dir .GetUsage ()) 86assert .Equal (t ,int64 (1 ),dir .GetItemCount ()) 87} 88 89// A depth-truncated directory that held real content exports with items >= 2, 90// so even when its apparent size coincides with the empty-dir placeholder its 91// stats must be preserved (not recomputed to zero). This pins the items == 1 92// discriminator in HasEmptyDirPlaceholderStats as intentional. 93func TestReadAnalysisPreservesTruncatedDirWithPlaceholderSize (t * testing.T ) { 94input := bytes .NewBufferString (` 95[1,2,{"progname":"gdu","progver":"development","timestamp":0}, 96[{"name":"/root","asize":1024,"dsize":0,"items":3}, 97[{"name":"truncated","asize":512,"dsize":0,"items":2}]]] 98` ) 99 100dir ,err := ReadAnalysis (input ) 101assert .NoError (t ,err ) 102dir .UpdateStats (make (fs.HardLinkedItems ,10 )) 103 104truncated := dir .Files [0 ].(* analyze.Dir ) 105assert .Equal (t ,int64 (512 ),truncated .GetSize ()) 106assert .Equal (t ,int64 (0 ),truncated .GetUsage ()) 107assert .Equal (t ,int64 (2 ),truncated .GetItemCount ()) 108} 109 110func TestReadAnalysisWithEmptyInput (t * testing.T ) { 111buff := bytes .NewBuffer ([]byte (`` )) 112 113_ ,err := ReadAnalysis (buff ) 114 115assert .Equal (t ,"unexpected end of JSON input" ,err .Error ()) 116} 117 118func TestReadAnalysisWithEmptyDict (t * testing.T ) { 119buff := bytes .NewBuffer ([]byte (`{}` )) 120 121_ ,err := ReadAnalysis (buff ) 122 123assert .Equal (t ,"JSON file does not contain top level array" ,err .Error ()) 124} 125 126func TestReadFromBrokenInput (t * testing.T ) { 127_ ,err := ReadAnalysis (& BrokenInput {}) 128 129assert .Equal (t ,"IO error" ,err .Error ()) 130} 131 132func TestReadAnalysisWithEmptyArray (t * testing.T ) { 133buff := bytes .NewBuffer ([]byte (`[]` )) 134 135_ ,err := ReadAnalysis (buff ) 136 137assert .Equal (t ,"top level array must have at least 4 items" ,err .Error ()) 138} 139 140func TestReadAnalysisWithWrongContent (t * testing.T ) { 141buff := bytes .NewBuffer ([]byte (`[1,2,3,4]` )) 142 143_ ,err := ReadAnalysis (buff ) 144 145assert .Equal (t ,"array of maps not found in the top level array on 4th position" ,err .Error ()) 146} 147 148func TestReadAnalysisWithEmptyContent (t * testing.T ) { 149buff := bytes .NewBuffer ([]byte (`[1,2,3,[]]` )) 150 151_ ,err := ReadAnalysis (buff ) 152 153assert .Equal (t ,"directory array is empty" ,err .Error ()) 154} 155 156func TestReadAnalysisWithEmptyDirContent (t * testing.T ) { 157buff := bytes .NewBuffer ([]byte (`[1,2,3,[{}]]` )) 158 159_ ,err := ReadAnalysis (buff ) 160 161assert .Equal (t ,"directory name is not a string" ,err .Error ()) 162} 163 164func TestReadAnalysisWithWrongDirItem (t * testing.T ) { 165buff := bytes .NewBuffer ([]byte (`[1,2,3,[1, 2, 3]]` )) 166 167_ ,err := ReadAnalysis (buff ) 168 169assert .Equal (t ,"directory item is not a map" ,err .Error ()) 170} 171 172func TestReadAnalysisWithWrongName (t * testing.T ) { 173buff := bytes .NewBuffer ([]byte (`[1,2,3,[{"name":"/"},{"name":42}]]` )) 174 175_ ,err := ReadAnalysis (buff ) 176 177assert .Equal (t ,"file name is not a string" ,err .Error ()) 178} 179 180func TestReadAnalysisWithWrongSubdirItem (t * testing.T ) { 181buff := bytes .NewBuffer ([]byte (`[1,2,3,[{"name":"xxx"}, [1,2,3]]]` )) 182 183_ ,err := ReadAnalysis (buff ) 184 185assert .Equal (t ,"directory item is not a map" ,err .Error ()) 186} 187 188type BrokenInput struct {} 189 190func (i * BrokenInput )Read (p []byte ) (n int ,err error ) { 191return 0 ,errors .New ("IO error" ) 192}