github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/cmd/swarm/run_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"crypto/ecdsa"
    22  	"flag"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"net"
    26  	"os"
    27  	"path"
    28  	"path/filepath"
    29  	"runtime"
    30  	"sync"
    31  	"syscall"
    32  	"testing"
    33  	"time"
    34  
    35  	"github.com/docker/docker/pkg/reexec"
    36  	"github.com/ebceco/ebc/accounts"
    37  	"github.com/ebceco/ebc/accounts/keystore"
    38  	"github.com/ebceco/ebc/internal/cmdtest"
    39  	"github.com/ebceco/ebc/node"
    40  	"github.com/ebceco/ebc/p2p"
    41  	"github.com/ebceco/ebc/rpc"
    42  	"github.com/ebceco/ebc/swarm"
    43  	"github.com/ebceco/ebc/swarm/api"
    44  	swarmhttp "github.com/ebceco/ebc/swarm/api/http"
    45  )
    46  
    47  var loglevel = flag.Int("loglevel", 3, "verbosity of logs")
    48  
    49  func init() {
    50  	// Run the app if we've been exec'd as "swarm-test" in runSwarm.
    51  	reexec.Register("swarm-test", func() {
    52  		if err := app.Run(os.Args); err != nil {
    53  			fmt.Fprintln(os.Stderr, err)
    54  			os.Exit(1)
    55  		}
    56  		os.Exit(0)
    57  	})
    58  }
    59  
    60  const clusterSize = 3
    61  
    62  var clusteronce sync.Once
    63  var cluster *testCluster
    64  
    65  func initCluster(t *testing.T) {
    66  	clusteronce.Do(func() {
    67  		cluster = newTestCluster(t, clusterSize)
    68  	})
    69  }
    70  
    71  func serverFunc(api *api.API) swarmhttp.TestServer {
    72  	return swarmhttp.NewServer(api, "")
    73  }
    74  func TestMain(m *testing.M) {
    75  	// check if we have been reexec'd
    76  	if reexec.Init() {
    77  		return
    78  	}
    79  	os.Exit(m.Run())
    80  }
    81  
    82  func runSwarm(t *testing.T, args ...string) *cmdtest.TestCmd {
    83  	tt := cmdtest.NewTestCmd(t, nil)
    84  
    85  	// Boot "swarm". This actually runs the test binary but the TestMain
    86  	// function will prevent any tests from running.
    87  	tt.Run("swarm-test", args...)
    88  
    89  	return tt
    90  }
    91  
    92  type testCluster struct {
    93  	Nodes  []*testNode
    94  	TmpDir string
    95  }
    96  
    97  // newTestCluster starts a test swarm cluster of the given size.
    98  //
    99  // A temporary directory is created and each node gets a data directory inside
   100  // it.
   101  //
   102  // Each node listens on 127.0.0.1 with random ports for both the HTTP and p2p
   103  // ports (assigned by first listening on 127.0.0.1:0 and then passing the ports
   104  // as flags).
   105  //
   106  // When starting more than one node, they are connected together using the
   107  // admin SetPeer RPC method.
   108  
   109  func newTestCluster(t *testing.T, size int) *testCluster {
   110  	cluster := &testCluster{}
   111  	defer func() {
   112  		if t.Failed() {
   113  			cluster.Shutdown()
   114  		}
   115  	}()
   116  
   117  	tmpdir, err := ioutil.TempDir("", "swarm-test")
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	cluster.TmpDir = tmpdir
   122  
   123  	// start the nodes
   124  	cluster.StartNewNodes(t, size)
   125  
   126  	if size == 1 {
   127  		return cluster
   128  	}
   129  
   130  	// connect the nodes together
   131  	for _, node := range cluster.Nodes {
   132  		if err := node.Client.Call(nil, "admin_addPeer", cluster.Nodes[0].Enode); err != nil {
   133  			t.Fatal(err)
   134  		}
   135  	}
   136  
   137  	// wait until all nodes have the correct number of peers
   138  outer:
   139  	for _, node := range cluster.Nodes {
   140  		var peers []*p2p.PeerInfo
   141  		for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(50 * time.Millisecond) {
   142  			if err := node.Client.Call(&peers, "admin_peers"); err != nil {
   143  				t.Fatal(err)
   144  			}
   145  			if len(peers) == len(cluster.Nodes)-1 {
   146  				continue outer
   147  			}
   148  		}
   149  		t.Fatalf("%s only has %d / %d peers", node.Name, len(peers), len(cluster.Nodes)-1)
   150  	}
   151  
   152  	return cluster
   153  }
   154  
   155  func (c *testCluster) Shutdown() {
   156  	for _, node := range c.Nodes {
   157  		node.Shutdown()
   158  	}
   159  	os.RemoveAll(c.TmpDir)
   160  }
   161  
   162  func (c *testCluster) Stop() {
   163  	for _, node := range c.Nodes {
   164  		node.Shutdown()
   165  	}
   166  }
   167  
   168  func (c *testCluster) StartNewNodes(t *testing.T, size int) {
   169  	c.Nodes = make([]*testNode, 0, size)
   170  	for i := 0; i < size; i++ {
   171  		dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
   172  		if err := os.Mkdir(dir, 0700); err != nil {
   173  			t.Fatal(err)
   174  		}
   175  
   176  		node := newTestNode(t, dir)
   177  		node.Name = fmt.Sprintf("swarm%02d", i)
   178  
   179  		c.Nodes = append(c.Nodes, node)
   180  	}
   181  }
   182  
   183  func (c *testCluster) StartExistingNodes(t *testing.T, size int, bzzaccount string) {
   184  	c.Nodes = make([]*testNode, 0, size)
   185  	for i := 0; i < size; i++ {
   186  		dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
   187  		node := existingTestNode(t, dir, bzzaccount)
   188  		node.Name = fmt.Sprintf("swarm%02d", i)
   189  
   190  		c.Nodes = append(c.Nodes, node)
   191  	}
   192  }
   193  
   194  func (c *testCluster) Cleanup() {
   195  	os.RemoveAll(c.TmpDir)
   196  }
   197  
   198  type testNode struct {
   199  	Name       string
   200  	Addr       string
   201  	URL        string
   202  	Enode      string
   203  	Dir        string
   204  	IpcPath    string
   205  	PrivateKey *ecdsa.PrivateKey
   206  	Client     *rpc.Client
   207  	Cmd        *cmdtest.TestCmd
   208  }
   209  
   210  const testPassphrase = "swarm-test-passphrase"
   211  
   212  func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accounts.Account) {
   213  	// create key
   214  	conf = &node.Config{
   215  		DataDir: dir,
   216  		IPCPath: "bzzd.ipc",
   217  		NoUSB:   true,
   218  	}
   219  	n, err := node.New(conf)
   220  	if err != nil {
   221  		t.Fatal(err)
   222  	}
   223  	account, err = n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase)
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  
   228  	// use a unique IPCPath when running tests on Windows
   229  	if runtime.GOOS == "windows" {
   230  		conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", account.Address.String())
   231  	}
   232  
   233  	return conf, account
   234  }
   235  
   236  func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
   237  	conf, _ := getTestAccount(t, dir)
   238  	node := &testNode{Dir: dir}
   239  
   240  	// use a unique IPCPath when running tests on Windows
   241  	if runtime.GOOS == "windows" {
   242  		conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", bzzaccount)
   243  	}
   244  
   245  	// assign ports
   246  	ports, err := getAvailableTCPPorts(2)
   247  	if err != nil {
   248  		t.Fatal(err)
   249  	}
   250  	p2pPort := ports[0]
   251  	httpPort := ports[1]
   252  
   253  	// start the node
   254  	node.Cmd = runSwarm(t,
   255  		"--port", p2pPort,
   256  		"--nat", "extip:127.0.0.1",
   257  		"--nodiscover",
   258  		"--datadir", dir,
   259  		"--ipcpath", conf.IPCPath,
   260  		"--ens-api", "",
   261  		"--bzzaccount", bzzaccount,
   262  		"--bzznetworkid", "321",
   263  		"--bzzport", httpPort,
   264  		"--verbosity", fmt.Sprint(*loglevel),
   265  	)
   266  	node.Cmd.InputLine(testPassphrase)
   267  	defer func() {
   268  		if t.Failed() {
   269  			node.Shutdown()
   270  		}
   271  	}()
   272  
   273  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   274  	defer cancel()
   275  
   276  	// ensure that all ports have active listeners
   277  	// so that the next node will not get the same
   278  	// when calling getAvailableTCPPorts
   279  	err = waitTCPPorts(ctx, ports...)
   280  	if err != nil {
   281  		t.Fatal(err)
   282  	}
   283  
   284  	// wait for the node to start
   285  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   286  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   287  		if err == nil {
   288  			break
   289  		}
   290  	}
   291  	if node.Client == nil {
   292  		t.Fatal(err)
   293  	}
   294  
   295  	// load info
   296  	var info swarm.Info
   297  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   298  		t.Fatal(err)
   299  	}
   300  	node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
   301  	node.URL = "http://" + node.Addr
   302  
   303  	var nodeInfo p2p.NodeInfo
   304  	if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
   305  		t.Fatal(err)
   306  	}
   307  	node.Enode = nodeInfo.Enode
   308  	node.IpcPath = conf.IPCPath
   309  	return node
   310  }
   311  
   312  func newTestNode(t *testing.T, dir string) *testNode {
   313  
   314  	conf, account := getTestAccount(t, dir)
   315  	ks := keystore.NewKeyStore(path.Join(dir, "keystore"), 1<<18, 1)
   316  
   317  	pk := decryptStoreAccount(ks, account.Address.Hex(), []string{testPassphrase})
   318  
   319  	node := &testNode{Dir: dir, PrivateKey: pk}
   320  
   321  	// assign ports
   322  	ports, err := getAvailableTCPPorts(2)
   323  	if err != nil {
   324  		t.Fatal(err)
   325  	}
   326  	p2pPort := ports[0]
   327  	httpPort := ports[1]
   328  
   329  	// start the node
   330  	node.Cmd = runSwarm(t,
   331  		"--port", p2pPort,
   332  		"--nat", "extip:127.0.0.1",
   333  		"--nodiscover",
   334  		"--datadir", dir,
   335  		"--ipcpath", conf.IPCPath,
   336  		"--ens-api", "",
   337  		"--bzzaccount", account.Address.String(),
   338  		"--bzznetworkid", "321",
   339  		"--bzzport", httpPort,
   340  		"--verbosity", fmt.Sprint(*loglevel),
   341  	)
   342  	node.Cmd.InputLine(testPassphrase)
   343  	defer func() {
   344  		if t.Failed() {
   345  			node.Shutdown()
   346  		}
   347  	}()
   348  
   349  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   350  	defer cancel()
   351  
   352  	// ensure that all ports have active listeners
   353  	// so that the next node will not get the same
   354  	// when calling getAvailableTCPPorts
   355  	err = waitTCPPorts(ctx, ports...)
   356  	if err != nil {
   357  		t.Fatal(err)
   358  	}
   359  
   360  	// wait for the node to start
   361  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   362  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   363  		if err == nil {
   364  			break
   365  		}
   366  	}
   367  	if node.Client == nil {
   368  		t.Fatal(err)
   369  	}
   370  
   371  	// load info
   372  	var info swarm.Info
   373  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   374  		t.Fatal(err)
   375  	}
   376  	node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
   377  	node.URL = "http://" + node.Addr
   378  
   379  	var nodeInfo p2p.NodeInfo
   380  	if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
   381  		t.Fatal(err)
   382  	}
   383  	node.Enode = nodeInfo.Enode
   384  	node.IpcPath = conf.IPCPath
   385  	return node
   386  }
   387  
   388  func (n *testNode) Shutdown() {
   389  	if n.Cmd != nil {
   390  		n.Cmd.Kill()
   391  	}
   392  }
   393  
   394  // getAvailableTCPPorts returns a set of ports that
   395  // nothing is listening on at the time.
   396  //
   397  // Function assignTCPPort cannot be called in sequence
   398  // and guardantee that the same port will be returned in
   399  // different calls as the listener is closed within the function,
   400  // not after all listeners are started and selected unique
   401  // available ports.
   402  func getAvailableTCPPorts(count int) (ports []string, err error) {
   403  	for i := 0; i < count; i++ {
   404  		l, err := net.Listen("tcp", "127.0.0.1:0")
   405  		if err != nil {
   406  			return nil, err
   407  		}
   408  		// defer close in the loop to be sure the same port will not
   409  		// be selected in the next iteration
   410  		defer l.Close()
   411  
   412  		_, port, err := net.SplitHostPort(l.Addr().String())
   413  		if err != nil {
   414  			return nil, err
   415  		}
   416  		ports = append(ports, port)
   417  	}
   418  	return ports, nil
   419  }
   420  
   421  // waitTCPPorts blocks until tcp connections can be
   422  // established on all provided ports. It runs all
   423  // ports dialers in parallel, and returns the first
   424  // encountered error.
   425  // See waitTCPPort also.
   426  func waitTCPPorts(ctx context.Context, ports ...string) error {
   427  	var err error
   428  	// mu locks err variable that is assigned in
   429  	// other goroutines
   430  	var mu sync.Mutex
   431  
   432  	// cancel is canceling all goroutines
   433  	// when the firs error is returned
   434  	// to prevent unnecessary waiting
   435  	ctx, cancel := context.WithCancel(ctx)
   436  	defer cancel()
   437  
   438  	var wg sync.WaitGroup
   439  	for _, port := range ports {
   440  		wg.Add(1)
   441  		go func(port string) {
   442  			defer wg.Done()
   443  
   444  			e := waitTCPPort(ctx, port)
   445  
   446  			mu.Lock()
   447  			defer mu.Unlock()
   448  			if e != nil && err == nil {
   449  				err = e
   450  				cancel()
   451  			}
   452  		}(port)
   453  	}
   454  	wg.Wait()
   455  
   456  	return err
   457  }
   458  
   459  // waitTCPPort blocks until tcp connection can be established
   460  // ona provided port. It has a 3 minute timeout as maximum,
   461  // to prevent long waiting, but it can be shortened with
   462  // a provided context instance. Dialer has a 10 second timeout
   463  // in every iteration, and connection refused error will be
   464  // retried in 100 milliseconds periods.
   465  func waitTCPPort(ctx context.Context, port string) error {
   466  	ctx, cancel := context.WithTimeout(ctx, 3*time.Minute)
   467  	defer cancel()
   468  
   469  	for {
   470  		c, err := (&net.Dialer{Timeout: 10 * time.Second}).DialContext(ctx, "tcp", "127.0.0.1:"+port)
   471  		if err != nil {
   472  			if operr, ok := err.(*net.OpError); ok {
   473  				if syserr, ok := operr.Err.(*os.SyscallError); ok && syserr.Err == syscall.ECONNREFUSED {
   474  					time.Sleep(100 * time.Millisecond)
   475  					continue
   476  				}
   477  			}
   478  			return err
   479  		}
   480  		return c.Close()
   481  	}
   482  }