Files
mattermost/app/diagnostics_test.go

170 lines
3.6 KiB
Go
Raw Normal View History

// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/model"
)
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
}
func TestPluginSetting(t *testing.T) {
settings := &model.PluginSettings{
Plugins: map[string]interface{}{
"test": map[string]string{
"foo": "bar",
},
},
}
assert.Equal(t, "bar", pluginSetting(settings, "test", "foo", "asd"))
assert.Equal(t, "asd", pluginSetting(settings, "test", "qwe", "asd"))
}
2017-12-07 17:03:11 -05:00
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"))
}
func TestDiagnostics(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
if testing.Short() {
t.SkipNow()
}
data, server := newTestServer()
defer server.Close()
diagnosticId := "i am not real"
th.App.SetDiagnosticId(diagnosticId)
th.App.initDiagnostics(server.URL)
// Should send a client identify message
select {
case identifyMessage := <-data:
t.Log("Got idmessage:\n" + identifyMessage)
if !strings.Contains(identifyMessage, diagnosticId) {
t.Fail()
}
case <-time.After(time.Second * 1):
2018-03-29 16:04:54 +02:00
t.Fatal("Did not receive ID message")
}
t.Run("Send", func(t *testing.T) {
const TEST_VALUE = "stuff548959847"
th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
"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):
2018-03-29 16:04:54 +02:00
t.Fatal("Did not receive diagnostic")
}
})
t.Run("SendDailyDiagnostics", func(t *testing.T) {
th.App.SendDailyDiagnostics()
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,
TRACK_CONFIG_PLUGIN,
TRACK_ACTIVITY,
TRACK_SERVER,
PLT-7503: Create Message Export Scheduled Task and CLI Command (#7612) * Created message export scheduled task * Added CLI command to immediately kick off an export job * Added email addresses for users joining and leaving the channel to the export * Added support for both MySQL and PostgreSQL * Fixing gofmt error * Added a new ChannelMemberHistory store and associated tests * Updating the ChannelMemberHistory channel as users create/join/leave channels * Added user email to the message export object so it can be included in the actiance export xml * Don't fail to log a leave event if a corresponding join event wasn't logged * Adding copyright notices * Adding message export settings to daily diagnostics report * Added System Console integration for message export * Cleaned up TODOs * Made batch size configurable * Added export from timestamp to CLI command * Made ChannelMemberHistory table updates best effort * Added a context-based timeout option to the message export CLI * Minor PR updates/improvements * Removed unnecessary fields from MessageExport object to reduce query overhead * Removed JSON functions from the message export query in an effort to optimize performance * Changed the way that channel member history queries and purges work to better account for edge cases * Fixing a test I missed with the last refactor * Added file copy functionality to file backend, improved config validation, added default config values * Fixed file copy tests * More concise use of the testing libraries * Fixed context leak error * Changed default export path to correctly place an 'export' directory under the 'data' directory * Can't delete records from a read replica * Fixed copy file tests * Start job workers when license is applied, if configured to do so * Suggestions from the PR * Moar unit tests * Fixed test imports
2017-11-30 09:07:04 -05:00
TRACK_CONFIG_MESSAGE_EXPORT,
TRACK_PLUGINS,
} {
if !strings.Contains(info, item) {
t.Fatal("Sent diagnostics missing item: " + item)
}
}
})
t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
2017-11-09 14:46:20 -06:00
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
th.App.SendDailyDiagnostics()
select {
case <-data:
t.Fatal("Should not send diagnostics when they are disabled")
case <-time.After(time.Second * 1):
2018-03-29 16:04:54 +02:00
// Did not receive diagnostics
}
})
}