github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/daemon/config/builder_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types/filters"
     8  	"github.com/google/go-cmp/cmp"
     9  	"gotest.tools/v3/assert"
    10  	"gotest.tools/v3/fs"
    11  )
    12  
    13  func TestBuilderGC(t *testing.T) {
    14  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{
    15    "builder": {
    16      "gc": {
    17        "enabled": true,
    18        "policy": [
    19          {"keepStorage": "10GB", "filter": ["unused-for=2200h"]},
    20          {"keepStorage": "50GB", "filter": {"unused-for": {"3300h": true}}},
    21          {"keepStorage": "100GB", "all": true}
    22        ]
    23      }
    24    }
    25  }`))
    26  	defer tempFile.Remove()
    27  	configFile := tempFile.Path()
    28  
    29  	cfg, err := MergeDaemonConfigurations(&Config{}, nil, configFile)
    30  	assert.NilError(t, err)
    31  	assert.Assert(t, cfg.Builder.GC.Enabled)
    32  	f1 := filters.NewArgs()
    33  	f1.Add("unused-for", "2200h")
    34  	f2 := filters.NewArgs()
    35  	f2.Add("unused-for", "3300h")
    36  	expectedPolicy := []BuilderGCRule{
    37  		{KeepStorage: "10GB", Filter: BuilderGCFilter(f1)},
    38  		{KeepStorage: "50GB", Filter: BuilderGCFilter(f2)}, /* parsed from deprecated form */
    39  		{KeepStorage: "100GB", All: true},
    40  	}
    41  	assert.DeepEqual(t, cfg.Builder.GC.Policy, expectedPolicy, cmp.AllowUnexported(BuilderGCFilter{}))
    42  	// double check to please the skeptics
    43  	assert.Assert(t, filters.Args(cfg.Builder.GC.Policy[0].Filter).UniqueExactMatch("unused-for", "2200h"))
    44  	assert.Assert(t, filters.Args(cfg.Builder.GC.Policy[1].Filter).UniqueExactMatch("unused-for", "3300h"))
    45  }
    46  
    47  // TestBuilderGCFilterUnmarshal is a regression test for https://github.com/moby/moby/issues/44361,
    48  // where and incorrectly formatted gc filter option ("unused-for2200h",
    49  // missing a "=" separator). resulted in a panic during unmarshal.
    50  func TestBuilderGCFilterUnmarshal(t *testing.T) {
    51  	var cfg BuilderGCConfig
    52  	err := json.Unmarshal([]byte(`{"poliCy": [{"keepStorage": "10GB", "filter": ["unused-for2200h"]}]}`), &cfg)
    53  	assert.Check(t, err)
    54  	expectedPolicy := []BuilderGCRule{{
    55  		KeepStorage: "10GB", Filter: BuilderGCFilter(filters.NewArgs(filters.Arg("unused-for2200h", ""))),
    56  	}}
    57  	assert.DeepEqual(t, cfg.Policy, expectedPolicy, cmp.AllowUnexported(BuilderGCFilter{}))
    58  }