2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-12-12 18:00:20 +01:00
|
|
|
|
|
|
|
package log_test
|
|
|
|
|
|
|
|
import (
|
2022-07-19 10:47:55 +02:00
|
|
|
"fmt"
|
2022-04-26 17:55:24 +02:00
|
|
|
"regexp"
|
2021-12-12 18:00:20 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-12-12 18:00:20 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
|
|
|
"gopkg.in/mcuadros/go-syslog.v2"
|
|
|
|
"gopkg.in/mcuadros/go-syslog.v2/format"
|
|
|
|
)
|
|
|
|
|
2022-04-26 17:55:24 +02:00
|
|
|
const longMessage = "condimentum lacinia quis vel eros donec ac odio tempor orci dapibus ultrices in iaculis nunc sed augue lacus viverra vitae congue eu consequat ac felis donec et odio pellentesque diam volutpat commodo sed egestas egestas fringilla phasellus faucibus scelerisque eleifend donec pretium vulputate sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur vitae nunc sed velit dignissim sodales ut eu sem integer vitae justo eget magna fermentum iaculis eu non diam phasellus vestibulum lorem sed risus ultricies tristique nulla aliquet enim tortor at auctor urna nunc id cursus metus aliquam eleifend mi in nulla posuere sollicitudin aliquam ultrices sagittis orci a scelerisque purus semper eget duis at tellus at urna condimentum mattis pellentesque id nibh tortor id aliquet lectus proin nibh nisl condimentum id venenatis a condimentum vitae sapien pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas sed tempus urna et pharetra pharetra massa massa ultricies mi quis hendrerit dolor magna eget est lorem ipsum dolor sit amet consectetur adipiscing elit pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas integer eget aliquet nibh praesent tristique magna sit amet purus gravida quis blandit turpis cursus in hac habitasse platea dictumst quisque sagittis purus sit amet volutpat consequat mauris nunc congue nisi vitae suscipit tellus mauris a diam maecenas sed enim ut sem viverra aliquet eget sit amet tellus cras adipiscing enim eu turpis egestas pretium aenean pharetra magna ac placerat vestibulum lectus mauris ultrices eros in cursus turpis massa tincidunt dui ut ornare lectus sit amet est placerat in egestas erat imperdiet sed euismod nisi porta lorem mollis aliquam ut porttitor leo a diam sollicitudin tempor id eu nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper sit amet risus nullam eget felis eget nunc lobortis mattis aliquam faucibus purus in massa tempor nec feugiat nisl pretium fusce id velit ut tortor pretium viverra suspendisse potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus in hac habitasse platea dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt lobortis feugiat vivamus at augue eget arcu dictum varius duis at consectetur lorem donec massa sapien faucibus et molestie ac feugiat sed lectus vestibulum mattis ullamcorper velit sed ullamcorper morbi tincidunt ornare massa eget egestas purus viverra accumsan in nisl nisi scelerisque eu ultrices vitae auctor eu augue ut lectus arcu bibendum at varius vel pharetra vel turpis nunc eget lorem dolor sed viverra ipsum nunc aliquet bibendum enim facilisis gravida neque convallis a cras semper auctor neque vitae tempus quam pellentesque nec nam aliquam sem et tortor consequat id porta nibh venenatis cras sed felis eget velit aliquet sagittis id consectetur purus ut faucibus pulvinar elementum integer enim neque volutpat ac tincidunt vitae semper quis lectus nulla at volutpat diam ut venenatis tellus in metus vulputate eu scelerisque felis imperdiet proin fermentum leo vel orci porta non pulvinar neque laoreet suspendisse interdum consectetur libero id faucibus nisl tincidunt eget nullam non nisi est sit amet facilisis magna etiam tempor orci eu lobortis elementum nibh tellus molestie nunc non blandit massa enim nec dui nunc mattis enim ut tellus elementum sagittis vitae et leo duis ut diam quam nulla porttitor massa id neque aliquam vestibulum morbi blandit cursus risus at ultrices mi tempus imperdiet nulla malesuada pellentesque elit eget gravida cum sociis natoque penatibus et magnis dis parturient montes nascetur ridiculus mus mauris vitae ultricies leo integer malesuada nunc vel risus commodo viverra maecenas accumsan lacus vel facilisis volutpat est velit egestas dui id ornare arcu odio ut sem nulla pharetra diam sit amet nisl suscipit adipiscing bibendum est ultricies integer quis auctor elit sed vulputate mi sit amet mauris commodo quis imperdiet massa tincidunt n
|
|
|
|
|
2021-12-12 18:00:20 +01:00
|
|
|
type SyslogTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
syslogServer *syslog.Server
|
|
|
|
syslogChannel chan format.LogParts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SyslogTestSuite) SetupTest() {
|
|
|
|
testrig.InitTestConfig()
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
config.SetSyslogEnabled(true)
|
|
|
|
config.SetSyslogProtocol("udp")
|
|
|
|
config.SetSyslogAddress("127.0.0.1:42069")
|
|
|
|
|
2021-12-12 18:00:20 +01:00
|
|
|
server, channel, err := testrig.InitTestSyslog()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
suite.syslogServer = server
|
|
|
|
suite.syslogChannel = channel
|
|
|
|
|
|
|
|
testrig.InitTestLog()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SyslogTestSuite) TearDownTest() {
|
|
|
|
if err := suite.syslogServer.Kill(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SyslogTestSuite) TestSyslog() {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Info(nil, "this is a test of the emergency broadcast system!")
|
2021-12-12 18:00:20 +01:00
|
|
|
|
2022-04-26 17:55:24 +02:00
|
|
|
entry := <-suite.syslogChannel
|
2022-07-19 10:47:55 +02:00
|
|
|
suite.Regexp(regexp.MustCompile(`timestamp=.* func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"])
|
2022-04-26 17:55:24 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:07:55 +02:00
|
|
|
func (suite *SyslogTestSuite) TestSyslogDisableTimestamp() {
|
|
|
|
// Get the current format.
|
|
|
|
timefmt := log.TimeFormat()
|
|
|
|
|
|
|
|
// Set an empty timestamp.
|
|
|
|
log.SetTimeFormat("")
|
|
|
|
|
|
|
|
// Set old timestamp on return.
|
|
|
|
defer log.SetTimeFormat(timefmt)
|
|
|
|
|
|
|
|
log.Info(nil, "this is a test of the emergency broadcast system!")
|
|
|
|
|
|
|
|
entry := <-suite.syslogChannel
|
|
|
|
suite.Regexp(regexp.MustCompile(`func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"])
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:55:24 +02:00
|
|
|
func (suite *SyslogTestSuite) TestSyslogLongMessage() {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Warn(nil, longMessage)
|
2022-07-19 10:47:55 +02:00
|
|
|
|
|
|
|
funcName := log.Caller(2)
|
|
|
|
prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=WARN msg="`, funcName)
|
2022-04-26 17:55:24 +02:00
|
|
|
|
|
|
|
entry := <-suite.syslogChannel
|
2022-10-01 17:36:08 +02:00
|
|
|
regex := fmt.Sprintf(`timestamp=.* func=.* level=WARN msg="%s`, longMessage[:2048-len(prefix)])
|
2022-07-19 10:47:55 +02:00
|
|
|
suite.Regexp(regexp.MustCompile(regex), entry["content"])
|
2022-04-26 17:55:24 +02:00
|
|
|
}
|
|
|
|
|
2021-12-12 18:00:20 +01:00
|
|
|
func TestSyslogTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &SyslogTestSuite{})
|
|
|
|
}
|