diagnostics_test.go 3.62 KB
Newer Older
1
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
// See License.txt for license information.

package app

import (
	"bytes"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"

Chris's avatar
Chris committed
15 16 17
	"github.com/stretchr/testify/assert"

	"github.com/mattermost/mattermost-server/model"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
)

func newTestServer() (chan string, *httptest.Server) {
	result := make(chan string, 100)

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		buf := bytes.NewBuffer(nil)
		io.Copy(buf, r.Body)

		result <- buf.String()
	}))

	return result, server
}

33
func TestPluginSetting(t *testing.T) {
Chris's avatar
Chris committed
34 35 36 37 38
	settings := &model.PluginSettings{
		Plugins: map[string]interface{}{
			"test": map[string]string{
				"foo": "bar",
			},
39 40
		},
	}
Chris's avatar
Chris committed
41 42
	assert.Equal(t, "bar", pluginSetting(settings, "test", "foo", "asd"))
	assert.Equal(t, "asd", pluginSetting(settings, "test", "qwe", "asd"))
43 44
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58
func TestPluginActivated(t *testing.T) {
	states := map[string]*model.PluginState{
		"foo": &model.PluginState{
			Enable: true,
		},
		"bar": &model.PluginState{
			Enable: false,
		},
	}
	assert.True(t, pluginActivated(states, "foo"))
	assert.False(t, pluginActivated(states, "bar"))
	assert.False(t, pluginActivated(states, "none"))
}

59
func TestDiagnostics(t *testing.T) {
Chris's avatar
Chris committed
60
	th := Setup().InitBasic()
61
	defer th.TearDown()
62 63 64 65 66 67 68 69

	if testing.Short() {
		t.SkipNow()
	}

	data, server := newTestServer()
	defer server.Close()

70 71 72
	diagnosticId := "i am not real"
	th.App.SetDiagnosticId(diagnosticId)
	th.App.initDiagnostics(server.URL)
73 74 75 76 77

	// Should send a client identify message
	select {
	case identifyMessage := <-data:
		t.Log("Got idmessage:\n" + identifyMessage)
78
		if !strings.Contains(identifyMessage, diagnosticId) {
79 80 81 82 83 84 85 86
			t.Fail()
		}
	case <-time.After(time.Second * 1):
		t.Fatal("Did not recieve ID message")
	}

	t.Run("Send", func(t *testing.T) {
		const TEST_VALUE = "stuff548959847"
87
		th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
88 89 90 91 92 93 94 95 96 97 98 99 100 101
			"hey": TEST_VALUE,
		})
		select {
		case result := <-data:
			t.Log("Got diagnostic:\n" + result)
			if !strings.Contains(result, TEST_VALUE) {
				t.Fail()
			}
		case <-time.After(time.Second * 1):
			t.Fatal("Did not recieve diagnostic")
		}
	})

	t.Run("SendDailyDiagnostics", func(t *testing.T) {
Chris's avatar
Chris committed
102
		th.App.SendDailyDiagnostics()
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

		info := ""
		// Collect the info sent.
		for {
			done := false
			select {
			case result := <-data:
				info += result
			case <-time.After(time.Second * 1):
				// Done recieving
				done = true
				break
			}

			if done {
				break
			}
		}

		for _, item := range []string{
			TRACK_CONFIG_SERVICE,
			TRACK_CONFIG_TEAM,
			TRACK_CONFIG_SERVICE,
			TRACK_CONFIG_TEAM,
			TRACK_CONFIG_SQL,
			TRACK_CONFIG_LOG,
			TRACK_CONFIG_FILE,
			TRACK_CONFIG_RATE,
			TRACK_CONFIG_EMAIL,
			TRACK_CONFIG_PRIVACY,
			TRACK_CONFIG_OAUTH,
			TRACK_CONFIG_LDAP,
			TRACK_CONFIG_COMPLIANCE,
			TRACK_CONFIG_LOCALIZATION,
			TRACK_CONFIG_SAML,
			TRACK_CONFIG_PASSWORD,
			TRACK_CONFIG_CLUSTER,
			TRACK_CONFIG_METRICS,
			TRACK_CONFIG_WEBRTC,
			TRACK_CONFIG_SUPPORT,
			TRACK_CONFIG_NATIVEAPP,
			TRACK_CONFIG_ANALYTICS,
145
			TRACK_CONFIG_PLUGIN,
146 147
			TRACK_ACTIVITY,
			TRACK_SERVER,
148
			TRACK_CONFIG_MESSAGE_EXPORT,
149
			TRACK_PLUGINS,
150 151 152 153 154 155 156 157
		} {
			if !strings.Contains(info, item) {
				t.Fatal("Sent diagnostics missing item: " + item)
			}
		}
	})

	t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
Chris's avatar
Chris committed
158
		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
159

Chris's avatar
Chris committed
160
		th.App.SendDailyDiagnostics()
161 162 163 164 165 166 167 168 169

		select {
		case <-data:
			t.Fatal("Should not send diagnostics when they are disabled")
		case <-time.After(time.Second * 1):
			// Did not recieve diagnostics
		}
	})
}