github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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/jobs" 14 "github.com/mattermost/mattermost-server/utils" 15 "github.com/stretchr/testify/require" 16 ) 17 18 type ServerTestHelper struct { 19 configPath string 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 configPath: utils.FindConfigFile("config.json"), 40 disableConfigWatch: true, 41 interruptChan: interruptChan, 42 originalInterval: originalInterval, 43 } 44 return th 45 } 46 47 func (th *ServerTestHelper) TearDownServerTest() { 48 jobs.DEFAULT_WATCHER_POLLING_INTERVAL = th.originalInterval 49 } 50 51 func TestRunServerSuccess(t *testing.T) { 52 th := SetupServerTest() 53 defer th.TearDownServerTest() 54 55 err := runServer(th.configPath, th.disableConfigWatch, false, th.interruptChan) 56 require.NoError(t, err) 57 } 58 59 func TestRunServerInvalidConfigFile(t *testing.T) { 60 th := SetupServerTest() 61 defer th.TearDownServerTest() 62 63 // Start the server with an unreadable config file 64 unreadableConfigFile, err := ioutil.TempFile("", "mattermost-unreadable-config-file-") 65 if err != nil { 66 panic(err) 67 } 68 os.Chmod(unreadableConfigFile.Name(), 0200) 69 defer os.Remove(unreadableConfigFile.Name()) 70 71 err = runServer(unreadableConfigFile.Name(), th.disableConfigWatch, false, th.interruptChan) 72 require.Error(t, err) 73 } 74 75 func TestRunServerSystemdNotification(t *testing.T) { 76 th := SetupServerTest() 77 defer th.TearDownServerTest() 78 79 // Get a random temporary filename for using as a mock systemd socket 80 socketFile, err := ioutil.TempFile("", "mattermost-systemd-mock-socket-") 81 if err != nil { 82 panic(err) 83 } 84 socketPath := socketFile.Name() 85 os.Remove(socketPath) 86 87 // Set the socket path in the process environment 88 originalSocket := os.Getenv("NOTIFY_SOCKET") 89 os.Setenv("NOTIFY_SOCKET", socketPath) 90 defer os.Setenv("NOTIFY_SOCKET", originalSocket) 91 92 // Open the socket connection 93 addr := &net.UnixAddr{ 94 Name: socketPath, 95 Net: "unixgram", 96 } 97 connection, err := net.ListenUnixgram("unixgram", addr) 98 if err != nil { 99 panic(err) 100 } 101 defer connection.Close() 102 defer os.Remove(socketPath) 103 104 // Listen for socket data 105 socketReader := make(chan string) 106 go func(ch chan string) { 107 buffer := make([]byte, 512) 108 count, err := connection.Read(buffer) 109 if err != nil { 110 panic(err) 111 } 112 data := buffer[0:count] 113 ch <- string(data) 114 }(socketReader) 115 116 // Start and stop the server 117 err = runServer(th.configPath, th.disableConfigWatch, false, th.interruptChan) 118 require.NoError(t, err) 119 120 // Ensure the notification has been sent on the socket and is correct 121 notification := <-socketReader 122 require.Equal(t, notification, "READY=1") 123 } 124 125 func TestRunServerNoSystemd(t *testing.T) { 126 th := SetupServerTest() 127 defer th.TearDownServerTest() 128 129 // Temporarily remove any Systemd socket defined in the environment 130 originalSocket := os.Getenv("NOTIFY_SOCKET") 131 os.Unsetenv("NOTIFY_SOCKET") 132 defer os.Setenv("NOTIFY_SOCKET", originalSocket) 133 134 err := runServer(th.configPath, th.disableConfigWatch, false, th.interruptChan) 135 require.NoError(t, err) 136 }