1414package audit
1515
1616import (
17+ "fmt"
18+ "io/ioutil"
1719 "os"
1820 "path"
1921 "path/filepath"
2022 "runtime"
23+ "testing"
24+ "time"
2125
2226 . "github.com/pingcap/check"
27+ "github.com/pingcap/tiup/pkg/base52"
2328 "golang.org/x/sync/errgroup"
2429)
2530
31+ func Test (t * testing.T ) { TestingT (t ) }
32+
2633var _ = Suite (& testAuditSuite {})
2734
2835type testAuditSuite struct {}
@@ -36,17 +43,29 @@ func auditDir() string {
3643 return path .Join (currentDir (), "testdata" , "audit" )
3744}
3845
39- func ( s * testAuditSuite ) SetUpSuite ( c * C ) {
46+ func resetDir ( ) {
4047 _ = os .RemoveAll (auditDir ())
4148 _ = os .MkdirAll (auditDir (), 0777 )
4249}
4350
51+ func readFakeStdout (f * os.File ) string {
52+ _ , _ = f .Seek (0 , 0 )
53+ read , _ := ioutil .ReadAll (f )
54+ return string (read )
55+ }
56+
57+ func (s * testAuditSuite ) SetUpSuite (c * C ) {
58+ resetDir ()
59+ }
60+
4461func (s * testAuditSuite ) TearDownSuite (c * C ) {
45- _ = os .RemoveAll (auditDir ())
62+ _ = os .RemoveAll (auditDir ()) // path.Join(currentDir(), "testdata"))
4663}
4764
4865func (s * testAuditSuite ) TestOutputAuditLog (c * C ) {
4966 dir := auditDir ()
67+ resetDir ()
68+
5069 var g errgroup.Group
5170 for i := 0 ; i < 20 ; i ++ {
5271 g .Go (func () error { return OutputAuditLog (dir , []byte ("audit log" )) })
@@ -56,10 +75,74 @@ func (s *testAuditSuite) TestOutputAuditLog(c *C) {
5675
5776 var paths []string
5877 err = filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
59- // simply filter the not relate files.
60- paths = append (paths , path )
78+ if ! info .IsDir () {
79+ paths = append (paths , path )
80+ }
6181 return nil
6282 })
6383 c .Assert (err , IsNil )
6484 c .Assert (len (paths ), Equals , 20 )
6585}
86+
87+ func (s * testAuditSuite ) TestShowAuditLog (c * C ) {
88+ dir := auditDir ()
89+ resetDir ()
90+
91+ originStdout := os .Stdout
92+ defer func () {
93+ os .Stdout = originStdout
94+ }()
95+
96+ fakeStdout := path .Join (currentDir (), "fake-stdout" )
97+ defer os .Remove (fakeStdout )
98+
99+ openStdout := func () * os.File {
100+ _ = os .Remove (fakeStdout )
101+ f , err := os .OpenFile (fakeStdout , os .O_CREATE | os .O_RDWR , 0644 )
102+ c .Assert (err , IsNil )
103+ os .Stdout = f
104+ return f
105+ }
106+
107+ second := int64 (1604413577 )
108+ nanoSecond := int64 (1604413624836105381 )
109+
110+ fname := filepath .Join (dir , base52 .Encode (second ))
111+ c .Assert (ioutil .WriteFile (fname , []byte ("test with second" ), 0644 ), IsNil )
112+ fname = filepath .Join (dir , base52 .Encode (nanoSecond ))
113+ c .Assert (ioutil .WriteFile (fname , []byte ("test with nanosecond" ), 0644 ), IsNil )
114+
115+ f := openStdout ()
116+ c .Assert (ShowAuditList (dir ), IsNil )
117+
118+ c .Assert (readFakeStdout (f ), Equals , fmt .Sprintf (`ID Time Command
119+ -- ---- -------
120+ ftmpqzww84Q %s test with nanosecond
121+ 4F7ZTL %s test with second
122+ ` ,
123+ time .Unix (nanoSecond / 1e9 , 0 ).Format (time .RFC3339 ),
124+ time .Unix (second , 0 ).Format (time .RFC3339 ),
125+ ))
126+ f .Close ()
127+
128+ f = openStdout ()
129+ c .Assert (ShowAuditLog (dir , "4F7ZTL" ), IsNil )
130+ c .Assert (readFakeStdout (f ), Equals , fmt .Sprintf (`---------------------------------------
131+ - OPERATION TIME: %s -
132+ ---------------------------------------
133+ test with second` ,
134+ time .Unix (second , 0 ).Format ("2006-01-02T15:04:05" ),
135+ ))
136+
137+ f .Close ()
138+
139+ f = openStdout ()
140+ c .Assert (ShowAuditLog (dir , "ftmpqzww84Q" ), IsNil )
141+ c .Assert (readFakeStdout (f ), Equals , fmt .Sprintf (`---------------------------------------
142+ - OPERATION TIME: %s -
143+ ---------------------------------------
144+ test with nanosecond` ,
145+ time .Unix (nanoSecond / 1e9 , 0 ).Format ("2006-01-02T15:04:05" ),
146+ ))
147+ f .Close ()
148+ }
0 commit comments