github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/consolecmd_test.go (about)

     1  package launcher
     2  
     3  import (
     4  	"crypto/rand"
     5  	"math/big"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/unicornultrafoundation/go-u2u/params"
    15  
    16  	"github.com/unicornultrafoundation/go-u2u/integration/makefakegenesis"
    17  )
    18  
    19  const (
    20  	ipcAPIs  = "abft:1.0 admin:1.0 dag:1.0 debug:1.0 net:1.0 personal:1.0 rpc:1.0 trace:1.0 txpool:1.0 web3:1.0"
    21  	httpAPIs = "abft:1.0 dag:1.0 rpc:1.0 web3:1.0"
    22  )
    23  
    24  // Tests that a node embedded within a console can be started up properly and
    25  // then terminated by closing the input stream.
    26  func TestConsoleWelcome(t *testing.T) {
    27  	// Start an u2u console, make sure it's cleaned up and terminate the console
    28  	cli := exec(t,
    29  		"--fakenet", "0/1", "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--cache", "7923",
    30  		"console")
    31  
    32  	// Gather all the infos the welcome message needs to contain
    33  	cli.SetTemplateFunc("goos", func() string { return runtime.GOOS })
    34  	cli.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
    35  	cli.SetTemplateFunc("gover", runtime.Version)
    36  	cli.SetTemplateFunc("version", func() string { return params.VersionWithCommit("", "") })
    37  	cli.SetTemplateFunc("niltime", genesisStart)
    38  	cli.SetTemplateFunc("apis", func() string { return ipcAPIs })
    39  
    40  	// Verify the actual welcome message to the required template
    41  	cli.Expect(`
    42  Welcome to the Hashgraph JavaScript console!
    43  
    44  instance: go-u2u/v{{version}}/{{goos}}-{{goarch}}/{{gover}}
    45  coinbase: {{.Coinbase}}
    46  at block: 1 ({{niltime}})
    47   datadir: {{.Datadir}}
    48   modules: {{apis}}
    49  
    50  To exit, press ctrl-d
    51  > {{.InputLine "exit"}}
    52  `)
    53  	cli.ExpectExit()
    54  }
    55  
    56  // Tests that a console can be attached to a running node via various means.
    57  func TestIPCAttachWelcome(t *testing.T) {
    58  	// Configure the instance for IPC attachement
    59  	var ipc string
    60  	if runtime.GOOS == "windows" {
    61  		ipc = `\\.\pipe\hashgraph.ipc`
    62  	} else {
    63  		ws := tmpdir(t)
    64  		defer os.RemoveAll(ws)
    65  		ipc = filepath.Join(ws, "u2u.ipc")
    66  	}
    67  	cli := exec(t,
    68  		"--fakenet", "0/1", "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    69  		"--ipcpath", ipc, "--cache", "7923", "--datadir.minfreedisk", "1")
    70  
    71  	waitForEndpoint(t, ipc, 60*time.Second)
    72  	testAttachWelcome(t, cli, "ipc:"+ipc, ipcAPIs)
    73  
    74  	cli.Kill()
    75  	cli.WaitExit()
    76  }
    77  
    78  func TestHTTPAttachWelcome(t *testing.T) {
    79  	port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
    80  	cli := exec(t,
    81  		"--fakenet", "0/1", "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    82  		"--http", "--http.port", port, "--cache", "7923", "--datadir.minfreedisk", "1")
    83  
    84  	endpoint := "http://127.0.0.1:" + port
    85  	waitForEndpoint(t, endpoint, 60*time.Second)
    86  	testAttachWelcome(t, cli, "http://localhost:"+port, httpAPIs)
    87  
    88  	cli.Kill()
    89  	cli.WaitExit()
    90  }
    91  
    92  func TestWSAttachWelcome(t *testing.T) {
    93  	port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
    94  
    95  	cli := exec(t,
    96  		"--fakenet", "0/1", "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
    97  		"--ws", "--ws.port", port, "--cache", "7923", "--datadir.minfreedisk", "1")
    98  
    99  	endpoint := "ws://127.0.0.1:" + port
   100  	waitForEndpoint(t, endpoint, 60*time.Second)
   101  	testAttachWelcome(t, cli, "ws://localhost:"+port, httpAPIs)
   102  
   103  	cli.Kill()
   104  	cli.WaitExit()
   105  }
   106  
   107  func testAttachWelcome(t *testing.T, cli *testcli, endpoint, apis string) {
   108  	// Attach to a running u2u node and terminate immediately
   109  	attach := exec(t, "attach", endpoint)
   110  
   111  	// Gather all the infos the welcome message needs to contain
   112  	attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
   113  	attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
   114  	attach.SetTemplateFunc("gover", runtime.Version)
   115  	attach.SetTemplateFunc("version", func() string { return params.VersionWithCommit("", "") })
   116  	attach.SetTemplateFunc("niltime", genesisStart)
   117  	attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
   118  	attach.SetTemplateFunc("datadir", func() string { return cli.Datadir })
   119  	attach.SetTemplateFunc("coinbase", func() string { return cli.Coinbase })
   120  	attach.SetTemplateFunc("apis", func() string { return apis })
   121  
   122  	// Verify the actual welcome message to the required template
   123  	attach.Expect(`
   124  Welcome to the Hashgraph JavaScript console!
   125  
   126  instance: go-u2u/v{{version}}/{{goos}}-{{goarch}}/{{gover}}
   127  coinbase: {{coinbase}}
   128  at block: 1 ({{niltime}}){{if ipc}}
   129   datadir: {{datadir}}{{end}}
   130   modules: {{apis}}
   131  
   132  To exit, press ctrl-d
   133  > {{.InputLine "exit" }}
   134  `)
   135  	attach.ExpectExit()
   136  }
   137  
   138  // trulyRandInt generates a crypto random integer used by the console tests to
   139  // not clash network ports with other tests running cocurrently.
   140  func trulyRandInt(lo, hi int) int {
   141  	num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
   142  	return int(num.Int64()) + lo
   143  }
   144  
   145  func genesisStart() string {
   146  	return time.Unix(int64(makefakegenesis.FakeGenesisTime.Unix()), 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
   147  }