package auto import ( "bufio" "fmt" "io/ioutil" "os" "testing" "time" ) func (p *auto) Dirs(t *testing.T) (int64, int) { dirEntry, err := ioutil.ReadDir(p.config.PProfProfilePath) if err != nil { t.Fatal(err) return 0, 0 } var size int64 for _, entry := range dirEntry { size = size + entry.Size() } return size, len(dirEntry) } func TestAutoExpirationClean(t *testing.T) { dir := BuildFile(t) defer os.RemoveAll(dir) p := NewAutoPProf(Config{ PProfProfilePath: dir, }) p.init() size, num := p.Dirs(t) t.Logf("size %d,num:%d", size, num) type fields struct { config Config fixedQuotient int perHourQuotient []int detectInterval time.Duration recordSensitivity float64 maxDiskUsage float64 periodRecordCount uint } tests := []struct { name string fields fields want int }{ { name: "test", fields: fields{ config: p.config, fixedQuotient: p.fixedQuotient, perHourQuotient: p.perHourQuotient, detectInterval: p.detectInterval, recordSensitivity: p.recordSensitivity, maxDiskUsage: p.maxDiskUsage, periodRecordCount: p.periodRecordCount, }, want: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &auto{ config: tt.fields.config, fixedQuotient: tt.fields.fixedQuotient, perHourQuotient: tt.fields.perHourQuotient, detectInterval: tt.fields.detectInterval, recordSensitivity: tt.fields.recordSensitivity, maxDiskUsage: tt.fields.maxDiskUsage, periodRecordCount: tt.fields.periodRecordCount, } p.cleanByDate() size, num := p.Dirs(t) t.Logf("size %d,num:%d", size, num) if num != tt.want { t.Error("not the desired result,want result is", tt.want) } }) } } func BuildFile(t *testing.T) string { dir, err := ioutil.TempDir("/usr", "testClean") if err != nil { t.Error(err) return "" } fmt.Println("tempDir", dir) for i := 0; i < 100; i++ { f, err := ioutil.TempFile(dir, "20221227-000820-") if err != nil { t.Error(err) return "" } write := bufio.NewWriter(f) str := "aaaaa" for j := 0; j < 209716; j++ { write.WriteString(str) } write.Flush() err = f.Close() if err != nil { t.Error(err) return "" } } return dir } func TestAutoOverDiskLimitCapacityClean(t *testing.T) { dir := BuildFile(t) defer os.RemoveAll(dir) p := NewAutoPProf(Config{ PProfProfilePath: dir, MaxDiskUsage: "50MB", }) p.init() size, num := p.Dirs(t) t.Logf("size %d,num:%d", size, num) type fields struct { config Config fixedQuotient int perHourQuotient []int detectInterval time.Duration recordSensitivity float64 maxDiskUsage float64 periodRecordCount uint } tests := []struct { name string fields fields want int want1 float64 }{ { name: "test", fields: fields{ config: p.config, fixedQuotient: p.fixedQuotient, perHourQuotient: p.perHourQuotient, detectInterval: p.detectInterval, recordSensitivity: p.recordSensitivity, maxDiskUsage: p.maxDiskUsage, periodRecordCount: p.periodRecordCount, }, want: 50, want1: p.maxDiskUsage, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &auto{ config: tt.fields.config, fixedQuotient: tt.fields.fixedQuotient, perHourQuotient: tt.fields.perHourQuotient, detectInterval: tt.fields.detectInterval, recordSensitivity: tt.fields.recordSensitivity, maxDiskUsage: tt.fields.maxDiskUsage, periodRecordCount: tt.fields.periodRecordCount, } p.cleanBySpace() size, num := p.Dirs(t) t.Logf("size %d,num:%d", size, num) if int(size) < tt.want || float64(num) < tt.want1 { t.Error("not the desired result,err result is,want result is", size, num, tt.want, tt.want1) } }) } }