clean_test.go 3.85 KB
Newer Older
“李磊”'s avatar
“李磊” committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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)
			}
		})
	}
}