* Introducing unit (not integration) tests for the app layer * Initial support for unit tests at the API * Adding unit tests support to the store layer * Add unit tests support in commands * Adding last tests needed for run unit tests properly * Fixing govet * Removing some duplication * Fixing tests * Fixing tests * Not compiling test helpers with the main module for api * Revert "Not compiling test helpers with the main module for api" This reverts commit 36a199bbe0f7503f665f5d6c7b41c53aabc2e54f. * Fixing tests * Fixing unit tests * More consistency between api4/apiteslib.go and app/helper_test.go * Renaming things to make more obvious the new Setup functions purpose * Reverting change in go.sum * Start with empty mock for app layer * Start with empty mock for api layer * Start with empty mock for web layer * Renaming SetupWithStoreMockConfig to SetupConfigWithStoreMock * Fixing tests on web package * Removing unnecesary function
131 строка
3.4 KiB
Go
131 строка
3.4 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package commands
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/config"
|
|
"github.com/mattermost/mattermost-server/v5/jobs"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type ServerTestHelper struct {
|
|
disableConfigWatch bool
|
|
interruptChan chan os.Signal
|
|
originalInterval int
|
|
}
|
|
|
|
func SetupServerTest(t testing.TB) *ServerTestHelper {
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
// Build a channel that will be used by the server to receive system signals...
|
|
interruptChan := make(chan os.Signal, 1)
|
|
// ...and sent it immediately a SIGINT value.
|
|
// This will make the server loop stop as soon as it started successfully.
|
|
interruptChan <- syscall.SIGINT
|
|
|
|
// Let jobs poll for termination every 0.2s (instead of every 15s by default)
|
|
// Otherwise we would have to wait the whole polling duration before the test
|
|
// terminates.
|
|
originalInterval := jobs.DEFAULT_WATCHER_POLLING_INTERVAL
|
|
jobs.DEFAULT_WATCHER_POLLING_INTERVAL = 200
|
|
|
|
th := &ServerTestHelper{
|
|
disableConfigWatch: true,
|
|
interruptChan: interruptChan,
|
|
originalInterval: originalInterval,
|
|
}
|
|
return th
|
|
}
|
|
|
|
func (th *ServerTestHelper) TearDownServerTest() {
|
|
jobs.DEFAULT_WATCHER_POLLING_INTERVAL = th.originalInterval
|
|
}
|
|
|
|
func TestRunServerSuccess(t *testing.T) {
|
|
th := SetupServerTest(t)
|
|
defer th.TearDownServerTest()
|
|
|
|
configStore, err := config.NewMemoryStore()
|
|
require.NoError(t, err)
|
|
|
|
err = runServer(configStore, th.disableConfigWatch, false, th.interruptChan)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestRunServerSystemdNotification(t *testing.T) {
|
|
th := SetupServerTest(t)
|
|
defer th.TearDownServerTest()
|
|
|
|
// Get a random temporary filename for using as a mock systemd socket
|
|
socketFile, err := ioutil.TempFile("", "mattermost-systemd-mock-socket-")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
socketPath := socketFile.Name()
|
|
os.Remove(socketPath)
|
|
|
|
// Set the socket path in the process environment
|
|
originalSocket := os.Getenv("NOTIFY_SOCKET")
|
|
os.Setenv("NOTIFY_SOCKET", socketPath)
|
|
defer os.Setenv("NOTIFY_SOCKET", originalSocket)
|
|
|
|
// Open the socket connection
|
|
addr := &net.UnixAddr{
|
|
Name: socketPath,
|
|
Net: "unixgram",
|
|
}
|
|
connection, err := net.ListenUnixgram("unixgram", addr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer connection.Close()
|
|
defer os.Remove(socketPath)
|
|
|
|
// Listen for socket data
|
|
socketReader := make(chan string)
|
|
go func(ch chan string) {
|
|
buffer := make([]byte, 512)
|
|
count, readErr := connection.Read(buffer)
|
|
if readErr != nil {
|
|
panic(readErr)
|
|
}
|
|
data := buffer[0:count]
|
|
ch <- string(data)
|
|
}(socketReader)
|
|
|
|
configStore, err := config.NewMemoryStore()
|
|
require.NoError(t, err)
|
|
|
|
// Start and stop the server
|
|
err = runServer(configStore, th.disableConfigWatch, false, th.interruptChan)
|
|
require.NoError(t, err)
|
|
|
|
// Ensure the notification has been sent on the socket and is correct
|
|
notification := <-socketReader
|
|
require.Equal(t, notification, "READY=1")
|
|
}
|
|
|
|
func TestRunServerNoSystemd(t *testing.T) {
|
|
th := SetupServerTest(t)
|
|
defer th.TearDownServerTest()
|
|
|
|
// Temporarily remove any Systemd socket defined in the environment
|
|
originalSocket := os.Getenv("NOTIFY_SOCKET")
|
|
os.Unsetenv("NOTIFY_SOCKET")
|
|
defer os.Setenv("NOTIFY_SOCKET", originalSocket)
|
|
|
|
configStore, err := config.NewMemoryStore()
|
|
require.NoError(t, err)
|
|
|
|
err = runServer(configStore, th.disableConfigWatch, false, th.interruptChan)
|
|
require.NoError(t, err)
|
|
}
|