zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/api/config/config_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"zotregistry.io/zot/pkg/api/config"
    10  )
    11  
    12  func TestConfig(t *testing.T) {
    13  	Convey("Test config utils", t, func() {
    14  		firstStorageConfig := config.StorageConfig{
    15  			GC: true, Dedupe: true,
    16  			GCDelay: 1 * time.Minute, GCInterval: 1 * time.Hour,
    17  		}
    18  		secondStorageConfig := config.StorageConfig{
    19  			GC: true, Dedupe: true,
    20  			GCDelay: 1 * time.Minute, GCInterval: 1 * time.Hour,
    21  		}
    22  
    23  		So(firstStorageConfig.ParamsEqual(secondStorageConfig), ShouldBeTrue)
    24  
    25  		firstStorageConfig.GC = false
    26  
    27  		So(firstStorageConfig.ParamsEqual(secondStorageConfig), ShouldBeFalse)
    28  
    29  		firstStorageConfig.GC = true
    30  		firstStorageConfig.Dedupe = false
    31  
    32  		So(firstStorageConfig.ParamsEqual(secondStorageConfig), ShouldBeFalse)
    33  
    34  		firstStorageConfig.Dedupe = true
    35  		firstStorageConfig.GCDelay = 2 * time.Minute
    36  
    37  		So(firstStorageConfig.ParamsEqual(secondStorageConfig), ShouldBeFalse)
    38  
    39  		firstStorageConfig.GCDelay = 1 * time.Minute
    40  		firstStorageConfig.GCInterval = 2 * time.Hour
    41  
    42  		So(firstStorageConfig.ParamsEqual(secondStorageConfig), ShouldBeFalse)
    43  
    44  		firstStorageConfig.GCInterval = 1 * time.Hour
    45  
    46  		So(firstStorageConfig.ParamsEqual(secondStorageConfig), ShouldBeTrue)
    47  
    48  		isSame, err := config.SameFile("test-config", "test")
    49  		So(err, ShouldNotBeNil)
    50  		So(isSame, ShouldBeFalse)
    51  
    52  		dir1 := t.TempDir()
    53  
    54  		isSame, err = config.SameFile(dir1, "test")
    55  		So(err, ShouldNotBeNil)
    56  		So(isSame, ShouldBeFalse)
    57  
    58  		dir2 := t.TempDir()
    59  
    60  		isSame, err = config.SameFile(dir1, dir2)
    61  		So(err, ShouldBeNil)
    62  		So(isSame, ShouldBeFalse)
    63  
    64  		isSame, err = config.SameFile(dir1, dir1)
    65  		So(err, ShouldBeNil)
    66  		So(isSame, ShouldBeTrue)
    67  	})
    68  
    69  	Convey("Test DeepCopy() & Sanitize()", t, func() {
    70  		conf := config.New()
    71  		So(conf, ShouldNotBeNil)
    72  		authConfig := &config.AuthConfig{LDAP: (&config.LDAPConfig{}).SetBindPassword("oina")}
    73  		conf.HTTP.Auth = authConfig
    74  		So(func() { conf.Sanitize() }, ShouldNotPanic)
    75  		conf = conf.Sanitize()
    76  		So(conf.HTTP.Auth.LDAP.BindPassword(), ShouldEqual, "******")
    77  
    78  		// negative
    79  		obj := make(chan int)
    80  		err := config.DeepCopy(conf, obj)
    81  		So(err, ShouldNotBeNil)
    82  		err = config.DeepCopy(obj, conf)
    83  		So(err, ShouldNotBeNil)
    84  	})
    85  
    86  	Convey("Test IsRetentionEnabled()", t, func() {
    87  		conf := config.New()
    88  		So(conf.IsRetentionEnabled(), ShouldBeFalse)
    89  
    90  		conf.Storage.Retention.Policies = []config.RetentionPolicy{
    91  			{
    92  				Repositories: []string{"repo"},
    93  			},
    94  		}
    95  
    96  		So(conf.IsRetentionEnabled(), ShouldBeFalse)
    97  
    98  		policies := []config.RetentionPolicy{
    99  			{
   100  				Repositories: []string{"repo"},
   101  				KeepTags: []config.KeepTagsPolicy{
   102  					{
   103  						Patterns:                []string{"tag"},
   104  						MostRecentlyPulledCount: 2,
   105  					},
   106  				},
   107  			},
   108  		}
   109  
   110  		conf.Storage.Retention = config.ImageRetention{
   111  			Policies: policies,
   112  		}
   113  
   114  		So(conf.IsRetentionEnabled(), ShouldBeTrue)
   115  
   116  		subPaths := make(map[string]config.StorageConfig)
   117  
   118  		subPaths["/a"] = config.StorageConfig{
   119  			GC: true,
   120  			Retention: config.ImageRetention{
   121  				Policies: policies,
   122  			},
   123  		}
   124  
   125  		conf.Storage.SubPaths = subPaths
   126  
   127  		So(conf.IsRetentionEnabled(), ShouldBeTrue)
   128  	})
   129  }