github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/storage_test.go (about)

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