github.com/turingchain2020/turingchain@v1.1.21/cmd/autotest/types/baseCase.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import (
     8  	"encoding/json"
     9  	"errors"
    10  
    11  	"github.com/turingchain2020/turingchain/common/log/log15"
    12  	turingchainType "github.com/turingchain2020/turingchain/system/dapp/commands/types"
    13  )
    14  
    15  //CaseFunc interface for testCase
    16  type CaseFunc interface {
    17  	//获取用例id
    18  	GetID() string
    19  	//获取命令行
    20  	GetCmd() string
    21  	//获取依赖数组
    22  	GetDep() []string
    23  	//获取重复次数
    24  	GetRepeat() int
    25  	//直接获取基类类型指针,方便获取所有成员
    26  	GetBaseCase() *BaseCase
    27  	//一个用例的输入依赖于另一个用例输出,设置依赖的输出数据
    28  	SetDependData(interface{})
    29  	//执行用例命令,并返回用例打包结构
    30  	SendCommand(packID string) (PackFunc, error)
    31  }
    32  
    33  //PackFunc interface for check testCase result
    34  type PackFunc interface {
    35  	//获取id
    36  	GetPackID() string
    37  	//设置id
    38  	SetPackID(id string)
    39  	//获取用例的基类指针
    40  	GetBaseCase() *BaseCase
    41  	//获取交易哈希
    42  	GetTxHash() string
    43  	//获取交易回执,json字符串
    44  	GetTxReceipt() string
    45  	//获取基类打包类型指针,提供直接访问成员
    46  	GetBasePack() *BaseCasePack
    47  	//设置log15日志
    48  	SetLogger(fLog log15.Logger, tLog log15.Logger)
    49  	//获取check函数字典
    50  	GetCheckHandlerMap() interface{}
    51  	//获取依赖数据
    52  	GetDependData() interface{}
    53  	//执行结果check
    54  	CheckResult(interface{}) (bool, bool)
    55  }
    56  
    57  //BaseCase base test case
    58  type BaseCase struct {
    59  	ID        string   `toml:"id"`
    60  	Command   string   `toml:"command"`
    61  	Dep       []string `toml:"dep,omitempty"`
    62  	CheckItem []string `toml:"checkItem,omitempty"`
    63  	Repeat    int      `toml:"repeat,omitempty"`
    64  	Fail      bool     `toml:"fail,omitempty"`
    65  }
    66  
    67  //check item handler
    68  //适配autotest早期版本,handlerfunc的参数为json的map形式,后续统一使用turingchain的TxDetailResult结构体结构
    69  
    70  //CheckHandlerFuncDiscard 检查func
    71  type CheckHandlerFuncDiscard func(map[string]interface{}) bool
    72  
    73  //CheckHandlerMapDiscard 检查map
    74  type CheckHandlerMapDiscard map[string]CheckHandlerFuncDiscard
    75  
    76  //建议使用
    77  
    78  //CheckHandlerParamType 检查参数类型
    79  type CheckHandlerParamType *turingchainType.TxDetailResult
    80  
    81  //CheckHandlerFunc 检查func
    82  type CheckHandlerFunc func(CheckHandlerParamType) bool
    83  
    84  //CheckHandlerMap 检查map
    85  type CheckHandlerMap map[string]CheckHandlerFunc
    86  
    87  //BaseCasePack pack testCase with some check info
    88  type BaseCasePack struct {
    89  	TCase      CaseFunc
    90  	CheckTimes int
    91  	TxHash     string
    92  	TxReceipt  string
    93  	PackID     string
    94  	FLog       log15.Logger
    95  	TLog       log15.Logger
    96  }
    97  
    98  //DefaultSend default send command implementation, only for transaction type case
    99  func DefaultSend(testCase CaseFunc, testPack PackFunc, packID string) (PackFunc, error) {
   100  
   101  	baseCase := testCase.GetBaseCase()
   102  	txHash, bSuccess := SendTxCommand(baseCase.Command)
   103  	if !bSuccess {
   104  		return nil, errors.New(txHash)
   105  	}
   106  	pack := testPack.GetBasePack()
   107  	pack.TxHash = txHash
   108  	pack.TCase = testCase
   109  
   110  	pack.PackID = packID
   111  	pack.CheckTimes = 0
   112  	return testPack, nil
   113  }
   114  
   115  //SendCommand interface CaseFunc implementing by BaseCase
   116  func (t *BaseCase) SendCommand(packID string) (PackFunc, error) {
   117  	return nil, nil
   118  }
   119  
   120  //GetID 获取id
   121  func (t *BaseCase) GetID() string {
   122  
   123  	return t.ID
   124  }
   125  
   126  //GetCmd 获取cmd
   127  func (t *BaseCase) GetCmd() string {
   128  
   129  	return t.Command
   130  }
   131  
   132  //GetDep 获取dep
   133  func (t *BaseCase) GetDep() []string {
   134  
   135  	return t.Dep
   136  }
   137  
   138  //GetRepeat 获取repeat
   139  func (t *BaseCase) GetRepeat() int {
   140  
   141  	return t.Repeat
   142  }
   143  
   144  //GetBaseCase 获取基础用例
   145  func (t *BaseCase) GetBaseCase() *BaseCase {
   146  
   147  	return t
   148  }
   149  
   150  //SetDependData 获取依赖数据
   151  func (t *BaseCase) SetDependData(interface{}) {
   152  
   153  }
   154  
   155  //interface PackFunc implementing by BaseCasePack
   156  
   157  //GetPackID 获取pack id
   158  func (pack *BaseCasePack) GetPackID() string {
   159  
   160  	return pack.PackID
   161  }
   162  
   163  //SetPackID 设置pack id
   164  func (pack *BaseCasePack) SetPackID(id string) {
   165  
   166  	pack.PackID = id
   167  }
   168  
   169  //GetBaseCase 获取基础用例
   170  func (pack *BaseCasePack) GetBaseCase() *BaseCase {
   171  
   172  	return pack.TCase.GetBaseCase()
   173  }
   174  
   175  //GetTxHash 获取交易hash
   176  func (pack *BaseCasePack) GetTxHash() string {
   177  
   178  	return pack.TxHash
   179  }
   180  
   181  //GetTxReceipt 获取交易接收方
   182  func (pack *BaseCasePack) GetTxReceipt() string {
   183  
   184  	return pack.TxReceipt
   185  }
   186  
   187  //SetLogger 设置日志
   188  func (pack *BaseCasePack) SetLogger(fLog log15.Logger, tLog log15.Logger) {
   189  
   190  	pack.FLog = fLog
   191  	pack.TLog = tLog
   192  }
   193  
   194  //GetBasePack 获取基础pack
   195  func (pack *BaseCasePack) GetBasePack() *BaseCasePack {
   196  
   197  	return pack
   198  }
   199  
   200  //GetDependData 获取依赖数据
   201  func (pack *BaseCasePack) GetDependData() interface{} {
   202  
   203  	return nil
   204  }
   205  
   206  //GetCheckHandlerMap 获取map
   207  func (pack *BaseCasePack) GetCheckHandlerMap() interface{} {
   208  
   209  	//return make(map[string]CheckHandlerFunc, 1)
   210  	return nil
   211  }
   212  
   213  //CheckResult 检查结果
   214  func (pack *BaseCasePack) CheckResult(handlerMap interface{}) (bCheck bool, bSuccess bool) {
   215  
   216  	bCheck = false
   217  	bSuccess = false
   218  
   219  	tCase := pack.TCase.GetBaseCase()
   220  	txInfo, bReady := GetTxInfo(pack.TxHash)
   221  
   222  	if !bReady && (txInfo != "tx not exist\n" || pack.CheckTimes >= CheckTimeout) {
   223  
   224  		pack.TxReceipt = txInfo
   225  		pack.FLog.Error("CheckTimeout", "TestID", pack.PackID, "ErrInfo", txInfo)
   226  		pack.TxReceipt = txInfo
   227  		return true, false
   228  	}
   229  
   230  	if bReady {
   231  
   232  		bCheck = true
   233  		var tyname string
   234  		var jsonMap map[string]interface{}
   235  		var txRecp turingchainType.TxDetailResult
   236  		pack.TxReceipt = txInfo
   237  		pack.FLog.Info("TxReceiptJson", "TestID", pack.PackID)
   238  		//hack, for pretty json log
   239  		pack.FLog.Info("PrettyJsonLogFormat", "TxReceipt", []byte(txInfo))
   240  		//适配前期的接口
   241  		err := json.Unmarshal([]byte(txInfo), &jsonMap)
   242  		err1 := json.Unmarshal([]byte(txInfo), &txRecp)
   243  
   244  		if err != nil || err1 != nil {
   245  
   246  			pack.FLog.Error("UnMarshalFailed", "TestID", pack.PackID, "jsonStr", txInfo, "ErrInfo", err.Error())
   247  			return true, false
   248  		}
   249  
   250  		tyname, bSuccess = GetTxRecpTyname(jsonMap)
   251  		pack.FLog.Info("CheckItemResult", "TestID", pack.PackID, "RecpTyname", tyname)
   252  
   253  		if !bSuccess {
   254  
   255  			logArr := jsonMap["receipt"].(map[string]interface{})["logs"].([]interface{})
   256  			logErrInfo := ""
   257  			for _, log := range logArr {
   258  
   259  				logMap := log.(map[string]interface{})
   260  
   261  				if logMap["tyName"].(string) == "LogErr" {
   262  
   263  					logErrInfo = logMap["log"].(string)
   264  					break
   265  				}
   266  			}
   267  			pack.FLog.Error("ExecPack", "TestID", pack.PackID,
   268  				"LogErrInfo", logErrInfo)
   269  
   270  		} else {
   271  
   272  			//为了兼容前期autotest的map形式交易回执
   273  			if funcMap, ok := handlerMap.(CheckHandlerMapDiscard); ok {
   274  
   275  				for _, item := range tCase.CheckItem {
   276  
   277  					checkHandler, exist := funcMap[item]
   278  					if exist {
   279  
   280  						itemRes := checkHandler(jsonMap)
   281  						bSuccess = bSuccess && itemRes
   282  						pack.FLog.Info("CheckItemResult", "TestID", pack.PackID, "Item", item, "Passed", itemRes)
   283  					}
   284  				}
   285  
   286  			} else if funcMap, ok := handlerMap.(CheckHandlerMap); ok { //采用结构体形式回执
   287  
   288  				for _, item := range tCase.CheckItem {
   289  
   290  					checkHandler, exist := funcMap[item]
   291  					if exist {
   292  
   293  						itemRes := checkHandler(&txRecp)
   294  						bSuccess = bSuccess && itemRes
   295  						pack.FLog.Info("CheckItemResult", "TestID", pack.PackID, "Item", item, "Passed", itemRes)
   296  					}
   297  				}
   298  			}
   299  		}
   300  	}
   301  
   302  	pack.CheckTimes++
   303  	return bCheck, bSuccess
   304  }