github.com/rifflearning/mattermost-server@v5.11.1+incompatible/testlib/resources.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  	"fmt"
     8  	"io/ioutil"
     9  	"os"
    10  	"path"
    11  	"path/filepath"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/pkg/errors"
    15  
    16  	"github.com/mattermost/mattermost-server/utils"
    17  	"github.com/mattermost/mattermost-server/utils/fileutils"
    18  )
    19  
    20  const (
    21  	resourceTypeFile = iota
    22  	resourceTypeFolder
    23  )
    24  
    25  const (
    26  	actionCopy = iota
    27  	actionSymlink
    28  )
    29  
    30  type testResourceDetails struct {
    31  	src     string
    32  	dest    string
    33  	resType int8
    34  	action  int8
    35  }
    36  
    37  func getTestResourcesToSetup() []testResourceDetails {
    38  	var srcPath string
    39  	var found bool
    40  
    41  	var testResourcesToSetup = []testResourceDetails{
    42  		{"i18n", "i18n", resourceTypeFolder, actionSymlink},
    43  		{"templates", "templates", resourceTypeFolder, actionSymlink},
    44  		{"tests", "tests", resourceTypeFolder, actionSymlink},
    45  		{"fonts", "fonts", resourceTypeFolder, actionSymlink},
    46  		{"utils/policies-roles-mapping.json", "utils/policies-roles-mapping.json", resourceTypeFile, actionSymlink},
    47  	}
    48  
    49  	// Finding resources and setting full path to source to be used for further processing
    50  	for i, testResource := range testResourcesToSetup {
    51  		if testResource.resType == resourceTypeFile {
    52  			srcPath = fileutils.FindFile(testResource.src)
    53  			if srcPath == "" {
    54  				panic(fmt.Sprintf("Failed to find file %s", testResource.src))
    55  			}
    56  
    57  			testResourcesToSetup[i].src = srcPath
    58  		} else if testResource.resType == resourceTypeFolder {
    59  			srcPath, found = fileutils.FindDir(testResource.src)
    60  			if found == false {
    61  				panic(fmt.Sprintf("Failed to find folder %s", testResource.src))
    62  			}
    63  
    64  			testResourcesToSetup[i].src = srcPath
    65  		} else {
    66  			panic(fmt.Sprintf("Invalid resource type: %d", testResource.resType))
    67  		}
    68  	}
    69  
    70  	return testResourcesToSetup
    71  }
    72  
    73  func SetupTestResources() (string, error) {
    74  	testResourcesToSetup := getTestResourcesToSetup()
    75  
    76  	tempDir, err := ioutil.TempDir("", "testlib")
    77  	if err != nil {
    78  		return "", errors.Wrap(err, "failed to create temporary directory")
    79  	}
    80  
    81  	pluginsDir := path.Join(tempDir, "plugins")
    82  	err = os.Mkdir(pluginsDir, 0700)
    83  	if err != nil {
    84  		return "", errors.Wrapf(err, "failed to create plugins directory %s", pluginsDir)
    85  	}
    86  
    87  	err = setupConfig(path.Join(tempDir, "config"))
    88  	if err != nil {
    89  		return "", errors.Wrap(err, "failed to setup config")
    90  	}
    91  
    92  	var resourceDestInTemp string
    93  
    94  	// Setting up test resources in temp.
    95  	// Action in each resource tells whether it needs to be copied or just symlinked
    96  	for _, testResource := range testResourcesToSetup {
    97  		resourceDestInTemp = filepath.Join(tempDir, testResource.dest)
    98  
    99  		if testResource.action == actionCopy {
   100  			if testResource.resType == resourceTypeFile {
   101  				err = utils.CopyFile(testResource.src, resourceDestInTemp)
   102  				if err != nil {
   103  					return "", errors.Wrapf(err, "failed to copy file %s to %s", testResource.src, resourceDestInTemp)
   104  				}
   105  			} else if testResource.resType == resourceTypeFolder {
   106  				err = utils.CopyDir(testResource.src, resourceDestInTemp)
   107  				if err != nil {
   108  					return "", errors.Wrapf(err, "failed to copy folder %s to %s", testResource.src, resourceDestInTemp)
   109  				}
   110  			}
   111  		} else if testResource.action == actionSymlink {
   112  			destDir := path.Dir(resourceDestInTemp)
   113  			if destDir != "." {
   114  				err = os.MkdirAll(destDir, os.ModePerm)
   115  				if err != nil {
   116  					return "", errors.Wrapf(err, "failed to make dir %s", destDir)
   117  				}
   118  			}
   119  
   120  			err = os.Symlink(testResource.src, resourceDestInTemp)
   121  			if err != nil {
   122  				return "", errors.Wrapf(err, "failed to symlink %s to %s", testResource.src, resourceDestInTemp)
   123  			}
   124  		} else {
   125  			return "", errors.Wrapf(err, "Invalid action: %d", testResource.action)
   126  		}
   127  
   128  	}
   129  
   130  	return tempDir, nil
   131  }
   132  
   133  func setupConfig(configDir string) error {
   134  	var err error
   135  	var config model.Config
   136  
   137  	config.SetDefaults()
   138  
   139  	err = os.Mkdir(configDir, 0700)
   140  	if err != nil {
   141  		return errors.Wrapf(err, "failed to create config directory %s", configDir)
   142  	}
   143  
   144  	defaultJson := path.Join(configDir, "default.json")
   145  	err = ioutil.WriteFile(defaultJson, []byte(config.ToJson()), 0644)
   146  	if err != nil {
   147  		return errors.Wrapf(err, "failed to write config to %s", defaultJson)
   148  	}
   149  
   150  	configJson := path.Join(configDir, "config.json")
   151  	err = utils.CopyFile(defaultJson, configJson)
   152  	if err != nil {
   153  		return errors.Wrapf(err, "failed to copy file %s to %s", defaultJson, configJson)
   154  	}
   155  
   156  	return nil
   157  }