Target for cheking not commited config files.

Signed-off-by: laurentiuNiculae <themelopeus@gmail.com>

Separated updateDistSpec functionality

Removed rewriting of config when distSpecVersion was wrong
This commit is contained in:
laurentiuNiculae
2022-03-30 16:02:03 +03:00
committed by Ramkumar Chinchani
parent efc55b013e
commit 0d4cc8736d
4 changed files with 79 additions and 90 deletions
+17 -15
View File
@@ -354,24 +354,23 @@ func applyDefaultValues(config *config.Config, viperInstance *viper.Viper) {
}
}
if config.DistSpecVersion != distspec.Version {
log.Warn().Err(errors.ErrBadConfig).
Msgf("config dist-spec version: %s differs from version actually used: %s, will be corrected automatically",
config.DistSpecVersion, distspec.Version)
// rewrite the config file
viperInstance.Set("distSpecVersion", distspec.Version)
err := viperInstance.WriteConfig()
if err != nil {
log.Warn().Err(errors.ErrBadConfig).
Msg("can't rewrite the config file")
}
config.DistSpecVersion = distspec.Version
if !config.Storage.GC && viperInstance.Get("storage::gcdelay") == nil {
config.Storage.GCDelay = 0
}
}
func updateDistSpecVersion(config *config.Config) {
if config.DistSpecVersion == distspec.Version {
return
}
log.Warn().
Msgf("config dist-spec version: %s differs from version actually used: %s",
config.DistSpecVersion, distspec.Version)
config.DistSpecVersion = distspec.Version
}
func LoadConfiguration(config *config.Config, configPath string) error {
// Default is dot (.) but because we allow glob patterns in authz
// we need another key delimiter.
@@ -413,5 +412,8 @@ func LoadConfiguration(config *config.Config, configPath string) error {
return err
}
// update distSpecVersion
updateDistSpecVersion(config)
return nil
}
+18 -51
View File
@@ -10,7 +10,6 @@ import (
"testing"
"time"
distspec "github.com/opencontainers/distribution-spec/specs-go"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/api"
@@ -354,6 +353,24 @@ func TestGC(t *testing.T) {
err = cli.LoadConfiguration(config, file.Name())
So(err, ShouldNotBeNil)
})
Convey("GC delay when GC = false", func() {
config := config.New()
file, err := ioutil.TempFile("", "gc-false-config-*.json")
So(err, ShouldBeNil)
defer os.Remove(file.Name())
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot",
"gc": false}, "http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false},
"log": {"level": "debug"}}`)
err = ioutil.WriteFile(file.Name(), content, 0o600)
So(err, ShouldBeNil)
err = cli.LoadConfiguration(config, file.Name())
So(err, ShouldBeNil)
So(config.Storage.GCDelay, ShouldEqual, 0)
})
})
}
@@ -533,53 +550,3 @@ func TestScrub(t *testing.T) {
})
})
}
func TestApplyDefaultValues(t *testing.T) {
Convey("Test rewriting of config file if version is wrong", t, func() {
configContent := []byte(`{"distSpecVersion": "1.0.0555", "storage": {"rootDirectory": "/tmp/zot"},
"http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false},
"log": {"level": "debug"}}
`)
oldConfig := config.New()
file, err := ioutil.TempFile("", "default-val-test-config*.json")
So(err, ShouldBeNil)
defer os.Remove(file.Name())
_, err = file.Write(configContent)
So(err, ShouldBeNil)
Convey("The file can be rewritten", func() {
err = os.Chmod(file.Name(), 0o777)
So(err, ShouldBeNil)
err = cli.LoadConfiguration(oldConfig, file.Name())
So(err, ShouldBeNil)
configContent, err = ioutil.ReadFile(file.Name())
So(err, ShouldBeNil)
newConfig := config.New()
err = json.Unmarshal(configContent, newConfig)
So(err, ShouldBeNil)
So(newConfig.DistSpecVersion, ShouldEqual, distspec.Version)
So(newConfig.DistSpecVersion, ShouldEqual, distspec.Version)
})
Convey("The file can't be rewritten", func() {
err = os.Chmod(file.Name(), 0o444)
So(err, ShouldBeNil)
err = cli.LoadConfiguration(oldConfig, file.Name())
So(err, ShouldBeNil)
configContent, err = ioutil.ReadFile(file.Name())
So(err, ShouldBeNil)
newConfig := config.New()
err = json.Unmarshal(configContent, newConfig)
So(err, ShouldBeNil)
So(newConfig.DistSpecVersion, ShouldEqual, "1.0.0555")
So(oldConfig.DistSpecVersion, ShouldEqual, distspec.Version)
})
})
}