summaryrefslogtreecommitdiffstats
path: root/cmd/platform/server_test.go
blob: 15f9a357a3a537fee4bae51810bbc0a35653139d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package main

import (
	"io/ioutil"
	"os"
	"syscall"
	"testing"

	"github.com/mattermost/mattermost-server/jobs"
	"github.com/mattermost/mattermost-server/utils"
	"github.com/stretchr/testify/require"
)

type ServerTestHelper struct {
	configPath         string
	disableConfigWatch bool
	interruptChan      chan os.Signal
	originalInterval   int
}

func SetupServerTest() *ServerTestHelper {
	// 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{
		configPath:         utils.FindConfigFile("config.json"),
		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()
	defer th.TearDownServerTest()

	err := runServer(th.configPath, th.disableConfigWatch, th.interruptChan)
	require.NoError(t, err)
}

func TestRunServerInvalidConfigFile(t *testing.T) {
	th := SetupServerTest()
	defer th.TearDownServerTest()

	// Start the server with an unreadable config file
	unreadableConfigFile, err := ioutil.TempFile("", "mattermost-unreadable-config-file-")
	if err != nil {
		panic(err)
	}
	os.Chmod(unreadableConfigFile.Name(), 0200)
	defer os.Remove(unreadableConfigFile.Name())

	err = runServer(unreadableConfigFile.Name(), th.disableConfigWatch, th.interruptChan)
	require.Error(t, err)
}