github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/cmd/mattermost/commands/server_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package commands 5 6 import ( 7 "io/ioutil" 8 "net" 9 "os" 10 "syscall" 11 "testing" 12 13 "github.com/mattermost/mattermost-server/config" 14 "github.com/mattermost/mattermost-server/jobs" 15 "github.com/stretchr/testify/require" 16 ) 17 18 type ServerTestHelper struct { 19 configStore config.Store 20 disableConfigWatch bool 21 interruptChan chan os.Signal 22 originalInterval int 23 } 24 25 func SetupServerTest() *ServerTestHelper { 26 // Build a channel that will be used by the server to receive system signals... 27 interruptChan := make(chan os.Signal, 1) 28 // ...and sent it immediately a SIGINT value. 29 // This will make the server loop stop as soon as it started successfully. 30 interruptChan <- syscall.SIGINT 31 32 // Let jobs poll for termination every 0.2s (instead of every 15s by default) 33 // Otherwise we would have to wait the whole polling duration before the test 34 // terminates. 35 originalInterval := jobs.DEFAULT_WATCHER_POLLING_INTERVAL 36 jobs.DEFAULT_WATCHER_POLLING_INTERVAL = 200 37 38 th := &ServerTestHelper{ 39 disableConfigWatch: true, 40 interruptChan: interruptChan, 41 originalInterval: originalInterval, 42 } 43 return th 44 } 45 46 func (th *ServerTestHelper) TearDownServerTest() { 47 jobs.DEFAULT_WATCHER_POLLING_INTERVAL = th.originalInterval 48 } 49 50 func TestRunServerSuccess(t *testing.T) { 51 th := SetupServerTest() 52 defer th.TearDownServerTest() 53 54 configStore, err := config.NewMemoryStore() 55 require.NoError(t, err) 56 57 err = runServer(configStore, th.disableConfigWatch, false, th.interruptChan) 58 require.NoError(t, err) 59 } 60 61 func TestRunServerSystemdNotification(t *testing.T) { 62 th := SetupServerTest() 63 defer th.TearDownServerTest() 64 65 // Get a random temporary filename for using as a mock systemd socket 66 socketFile, err := ioutil.TempFile("", "mattermost-systemd-mock-socket-") 67 if err != nil { 68 panic(err) 69 } 70 socketPath := socketFile.Name() 71 os.Remove(socketPath) 72 73 // Set the socket path in the process environment 74 originalSocket := os.Getenv("NOTIFY_SOCKET") 75 os.Setenv("NOTIFY_SOCKET", socketPath) 76 defer os.Setenv("NOTIFY_SOCKET", originalSocket) 77 78 // Open the socket connection 79 addr := &net.UnixAddr{ 80 Name: socketPath, 81 Net: "unixgram", 82 } 83 connection, err := net.ListenUnixgram("unixgram", addr) 84 if err != nil { 85 panic(err) 86 } 87 defer connection.Close() 88 defer os.Remove(socketPath) 89 90 // Listen for socket data 91 socketReader := make(chan string) 92 go func(ch chan string) { 93 buffer := make([]byte, 512) 94 count, readErr := connection.Read(buffer) 95 if readErr != nil { 96 panic(readErr) 97 } 98 data := buffer[0:count] 99 ch <- string(data) 100 }(socketReader) 101 102 configStore, err := config.NewMemoryStore() 103 require.NoError(t, err) 104 105 // Start and stop the server 106 err = runServer(configStore, th.disableConfigWatch, false, th.interruptChan) 107 require.NoError(t, err) 108 109 // Ensure the notification has been sent on the socket and is correct 110 notification := <-socketReader 111 require.Equal(t, notification, "READY=1") 112 } 113 114 func TestRunServerNoSystemd(t *testing.T) { 115 th := SetupServerTest() 116 defer th.TearDownServerTest() 117 118 // Temporarily remove any Systemd socket defined in the environment 119 originalSocket := os.Getenv("NOTIFY_SOCKET") 120 os.Unsetenv("NOTIFY_SOCKET") 121 defer os.Setenv("NOTIFY_SOCKET", originalSocket) 122 123 configStore, err := config.NewMemoryStore() 124 require.NoError(t, err) 125 126 err = runServer(configStore, th.disableConfigWatch, false, th.interruptChan) 127 require.NoError(t, err) 128 }