code.gitea.io/gitea@v1.19.3/modules/setting/storage_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	ini "gopkg.in/ini.v1"
    11  )
    12  
    13  func Test_getStorageCustomType(t *testing.T) {
    14  	iniStr := `
    15  [attachment]
    16  STORAGE_TYPE = my_minio
    17  MINIO_BUCKET = gitea-attachment
    18  
    19  [storage.my_minio]
    20  STORAGE_TYPE = minio
    21  MINIO_ENDPOINT = my_minio:9000
    22  `
    23  	cfg, err := ini.Load([]byte(iniStr))
    24  	assert.NoError(t, err)
    25  
    26  	sec := cfg.Section("attachment")
    27  	storageType := sec.Key("STORAGE_TYPE").MustString("")
    28  	storage := getStorage(cfg, "attachments", storageType, sec)
    29  
    30  	assert.EqualValues(t, "minio", storage.Type)
    31  	assert.EqualValues(t, "my_minio:9000", storage.Section.Key("MINIO_ENDPOINT").String())
    32  	assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
    33  }
    34  
    35  func Test_getStorageNameSectionOverridesTypeSection(t *testing.T) {
    36  	iniStr := `
    37  [attachment]
    38  STORAGE_TYPE = minio
    39  
    40  [storage.attachments]
    41  MINIO_BUCKET = gitea-attachment
    42  
    43  [storage.minio]
    44  MINIO_BUCKET = gitea
    45  `
    46  	cfg, err := ini.Load([]byte(iniStr))
    47  	assert.NoError(t, err)
    48  
    49  	sec := cfg.Section("attachment")
    50  	storageType := sec.Key("STORAGE_TYPE").MustString("")
    51  	storage := getStorage(cfg, "attachments", storageType, sec)
    52  
    53  	assert.EqualValues(t, "minio", storage.Type)
    54  	assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
    55  }
    56  
    57  func Test_getStorageTypeSectionOverridesStorageSection(t *testing.T) {
    58  	iniStr := `
    59  [attachment]
    60  STORAGE_TYPE = minio
    61  
    62  [storage.minio]
    63  MINIO_BUCKET = gitea-minio
    64  
    65  [storage]
    66  MINIO_BUCKET = gitea
    67  `
    68  	cfg, err := ini.Load([]byte(iniStr))
    69  	assert.NoError(t, err)
    70  
    71  	sec := cfg.Section("attachment")
    72  	storageType := sec.Key("STORAGE_TYPE").MustString("")
    73  	storage := getStorage(cfg, "attachments", storageType, sec)
    74  
    75  	assert.EqualValues(t, "minio", storage.Type)
    76  	assert.EqualValues(t, "gitea-minio", storage.Section.Key("MINIO_BUCKET").String())
    77  }
    78  
    79  func Test_getStorageSpecificOverridesStorage(t *testing.T) {
    80  	iniStr := `
    81  [attachment]
    82  STORAGE_TYPE = minio
    83  MINIO_BUCKET = gitea-attachment
    84  
    85  [storage.attachments]
    86  MINIO_BUCKET = gitea
    87  
    88  [storage]
    89  STORAGE_TYPE = local
    90  `
    91  	cfg, err := ini.Load([]byte(iniStr))
    92  	assert.NoError(t, err)
    93  
    94  	sec := cfg.Section("attachment")
    95  	storageType := sec.Key("STORAGE_TYPE").MustString("")
    96  	storage := getStorage(cfg, "attachments", storageType, sec)
    97  
    98  	assert.EqualValues(t, "minio", storage.Type)
    99  	assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
   100  }
   101  
   102  func Test_getStorageGetDefaults(t *testing.T) {
   103  	cfg, err := ini.Load([]byte(""))
   104  	assert.NoError(t, err)
   105  
   106  	sec := cfg.Section("attachment")
   107  	storageType := sec.Key("STORAGE_TYPE").MustString("")
   108  	storage := getStorage(cfg, "attachments", storageType, sec)
   109  
   110  	assert.EqualValues(t, "gitea", storage.Section.Key("MINIO_BUCKET").String())
   111  }
   112  
   113  func Test_getStorageMultipleName(t *testing.T) {
   114  	iniStr := `
   115  [lfs]
   116  MINIO_BUCKET = gitea-lfs
   117  
   118  [attachment]
   119  MINIO_BUCKET = gitea-attachment
   120  
   121  [storage]
   122  MINIO_BUCKET = gitea-storage
   123  `
   124  	cfg, err := ini.Load([]byte(iniStr))
   125  	assert.NoError(t, err)
   126  
   127  	{
   128  		sec := cfg.Section("attachment")
   129  		storageType := sec.Key("STORAGE_TYPE").MustString("")
   130  		storage := getStorage(cfg, "attachments", storageType, sec)
   131  
   132  		assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
   133  	}
   134  	{
   135  		sec := cfg.Section("lfs")
   136  		storageType := sec.Key("STORAGE_TYPE").MustString("")
   137  		storage := getStorage(cfg, "lfs", storageType, sec)
   138  
   139  		assert.EqualValues(t, "gitea-lfs", storage.Section.Key("MINIO_BUCKET").String())
   140  	}
   141  	{
   142  		sec := cfg.Section("avatar")
   143  		storageType := sec.Key("STORAGE_TYPE").MustString("")
   144  		storage := getStorage(cfg, "avatars", storageType, sec)
   145  
   146  		assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
   147  	}
   148  }
   149  
   150  func Test_getStorageUseOtherNameAsType(t *testing.T) {
   151  	iniStr := `
   152  [attachment]
   153  STORAGE_TYPE = lfs
   154  
   155  [storage.lfs]
   156  MINIO_BUCKET = gitea-storage
   157  `
   158  	cfg, err := ini.Load([]byte(iniStr))
   159  	assert.NoError(t, err)
   160  
   161  	{
   162  		sec := cfg.Section("attachment")
   163  		storageType := sec.Key("STORAGE_TYPE").MustString("")
   164  		storage := getStorage(cfg, "attachments", storageType, sec)
   165  
   166  		assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
   167  	}
   168  	{
   169  		sec := cfg.Section("lfs")
   170  		storageType := sec.Key("STORAGE_TYPE").MustString("")
   171  		storage := getStorage(cfg, "lfs", storageType, sec)
   172  
   173  		assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
   174  	}
   175  }
   176  
   177  func Test_getStorageInheritStorageType(t *testing.T) {
   178  	iniStr := `
   179  [storage]
   180  STORAGE_TYPE = minio
   181  `
   182  	cfg, err := ini.Load([]byte(iniStr))
   183  	assert.NoError(t, err)
   184  
   185  	sec := cfg.Section("attachment")
   186  	storageType := sec.Key("STORAGE_TYPE").MustString("")
   187  	storage := getStorage(cfg, "attachments", storageType, sec)
   188  
   189  	assert.EqualValues(t, "minio", storage.Type)
   190  }
   191  
   192  func Test_getStorageInheritNameSectionType(t *testing.T) {
   193  	iniStr := `
   194  [storage.attachments]
   195  STORAGE_TYPE = minio
   196  `
   197  	cfg, err := ini.Load([]byte(iniStr))
   198  	assert.NoError(t, err)
   199  
   200  	sec := cfg.Section("attachment")
   201  	storageType := sec.Key("STORAGE_TYPE").MustString("")
   202  	storage := getStorage(cfg, "attachments", storageType, sec)
   203  
   204  	assert.EqualValues(t, "minio", storage.Type)
   205  }