Skip to content

Commit 41affbf

Browse files
committed
Add comments to TestQueryCacheStatus in pkg\ddc\alluxio\cache_test.go.
Signed-off-by: pullshot810 <pullshot810@outlook.com>
1 parent 1178175 commit 41affbf

1 file changed

Lines changed: 142 additions & 120 deletions

File tree

pkg/ddc/alluxio/cache_test.go

Lines changed: 142 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -36,127 +36,149 @@ import (
3636
"k8s.io/utils/ptr"
3737
)
3838

39+
// TestQueryCacheStatus is a unit test for the queryCacheStatus function.
40+
// It verifies the correctness of the function under different dataset conditions.
3941
func TestQueryCacheStatus(t *testing.T) {
40-
Convey("test queryCacheStatus ", t, func() {
41-
Convey("with dataset UFSTotal is not empty ", func() {
42-
var engine *AlluxioEngine
43-
patch1 := ApplyMethod(reflect.TypeOf(engine), "GetReportSummary",
44-
func(_ *AlluxioEngine) (string, error) {
45-
summary := mockAlluxioReportSummary()
46-
return summary, nil
47-
})
48-
defer patch1.Reset()
49-
50-
patch2 := ApplyFunc(utils.GetDataset,
51-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
52-
d := &datav1alpha1.Dataset{
53-
Status: datav1alpha1.DatasetStatus{
54-
UfsTotal: "52.18MiB",
55-
},
56-
}
57-
return d, nil
58-
})
59-
defer patch2.Reset()
60-
61-
patch3 := ApplyMethod(reflect.TypeOf(engine), "GetCacheHitStates",
62-
func(_ *AlluxioEngine) cacheHitStates {
63-
return cacheHitStates{
64-
bytesReadLocal: 20310917,
65-
bytesReadUfsAll: 32243712,
66-
}
67-
})
68-
defer patch3.Reset()
69-
70-
e := &AlluxioEngine{}
71-
got, err := e.queryCacheStatus()
72-
want := cacheStates{
73-
cacheCapacity: "19.07MiB",
74-
cached: "0.00B",
75-
cachedPercentage: "0.0%",
76-
cacheHitStates: cacheHitStates{
77-
bytesReadLocal: 20310917,
78-
bytesReadUfsAll: 32243712,
79-
},
80-
}
81-
82-
So(got, ShouldResemble, want)
83-
So(err, ShouldEqual, nil)
84-
})
85-
86-
Convey("with dataset UFSTotal is: [Calculating]", func() {
87-
var engine *AlluxioEngine
88-
patch1 := ApplyMethod(reflect.TypeOf(engine), "GetReportSummary",
89-
func(_ *AlluxioEngine) (string, error) {
90-
summary := mockAlluxioReportSummary()
91-
return summary, nil
92-
})
93-
defer patch1.Reset()
94-
95-
patch2 := ApplyFunc(utils.GetDataset,
96-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
97-
d := &datav1alpha1.Dataset{
98-
Status: datav1alpha1.DatasetStatus{
99-
UfsTotal: "[Calculating]",
100-
},
101-
}
102-
return d, nil
103-
})
104-
defer patch2.Reset()
105-
106-
patch3 := ApplyMethod(reflect.TypeOf(engine), "GetCacheHitStates",
107-
func(_ *AlluxioEngine) cacheHitStates {
108-
return cacheHitStates{}
109-
})
110-
defer patch3.Reset()
111-
112-
e := &AlluxioEngine{}
113-
got, err := e.queryCacheStatus()
114-
want := cacheStates{
115-
cacheCapacity: "19.07MiB",
116-
cached: "0.00B",
117-
}
118-
119-
So(got, ShouldResemble, want)
120-
So(err, ShouldEqual, nil)
121-
})
122-
123-
Convey("with dataset UFSTotal is empty", func() {
124-
var engine *AlluxioEngine
125-
patch1 := ApplyMethod(reflect.TypeOf(engine), "GetReportSummary",
126-
func(_ *AlluxioEngine) (string, error) {
127-
summary := mockAlluxioReportSummary()
128-
return summary, nil
129-
})
130-
defer patch1.Reset()
131-
132-
patch2 := ApplyFunc(utils.GetDataset,
133-
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
134-
d := &datav1alpha1.Dataset{
135-
Status: datav1alpha1.DatasetStatus{
136-
UfsTotal: "",
137-
},
138-
}
139-
return d, nil
140-
})
141-
defer patch2.Reset()
142-
143-
patch3 := ApplyMethod(reflect.TypeOf(engine), "GetCacheHitStates",
144-
func(_ *AlluxioEngine) cacheHitStates {
145-
return cacheHitStates{}
146-
})
147-
defer patch3.Reset()
148-
149-
e := &AlluxioEngine{}
150-
got, err := e.queryCacheStatus()
151-
want := cacheStates{
152-
cacheCapacity: "19.07MiB",
153-
cached: "0.00B",
154-
}
155-
156-
So(got, ShouldResemble, want)
157-
So(err, ShouldEqual, nil)
158-
})
159-
})
42+
// Define a test scenario using Convey
43+
Convey("test queryCacheStatus ", t, func() {
44+
45+
// Scenario: Dataset UFSTotal is not empty
46+
Convey("with dataset UFSTotal is not empty ", func() {
47+
var engine *AlluxioEngine
48+
49+
// Mock GetReportSummary method
50+
patch1 := ApplyMethod(reflect.TypeOf(engine), "GetReportSummary",
51+
func(_ *AlluxioEngine) (string, error) {
52+
summary := mockAlluxioReportSummary()
53+
return summary, nil
54+
})
55+
defer patch1.Reset()
56+
57+
// Mock GetDataset function to return a dataset with UFSTotal
58+
patch2 := ApplyFunc(utils.GetDataset,
59+
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
60+
d := &datav1alpha1.Dataset{
61+
Status: datav1alpha1.DatasetStatus{
62+
UfsTotal: "52.18MiB",
63+
},
64+
}
65+
return d, nil
66+
})
67+
defer patch2.Reset()
68+
69+
// Mock GetCacheHitStates method
70+
patch3 := ApplyMethod(reflect.TypeOf(engine), "GetCacheHitStates",
71+
func(_ *AlluxioEngine) cacheHitStates {
72+
return cacheHitStates{
73+
bytesReadLocal: 20310917,
74+
bytesReadUfsAll: 32243712,
75+
}
76+
})
77+
defer patch3.Reset()
78+
79+
e := &AlluxioEngine{}
80+
got, err := e.queryCacheStatus()
81+
want := cacheStates{
82+
cacheCapacity: "19.07MiB",
83+
cached: "0.00B",
84+
cachedPercentage: "0.0%",
85+
cacheHitStates: cacheHitStates{
86+
bytesReadLocal: 20310917,
87+
bytesReadUfsAll: 32243712,
88+
},
89+
}
90+
91+
// Verify the expected results
92+
So(got, ShouldResemble, want)
93+
So(err, ShouldEqual, nil)
94+
})
95+
96+
// Scenario: Dataset UFSTotal is [Calculating]
97+
Convey("with dataset UFSTotal is: [Calculating]", func() {
98+
var engine *AlluxioEngine
99+
100+
// Mock GetReportSummary method
101+
patch1 := ApplyMethod(reflect.TypeOf(engine), "GetReportSummary",
102+
func(_ *AlluxioEngine) (string, error) {
103+
summary := mockAlluxioReportSummary()
104+
return summary, nil
105+
})
106+
defer patch1.Reset()
107+
108+
// Mock GetDataset function to return a dataset with UFSTotal as [Calculating]
109+
patch2 := ApplyFunc(utils.GetDataset,
110+
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
111+
d := &datav1alpha1.Dataset{
112+
Status: datav1alpha1.DatasetStatus{
113+
UfsTotal: "[Calculating]",
114+
},
115+
}
116+
return d, nil
117+
})
118+
defer patch2.Reset()
119+
120+
// Mock GetCacheHitStates method
121+
patch3 := ApplyMethod(reflect.TypeOf(engine), "GetCacheHitStates",
122+
func(_ *AlluxioEngine) cacheHitStates {
123+
return cacheHitStates{}
124+
})
125+
defer patch3.Reset()
126+
127+
e := &AlluxioEngine{}
128+
got, err := e.queryCacheStatus()
129+
want := cacheStates{
130+
cacheCapacity: "19.07MiB",
131+
cached: "0.00B",
132+
}
133+
134+
// Verify the expected results
135+
So(got, ShouldResemble, want)
136+
So(err, ShouldEqual, nil)
137+
})
138+
139+
// Scenario: Dataset UFSTotal is empty
140+
Convey("with dataset UFSTotal is empty", func() {
141+
var engine *AlluxioEngine
142+
143+
// Mock GetReportSummary method
144+
patch1 := ApplyMethod(reflect.TypeOf(engine), "GetReportSummary",
145+
func(_ *AlluxioEngine) (string, error) {
146+
summary := mockAlluxioReportSummary()
147+
return summary, nil
148+
})
149+
defer patch1.Reset()
150+
151+
// Mock GetDataset function to return a dataset with an empty UFSTotal
152+
patch2 := ApplyFunc(utils.GetDataset,
153+
func(_ client.Client, _ string, _ string) (*datav1alpha1.Dataset, error) {
154+
d := &datav1alpha1.Dataset{
155+
Status: datav1alpha1.DatasetStatus{
156+
UfsTotal: "",
157+
},
158+
}
159+
return d, nil
160+
})
161+
defer patch2.Reset()
162+
163+
// Mock GetCacheHitStates method
164+
patch3 := ApplyMethod(reflect.TypeOf(engine), "GetCacheHitStates",
165+
func(_ *AlluxioEngine) cacheHitStates {
166+
return cacheHitStates{}
167+
})
168+
defer patch3.Reset()
169+
170+
e := &AlluxioEngine{}
171+
got, err := e.queryCacheStatus()
172+
want := cacheStates{
173+
cacheCapacity: "19.07MiB",
174+
cached: "0.00B",
175+
}
176+
177+
// Verify the expected results
178+
So(got, ShouldResemble, want)
179+
So(err, ShouldEqual, nil)
180+
})
181+
})
160182
}
161183

162184
func TestGetCacheHitStates(t *testing.T) {

0 commit comments

Comments
 (0)