github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/config/logging_test.go (about)

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