From cb284ac5b21693bb5534c6c1fc4ccf0570f8d73d Mon Sep 17 00:00:00 2001 From: Junjie Gao Date: Tue, 14 Jan 2025 07:04:59 +0000 Subject: [PATCH] test: update test Signed-off-by: Junjie Gao --- pkg/configutil/once_test.go | 22 ++++++++++++++++++++++ pkg/configutil/util_test.go | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/pkg/configutil/once_test.go b/pkg/configutil/once_test.go index 5f6ebdef..315dd12d 100644 --- a/pkg/configutil/once_test.go +++ b/pkg/configutil/once_test.go @@ -17,12 +17,18 @@ import ( "os" "path/filepath" "strings" + "sync" "testing" + "github.com/notaryproject/notation-go/config" "github.com/notaryproject/notation-go/dir" + "github.com/notaryproject/notation/internal/envelope" ) func TestLoadConfigOnce(t *testing.T) { + defer func() { + LoadConfigOnce = resetLoadConfigOnce() + }() config1, err := LoadConfigOnce() if err != nil { t.Fatal("LoadConfigOnce failed.") @@ -40,6 +46,7 @@ func TestLoadConfigOnceError(t *testing.T) { dir.UserConfigDir = t.TempDir() defer func() { dir.UserConfigDir = "" + LoadConfigOnce = resetLoadConfigOnce() }() if err := os.WriteFile(filepath.Join(dir.UserConfigDir, dir.PathConfigFile), []byte("invalid json"), 0600); err != nil { t.Fatal("Failed to create file.") @@ -54,3 +61,18 @@ func TestLoadConfigOnceError(t *testing.T) { t.Fatal("LoadConfigOnce should return the same error.") } } + +func resetLoadConfigOnce() func() (*config.Config, error) { + return sync.OnceValues(func() (*config.Config, error) { + configInfo, err := config.LoadConfig() + if err != nil { + return nil, err + } + // set default value + configInfo.SignatureFormat = strings.ToLower(configInfo.SignatureFormat) + if configInfo.SignatureFormat == "" { + configInfo.SignatureFormat = envelope.JWS + } + return configInfo, nil + }) +} diff --git a/pkg/configutil/util_test.go b/pkg/configutil/util_test.go index dda8b8ed..6f21266f 100644 --- a/pkg/configutil/util_test.go +++ b/pkg/configutil/util_test.go @@ -27,6 +27,7 @@ func TestIsRegistryInsecure(t *testing.T) { // for restore dir defer func(oldDir string) { dir.UserConfigDir = oldDir + LoadConfigOnce = resetLoadConfigOnce() }(dir.UserConfigDir) // update config dir dir.UserConfigDir = "testdata" @@ -56,6 +57,7 @@ func TestIsRegistryInsecureMissingConfig(t *testing.T) { // for restore dir defer func(oldDir string) { dir.UserConfigDir = oldDir + LoadConfigOnce = resetLoadConfigOnce() }(dir.UserConfigDir) // update config dir dir.UserConfigDir = "./testdata2" @@ -88,6 +90,7 @@ func TestIsRegistryInsecureConfigPermissionError(t *testing.T) { defer func(oldDir string) error { // restore permission dir.UserConfigDir = oldDir + LoadConfigOnce = resetLoadConfigOnce() return os.Chmod(filepath.Join(configDir, "config.json"), 0644) }(dir.UserConfigDir) @@ -107,6 +110,7 @@ func TestIsRegistryInsecureConfigPermissionError(t *testing.T) { func TestResolveKey(t *testing.T) { defer func(oldDir string) { dir.UserConfigDir = oldDir + LoadConfigOnce = resetLoadConfigOnce() }(dir.UserConfigDir) t.Run("valid e2e key", func(t *testing.T) {