mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* diagnostics_test.go: fix spacing * diagnostics_test.go: explicitly assert payload This fails, since the package is currently receiving struct pointers and won't set the MessageId or Timestamp on the corresponding Message. * MM-18115: fix segment v3 usage In v5.14, we updated [github.com/segmentio/analytics-go](https://github.com/segmentio/analytics-go) to v3 as part of https://mattermost.atlassian.net/browse/MM-12389. As noted in the [migration guide](https://segment.com/docs/sources/server/go/#migrating-from-v2), the API subtly changed to expect a struct value and not a struct pointer: ```go // in v2, you would call the `Track` method with a `Track` struct. client.Track(&track) // in v3, you would call the `Enqueue` method with a `Track` struct. // Note that a pointer is not used here. client.Enqueue(track) ``` Unfortunately, we kept passing a pointer, and the package didn't complain since it only required an interface -- which the pointer to these structs still implemented. Internally, it only checked for the value types, and failed to annotate our payloads with the requisite metadata. Upstream, segment.io accepted the payload, but then discarded it silently. This has since been reported and fixed in https://github.com/segmentio/analytics-go/pull/146, but isn't yet part of a tagged release of the package. Fix our code to pass struct values instead. Fixes: MM-18115
201 lines
4.8 KiB
Go
201 lines
4.8 KiB
Go
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
func TestPluginSetting(t *testing.T) {
|
|
settings := &model.PluginSettings{
|
|
Plugins: map[string]map[string]interface{}{
|
|
"test": map[string]interface{}{
|
|
"foo": "bar",
|
|
},
|
|
},
|
|
}
|
|
assert.Equal(t, "bar", pluginSetting(settings, "test", "foo", "asd"))
|
|
assert.Equal(t, "asd", pluginSetting(settings, "test", "qwe", "asd"))
|
|
}
|
|
|
|
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) {
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
type payload struct {
|
|
MessageId string
|
|
SentAt time.Time
|
|
Batch []struct {
|
|
MessageId string
|
|
UserId string
|
|
Event string
|
|
Timestamp time.Time
|
|
Properties map[string]interface{}
|
|
}
|
|
Context struct {
|
|
Library struct {
|
|
Name string
|
|
Version string
|
|
}
|
|
}
|
|
}
|
|
|
|
data := make(chan payload, 100)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
require.NoError(t, err)
|
|
|
|
var p payload
|
|
err = json.Unmarshal(body, &p)
|
|
require.NoError(t, err)
|
|
|
|
data <- p
|
|
}))
|
|
defer server.Close()
|
|
|
|
diagnosticID := "test-diagnostic-id-12345"
|
|
th.App.SetDiagnosticId(diagnosticID)
|
|
th.Server.initDiagnostics(server.URL)
|
|
|
|
assertPayload := func(t *testing.T, actual payload, event string, properties map[string]interface{}) {
|
|
assert.NotEmpty(t, actual.MessageId)
|
|
assert.False(t, actual.SentAt.IsZero())
|
|
if assert.Len(t, actual.Batch, 1) {
|
|
assert.NotEmpty(t, actual.Batch[0].MessageId, "message id should not be empty")
|
|
assert.Equal(t, diagnosticID, actual.Batch[0].UserId)
|
|
if event != "" {
|
|
assert.Equal(t, event, actual.Batch[0].Event)
|
|
}
|
|
assert.False(t, actual.Batch[0].Timestamp.IsZero(), "batch timestamp should not be the zero value")
|
|
if properties != nil {
|
|
assert.Equal(t, properties, actual.Batch[0].Properties)
|
|
}
|
|
}
|
|
assert.Equal(t, "analytics-go", actual.Context.Library.Name)
|
|
assert.Equal(t, "3.0.0", actual.Context.Library.Version)
|
|
}
|
|
|
|
// Should send a client identify message
|
|
select {
|
|
case identifyMessage := <-data:
|
|
assertPayload(t, identifyMessage, "", nil)
|
|
case <-time.After(time.Second * 1):
|
|
require.Fail(t, "Did not receive ID message")
|
|
}
|
|
|
|
t.Run("Send", func(t *testing.T) {
|
|
testValue := "test-send-value-6789"
|
|
th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
|
|
"hey": testValue,
|
|
})
|
|
select {
|
|
case result := <-data:
|
|
assertPayload(t, result, "Testing Diagnostic", map[string]interface{}{
|
|
"hey": testValue,
|
|
})
|
|
case <-time.After(time.Second * 1):
|
|
require.Fail(t, "Did not receive diagnostic")
|
|
}
|
|
})
|
|
|
|
t.Run("SendDailyDiagnostics", func(t *testing.T) {
|
|
th.App.sendDailyDiagnostics(true)
|
|
|
|
var info []string
|
|
// Collect the info sent.
|
|
Loop:
|
|
for {
|
|
select {
|
|
case result := <-data:
|
|
assertPayload(t, result, "", nil)
|
|
info = append(info, result.Batch[0].Event)
|
|
case <-time.After(time.Second * 1):
|
|
break Loop
|
|
}
|
|
}
|
|
|
|
for _, item := range []string{
|
|
TRACK_CONFIG_SERVICE,
|
|
TRACK_CONFIG_TEAM,
|
|
TRACK_CONFIG_SQL,
|
|
TRACK_CONFIG_LOG,
|
|
TRACK_CONFIG_NOTIFICATION_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_SUPPORT,
|
|
TRACK_CONFIG_NATIVEAPP,
|
|
TRACK_CONFIG_ANALYTICS,
|
|
TRACK_CONFIG_PLUGIN,
|
|
TRACK_ACTIVITY,
|
|
TRACK_SERVER,
|
|
TRACK_CONFIG_MESSAGE_EXPORT,
|
|
TRACK_PLUGINS,
|
|
} {
|
|
require.Contains(t, info, item)
|
|
}
|
|
})
|
|
|
|
t.Run("SendDailyDiagnosticsNoSegmentKey", func(t *testing.T) {
|
|
th.App.SendDailyDiagnostics()
|
|
|
|
select {
|
|
case <-data:
|
|
require.Fail(t, "Should not send diagnostics when the segment key is not set")
|
|
case <-time.After(time.Second * 1):
|
|
// Did not receive diagnostics
|
|
}
|
|
})
|
|
|
|
t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
|
|
|
|
th.App.sendDailyDiagnostics(true)
|
|
|
|
select {
|
|
case <-data:
|
|
require.Fail(t, "Should not send diagnostics when they are disabled")
|
|
case <-time.After(time.Second * 1):
|
|
// Did not receive diagnostics
|
|
}
|
|
})
|
|
}
|