Skip to content

Commit 930bf5f

Browse files
committed
[FAB-4299] Improve UT coverage of orderer/localconfig
Change-Id: I13eb4502eebd1e16bc3217b73c8c3670e8ebad0c Signed-off-by: Jay Guo <[email protected]>
1 parent 3b40efa commit 930bf5f

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

orderer/localconfig/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (c *TopLevel) completeInitialization(configDir string) {
224224
logger.Panicf("General.Kafka.TLS.PrivateKey must be set if General.Kafka.TLS.Enabled is set to true.")
225225
case c.Kafka.TLS.Enabled && c.Kafka.TLS.RootCAs == nil:
226226
logger.Panicf("General.Kafka.TLS.CertificatePool must be set if General.Kafka.TLS.Enabled is set to true.")
227-
case c.General.Profile.Enabled && (c.General.Profile.Address == ""):
227+
case c.General.Profile.Enabled && c.General.Profile.Address == "":
228228
logger.Infof("Profiling enabled and General.Profile.Address unset, setting to %s", defaults.General.Profile.Address)
229229
c.General.Profile.Address = defaults.General.Profile.Address
230230
case c.General.LocalMSPDir == "":
@@ -252,7 +252,7 @@ func (c *TopLevel) completeInitialization(configDir string) {
252252
}
253253

254254
func translateCAs(configDir string, certificateAuthorities []string) []string {
255-
results := make([]string, 0)
255+
var results []string
256256
for _, ca := range certificateAuthorities {
257257
result := cf.TranslatePath(configDir, ca)
258258
results = append(results, result)

orderer/localconfig/config_test.go

+39-30
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,50 @@ package config
1818

1919
import (
2020
"fmt"
21+
"io/ioutil"
2122
"os"
23+
"path/filepath"
2224
"testing"
2325
"time"
2426

25-
"github.com/hyperledger/fabric/common/viperutil"
26-
cf "github.com/hyperledger/fabric/core/config"
27-
28-
"github.com/spf13/viper"
2927
"github.com/stretchr/testify/assert"
3028
)
3129

3230
func TestGoodConfig(t *testing.T) {
33-
config := Load()
34-
if config == nil {
35-
t.Fatalf("Could not load config")
36-
}
37-
t.Logf("%+v", config)
31+
assert.NotNil(t, Load(), "Could not load config")
3832
}
3933

40-
func TestBadConfig(t *testing.T) {
41-
config := viper.New()
42-
config.SetConfigName("orderer")
43-
cf.AddDevConfigPath(config)
34+
func TestMissingConfigFile(t *testing.T) {
35+
envVar1 := "FABRIC_CFG_PATH"
36+
envVal1 := "invalid fabric cfg path"
37+
os.Setenv(envVar1, envVal1)
38+
defer os.Unsetenv(envVar1)
4439

45-
err := config.ReadInConfig()
46-
if err != nil {
47-
t.Fatalf("Error reading %s plugin config: %s", Prefix, err)
40+
assert.Panics(t, func() { Load() }, "Should panic")
41+
}
42+
43+
func TestMalformedConfigFile(t *testing.T) {
44+
name, err := ioutil.TempDir("", "hyperledger_fabric")
45+
assert.Nil(t, err, "Error creating temp dir: %s", err)
46+
defer func() {
47+
err = os.RemoveAll(name)
48+
assert.Nil(t, os.RemoveAll(name), "Error removing temp dir: %s", err)
49+
}()
50+
51+
{
52+
// Create a malformed orderer.yaml file in temp dir
53+
f, err := os.OpenFile(filepath.Join(name, "orderer.yaml"), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
54+
assert.Nil(t, err, "Error creating file: %s", err)
55+
f.WriteString("General: 42")
56+
assert.NoError(t, f.Close(), "Error closing file")
4857
}
4958

50-
var uconf struct{}
59+
envVar1 := "FABRIC_CFG_PATH"
60+
envVal1 := name
61+
os.Setenv(envVar1, envVal1)
62+
defer os.Unsetenv(envVar1)
5163

52-
err = viperutil.EnhancedExactUnmarshal(config, &uconf)
53-
if err == nil {
54-
t.Fatalf("Should have failed to unmarshal")
55-
}
64+
assert.Panics(t, func() { Load() }, "Should panic")
5665
}
5766

5867
// TestEnvInnerVar verifies that with the Unmarshal function that
@@ -70,17 +79,11 @@ func TestEnvInnerVar(t *testing.T) {
7079
defer os.Unsetenv(envVar2)
7180
config := Load()
7281

73-
if config == nil {
74-
t.Fatalf("Could not load config")
75-
}
82+
assert.NotNil(t, config, "Could not load config")
83+
assert.Equal(t, config.General.ListenPort, envVal1, "Environmental override of inner config test 1 did not work")
7684

77-
if config.General.ListenPort != envVal1 {
78-
t.Fatalf("Environmental override of inner config test 1 did not work")
79-
}
8085
v2, _ := time.ParseDuration(envVal2)
81-
if config.Kafka.Retry.Period != v2 {
82-
t.Fatalf("Environmental override of inner config test 2 did not work")
83-
}
86+
assert.Equal(t, config.Kafka.Retry.Period, v2, "Environmental override of inner config test 2 did not work")
8487
}
8588

8689
const DummyPath = "/dummy/path"
@@ -107,3 +110,9 @@ func TestKafkaTLSConfig(t *testing.T) {
107110
})
108111
}
109112
}
113+
114+
func TestProfileConfig(t *testing.T) {
115+
uconf := &TopLevel{General: General{Profile: Profile{Enabled: true}}}
116+
uconf.completeInitialization(DummyPath)
117+
assert.Equal(t, defaults.General.Profile.Address, uconf.General.Profile.Address, "Expected profile address to be filled with default value")
118+
}

0 commit comments

Comments
 (0)