github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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]interface{}{
    36  			"test": map[string]string{
    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().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()
   103  
   104  		info := ""
   105  		// Collect the info sent.
   106  		for {
   107  			done := false
   108  			select {
   109  			case result := <-data:
   110  				info += result
   111  			case <-time.After(time.Second * 1):
   112  				// Done recieving
   113  				done = true
   114  				break
   115  			}
   116  
   117  			if done {
   118  				break
   119  			}
   120  		}
   121  
   122  		for _, item := range []string{
   123  			TRACK_CONFIG_SERVICE,
   124  			TRACK_CONFIG_TEAM,
   125  			TRACK_CONFIG_SERVICE,
   126  			TRACK_CONFIG_TEAM,
   127  			TRACK_CONFIG_SQL,
   128  			TRACK_CONFIG_LOG,
   129  			TRACK_CONFIG_FILE,
   130  			TRACK_CONFIG_RATE,
   131  			TRACK_CONFIG_EMAIL,
   132  			TRACK_CONFIG_PRIVACY,
   133  			TRACK_CONFIG_OAUTH,
   134  			TRACK_CONFIG_LDAP,
   135  			TRACK_CONFIG_COMPLIANCE,
   136  			TRACK_CONFIG_LOCALIZATION,
   137  			TRACK_CONFIG_SAML,
   138  			TRACK_CONFIG_PASSWORD,
   139  			TRACK_CONFIG_CLUSTER,
   140  			TRACK_CONFIG_METRICS,
   141  			TRACK_CONFIG_WEBRTC,
   142  			TRACK_CONFIG_SUPPORT,
   143  			TRACK_CONFIG_NATIVEAPP,
   144  			TRACK_CONFIG_ANALYTICS,
   145  			TRACK_CONFIG_PLUGIN,
   146  			TRACK_ACTIVITY,
   147  			TRACK_SERVER,
   148  			TRACK_CONFIG_MESSAGE_EXPORT,
   149  			TRACK_PLUGINS,
   150  		} {
   151  			if !strings.Contains(info, item) {
   152  				t.Fatal("Sent diagnostics missing item: " + item)
   153  			}
   154  		}
   155  	})
   156  
   157  	t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
   158  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
   159  
   160  		th.App.SendDailyDiagnostics()
   161  
   162  		select {
   163  		case <-data:
   164  			t.Fatal("Should not send diagnostics when they are disabled")
   165  		case <-time.After(time.Second * 1):
   166  			// Did not receive diagnostics
   167  		}
   168  	})
   169  }