@@ -18,96 +18,106 @@ package operations
1818
1919import (
2020 "errors"
21- "testing"
2221
23- "github.com/brahma-adshonor/gohook "
22+ "github.com/agiledragon/gomonkey/v2 "
2423 "github.com/fluid-cloudnative/fluid/pkg/utils/fake"
24+
25+ . "github.com/onsi/ginkgo"
26+ . "github.com/onsi/gomega"
2527)
2628
27- func TestAlluxioFileUtils_CachedState (t * testing.T ) {
28- ExecCommon := func (a AlluxioFileUtils , command []string , verbose bool ) (stdout string , stderr string , err error ) {
29- return "Alluxio cluster summary: \n Master Address: 192.168.0.193:20009 \n Used Capacity: 0B\n " , "" , nil
30- }
31- ExecErr := func (a AlluxioFileUtils , command []string , verbose bool ) (stdout string , stderr string , err error ) {
32- return "" , "" , errors .New ("fail to run the command" )
33- }
34- wrappedUnhookExec := func () {
35- err := gohook .UnHook (AlluxioFileUtils .exec )
36- if err != nil {
37- t .Fatal (err .Error ())
29+
30+ var _ = Describe ("AlluxioFileUtils.CachedState" , func () {
31+ var (
32+ a * AlluxioFileUtils
33+ patch * gomonkey.Patches
34+ )
35+
36+ BeforeEach (func () {
37+ a = & AlluxioFileUtils {log : fake .NullLogger ()}
38+ })
39+
40+ AfterEach (func () {
41+ if patch != nil {
42+ patch .Reset ()
3843 }
39- }
40-
41- err := gohook . Hook ( AlluxioFileUtils . exec , ExecErr , nil )
42- if err != nil {
43- t . Fatal ( err . Error ())
44- }
45- a := & AlluxioFileUtils { log : fake . NullLogger () }
46- _ , err = a . CachedState ( )
47- if err == nil {
48- t . Error ( "check failure, want err, got nil" )
49- }
50- wrappedUnhookExec ( )
51-
52- err = gohook . Hook ( AlluxioFileUtils . exec , ExecCommon , nil )
53- if err != nil {
54- t . Fatal ( err . Error ())
55- }
56- cached , err := a . CachedState ()
57- if err != nil {
58- t . Errorf ( "check failure, want nil, got err: %v" , err )
59- }
60- if cached != 0 {
61- t . Errorf ( "check failure, want 0, got: %d" , cached )
62- }
63- wrappedUnhookExec ( )
64- }
65-
66- func TestAlluxioFIlUtils_CleanCache ( t * testing. T ) {
67- ExecCommonUbuntu := func ( a AlluxioFileUtils , command [] string , verbose bool ) ( stdout string , stderr string , err error ) {
68- return "Ubuntu" , "" , nil
69- }
70- ExecCommonAlpine := func ( a AlluxioFileUtils , command [] string , verbose bool ) ( stdout string , stderr string , err error ) {
71- return "Alpine" , "" , nil
72- }
73- ExecErr := func (a AlluxioFileUtils , command [] string , verbose bool ) ( stdout string , stderr string , err error ) {
74- return "" , "" , errors . New ( "fail to run the command" )
75- }
76- wrappedUnhookExec := func () {
77- err := gohook . UnHook ( AlluxioFileUtils . exec )
78- if err != nil {
79- t . Fatal ( err . Error () )
44+ })
45+
46+ Context ( "when exec returns an error" , func () {
47+ It ( "should return an error" , func () {
48+ ExecErr := func ( command [] string , verbose bool ) ( stdout string , stderr string , err error ) {
49+ return "" , "" , errors . New ( "fail to run the command" )
50+ }
51+ patch = gomonkey . ApplyPrivateMethod ( a , "exec" , ExecErr )
52+
53+ _ , err := a . CachedState ( )
54+ Expect ( err ). To ( HaveOccurred ())
55+ } )
56+ })
57+
58+ Context ( "when exec executes successfully" , func () {
59+ It ( "should not return an error" , func () {
60+ ExecCommon := func ( command [] string , verbose bool ) ( stdout string , stderr string , err error ) {
61+ return "Alluxio cluster summary: \n Master Address: 192.168.0.193:20009 \n Used Capacity: 0B \n " , "" , nil
62+ }
63+ patch = gomonkey . ApplyPrivateMethod ( a , "exec" , ExecCommon )
64+
65+ cached , err := a . CachedState ()
66+ Expect ( err ). NotTo ( HaveOccurred () )
67+ Expect ( cached ). To ( Equal ( int64 ( 0 )))
68+ } )
69+ })
70+ })
71+
72+ var _ = Describe ( " AlluxioFileUtils.CleanCache" , func ( ) {
73+ var (
74+ a * AlluxioFileUtils
75+ patch * gomonkey. Patches
76+ )
77+
78+ BeforeEach ( func () {
79+ a = & AlluxioFileUtils { log : fake . NullLogger ()}
80+ })
81+
82+ AfterEach ( func () {
83+ if patch != nil {
84+ patch . Reset ( )
8085 }
81- }
82-
83- err := gohook .Hook (AlluxioFileUtils .exec , ExecErr , nil )
84- if err != nil {
85- t .Fatal (err .Error ())
86- }
87- a := & AlluxioFileUtils {log : fake .NullLogger ()}
88- err = a .CleanCache ("/" , 30 )
89- if err == nil {
90- t .Error ("check failure, want err, got nil" )
91- }
92- wrappedUnhookExec ()
93-
94- err = gohook .Hook (AlluxioFileUtils .exec , ExecCommonUbuntu , nil )
95- if err != nil {
96- t .Fatal (err .Error ())
97- }
98- err = a .CleanCache ("/" , 30 )
99- if err != nil {
100- t .Errorf ("check failure, want nil, got err: %v" , err )
101- }
102- wrappedUnhookExec ()
103-
104- err = gohook .Hook (AlluxioFileUtils .exec , ExecCommonAlpine , nil )
105- if err != nil {
106- t .Fatal (err .Error ())
107- }
108- err = a .CleanCache ("/" , 30 )
109- if err != nil {
110- t .Errorf ("check failure, want nil, got err: %v" , err )
111- }
112- wrappedUnhookExec ()
113- }
86+ })
87+
88+ Context ("when exec returns an error" , func () {
89+ It ("should return an error" , func () {
90+ ExecErr := func (command []string , verbose bool ) (stdout string , stderr string , err error ) {
91+ return "" , "" , errors .New ("fail to run the command" )
92+ }
93+ patch = gomonkey .ApplyPrivateMethod (a , "exec" , ExecErr )
94+
95+ err := a .CleanCache ("/" , 30 )
96+ Expect (err ).To (HaveOccurred ())
97+ })
98+ })
99+
100+ Context ("when exec executes successfully on Ubuntu" , func () {
101+ It ("should not return an error" , func () {
102+ ExecCommonUbuntu := func (command []string , verbose bool ) (stdout string , stderr string , err error ) {
103+ return "Ubuntu" , "" , nil
104+ }
105+ patch = gomonkey .ApplyPrivateMethod (a , "exec" , ExecCommonUbuntu )
106+
107+ err := a .CleanCache ("/" , 30 )
108+ Expect (err ).NotTo (HaveOccurred ())
109+ })
110+ })
111+
112+ Context ("when exec executes successfully on Alpine" , func () {
113+ It ("should not return an error" , func () {
114+ ExecCommonAlpine := func (command []string , verbose bool ) (stdout string , stderr string , err error ) {
115+ return "Alpine" , "" , nil
116+ }
117+ patch = gomonkey .ApplyPrivateMethod (a , "exec" , ExecCommonAlpine )
118+
119+ err := a .CleanCache ("/" , 30 )
120+ Expect (err ).NotTo (HaveOccurred ())
121+ })
122+ })
123+ })
0 commit comments