github.com/loomnetwork/gamechain@v0.0.0-20200406110549-36c47eb97a92/e2e/e2e_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path"
     9  	"path/filepath"
    10  	"strconv"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/loomnetwork/e2e/common"
    16  	assert "github.com/stretchr/testify/require"
    17  )
    18  
    19  type testdata struct {
    20  	name       string
    21  	testFile   string
    22  	validators int
    23  	accounts   int
    24  	genFile    string
    25  	replacementTokens map[string]string
    26  }
    27  
    28  var (
    29  	isZbCliBuilt = false
    30  	isGamechainBuilt = false
    31  )
    32  
    33  func getLoomchainDir() string {
    34  	goPath := os.Getenv("GOPATH")
    35  	return path.Join(goPath, "src", "github.com/loomnetwork/loomchain")
    36  }
    37  
    38  func buildGamechain(t *testing.T) {
    39  	if isGamechainBuilt {
    40  		return
    41  	}
    42  
    43  	// build loomchain with gamechain compiled in
    44  	if err := runCommand(getLoomchainDir(), "make", "gamechain"); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	isGamechainBuilt = true
    49  }
    50  
    51  func buildZbCli( t *testing.T) {
    52  	if isZbCliBuilt {
    53  		return
    54  	}
    55  
    56  	currentDir, err := os.Getwd()
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	if err := runCommand(filepath.Dir(currentDir), "make", "cli", "-B"); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	isZbCliBuilt = true
    66  }
    67  
    68  func runCommand(workingDir, binary string, args ...string) error {
    69  	binary, err := exec.LookPath(binary)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	currentDir, err := os.Getwd()
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	//noinspection ALL
    80  	defer os.Chdir(currentDir)
    81  	err = os.Chdir(workingDir)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	args = append([]string{binary}, args...)
    87  
    88  	cmd := exec.Cmd{
    89  		Path: binary,
    90  		Args: args,
    91  	}
    92  	combinedOutput, err := cmd.CombinedOutput()
    93  	if err != nil {
    94  		return fmt.Errorf("fail to execute command: %s\n%v\n%s", strings.Join(cmd.Args, " "), err, string(combinedOutput))
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  func runE2E(t *testing.T, name string, testFile string, validators int, accounts int, genFile string) {
   101  	singleNode, _ := strconv.ParseBool(os.Getenv("SINGLENODE"))
   102  	fmt.Printf("Running in SINGLENODE mode: %t\n", singleNode)
   103  
   104  	// skip multi-node tests?
   105  	if singleNode && validators > 1 {
   106  		return
   107  	}
   108  
   109  	config, err := common.NewConfig(name, testFile, genFile, validators, accounts)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	buildZbCli(t)
   115  
   116  	// copy zb-cli to basedir
   117  	currentDir, err := os.Getwd()
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	zbCliBytes, err := ioutil.ReadFile(path.Join(currentDir, "..", "bin", "zb-cli"))
   123  	assert.Nil(t, err)
   124  	err = ioutil.WriteFile(path.Join(config.BaseDir, "zb-cli"), zbCliBytes, os.ModePerm)
   125  	assert.Nil(t, err)
   126  
   127  	if err := common.DoRun(*config); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	// pause before running the next test
   132  	time.Sleep(3000 * time.Millisecond)
   133  
   134  	// clean up test data if successful
   135  	//noinspection ALL
   136  	os.RemoveAll(config.BaseDir)
   137  }
   138  
   139  func prepareTestFile(t *testing.T, testFile string, replacementTokens map[string]string) string {
   140  	data, err := ioutil.ReadFile(testFile)
   141  	assert.Nil(t, err)
   142  	str := string(data)
   143  
   144  	if len(replacementTokens) > 0 {
   145  		for tokenKey, tokenValue := range replacementTokens {
   146  			tokenKey = "{{" + tokenKey + "}}"
   147  
   148  			str = strings.Replace(str, tokenKey, tokenValue, -1)
   149  		}
   150  	}
   151  
   152  	_, testFileName := filepath.Split(testFile)
   153  
   154  	tempFile, err := ioutil.TempFile("", testFileName)
   155  	assert.Nil(t, err)
   156  	_, err = tempFile.WriteString(str)
   157  	assert.Nil(t, err)
   158  	//noinspection ALL
   159  	defer tempFile.Close()
   160  
   161  	return tempFile.Name()
   162  }
   163  
   164  func runE2ETests(t *testing.T, tests []testdata) {
   165  	// required to have loom binary
   166  	common.LoomPath = path.Join(getLoomchainDir(), "gamechain")
   167  	common.ContractDir = "./contracts"
   168  	common.BaseDir = "temp-test-data"
   169  
   170  	buildGamechain(t)
   171  
   172  	// create ContractDir to make e2e happy
   173  	if err := os.MkdirAll(common.ContractDir, os.ModePerm); err != nil {
   174  		t.Fatal(err)
   175  	}
   176  
   177  	for _, test := range tests {
   178  		tempTestFilePath := prepareTestFile(t, test.testFile, test.replacementTokens)
   179  
   180  		//noinspection ALL
   181  		defer os.Remove(tempTestFilePath)
   182  		t.Run(test.name, func(t *testing.T) {
   183  			runE2E(t, test.name, tempTestFilePath, test.validators, test.accounts, test.genFile)
   184  		})
   185  	}
   186  }
   187  
   188  func TestE2EOracle(t *testing.T) {
   189  	tests := []testdata {
   190  		{"zb-oracle-1", "test_oracle.toml", 1, 10, "../test_data/simple-genesis.json", nil},
   191  		{"zb-oracle-8", "test_oracle.toml", 8, 10, "../test_data/simple-genesis.json", nil},
   192  	}
   193  
   194  	runE2ETests(t, tests)
   195  }
   196  
   197  func TestE2EAccount(t *testing.T) {
   198  	tests := []testdata {
   199  		{"zb-account-1", "test_account.toml", 1, 10, "../test_data/simple-genesis.json", nil},
   200  		{"zb-account-4", "test_account.toml", 4, 10, "../test_data/simple-genesis.json", nil},
   201  	}
   202  
   203  	runE2ETests(t, tests)
   204  }
   205  
   206  func TestE2EDeck(t *testing.T) {
   207  	tests := []testdata {
   208  		{"zb-deck-1", "test_deck.toml", 1, 10, "../test_data/simple-genesis.json", nil},
   209  		{"zb-deck-4", "test_deck.toml", 4, 10, "../test_data/simple-genesis.json", nil},
   210  	}
   211  
   212  	runE2ETests(t, tests)
   213  }
   214  
   215  func TestE2EOverlord(t *testing.T) {
   216  	tests := []testdata {
   217  		{"zb-overlord-1", "test_overlord.toml", 1, 10, "../test_data/simple-genesis.json", nil},
   218  		{"zb-overlord-4", "test_overlord.toml", 4, 10, "../test_data/simple-genesis.json", nil},
   219  	}
   220  
   221  	runE2ETests(t, tests)
   222  }
   223  
   224  func TestE2EMatchMaking(t *testing.T) {
   225  	tests := []testdata {
   226  		{"zb-findmatch-1", "test_findmatch.toml", 1, 10, "../test_data/simple-genesis.json", nil},
   227  		{"zb-findmatch-4", "test_findmatch.toml", 4, 10, "../test_data/simple-genesis.json", nil},
   228  	}
   229  
   230  	runE2ETests(t, tests)
   231  }
   232  
   233  func TestE2EGameplay(t *testing.T) {
   234  	replacementTokens := map[string]string{
   235  		"BackendLogicEnabled": "false",
   236  	}
   237  	tests := []testdata{
   238  		{"zb-gameplay-1", "test_gameplay.toml", 1, 10, "../test_data/simple-genesis.json", replacementTokens},
   239  		{"zb-gameplay-1", "test_gameplay.toml", 4, 10, "../test_data/simple-genesis.json", replacementTokens},
   240  	}
   241  
   242  	runE2ETests(t, tests)
   243  }
   244  
   245  func TestE2EGameplayBackendLogic(t *testing.T) {
   246  	replacementTokens := map[string]string{
   247  		"BackendLogicEnabled": "true",
   248  	}
   249  	tests := []testdata{
   250  		{"zb-gameplay-1", "test_gameplay.toml", 1, 10, "../test_data/simple-genesis.json", replacementTokens},
   251  		{"zb-gameplay-1", "test_gameplay.toml", 4, 10, "../test_data/simple-genesis.json", replacementTokens},
   252  	}
   253  
   254  	runE2ETests(t, tests)
   255  }