github.com/turingchain2020/turingchain@v1.1.21/cmd/autotest/testflow/config.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 testflow
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"sync"
    11  	"time"
    12  
    13  	"github.com/turingchain2020/turingchain/cmd/autotest/types"
    14  	"github.com/turingchain2020/turingchain/common/log/log15"
    15  	"github.com/BurntSushi/toml"
    16  )
    17  
    18  var (
    19  	fileLog = log15.New()
    20  	stdLog  = log15.New()
    21  )
    22  
    23  //contract type
    24  /*
    25  trc,
    26  token,
    27  trade,
    28  
    29  */
    30  
    31  //TestCaseFile 测试用例文件
    32  type TestCaseFile struct {
    33  	Dapp     string `toml:"dapp"`
    34  	Filename string `toml:"filename"`
    35  }
    36  
    37  //TestCaseConfig 测试用例配置
    38  type TestCaseConfig struct {
    39  	CliCommand      string         `toml:"cliCmd"`
    40  	CheckTimeout    int            `toml:"checkTimeout"`
    41  	TestCaseFileArr []TestCaseFile `toml:"TestCaseFile"`
    42  }
    43  
    44  //AutoTestResult 自动测试结果
    45  type AutoTestResult struct {
    46  	dapp       string
    47  	totalCase  int
    48  	failCase   int
    49  	failCaseID []string
    50  }
    51  
    52  //var
    53  var (
    54  	configFile     string
    55  	resultChan     = make(chan *AutoTestResult, 1)
    56  	testResultArr  = make([]*AutoTestResult, 0)
    57  	autoTestConfig = &TestCaseConfig{}
    58  
    59  	checkSleepTime = 1 //second, s
    60  )
    61  
    62  //TestRunner 测试接口
    63  type TestRunner interface {
    64  	RunTest(tomlFile string, wg *sync.WaitGroup)
    65  }
    66  
    67  //InitFlowConfig 初始化配置
    68  func InitFlowConfig(conf string, log string) {
    69  
    70  	fileLog.SetHandler(log15.Must.FileHandler(log, types.AutoTestLogFormat()))
    71  	configFile = conf
    72  
    73  }
    74  
    75  //StartAutoTest 自动测试
    76  func StartAutoTest() bool {
    77  
    78  	stdLog.Info("[================================BeginAutoTest===============================]")
    79  	fileLog.Info("[================================BeginAutoTest===============================]")
    80  	var wg sync.WaitGroup
    81  
    82  	if _, err := toml.DecodeFile(configFile, &autoTestConfig); err != nil {
    83  
    84  		stdLog.Error("TomlDecodeAutoTestConfig", "Filename", configFile, "Err", err.Error())
    85  		return false
    86  	}
    87  
    88  	if len(autoTestConfig.CliCommand) == 0 {
    89  
    90  		stdLog.Error("NullTuringchainCli")
    91  		return false
    92  	}
    93  	//init types
    94  	types.Init(autoTestConfig.CliCommand, autoTestConfig.CheckTimeout/checkSleepTime)
    95  
    96  	for _, caseFile := range autoTestConfig.TestCaseFileArr {
    97  
    98  		wg.Add(1)
    99  		go newTestFlow(caseFile.Dapp, caseFile.Filename, &wg)
   100  	}
   101  
   102  	//collect test results
   103  	go func() {
   104  		for {
   105  			res, more := <-resultChan
   106  			if more {
   107  				testResultArr = append(testResultArr, res)
   108  			} else {
   109  				return
   110  			}
   111  		}
   112  
   113  	}()
   114  
   115  	wg.Wait()
   116  	close(resultChan)
   117  	time.Sleep(1 * time.Second)
   118  
   119  	//log with test result
   120  	bSuccess := true
   121  	fmt.Println("==================================AutoTestResultSummary======================================")
   122  	fileLog.Info("====================================AutoTestResultSummary========================================")
   123  	for _, res := range testResultArr {
   124  
   125  		if res.failCase > 0 {
   126  
   127  			bSuccess = false
   128  			stdLog.Error("TestFailed", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase, "FailID", res.failCaseID)
   129  			fileLog.Error("TestFailed", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase, "FailID", res.failCaseID)
   130  		} else {
   131  
   132  			stdLog.Info("TestSuccess", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase)
   133  			fileLog.Info("TestSuccess", "dapp", res.dapp, "TotalCase", res.totalCase, "TotalFail", res.failCase)
   134  		}
   135  	}
   136  
   137  	return bSuccess
   138  }
   139  
   140  func newTestFlow(dapp string, filename string, wg *sync.WaitGroup) {
   141  
   142  	defer wg.Done()
   143  
   144  	configType := types.GetAutoTestConfig(dapp)
   145  
   146  	if configType == nil {
   147  
   148  		stdLog.Error("GetAutoTestConfigType", "DappName", dapp, "Error", "NotSupportAutoTestType")
   149  		return
   150  	}
   151  
   152  	if configType.Kind() == reflect.Ptr {
   153  		configType = configType.Elem()
   154  	}
   155  
   156  	caseConf := reflect.New(configType)
   157  
   158  	if _, err := toml.DecodeFile(filename, caseConf.Interface()); err != nil {
   159  
   160  		stdLog.Error("TomlDecodeTestCaseFile", "Dapp", dapp, "Filename", filename, "Error", err.Error())
   161  		return
   162  	}
   163  
   164  	//get config fields
   165  	fields := caseConf.Elem().NumField()
   166  	caseArrList := make([]interface{}, fields)
   167  
   168  	for i := 0; i < fields; i++ {
   169  
   170  		caseArrList[i] = caseConf.Elem().Field(i).Interface()
   171  	}
   172  
   173  	tester := NewTestOperator(stdLog, fileLog, dapp)
   174  
   175  	go tester.AddCaseArray(caseArrList...)
   176  	go tester.HandleDependency()
   177  	go tester.RunSendFlow()
   178  	go tester.RunCheckFlow()
   179  
   180  	testRes := tester.WaitTest()
   181  	resultChan <- testRes
   182  
   183  }