Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAB-17508] Removing viper library from viperutil #1511

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 24 additions & 49 deletions common/viperutil/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/Shopify/sarama"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/orderer/mocks/util"
"github.com/spf13/viper"
)

const Prefix = "VIPERUTIL"
Expand All @@ -34,12 +33,8 @@ func TestEnvSlice(t *testing.T) {
envVal := "[a, b, c]"
os.Setenv(envVar, envVal)
defer os.Unsetenv(envVar)
config := viper.New()
config := New()
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
config.SetConfigType("yaml")

data := "---\nInner:\n Slice: [d,e,f]"

Expand All @@ -50,7 +45,7 @@ func TestEnvSlice(t *testing.T) {
}

var uconf testSlice
if err := EnhancedExactUnmarshal(config, &uconf); err != nil {
if err := config.EnhancedExactUnmarshal(&uconf); err != nil {
t.Fatalf("Failed to unmarshal with: %s", err)
}

Expand All @@ -67,8 +62,7 @@ func TestKafkaVersionDecode(t *testing.T) {
}
}

config := viper.New()
config.SetConfigType("yaml")
config := New()

testCases := []struct {
data string
Expand Down Expand Up @@ -114,7 +108,7 @@ func TestKafkaVersionDecode(t *testing.T) {
}

var uconf testKafkaVersion
err = EnhancedExactUnmarshal(config, &uconf)
err = config.EnhancedExactUnmarshal(&uconf)

if tc.errExpected {
if err == nil {
Expand All @@ -141,8 +135,7 @@ type testByteSize struct {
}

func TestByteSize(t *testing.T) {
config := viper.New()
config.SetConfigType("yaml")
config := New()

testCases := []struct {
data string
Expand Down Expand Up @@ -178,7 +171,7 @@ func TestByteSize(t *testing.T) {
t.Fatalf("Error reading config: %s", err)
}
var uconf testByteSize
err = EnhancedExactUnmarshal(config, &uconf)
err = config.EnhancedExactUnmarshal(&uconf)
if err != nil {
t.Fatalf("Failed to unmarshal with: %s", err)
}
Expand All @@ -190,16 +183,15 @@ func TestByteSize(t *testing.T) {
}

func TestByteSizeOverflow(t *testing.T) {
config := viper.New()
config.SetConfigType("yaml")
config := New()

data := "---\nInner:\n ByteSize: 4GB"
err := config.ReadConfig(bytes.NewReader([]byte(data)))
if err != nil {
t.Fatalf("Error reading config: %s", err)
}
var uconf testByteSize
err = EnhancedExactUnmarshal(config, &uconf)
err = config.EnhancedExactUnmarshal(&uconf)
if err == nil {
t.Fatalf("Should have failed to unmarshal")
}
Expand All @@ -217,15 +209,14 @@ func TestStringNotFromFile(t *testing.T) {
expectedValue := "expected_value"
yaml := fmt.Sprintf("---\nInner:\n Single: %s\n", expectedValue)

config := viper.New()
config.SetConfigType("yaml")
config := New()

if err := config.ReadConfig(bytes.NewReader([]byte(yaml))); err != nil {
t.Fatalf("Error reading config: %s", err)
}

var uconf stringFromFileConfig
if err := EnhancedExactUnmarshal(config, &uconf); err != nil {
if err := config.EnhancedExactUnmarshal(&uconf); err != nil {
t.Fatalf("Failed to unmarshall: %s", err)
}

Expand Down Expand Up @@ -253,14 +244,13 @@ func TestStringFromFile(t *testing.T) {

yaml := fmt.Sprintf("---\nInner:\n Single:\n File: %s", file.Name())

config := viper.New()
config.SetConfigType("yaml")
config := New()

if err = config.ReadConfig(bytes.NewReader([]byte(yaml))); err != nil {
t.Fatalf("Error reading config: %s", err)
}
var uconf stringFromFileConfig
if err = EnhancedExactUnmarshal(config, &uconf); err != nil {
if err = config.EnhancedExactUnmarshal(&uconf); err != nil {
t.Fatalf("Failed to unmarshall: %s", err)
}

Expand Down Expand Up @@ -292,14 +282,13 @@ func TestPEMBlocksFromFile(t *testing.T) {

yaml := fmt.Sprintf("---\nInner:\n Multiple:\n File: %s", file.Name())

config := viper.New()
config.SetConfigType("yaml")
config := New()

if err := config.ReadConfig(bytes.NewReader([]byte(yaml))); err != nil {
t.Fatalf("Error reading config: %v", err)
}
var uconf stringFromFileConfig
if err := EnhancedExactUnmarshal(config, &uconf); err != nil {
if err := config.EnhancedExactUnmarshal(&uconf); err != nil {
t.Fatalf("Failed to unmarshall: %v", err)
}

Expand Down Expand Up @@ -345,18 +334,14 @@ func TestPEMBlocksFromFileEnv(t *testing.T) {
envVal := file.Name()
os.Setenv(envVar, envVal)
defer os.Unsetenv(envVar)
config := viper.New()
config := New()
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
config.SetConfigType("yaml")

if err := config.ReadConfig(bytes.NewReader([]byte(tc.data))); err != nil {
t.Fatalf("Error reading config: %v", err)
}
var uconf stringFromFileConfig
if err := EnhancedExactUnmarshal(config, &uconf); err != nil {
if err := config.EnhancedExactUnmarshal(&uconf); err != nil {
t.Fatalf("Failed to unmarshall: %v", err)
}

Expand All @@ -370,14 +355,13 @@ func TestPEMBlocksFromFileEnv(t *testing.T) {
func TestStringFromFileNotSpecified(t *testing.T) {
yaml := "---\nInner:\n Single:\n File:\n"

config := viper.New()
config.SetConfigType("yaml")
config := New()

if err := config.ReadConfig(bytes.NewReader([]byte(yaml))); err != nil {
t.Fatalf("Error reading config: %s", err)
}
var uconf stringFromFileConfig
if err := EnhancedExactUnmarshal(config, &uconf); err == nil {
if err := config.EnhancedExactUnmarshal(&uconf); err == nil {
t.Fatalf("Should of failed to unmarshall.")
}
}
Expand Down Expand Up @@ -411,20 +395,16 @@ func TestStringFromFileEnv(t *testing.T) {
envVal := file.Name()
os.Setenv(envVar, envVal)
defer os.Unsetenv(envVar)
config := viper.New()
config := New()
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
config.SetConfigType("yaml")

if err = config.ReadConfig(bytes.NewReader([]byte(tc.data))); err != nil {
t.Fatalf("Error reading %s plugin config: %s", Prefix, err)
}

var uconf stringFromFileConfig

err = EnhancedExactUnmarshal(config, &uconf)
err = config.EnhancedExactUnmarshal(&uconf)
if err != nil {
t.Fatalf("Failed to unmarshal with: %s", err)
}
Expand All @@ -444,16 +424,15 @@ Foo: bar
Hello:
World: 42
`
config := viper.New()
config.SetConfigType("yaml")
config := New()
if err := config.ReadConfig(bytes.NewReader([]byte(yaml))); err != nil {
t.Fatalf("Error reading config: %s", err)
}
var conf struct {
Foo string
Hello struct{ World int }
}
if err := EnhancedExactUnmarshal(config, &conf); err != nil {
if err := config.EnhancedExactUnmarshal(&conf); err != nil {
t.Fatalf("Error unmarshalling: %s", err)
}

Expand All @@ -473,12 +452,8 @@ BCCSP:
Security: 999
`

config := viper.New()
config := New()
config.SetEnvPrefix("VIPERUTIL")
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
config.SetConfigType("yaml")

overrideVar := "VIPERUTIL_BCCSP_SW_SECURITY"
os.Setenv(overrideVar, "1111")
Expand All @@ -488,7 +463,7 @@ BCCSP:
}

var tc testConfig
if err := EnhancedExactUnmarshal(config, &tc); err != nil {
if err := config.EnhancedExactUnmarshal(&tc); err != nil {
t.Fatalf("Error unmarshaling: %s", err)
}

Expand Down
Loading