github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/services/filesstore/filesstore_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package filesstore
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  	"github.com/stretchr/testify/suite"
    15  
    16  	"github.com/crspeller/mattermost-server/mlog"
    17  	"github.com/crspeller/mattermost-server/model"
    18  	"github.com/crspeller/mattermost-server/utils"
    19  )
    20  
    21  type FileBackendTestSuite struct {
    22  	suite.Suite
    23  
    24  	settings model.FileSettings
    25  	backend  FileBackend
    26  }
    27  
    28  func TestLocalFileBackendTestSuite(t *testing.T) {
    29  	// Setup a global logger to catch tests logging outside of app context
    30  	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
    31  	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
    32  		EnableConsole: true,
    33  		ConsoleJson:   true,
    34  		ConsoleLevel:  "error",
    35  		EnableFile:    false,
    36  	}))
    37  
    38  	dir, err := ioutil.TempDir("", "")
    39  	require.NoError(t, err)
    40  	defer os.RemoveAll(dir)
    41  
    42  	suite.Run(t, &FileBackendTestSuite{
    43  		settings: model.FileSettings{
    44  			DriverName: model.NewString(model.IMAGE_DRIVER_LOCAL),
    45  			Directory:  &dir,
    46  		},
    47  	})
    48  }
    49  
    50  func TestS3FileBackendTestSuite(t *testing.T) {
    51  	runBackendTest(t, false)
    52  }
    53  
    54  func TestS3FileBackendTestSuiteWithEncryption(t *testing.T) {
    55  	runBackendTest(t, true)
    56  }
    57  
    58  func runBackendTest(t *testing.T, encrypt bool) {
    59  	s3Host := os.Getenv("CI_MINIO_HOST")
    60  	if s3Host == "" {
    61  		s3Host = "dockerhost"
    62  	}
    63  
    64  	s3Port := os.Getenv("CI_MINIO_PORT")
    65  	if s3Port == "" {
    66  		s3Port = "9001"
    67  	}
    68  
    69  	s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port)
    70  
    71  	suite.Run(t, &FileBackendTestSuite{
    72  		settings: model.FileSettings{
    73  			DriverName:              model.NewString(model.IMAGE_DRIVER_S3),
    74  			AmazonS3AccessKeyId:     model.NewString(model.MINIO_ACCESS_KEY),
    75  			AmazonS3SecretAccessKey: model.NewString(model.MINIO_SECRET_KEY),
    76  			AmazonS3Bucket:          model.NewString(model.MINIO_BUCKET),
    77  			AmazonS3Region:          model.NewString(""),
    78  			AmazonS3Endpoint:        model.NewString(s3Endpoint),
    79  			AmazonS3SSL:             model.NewBool(false),
    80  			AmazonS3SSE:             model.NewBool(encrypt),
    81  		},
    82  	})
    83  }
    84  
    85  func (s *FileBackendTestSuite) SetupTest() {
    86  	utils.TranslationsPreInit()
    87  
    88  	backend, err := NewFileBackend(&s.settings, true)
    89  	require.Nil(s.T(), err)
    90  	s.backend = backend
    91  }
    92  
    93  func (s *FileBackendTestSuite) TestConnection() {
    94  	s.Nil(s.backend.TestConnection())
    95  }
    96  
    97  func (s *FileBackendTestSuite) TestReadWriteFile() {
    98  	b := []byte("test")
    99  	path := "tests/" + model.NewId()
   100  
   101  	written, err := s.backend.WriteFile(bytes.NewReader(b), path)
   102  	s.Nil(err)
   103  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   104  	defer s.backend.RemoveFile(path)
   105  
   106  	read, err := s.backend.ReadFile(path)
   107  	s.Nil(err)
   108  
   109  	readString := string(read)
   110  	s.EqualValues(readString, "test")
   111  }
   112  
   113  func (s *FileBackendTestSuite) TestReadWriteFileImage() {
   114  	b := []byte("testimage")
   115  	path := "tests/" + model.NewId() + ".png"
   116  
   117  	written, err := s.backend.WriteFile(bytes.NewReader(b), path)
   118  	s.Nil(err)
   119  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   120  	defer s.backend.RemoveFile(path)
   121  
   122  	read, err := s.backend.ReadFile(path)
   123  	s.Nil(err)
   124  
   125  	readString := string(read)
   126  	s.EqualValues(readString, "testimage")
   127  }
   128  
   129  func (s *FileBackendTestSuite) TestFileExists() {
   130  	b := []byte("testimage")
   131  	path := "tests/" + model.NewId() + ".png"
   132  
   133  	_, err := s.backend.WriteFile(bytes.NewReader(b), path)
   134  	s.Nil(err)
   135  	defer s.backend.RemoveFile(path)
   136  
   137  	res, err := s.backend.FileExists(path)
   138  	s.Nil(err)
   139  	s.True(res)
   140  
   141  	res, err = s.backend.FileExists("tests/idontexist.png")
   142  	s.Nil(err)
   143  	s.False(res)
   144  }
   145  
   146  func (s *FileBackendTestSuite) TestCopyFile() {
   147  	b := []byte("test")
   148  	path1 := "tests/" + model.NewId()
   149  	path2 := "tests/" + model.NewId()
   150  
   151  	written, err := s.backend.WriteFile(bytes.NewReader(b), path1)
   152  	s.Nil(err)
   153  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   154  	defer s.backend.RemoveFile(path1)
   155  
   156  	err = s.backend.CopyFile(path1, path2)
   157  	s.Nil(err)
   158  	defer s.backend.RemoveFile(path2)
   159  
   160  	_, err = s.backend.ReadFile(path1)
   161  	s.Nil(err)
   162  
   163  	_, err = s.backend.ReadFile(path2)
   164  	s.Nil(err)
   165  }
   166  
   167  func (s *FileBackendTestSuite) TestCopyFileToDirectoryThatDoesntExist() {
   168  	b := []byte("test")
   169  	path1 := "tests/" + model.NewId()
   170  	path2 := "tests/newdirectory/" + model.NewId()
   171  
   172  	written, err := s.backend.WriteFile(bytes.NewReader(b), path1)
   173  	s.Nil(err)
   174  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   175  	defer s.backend.RemoveFile(path1)
   176  
   177  	err = s.backend.CopyFile(path1, path2)
   178  	s.Nil(err)
   179  	defer s.backend.RemoveFile(path2)
   180  
   181  	_, err = s.backend.ReadFile(path1)
   182  	s.Nil(err)
   183  
   184  	_, err = s.backend.ReadFile(path2)
   185  	s.Nil(err)
   186  }
   187  
   188  func (s *FileBackendTestSuite) TestMoveFile() {
   189  	b := []byte("test")
   190  	path1 := "tests/" + model.NewId()
   191  	path2 := "tests/" + model.NewId()
   192  
   193  	written, err := s.backend.WriteFile(bytes.NewReader(b), path1)
   194  	s.Nil(err)
   195  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   196  	defer s.backend.RemoveFile(path1)
   197  
   198  	s.Nil(s.backend.MoveFile(path1, path2))
   199  	defer s.backend.RemoveFile(path2)
   200  
   201  	_, err = s.backend.ReadFile(path1)
   202  	s.Error(err)
   203  
   204  	_, err = s.backend.ReadFile(path2)
   205  	s.Nil(err)
   206  }
   207  
   208  func (s *FileBackendTestSuite) TestRemoveFile() {
   209  	b := []byte("test")
   210  	path := "tests/" + model.NewId()
   211  
   212  	written, err := s.backend.WriteFile(bytes.NewReader(b), path)
   213  	s.Nil(err)
   214  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   215  	s.Nil(s.backend.RemoveFile(path))
   216  
   217  	_, err = s.backend.ReadFile(path)
   218  	s.Error(err)
   219  
   220  	written, err = s.backend.WriteFile(bytes.NewReader(b), "tests2/foo")
   221  	s.Nil(err)
   222  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   223  
   224  	written, err = s.backend.WriteFile(bytes.NewReader(b), "tests2/bar")
   225  	s.Nil(err)
   226  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   227  
   228  	written, err = s.backend.WriteFile(bytes.NewReader(b), "tests2/asdf")
   229  	s.Nil(err)
   230  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   231  
   232  	s.Nil(s.backend.RemoveDirectory("tests2"))
   233  }
   234  
   235  func (s *FileBackendTestSuite) TestListDirectory() {
   236  	b := []byte("test")
   237  	path1 := "19700101/" + model.NewId()
   238  	path2 := "19800101/" + model.NewId()
   239  
   240  	written, err := s.backend.WriteFile(bytes.NewReader(b), path1)
   241  	s.Nil(err)
   242  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   243  	defer s.backend.RemoveFile(path1)
   244  
   245  	written, err = s.backend.WriteFile(bytes.NewReader(b), path2)
   246  	s.Nil(err)
   247  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   248  	defer s.backend.RemoveFile(path2)
   249  
   250  	paths, err := s.backend.ListDirectory("")
   251  	s.Nil(err)
   252  
   253  	found1 := false
   254  	found2 := false
   255  	for _, path := range *paths {
   256  		if path == "19700101" {
   257  			found1 = true
   258  		} else if path == "19800101" {
   259  			found2 = true
   260  		}
   261  	}
   262  	s.True(found1)
   263  	s.True(found2)
   264  }
   265  
   266  func (s *FileBackendTestSuite) TestRemoveDirectory() {
   267  	b := []byte("test")
   268  
   269  	written, err := s.backend.WriteFile(bytes.NewReader(b), "tests2/foo")
   270  	s.Nil(err)
   271  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   272  
   273  	written, err = s.backend.WriteFile(bytes.NewReader(b), "tests2/bar")
   274  	s.Nil(err)
   275  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   276  
   277  	written, err = s.backend.WriteFile(bytes.NewReader(b), "tests2/aaa")
   278  	s.Nil(err)
   279  	s.EqualValues(len(b), written, "expected given number of bytes to have been written")
   280  
   281  	s.Nil(s.backend.RemoveDirectory("tests2"))
   282  
   283  	_, err = s.backend.ReadFile("tests2/foo")
   284  	s.Error(err)
   285  	_, err = s.backend.ReadFile("tests2/bar")
   286  	s.Error(err)
   287  	_, err = s.backend.ReadFile("tests2/asdf")
   288  	s.Error(err)
   289  }