github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/peer/config_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package peer 17 18 import ( 19 "net" 20 "testing" 21 22 "github.com/spf13/viper" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestCacheConfigurationNegative(t *testing.T) { 27 28 // set a bad peer.address 29 viper.Set("peer.addressAutoDetect", true) 30 viper.Set("peer.address", "testing.com") 31 cacheConfiguration() 32 err := CacheConfiguration() 33 assert.Error(t, err, "Expected error for bad configuration") 34 } 35 36 func TestConfiguration(t *testing.T) { 37 38 var ips []string 39 // get the interface addresses 40 if addresses, err := net.InterfaceAddrs(); err == nil { 41 for _, address := range addresses { 42 // eliminate loopback interfaces 43 if ip, ok := address.(*net.IPNet); ok && !ip.IP.IsLoopback() { 44 ips = append(ips, ip.IP.String()+":7051") 45 t.Logf("found interface address [%s]", ip.IP.String()) 46 } 47 } 48 } else { 49 t.Fatal("Failed to get interface addresses") 50 } 51 52 var tests = []struct { 53 name string 54 settings map[string]interface{} 55 validAddresses []string 56 invalidAddresses []string 57 }{ 58 { 59 name: "test1", 60 settings: map[string]interface{}{ 61 "peer.addressAutoDetect": false, 62 "peer.address": "testing.com:7051", 63 "peer.id": "testPeer", 64 }, 65 validAddresses: []string{"testing.com:7051"}, 66 invalidAddresses: ips, 67 }, 68 { 69 name: "test2", 70 settings: map[string]interface{}{ 71 "peer.addressAutoDetect": true, 72 "peer.address": "testing.com:7051", 73 "peer.id": "testPeer", 74 }, 75 validAddresses: ips, 76 invalidAddresses: []string{"testing.com:7051"}, 77 }, 78 } 79 80 for _, test := range tests { 81 test := test 82 t.Run(test.name, func(t *testing.T) { 83 for k, v := range test.settings { 84 viper.Set(k, v) 85 } 86 // reset the cache 87 configurationCached = false 88 // GetLocalAddress 89 address, err := GetLocalAddress() 90 assert.NoError(t, err, "GetLocalAddress returned unexpected error") 91 assert.Contains(t, test.validAddresses, address, 92 "GetLocalAddress returned unexpected address") 93 assert.NotContains(t, test.invalidAddresses, address, 94 "GetLocalAddress returned invalid address") 95 // reset the cache 96 configurationCached = false 97 // GetPeerEndpoint 98 pe, err := GetPeerEndpoint() 99 assert.NoError(t, err, "GetPeerEndpoint returned unexpected error") 100 assert.Equal(t, test.settings["peer.id"], pe.Id.Name, 101 "GetPeerEndpoint returned the wrong peer ID") 102 assert.Equal(t, address, pe.Address, 103 "GetPeerEndpoint returned the wrong peer address") 104 105 // now check with cached configuration 106 err = CacheConfiguration() 107 assert.NoError(t, err, "CacheConfiguration should not have returned an err") 108 // check functions again 109 // GetLocalAddress 110 address, err = GetLocalAddress() 111 assert.NoError(t, err, "GetLocalAddress should not have returned error") 112 assert.Contains(t, test.validAddresses, address, 113 "GetLocalAddress returned unexpected address") 114 assert.NotContains(t, test.invalidAddresses, address, 115 "GetLocalAddress returned invalid address") 116 // GetPeerEndpoint 117 pe, err = GetPeerEndpoint() 118 assert.NoError(t, err, "GetPeerEndpoint returned unexpected error") 119 assert.Equal(t, test.settings["peer.id"], pe.Id.Name, 120 "GetPeerEndpoint returned the wrong peer ID") 121 assert.Equal(t, address, pe.Address, 122 "GetPeerEndpoint returned the wrong peer address") 123 }) 124 } 125 }