github.com/adacta-ru/mattermost-server@v5.11.1+incompatible/app/diagnostics_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  
    17  	"github.com/mattermost/mattermost-server/model"
    18  )
    19  
    20  func newTestServer() (chan string, *httptest.Server) {
    21  	result := make(chan string, 100)
    22  
    23  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    24  		buf := bytes.NewBuffer(nil)
    25  		io.Copy(buf, r.Body)
    26  
    27  		result <- buf.String()
    28  	}))
    29  
    30  	return result, server
    31  }
    32  
    33  func TestPluginSetting(t *testing.T) {
    34  	settings := &model.PluginSettings{
    35  		Plugins: map[string]map[string]interface{}{
    36  			"test": map[string]interface{}{
    37  				"foo": "bar",
    38  			},
    39  		},
    40  	}
    41  	assert.Equal(t, "bar", pluginSetting(settings, "test", "foo", "asd"))
    42  	assert.Equal(t, "asd", pluginSetting(settings, "test", "qwe", "asd"))
    43  }
    44  
    45  func TestPluginActivated(t *testing.T) {
    46  	states := map[string]*model.PluginState{
    47  		"foo": &model.PluginState{
    48  			Enable: true,
    49  		},
    50  		"bar": &model.PluginState{
    51  			Enable: false,
    52  		},
    53  	}
    54  	assert.True(t, pluginActivated(states, "foo"))
    55  	assert.False(t, pluginActivated(states, "bar"))
    56  	assert.False(t, pluginActivated(states, "none"))
    57  }
    58  
    59  func TestDiagnostics(t *testing.T) {
    60  	th := Setup(t).InitBasic()
    61  	defer th.TearDown()
    62  
    63  	if testing.Short() {
    64  		t.SkipNow()
    65  	}
    66  
    67  	data, server := newTestServer()
    68  	defer server.Close()
    69  
    70  	diagnosticId := "i am not real"
    71  	th.App.SetDiagnosticId(diagnosticId)
    72  	th.App.initDiagnostics(server.URL)
    73  
    74  	// Should send a client identify message
    75  	select {
    76  	case identifyMessage := <-data:
    77  		t.Log("Got idmessage:\n" + identifyMessage)
    78  		if !strings.Contains(identifyMessage, diagnosticId) {
    79  			t.Fail()
    80  		}
    81  	case <-time.After(time.Second * 1):
    82  		t.Fatal("Did not receive ID message")
    83  	}
    84  
    85  	t.Run("Send", func(t *testing.T) {
    86  		const TEST_VALUE = "stuff548959847"
    87  		th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
    88  			"hey": TEST_VALUE,
    89  		})
    90  		select {
    91  		case result := <-data:
    92  			t.Log("Got diagnostic:\n" + result)
    93  			if !strings.Contains(result, TEST_VALUE) {
    94  				t.Fail()
    95  			}
    96  		case <-time.After(time.Second * 1):
    97  			t.Fatal("Did not receive diagnostic")
    98  		}
    99  	})
   100  
   101  	t.Run("SendDailyDiagnostics", func(t *testing.T) {
   102  		th.App.sendDailyDiagnostics(true)
   103  
   104  		info := ""
   105  		// Collect the info sent.
   106  	Loop:
   107  		for {
   108  			select {
   109  			case result := <-data:
   110  				info += result
   111  			case <-time.After(time.Second * 1):
   112  				break Loop
   113  			}
   114  		}
   115  
   116  		for _, item := range []string{
   117  			TRACK_CONFIG_SERVICE,
   118  			TRACK_CONFIG_TEAM,
   119  			TRACK_CONFIG_SERVICE,
   120  			TRACK_CONFIG_TEAM,
   121  			TRACK_CONFIG_SQL,
   122  			TRACK_CONFIG_LOG,
   123  			TRACK_CONFIG_FILE,
   124  			TRACK_CONFIG_RATE,
   125  			TRACK_CONFIG_EMAIL,
   126  			TRACK_CONFIG_PRIVACY,
   127  			TRACK_CONFIG_OAUTH,
   128  			TRACK_CONFIG_LDAP,
   129  			TRACK_CONFIG_COMPLIANCE,
   130  			TRACK_CONFIG_LOCALIZATION,
   131  			TRACK_CONFIG_SAML,
   132  			TRACK_CONFIG_PASSWORD,
   133  			TRACK_CONFIG_CLUSTER,
   134  			TRACK_CONFIG_METRICS,
   135  			TRACK_CONFIG_SUPPORT,
   136  			TRACK_CONFIG_NATIVEAPP,
   137  			TRACK_CONFIG_ANALYTICS,
   138  			TRACK_CONFIG_PLUGIN,
   139  			TRACK_ACTIVITY,
   140  			TRACK_SERVER,
   141  			TRACK_CONFIG_MESSAGE_EXPORT,
   142  			TRACK_PLUGINS,
   143  		} {
   144  			if !strings.Contains(info, item) {
   145  				t.Fatal("Sent diagnostics missing item: " + item)
   146  			}
   147  		}
   148  	})
   149  
   150  	t.Run("SendDailyDiagnosticsNoSegmentKey", func(t *testing.T) {
   151  		th.App.SendDailyDiagnostics()
   152  
   153  		select {
   154  		case <-data:
   155  			t.Fatal("Should not send diagnostics when the segment key is not set")
   156  		case <-time.After(time.Second * 1):
   157  			// Did not receive diagnostics
   158  		}
   159  	})
   160  
   161  	t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
   162  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
   163  
   164  		th.App.sendDailyDiagnostics(true)
   165  
   166  		select {
   167  		case <-data:
   168  			t.Fatal("Should not send diagnostics when they are disabled")
   169  		case <-time.After(time.Second * 1):
   170  			// Did not receive diagnostics
   171  		}
   172  	})
   173  }