parent
b8ba625dcc
commit
d3190b01e6
11 changed files with 45 additions and 368 deletions
@ -1,69 +0,0 @@ |
||||
// Copyright (c) 2022 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package logging |
||||
|
||||
import ( |
||||
"io/ioutil" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
// TestClearLogs tests that cearLogs removes only bridge old log files keeping last three of them.
|
||||
func TestClearLogs(t *testing.T) { |
||||
dir, err := ioutil.TempDir("", "clear-logs-test") |
||||
require.NoError(t, err) |
||||
|
||||
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "other.log"), []byte("Hello"), 0755)) |
||||
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "v1_10.log"), []byte("Hello"), 0755)) |
||||
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "v1_11.log"), []byte("Hello"), 0755)) |
||||
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "v2_12.log"), []byte("Hello"), 0755)) |
||||
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "v2_13.log"), []byte("Hello"), 0755)) |
||||
|
||||
require.NoError(t, clearLogs(dir, 3, 0)) |
||||
checkFileNames(t, dir, []string{ |
||||
"other.log", |
||||
"v1_11.log", |
||||
"v2_12.log", |
||||
"v2_13.log", |
||||
}) |
||||
} |
||||
|
||||
func checkFileNames(t *testing.T, dir string, expectedFileNames []string) { |
||||
fileNames := getFileNames(t, dir) |
||||
require.Equal(t, expectedFileNames, fileNames) |
||||
} |
||||
|
||||
func getFileNames(t *testing.T, dir string) []string { |
||||
files, err := ioutil.ReadDir(dir) |
||||
require.NoError(t, err) |
||||
|
||||
fileNames := []string{} |
||||
for _, file := range files { |
||||
fileNames = append(fileNames, file.Name()) |
||||
if file.IsDir() { |
||||
subDir := filepath.Join(dir, file.Name()) |
||||
subFileNames := getFileNames(t, subDir) |
||||
for _, subFileName := range subFileNames { |
||||
fileNames = append(fileNames, file.Name()+"/"+subFileName) |
||||
} |
||||
} |
||||
} |
||||
return fileNames |
||||
} |
||||
@ -1,75 +0,0 @@ |
||||
// Copyright (c) 2022 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package logging |
||||
|
||||
import "io" |
||||
|
||||
type Rotator struct { |
||||
getFile FileProvider |
||||
wc io.WriteCloser |
||||
size int |
||||
maxSize int |
||||
} |
||||
|
||||
type FileProvider func() (io.WriteCloser, error) |
||||
|
||||
func NewRotator(maxSize int, getFile FileProvider) (*Rotator, error) { |
||||
r := &Rotator{ |
||||
getFile: getFile, |
||||
maxSize: maxSize, |
||||
} |
||||
|
||||
if err := r.rotate(); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return r, nil |
||||
} |
||||
|
||||
func (r *Rotator) Write(p []byte) (int, error) { |
||||
if r.size+len(p) > r.maxSize { |
||||
if err := r.rotate(); err != nil { |
||||
return 0, err |
||||
} |
||||
} |
||||
|
||||
n, err := r.wc.Write(p) |
||||
if err != nil { |
||||
return n, err |
||||
} |
||||
|
||||
r.size += n |
||||
|
||||
return n, nil |
||||
} |
||||
|
||||
func (r *Rotator) rotate() error { |
||||
if r.wc != nil { |
||||
_ = r.wc.Close() |
||||
} |
||||
|
||||
wc, err := r.getFile() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
r.wc = wc |
||||
r.size = 0 |
||||
|
||||
return nil |
||||
} |
||||
@ -1,131 +0,0 @@ |
||||
// Copyright (c) 2022 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package logging |
||||
|
||||
import ( |
||||
"bytes" |
||||
"io" |
||||
"io/ioutil" |
||||
"os" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
type WriteCloser struct { |
||||
bytes.Buffer |
||||
} |
||||
|
||||
func (c *WriteCloser) Close() error { |
||||
return nil |
||||
} |
||||
|
||||
func TestRotator(t *testing.T) { |
||||
n := 0 |
||||
|
||||
getFile := func() (io.WriteCloser, error) { |
||||
n++ |
||||
return &WriteCloser{}, nil |
||||
} |
||||
|
||||
r, err := NewRotator(10, getFile) |
||||
require.NoError(t, err) |
||||
|
||||
_, err = r.Write([]byte("12345")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 1, n) |
||||
|
||||
_, err = r.Write([]byte("12345")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 1, n) |
||||
|
||||
_, err = r.Write([]byte("01234")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 2, n) |
||||
|
||||
_, err = r.Write([]byte("01234")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 2, n) |
||||
|
||||
_, err = r.Write([]byte("01234")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 3, n) |
||||
|
||||
_, err = r.Write([]byte("01234")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 3, n) |
||||
|
||||
_, err = r.Write([]byte("01234")) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, 4, n) |
||||
} |
||||
|
||||
func BenchmarkRotateRAMFile(b *testing.B) { |
||||
dir, err := ioutil.TempDir("", "rotate-benchmark") |
||||
require.NoError(b, err) |
||||
defer os.RemoveAll(dir) // nolint[errcheck]
|
||||
|
||||
benchRotate(b, MaxLogSize, getTestFile(b, dir, MaxLogSize-1)) |
||||
} |
||||
|
||||
func BenchmarkRotateDiskFile(b *testing.B) { |
||||
cache, err := os.UserCacheDir() |
||||
require.NoError(b, err) |
||||
|
||||
dir, err := ioutil.TempDir(cache, "rotate-benchmark") |
||||
require.NoError(b, err) |
||||
defer os.RemoveAll(dir) // nolint[errcheck]
|
||||
|
||||
benchRotate(b, MaxLogSize, getTestFile(b, dir, MaxLogSize-1)) |
||||
} |
||||
|
||||
func benchRotate(b *testing.B, logSize int, getFile func() (io.WriteCloser, error)) { |
||||
r, err := NewRotator(logSize, getFile) |
||||
require.NoError(b, err) |
||||
|
||||
for n := 0; n < b.N; n++ { |
||||
require.NoError(b, r.rotate()) |
||||
|
||||
f, ok := r.wc.(*os.File) |
||||
require.True(b, ok) |
||||
require.NoError(b, os.Remove(f.Name())) |
||||
} |
||||
} |
||||
|
||||
func getTestFile(b *testing.B, dir string, length int) func() (io.WriteCloser, error) { |
||||
return func() (io.WriteCloser, error) { |
||||
b.StopTimer() |
||||
defer b.StartTimer() |
||||
|
||||
f, err := ioutil.TempFile(dir, "log") |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if _, err := f.Write(make([]byte, length)); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if err := f.Sync(); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return f, nil |
||||
} |
||||
} |
||||
@ -1,49 +0,0 @@ |
||||
// Copyright (c) 2022 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package context |
||||
|
||||
import ( |
||||
"bytes" |
||||
"io/ioutil" |
||||
"runtime/pprof" |
||||
) |
||||
|
||||
type panicHandler struct { |
||||
t *bddT |
||||
} |
||||
|
||||
func newPanicHandler(t *bddT) *panicHandler { |
||||
return &panicHandler{ |
||||
t: t, |
||||
} |
||||
} |
||||
|
||||
// HandlePanic makes the panicHandler implement the panicHandler interface for bridge.
|
||||
func (ph *panicHandler) HandlePanic() { |
||||
r := recover() |
||||
if r != nil { |
||||
ph.t.Errorf("panic: %s", r) |
||||
|
||||
r := bytes.NewBufferString("") |
||||
_ = pprof.Lookup("goroutine").WriteTo(r, 2) |
||||
b, err := ioutil.ReadAll(r) |
||||
ph.t.Errorf("pprof details: %s %s", err, b) |
||||
|
||||
ph.t.FailNow() |
||||
} |
||||
} |
||||
Loading…
Reference in new issue