Commit a6a07ce8 authored by Matthew Sykes's avatar Matthew Sykes
Browse files

[FAB-10686] remove ledger assert functions



Change-Id: I6cd12bf46fbf3ba53a716ad325210cfe421ff16d
Signed-off-by: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent d82dffb2
......@@ -17,6 +17,7 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
ptestutils "github.com/hyperledger/fabric/protos/testutils"
"github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)
//BlockGenerator generates a series of blocks for testing
......@@ -30,7 +31,7 @@ type BlockGenerator struct {
// NewBlockGenerator instantiates new BlockGenerator for testing
func NewBlockGenerator(t *testing.T, ledgerID string, signTxs bool) (*BlockGenerator, *common.Block) {
gb, err := test.MakeGenesisBlock(ledgerID)
AssertNoError(t, err, "")
assert.NoError(t, err)
gb.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlagsSetValue(len(gb.Data.Data), pb.TxValidationCode_VALID)
return &BlockGenerator{1, gb.GetHeader().Hash(), signTxs, t}, gb
}
......
......@@ -20,137 +20,12 @@ import (
"archive/tar"
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
"github.com/golang/protobuf/proto"
)
// AssertNil varifies that the value is nil
func AssertNil(t testing.TB, value interface{}) {
if !isNil(value) {
t.Fatalf("Value not nil. value=[%#v]\n %s", value, getCallerInfo())
}
}
// AssertNotNil varifies that the value is not nil
func AssertNotNil(t testing.TB, value interface{}) {
if isNil(value) {
t.Fatalf("Values is nil. %s", getCallerInfo())
}
}
// AssertSame varifies that the two values are same
func AssertSame(t testing.TB, actual interface{}, expected interface{}) {
t.Logf("%s: AssertSame [%#v] and [%#v]", getCallerInfo(), actual, expected)
if actual != expected {
t.Fatalf("Values actual=[%#v] and expected=[%#v] do not point to same object. %s", actual, expected, getCallerInfo())
}
}
// AssertEquals varifies that the two values are equal
func AssertEquals(t testing.TB, actual interface{}, expected interface{}) {
t.Logf("%s: AssertEquals [%#v] and [%#v]", getCallerInfo(), actual, expected)
if expected == nil && isNil(actual) {
return
}
//convert JSONs to maps, this will allow comparison by DeepEqual
actual = convertJSONToMap(actual)
expected = convertJSONToMap(expected)
if pa, ok := actual.(proto.Message); ok {
if pe, ok := actual.(proto.Message); ok {
if proto.Equal(pa, pe) {
return
}
}
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Values are not equal.\n Actual=[%#v], \n Expected=[%#v]\n %s", actual, expected, getCallerInfo())
}
}
// convertJSONToMap will convert a JSON in a byte array to a MAP
func convertJSONToMap(value interface{}) interface{} {
var valueMap map[string]interface{}
valueBytes, ok := value.([]byte)
if ok {
if json.Unmarshal(valueBytes, &valueMap) == nil {
return valueMap
}
}
return value
}
// AssertNotEquals varifies that the two values are not equal
func AssertNotEquals(t testing.TB, actual interface{}, expected interface{}) {
if reflect.DeepEqual(actual, expected) {
t.Fatalf("Values are not supposed to be equal. Actual=[%#v], Expected=[%#v]\n %s", actual, expected, getCallerInfo())
}
}
// AssertError varifies that the err is not nil
func AssertError(t testing.TB, err error, message string) {
if err == nil {
t.Fatalf("%s\n %s", message, getCallerInfo())
}
}
// AssertNoError varifies that the err is nil
func AssertNoError(t testing.TB, err error, message string) {
if err != nil {
t.Fatalf("%s - Error: %s\n %s", message, err, getCallerInfo())
}
}
// AssertContains varifies that the slice contains the value
func AssertContains(t testing.TB, slice interface{}, value interface{}) {
if reflect.TypeOf(slice).Kind() != reflect.Slice && reflect.TypeOf(slice).Kind() != reflect.Array {
t.Fatalf("Type of argument 'slice' is expected to be a slice/array, found =[%s]\n %s", reflect.TypeOf(slice), getCallerInfo())
}
if !Contains(slice, value) {
t.Fatalf("Expected value [%s] not found in slice %s\n %s", value, slice, getCallerInfo())
}
}
// AssertContainsAll varifies that sliceActual is a superset of sliceExpected
func AssertContainsAll(t testing.TB, sliceActual interface{}, sliceExpected interface{}) {
if reflect.TypeOf(sliceActual).Kind() != reflect.Slice && reflect.TypeOf(sliceActual).Kind() != reflect.Array {
t.Fatalf("Type of argument 'sliceActual' is expected to be a slice/array, found =[%s]\n %s", reflect.TypeOf(sliceActual), getCallerInfo())
}
if reflect.TypeOf(sliceExpected).Kind() != reflect.Slice && reflect.TypeOf(sliceExpected).Kind() != reflect.Array {
t.Fatalf("Type of argument 'sliceExpected' is expected to be a slice/array, found =[%s]\n %s", reflect.TypeOf(sliceExpected), getCallerInfo())
}
array := reflect.ValueOf(sliceExpected)
for i := 0; i < array.Len(); i++ {
element := array.Index(i).Interface()
if !Contains(sliceActual, element) {
t.Fatalf("Expected value [%s] not found in slice %s\n %s", element, sliceActual, getCallerInfo())
}
}
}
// AssertPanic varifies that a panic is raised during a test
func AssertPanic(t testing.TB, msg string) {
x := recover()
if x == nil {
t.Fatal(msg)
} else {
t.Logf("A panic was caught successfully. Actual msg = %s", x)
}
}
// ConstructRandomBytes constructs random bytes of given size
func ConstructRandomBytes(t testing.TB, size int) []byte {
value := make([]byte, size)
......@@ -161,30 +36,6 @@ func ConstructRandomBytes(t testing.TB, size int) []byte {
return value
}
// Contains returns true iff the `value` is present in the `slice`
func Contains(slice interface{}, value interface{}) bool {
array := reflect.ValueOf(slice)
for i := 0; i < array.Len(); i++ {
element := array.Index(i).Interface()
if value == element || reflect.DeepEqual(element, value) {
return true
}
}
return false
}
func isNil(in interface{}) bool {
return in == nil || reflect.ValueOf(in).IsNil() || (reflect.TypeOf(in).Kind() == reflect.Slice && reflect.ValueOf(in).Len() == 0)
}
func getCallerInfo() string {
_, file, line, ok := runtime.Caller(2)
if !ok {
return "Could not retrieve caller's info"
}
return fmt.Sprintf("CallerInfo = [%s:%d]", file, line)
}
// TarFileEntry is a structure for adding test index files to an tar
type TarFileEntry struct {
Name, Body string
......
/*
Copyright IBM Corp. 2016 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 testutil
import "testing"
func TestSkipAll(t *testing.T) {
t.Skip(`No tests in this package for now - This package contains only utility functions that are meant to be used by other functional tests`)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment