github.com/rifflearning/mattermost-server@v5.11.1+incompatible/testlib/helper.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package testlib
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/mattermost/mattermost-server/mlog"
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/store"
    15  	"github.com/mattermost/mattermost-server/store/sqlstore"
    16  	"github.com/mattermost/mattermost-server/store/storetest"
    17  	"github.com/mattermost/mattermost-server/utils"
    18  )
    19  
    20  type MainHelper struct {
    21  	Settings         *model.SqlSettings
    22  	Store            store.Store
    23  	SqlSupplier      *sqlstore.SqlSupplier
    24  	ClusterInterface *FakeClusterInterface
    25  
    26  	status           int
    27  	testResourcePath string
    28  }
    29  
    30  type HelperOptions struct {
    31  	EnableStore     bool
    32  	EnableResources bool
    33  }
    34  
    35  func NewMainHelper() *MainHelper {
    36  	return NewMainHelperWithOptions(&HelperOptions{
    37  		EnableStore:     true,
    38  		EnableResources: true,
    39  	})
    40  }
    41  
    42  func NewMainHelperWithOptions(options *HelperOptions) *MainHelper {
    43  	var mainHelper MainHelper
    44  	flag.Parse()
    45  
    46  	// Setup a global logger to catch tests logging outside of app context
    47  	// The global logger will be stomped by apps initalizing but that's fine for testing.
    48  	// Ideally this won't happen.
    49  	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
    50  		EnableConsole: true,
    51  		ConsoleJson:   true,
    52  		ConsoleLevel:  "error",
    53  		EnableFile:    false,
    54  	}))
    55  
    56  	utils.TranslationsPreInit()
    57  
    58  	if options != nil {
    59  		if options.EnableStore {
    60  			mainHelper.setupStore()
    61  		}
    62  
    63  		if options.EnableResources {
    64  			mainHelper.setupResources()
    65  		}
    66  	}
    67  
    68  	return &mainHelper
    69  }
    70  
    71  func (h *MainHelper) Main(m *testing.M) {
    72  	if h.testResourcePath != "" {
    73  		prevDir, err := os.Getwd()
    74  		if err != nil {
    75  			panic("Failed to get current working directory: " + err.Error())
    76  		}
    77  
    78  		err = os.Chdir(h.testResourcePath)
    79  		if err != nil {
    80  			panic(fmt.Sprintf("Failed to set current working directory to %s: %s", h.testResourcePath, err.Error()))
    81  		}
    82  
    83  		defer func() {
    84  			err := os.Chdir(prevDir)
    85  			if err != nil {
    86  				panic(fmt.Sprintf("Failed to restore current working directory to %s: %s", prevDir, err.Error()))
    87  			}
    88  		}()
    89  	}
    90  
    91  	h.status = m.Run()
    92  }
    93  
    94  func (h *MainHelper) setupStore() {
    95  	driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
    96  	if driverName == "" {
    97  		driverName = model.DATABASE_DRIVER_MYSQL
    98  	}
    99  
   100  	h.Settings = storetest.MakeSqlSettings(driverName)
   101  
   102  	h.ClusterInterface = &FakeClusterInterface{}
   103  	h.SqlSupplier = sqlstore.NewSqlSupplier(*h.Settings, nil)
   104  	h.Store = &TestStore{
   105  		store.NewLayeredStore(h.SqlSupplier, nil, h.ClusterInterface),
   106  	}
   107  }
   108  
   109  func (h *MainHelper) setupResources() {
   110  	var err error
   111  	h.testResourcePath, err = SetupTestResources()
   112  	if err != nil {
   113  		panic("failed to setup test resources: " + err.Error())
   114  	}
   115  }
   116  
   117  func (h *MainHelper) Close() error {
   118  	if h.Settings != nil {
   119  		storetest.CleanupSqlSettings(h.Settings)
   120  	}
   121  	if h.testResourcePath != "" {
   122  		os.RemoveAll(h.testResourcePath)
   123  	}
   124  
   125  	os.Exit(h.status)
   126  
   127  	return nil
   128  }
   129  
   130  func (h *MainHelper) GetSqlSettings() *model.SqlSettings {
   131  	if h.Settings == nil {
   132  		panic("MainHelper not initialized with database access.")
   133  	}
   134  
   135  	return h.Settings
   136  }
   137  
   138  func (h *MainHelper) GetStore() store.Store {
   139  	if h.Store == nil {
   140  		panic("MainHelper not initialized with store.")
   141  	}
   142  
   143  	return h.Store
   144  }
   145  
   146  func (h *MainHelper) GetSqlSupplier() *sqlstore.SqlSupplier {
   147  	if h.SqlSupplier == nil {
   148  		panic("MainHelper not initialized with sql supplier.")
   149  	}
   150  
   151  	return h.SqlSupplier
   152  }
   153  
   154  func (h *MainHelper) GetClusterInterface() *FakeClusterInterface {
   155  	if h.ClusterInterface == nil {
   156  		panic("MainHelper not initialized with sql supplier.")
   157  	}
   158  
   159  	return h.ClusterInterface
   160  }