Skip to content

Commit b530f29

Browse files
committed
fix(cluster/audit): compatible with old second based ts
1 parent 871ae58 commit b530f29

2 files changed

Lines changed: 103 additions & 8 deletions

File tree

pkg/cluster/audit/audit.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ func ShowAuditList(dir string) error {
5656
if fi.IsDir() {
5757
continue
5858
}
59-
ts, err := base52.Decode(fi.Name())
59+
t, err := decodeAuditID(fi.Name())
6060
if err != nil {
6161
continue
6262
}
63-
t := time.Unix(ts/1e9, 0)
6463
cmd, err := firstLine(fi.Name())
6564
if err != nil {
6665
continue
@@ -93,7 +92,7 @@ func ShowAuditLog(dir string, auditID string) error {
9392
return errors.Errorf("cannot find the audit log '%s'", auditID)
9493
}
9594

96-
ts, err := base52.Decode(auditID)
95+
t, err := decodeAuditID(auditID)
9796
if err != nil {
9897
return errors.Annotatef(err, "unrecognized audit id '%s'", auditID)
9998
}
@@ -103,10 +102,23 @@ func ShowAuditLog(dir string, auditID string) error {
103102
return errors.Trace(err)
104103
}
105104

106-
t := time.Unix(ts/1e9, 0)
107105
hint := fmt.Sprintf("- OPERATION TIME: %s -", t.Format("2006-01-02T15:04:05"))
108106
line := strings.Repeat("-", len(hint))
109107
_, _ = os.Stdout.WriteString(color.MagentaString("%s\n%s\n%s\n", line, hint, line))
110108
_, _ = os.Stdout.Write(content)
111109
return nil
112110
}
111+
112+
//decodeAuditID decodes the auditID to unix timestamp
113+
func decodeAuditID(auditID string) (time.Time, error) {
114+
ts, err := base52.Decode(auditID)
115+
if err != nil {
116+
return time.Time{}, err
117+
}
118+
// compatible with old second based ts
119+
if ts>>32 > 0 {
120+
ts = ts / 1e9
121+
}
122+
t := time.Unix(ts, 0)
123+
return t, nil
124+
}

pkg/cluster/audit/audit_test.go

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
package audit
1515

1616
import (
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+
2633
var _ = Suite(&testAuditSuite{})
2734

2835
type 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+
4461
func (s *testAuditSuite) TearDownSuite(c *C) {
45-
_ = os.RemoveAll(auditDir())
62+
_ = os.RemoveAll(auditDir()) // path.Join(currentDir(), "testdata"))
4663
}
4764

4865
func (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

Comments
 (0)