-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-3723] Unit tests and cleanup for core/peer pkg
Added missing test coverage for config.go,removed unused code and cleaned up a bit to aid in eventually removing test code from peer.go. Increased # lines covered from 51% to 78% Change-Id: I0249d3c9edf6f764b2c1b66dc4af3af1048430a4 Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
- Loading branch information
1 parent
0fe5cb2
commit fdb6ce1
Showing
5 changed files
with
207 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package peer | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCacheConfigurationNegative(t *testing.T) { | ||
|
||
// set a bad peer.address | ||
viper.Set("peer.addressAutoDetect", true) | ||
viper.Set("peer.address", "testing.com") | ||
cacheConfiguration() | ||
err := CacheConfiguration() | ||
assert.Error(t, err, "Expected error for bad configuration") | ||
} | ||
|
||
func TestConfiguration(t *testing.T) { | ||
|
||
var ips []string | ||
// get the interface addresses | ||
if addresses, err := net.InterfaceAddrs(); err == nil { | ||
for _, address := range addresses { | ||
// eliminate loopback interfaces | ||
if ip, ok := address.(*net.IPNet); ok && !ip.IP.IsLoopback() { | ||
ips = append(ips, ip.IP.String()+":7051") | ||
t.Logf("found interface address [%s]", ip.IP.String()) | ||
} | ||
} | ||
} else { | ||
t.Fatal("Failed to get interface addresses") | ||
} | ||
|
||
var tests = []struct { | ||
name string | ||
settings map[string]interface{} | ||
validAddresses []string | ||
invalidAddresses []string | ||
}{ | ||
{ | ||
name: "test1", | ||
settings: map[string]interface{}{ | ||
"peer.addressAutoDetect": false, | ||
"peer.address": "testing.com:7051", | ||
"peer.id": "testPeer", | ||
}, | ||
validAddresses: []string{"testing.com:7051"}, | ||
invalidAddresses: ips, | ||
}, | ||
{ | ||
name: "test2", | ||
settings: map[string]interface{}{ | ||
"peer.addressAutoDetect": true, | ||
"peer.address": "testing.com:7051", | ||
"peer.id": "testPeer", | ||
}, | ||
validAddresses: ips, | ||
invalidAddresses: []string{"testing.com:7051"}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
for k, v := range test.settings { | ||
viper.Set(k, v) | ||
} | ||
// reset the cache | ||
configurationCached = false | ||
// GetLocalAddress | ||
address, err := GetLocalAddress() | ||
assert.NoError(t, err, "GetLocalAddress returned unexpected error") | ||
assert.Contains(t, test.validAddresses, address, | ||
"GetLocalAddress returned unexpected address") | ||
assert.NotContains(t, test.invalidAddresses, address, | ||
"GetLocalAddress returned invalid address") | ||
// reset the cache | ||
configurationCached = false | ||
// GetPeerEndpoint | ||
pe, err := GetPeerEndpoint() | ||
assert.NoError(t, err, "GetPeerEndpoint returned unexpected error") | ||
assert.Equal(t, test.settings["peer.id"], pe.Id.Name, | ||
"GetPeerEndpoint returned the wrong peer ID") | ||
assert.Equal(t, address, pe.Address, | ||
"GetPeerEndpoint returned the wrong peer address") | ||
|
||
// now check with cached configuration | ||
err = CacheConfiguration() | ||
assert.NoError(t, err, "CacheConfiguration should not have returned an err") | ||
// check functions again | ||
// GetLocalAddress | ||
address, err = GetLocalAddress() | ||
assert.NoError(t, err, "GetLocalAddress should not have returned error") | ||
assert.Contains(t, test.validAddresses, address, | ||
"GetLocalAddress returned unexpected address") | ||
assert.NotContains(t, test.invalidAddresses, address, | ||
"GetLocalAddress returned invalid address") | ||
// GetPeerEndpoint | ||
pe, err = GetPeerEndpoint() | ||
assert.NoError(t, err, "GetPeerEndpoint returned unexpected error") | ||
assert.Equal(t, test.settings["peer.id"], pe.Id.Name, | ||
"GetPeerEndpoint returned the wrong peer ID") | ||
assert.Equal(t, address, pe.Address, | ||
"GetPeerEndpoint returned the wrong peer address") | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters