1// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2// See LICENSE.txt for license information.
3
4package config
5
6import (
7	"testing"
8
9	"github.com/stretchr/testify/assert"
10	"github.com/stretchr/testify/require"
11)
12
13const (
14	validJSON = `{"file":{ "Type":"file"}}`
15	badJSON   = `{"file":{ Type="file"}}`
16)
17
18func TestNewLogConfigSrc(t *testing.T) {
19	store := NewTestMemoryStore()
20	require.NotNil(t, store)
21	err := store.SetFile("advancedlogging.conf", []byte(validJSON))
22	require.NoError(t, err)
23
24	tests := []struct {
25		name        string
26		dsn         string
27		configStore *Store
28		wantErr     bool
29		wantType    LogConfigSrc
30	}{
31		{name: "empty dsn", dsn: "", configStore: store, wantErr: true, wantType: nil},
32		{name: "garbage dsn", dsn: "!@wfejwcevioj", configStore: store, wantErr: true, wantType: nil},
33		{name: "valid json dsn", dsn: validJSON, configStore: store, wantErr: false, wantType: &jsonSrc{}},
34		{name: "invalid json dsn", dsn: badJSON, configStore: store, wantErr: true, wantType: nil},
35		{name: "valid filespec dsn", dsn: "advancedlogging.conf", configStore: store, wantErr: false, wantType: &fileSrc{}},
36		{name: "invalid filespec dsn", dsn: "/nobody/here.conf", configStore: store, wantErr: true, wantType: nil},
37	}
38	for _, tt := range tests {
39		t.Run(tt.name, func(t *testing.T) {
40			got, err := NewLogConfigSrc(tt.dsn, tt.configStore)
41			if tt.wantErr {
42				assert.Error(t, err)
43			} else {
44				assert.NoError(t, err)
45				assert.IsType(t, tt.wantType, got)
46			}
47		})
48	}
49}
50