Skip to content

Commit

Permalink
cluster/audit: add audit unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa committed Nov 2, 2020
1 parent 4934f34 commit 3344d2f
Showing 1 changed file with 65 additions and 0 deletions.
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)
}

0 comments on commit 3344d2f

Please sign in to comment.