@@ -18,41 +18,50 @@ package config
18
18
19
19
import (
20
20
"fmt"
21
+ "io/ioutil"
21
22
"os"
23
+ "path/filepath"
22
24
"testing"
23
25
"time"
24
26
25
- "github.com/hyperledger/fabric/common/viperutil"
26
- cf "github.com/hyperledger/fabric/core/config"
27
-
28
- "github.com/spf13/viper"
29
27
"github.com/stretchr/testify/assert"
30
28
)
31
29
32
30
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" )
38
32
}
39
33
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 )
44
39
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" )
48
57
}
49
58
50
- var uconf struct {}
59
+ envVar1 := "FABRIC_CFG_PATH"
60
+ envVal1 := name
61
+ os .Setenv (envVar1 , envVal1 )
62
+ defer os .Unsetenv (envVar1 )
51
63
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" )
56
65
}
57
66
58
67
// TestEnvInnerVar verifies that with the Unmarshal function that
@@ -70,17 +79,11 @@ func TestEnvInnerVar(t *testing.T) {
70
79
defer os .Unsetenv (envVar2 )
71
80
config := Load ()
72
81
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" )
76
84
77
- if config .General .ListenPort != envVal1 {
78
- t .Fatalf ("Environmental override of inner config test 1 did not work" )
79
- }
80
85
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" )
84
87
}
85
88
86
89
const DummyPath = "/dummy/path"
@@ -107,3 +110,9 @@ func TestKafkaTLSConfig(t *testing.T) {
107
110
})
108
111
}
109
112
}
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