Skip to content

Commit

Permalink
cluster/audit: audit log in seconds maybe leads to file overwriten (#879
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jsvisa authored Nov 3, 2020
1 parent 11535d2 commit 1a295b4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pkg/cluster/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ShowAuditList(dir string) error {
if err != nil {
continue
}
t := time.Unix(ts, 0)
t := time.Unix(ts/1e9, 0)
cmd, err := firstLine(fi.Name())
if err != nil {
continue
Expand All @@ -82,7 +82,7 @@ func ShowAuditList(dir string) error {

// OutputAuditLog outputs audit log.
func OutputAuditLog(dir string, data []byte) error {
fname := filepath.Join(dir, base52.Encode(time.Now().Unix()))
fname := filepath.Join(dir, base52.Encode(time.Now().UnixNano()))
return ioutil.WriteFile(fname, data, 0644)
}

Expand All @@ -103,7 +103,7 @@ func ShowAuditLog(dir string, auditID string) error {
return errors.Trace(err)
}

t := time.Unix(ts, 0)
t := time.Unix(ts/1e9, 0)
hint := fmt.Sprintf("- OPERATION TIME: %s -", t.Format("2006-01-02T15:04:05"))
line := strings.Repeat("-", len(hint))
_, _ = os.Stdout.WriteString(color.MagentaString("%s\n%s\n%s\n", line, hint, line))
Expand Down
65 changes: 65 additions & 0 deletions pkg/cluster/audit/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package audit

import (
"os"
"path"
"path/filepath"
"runtime"

. "github.com/pingcap/check"
"golang.org/x/sync/errgroup"
)

var _ = Suite(&testAuditSuite{})

type testAuditSuite struct{}

func currentDir() string {
_, file, _, _ := runtime.Caller(0)
return filepath.Dir(file)
}

func auditDir() string {
return path.Join(currentDir(), "testdata", "audit")
}

func (s *testAuditSuite) SetUpSuite(c *C) {
_ = os.RemoveAll(auditDir())
_ = os.MkdirAll(auditDir(), 0777)
}

func (s *testAuditSuite) TearDownSuite(c *C) {
_ = os.RemoveAll(auditDir())
}

func (s *testAuditSuite) TestOutputAuditLog(c *C) {
dir := auditDir()
var g errgroup.Group
for i := 0; i < 20; i++ {
g.Go(func() error { return OutputAuditLog(dir, []byte("audit log")) })
}
err := g.Wait()
c.Assert(err, IsNil)

var paths []string
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// simply filter the not relate files.
paths = append(paths, path)
return nil
})
c.Assert(err, IsNil)
c.Assert(len(paths), Equals, 20)
}
2 changes: 1 addition & 1 deletion tests/tiup-dm/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function tiup-dm() {
mkdir -p ~/.tiup/bin && cp -f ./root.json ~/.tiup/bin/
# echo "in function"
if [ -f "./bin/tiup-dm.test" ]; then
./bin/tiup-dm.test -test.coverprofile=./cover/cov.itest-$(date +'%s')-$RANDOM.out __DEVEL--i-heard-you-like-tests "$@"
./bin/tiup-dm.test -test.coverprofile=./cover/cov.itest-$(date +'%s')-$RANDOM.out __DEVEL--i-heard-you-like-tests "$@"
else
../../bin/tiup-dm "$@"
fi
Expand Down

0 comments on commit 1a295b4

Please sign in to comment.