Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 14, 2025
1 parent 574f9af commit cb284ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/configutil/once_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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.")
Expand All @@ -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
})
}
4 changes: 4 additions & 0 deletions pkg/configutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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) {
Expand Down

0 comments on commit cb284ac

Please sign in to comment.