github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/swarm/config_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package main
    13  
    14  import (
    15  	"fmt"
    16  	"io"
    17  	"io/ioutil"
    18  	"os"
    19  	"os/exec"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/Sberex/go-sberex/rpc"
    24  	"github.com/Sberex/go-sberex/swarm"
    25  	"github.com/Sberex/go-sberex/swarm/api"
    26  
    27  	"github.com/docker/docker/pkg/reexec"
    28  )
    29  
    30  func TestDumpConfig(t *testing.T) {
    31  	swarm := runSwarm(t, "dumpconfig")
    32  	defaultConf := api.NewDefaultConfig()
    33  	out, err := tomlSettings.Marshal(&defaultConf)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	swarm.Expect(string(out))
    38  	swarm.ExpectExit()
    39  }
    40  
    41  func TestFailsSwapEnabledNoSwapApi(t *testing.T) {
    42  	flags := []string{
    43  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
    44  		fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
    45  		fmt.Sprintf("--%s", SwarmSwapEnabledFlag.Name),
    46  	}
    47  
    48  	swarm := runSwarm(t, flags...)
    49  	swarm.Expect("Fatal: " + SWARM_ERR_SWAP_SET_NO_API + "\n")
    50  	swarm.ExpectExit()
    51  }
    52  
    53  func TestFailsNoBzzAccount(t *testing.T) {
    54  	flags := []string{
    55  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
    56  		fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
    57  	}
    58  
    59  	swarm := runSwarm(t, flags...)
    60  	swarm.Expect("Fatal: " + SWARM_ERR_NO_BZZACCOUNT + "\n")
    61  	swarm.ExpectExit()
    62  }
    63  
    64  func TestCmdLineOverrides(t *testing.T) {
    65  	dir, err := ioutil.TempDir("", "bzztest")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	defer os.RemoveAll(dir)
    70  
    71  	conf, account := getTestAccount(t, dir)
    72  	node := &testNode{Dir: dir}
    73  
    74  	// assign ports
    75  	httpPort, err := assignTCPPort()
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	flags := []string{
    81  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
    82  		fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
    83  		fmt.Sprintf("--%s", SwarmSyncEnabledFlag.Name),
    84  		fmt.Sprintf("--%s", CorsStringFlag.Name), "*",
    85  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
    86  		fmt.Sprintf("--%s", EnsAPIFlag.Name), "",
    87  		"--datadir", dir,
    88  		"--ipcpath", conf.IPCPath,
    89  	}
    90  	node.Cmd = runSwarm(t, flags...)
    91  	node.Cmd.InputLine(testPassphrase)
    92  	defer func() {
    93  		if t.Failed() {
    94  			node.Shutdown()
    95  		}
    96  	}()
    97  	// wait for the node to start
    98  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
    99  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   100  		if err == nil {
   101  			break
   102  		}
   103  	}
   104  	if node.Client == nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// load info
   109  	var info swarm.Info
   110  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	if info.Port != httpPort {
   115  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   116  	}
   117  
   118  	if info.NetworkId != 42 {
   119  		t.Fatalf("Expected network ID to be %d, got %d", 42, info.NetworkId)
   120  	}
   121  
   122  	if !info.SyncEnabled {
   123  		t.Fatal("Expected Sync to be enabled, but is false")
   124  	}
   125  
   126  	if info.Cors != "*" {
   127  		t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
   128  	}
   129  
   130  	node.Shutdown()
   131  }
   132  
   133  func TestFileOverrides(t *testing.T) {
   134  
   135  	// assign ports
   136  	httpPort, err := assignTCPPort()
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  
   141  	//create a config file
   142  	//first, create a default conf
   143  	defaultConf := api.NewDefaultConfig()
   144  	//change some values in order to test if they have been loaded
   145  	defaultConf.SyncEnabled = true
   146  	defaultConf.NetworkId = 54
   147  	defaultConf.Port = httpPort
   148  	defaultConf.StoreParams.DbCapacity = 9000000
   149  	defaultConf.ChunkerParams.Branches = 64
   150  	defaultConf.HiveParams.CallInterval = 6000000000
   151  	defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
   152  	defaultConf.SyncParams.KeyBufferSize = 512
   153  	//create a TOML string
   154  	out, err := tomlSettings.Marshal(&defaultConf)
   155  	if err != nil {
   156  		t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
   157  	}
   158  	//create file
   159  	f, err := ioutil.TempFile("", "testconfig.toml")
   160  	if err != nil {
   161  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   162  	}
   163  	//write file
   164  	_, err = f.WriteString(string(out))
   165  	if err != nil {
   166  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   167  	}
   168  	f.Sync()
   169  
   170  	dir, err := ioutil.TempDir("", "bzztest")
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	defer os.RemoveAll(dir)
   175  	conf, account := getTestAccount(t, dir)
   176  	node := &testNode{Dir: dir}
   177  
   178  	flags := []string{
   179  		fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(),
   180  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
   181  		"--ens-api", "",
   182  		"--ipcpath", conf.IPCPath,
   183  		"--datadir", dir,
   184  	}
   185  	node.Cmd = runSwarm(t, flags...)
   186  	node.Cmd.InputLine(testPassphrase)
   187  	defer func() {
   188  		if t.Failed() {
   189  			node.Shutdown()
   190  		}
   191  	}()
   192  	// wait for the node to start
   193  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   194  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   195  		if err == nil {
   196  			break
   197  		}
   198  	}
   199  	if node.Client == nil {
   200  		t.Fatal(err)
   201  	}
   202  
   203  	// load info
   204  	var info swarm.Info
   205  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   206  		t.Fatal(err)
   207  	}
   208  
   209  	if info.Port != httpPort {
   210  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   211  	}
   212  
   213  	if info.NetworkId != 54 {
   214  		t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
   215  	}
   216  
   217  	if !info.SyncEnabled {
   218  		t.Fatal("Expected Sync to be enabled, but is false")
   219  	}
   220  
   221  	if info.StoreParams.DbCapacity != 9000000 {
   222  		t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
   223  	}
   224  
   225  	if info.ChunkerParams.Branches != 64 {
   226  		t.Fatalf("Expected chunker params branches to be %d, got %d", 64, info.ChunkerParams.Branches)
   227  	}
   228  
   229  	if info.HiveParams.CallInterval != 6000000000 {
   230  		t.Fatalf("Expected HiveParams CallInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.CallInterval))
   231  	}
   232  
   233  	if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
   234  		t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
   235  	}
   236  
   237  	if info.SyncParams.KeyBufferSize != 512 {
   238  		t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
   239  	}
   240  
   241  	node.Shutdown()
   242  }
   243  
   244  func TestEnvVars(t *testing.T) {
   245  	// assign ports
   246  	httpPort, err := assignTCPPort()
   247  	if err != nil {
   248  		t.Fatal(err)
   249  	}
   250  
   251  	envVars := os.Environ()
   252  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmPortFlag.EnvVar, httpPort))
   253  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmNetworkIdFlag.EnvVar, "999"))
   254  	envVars = append(envVars, fmt.Sprintf("%s=%s", CorsStringFlag.EnvVar, "*"))
   255  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmSyncEnabledFlag.EnvVar, "true"))
   256  
   257  	dir, err := ioutil.TempDir("", "bzztest")
   258  	if err != nil {
   259  		t.Fatal(err)
   260  	}
   261  	defer os.RemoveAll(dir)
   262  	conf, account := getTestAccount(t, dir)
   263  	node := &testNode{Dir: dir}
   264  	flags := []string{
   265  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
   266  		"--ens-api", "",
   267  		"--datadir", dir,
   268  		"--ipcpath", conf.IPCPath,
   269  	}
   270  
   271  	//node.Cmd = runSwarm(t,flags...)
   272  	//node.Cmd.cmd.Env = envVars
   273  	//the above assignment does not work, so we need a custom Cmd here in order to pass envVars:
   274  	cmd := &exec.Cmd{
   275  		Path:   reexec.Self(),
   276  		Args:   append([]string{"swarm-test"}, flags...),
   277  		Stderr: os.Stderr,
   278  		Stdout: os.Stdout,
   279  	}
   280  	cmd.Env = envVars
   281  	//stdout, err := cmd.StdoutPipe()
   282  	//if err != nil {
   283  	//	t.Fatal(err)
   284  	//}
   285  	//stdout = bufio.NewReader(stdout)
   286  	var stdin io.WriteCloser
   287  	if stdin, err = cmd.StdinPipe(); err != nil {
   288  		t.Fatal(err)
   289  	}
   290  	if err := cmd.Start(); err != nil {
   291  		t.Fatal(err)
   292  	}
   293  
   294  	//cmd.InputLine(testPassphrase)
   295  	io.WriteString(stdin, testPassphrase+"\n")
   296  	defer func() {
   297  		if t.Failed() {
   298  			node.Shutdown()
   299  			cmd.Process.Kill()
   300  		}
   301  	}()
   302  	// wait for the node to start
   303  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   304  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   305  		if err == nil {
   306  			break
   307  		}
   308  	}
   309  
   310  	if node.Client == nil {
   311  		t.Fatal(err)
   312  	}
   313  
   314  	// load info
   315  	var info swarm.Info
   316  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   317  		t.Fatal(err)
   318  	}
   319  
   320  	if info.Port != httpPort {
   321  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   322  	}
   323  
   324  	if info.NetworkId != 999 {
   325  		t.Fatalf("Expected network ID to be %d, got %d", 999, info.NetworkId)
   326  	}
   327  
   328  	if info.Cors != "*" {
   329  		t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
   330  	}
   331  
   332  	if !info.SyncEnabled {
   333  		t.Fatal("Expected Sync to be enabled, but is false")
   334  	}
   335  
   336  	node.Shutdown()
   337  	cmd.Process.Kill()
   338  }
   339  
   340  func TestCmdLineOverridesFile(t *testing.T) {
   341  
   342  	// assign ports
   343  	httpPort, err := assignTCPPort()
   344  	if err != nil {
   345  		t.Fatal(err)
   346  	}
   347  
   348  	//create a config file
   349  	//first, create a default conf
   350  	defaultConf := api.NewDefaultConfig()
   351  	//change some values in order to test if they have been loaded
   352  	defaultConf.SyncEnabled = false
   353  	defaultConf.NetworkId = 54
   354  	defaultConf.Port = "8588"
   355  	defaultConf.StoreParams.DbCapacity = 9000000
   356  	defaultConf.ChunkerParams.Branches = 64
   357  	defaultConf.HiveParams.CallInterval = 6000000000
   358  	defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
   359  	defaultConf.SyncParams.KeyBufferSize = 512
   360  	//create a TOML file
   361  	out, err := tomlSettings.Marshal(&defaultConf)
   362  	if err != nil {
   363  		t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
   364  	}
   365  	//write file
   366  	f, err := ioutil.TempFile("", "testconfig.toml")
   367  	if err != nil {
   368  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   369  	}
   370  	//write file
   371  	_, err = f.WriteString(string(out))
   372  	if err != nil {
   373  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   374  	}
   375  	f.Sync()
   376  
   377  	dir, err := ioutil.TempDir("", "bzztest")
   378  	if err != nil {
   379  		t.Fatal(err)
   380  	}
   381  	defer os.RemoveAll(dir)
   382  	conf, account := getTestAccount(t, dir)
   383  	node := &testNode{Dir: dir}
   384  
   385  	expectNetworkId := uint64(77)
   386  
   387  	flags := []string{
   388  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "77",
   389  		fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
   390  		fmt.Sprintf("--%s", SwarmSyncEnabledFlag.Name),
   391  		fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(),
   392  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
   393  		"--ens-api", "",
   394  		"--datadir", dir,
   395  		"--ipcpath", conf.IPCPath,
   396  	}
   397  	node.Cmd = runSwarm(t, flags...)
   398  	node.Cmd.InputLine(testPassphrase)
   399  	defer func() {
   400  		if t.Failed() {
   401  			node.Shutdown()
   402  		}
   403  	}()
   404  	// wait for the node to start
   405  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   406  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   407  		if err == nil {
   408  			break
   409  		}
   410  	}
   411  	if node.Client == nil {
   412  		t.Fatal(err)
   413  	}
   414  
   415  	// load info
   416  	var info swarm.Info
   417  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   418  		t.Fatal(err)
   419  	}
   420  
   421  	if info.Port != httpPort {
   422  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   423  	}
   424  
   425  	if info.NetworkId != expectNetworkId {
   426  		t.Fatalf("Expected network ID to be %d, got %d", expectNetworkId, info.NetworkId)
   427  	}
   428  
   429  	if !info.SyncEnabled {
   430  		t.Fatal("Expected Sync to be enabled, but is false")
   431  	}
   432  
   433  	if info.StoreParams.DbCapacity != 9000000 {
   434  		t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
   435  	}
   436  
   437  	if info.ChunkerParams.Branches != 64 {
   438  		t.Fatalf("Expected chunker params branches to be %d, got %d", 64, info.ChunkerParams.Branches)
   439  	}
   440  
   441  	if info.HiveParams.CallInterval != 6000000000 {
   442  		t.Fatalf("Expected HiveParams CallInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.CallInterval))
   443  	}
   444  
   445  	if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
   446  		t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
   447  	}
   448  
   449  	if info.SyncParams.KeyBufferSize != 512 {
   450  		t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
   451  	}
   452  
   453  	node.Shutdown()
   454  }
   455  
   456  func TestValidateConfig(t *testing.T) {
   457  	for _, c := range []struct {
   458  		cfg *api.Config
   459  		err string
   460  	}{
   461  		{
   462  			cfg: &api.Config{EnsAPIs: []string{
   463  				"/data/testnet/geth.ipc",
   464  			}},
   465  		},
   466  		{
   467  			cfg: &api.Config{EnsAPIs: []string{
   468  				"http://127.0.0.1:1234",
   469  			}},
   470  		},
   471  		{
   472  			cfg: &api.Config{EnsAPIs: []string{
   473  				"ws://127.0.0.1:1234",
   474  			}},
   475  		},
   476  		{
   477  			cfg: &api.Config{EnsAPIs: []string{
   478  				"test:/data/testnet/geth.ipc",
   479  			}},
   480  		},
   481  		{
   482  			cfg: &api.Config{EnsAPIs: []string{
   483  				"test:ws://127.0.0.1:1234",
   484  			}},
   485  		},
   486  		{
   487  			cfg: &api.Config{EnsAPIs: []string{
   488  				"314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
   489  			}},
   490  		},
   491  		{
   492  			cfg: &api.Config{EnsAPIs: []string{
   493  				"314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
   494  			}},
   495  		},
   496  		{
   497  			cfg: &api.Config{EnsAPIs: []string{
   498  				"314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234",
   499  			}},
   500  		},
   501  		{
   502  			cfg: &api.Config{EnsAPIs: []string{
   503  				"test:314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
   504  			}},
   505  		},
   506  		{
   507  			cfg: &api.Config{EnsAPIs: []string{
   508  				"eth:314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
   509  			}},
   510  		},
   511  		{
   512  			cfg: &api.Config{EnsAPIs: []string{
   513  				"eth:314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:12344",
   514  			}},
   515  		},
   516  		{
   517  			cfg: &api.Config{EnsAPIs: []string{
   518  				"eth:",
   519  			}},
   520  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \"eth:\": missing url",
   521  		},
   522  		{
   523  			cfg: &api.Config{EnsAPIs: []string{
   524  				"314159265dD8dbb310642f98f50C066173C1259b@",
   525  			}},
   526  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \"314159265dD8dbb310642f98f50C066173C1259b@\": missing url",
   527  		},
   528  		{
   529  			cfg: &api.Config{EnsAPIs: []string{
   530  				":314159265dD8dbb310642f98f50C066173C1259",
   531  			}},
   532  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \":314159265dD8dbb310642f98f50C066173C1259\": missing tld",
   533  		},
   534  		{
   535  			cfg: &api.Config{EnsAPIs: []string{
   536  				"@/data/testnet/geth.ipc",
   537  			}},
   538  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \"@/data/testnet/geth.ipc\": missing contract address",
   539  		},
   540  	} {
   541  		err := validateConfig(c.cfg)
   542  		if c.err != "" && err.Error() != c.err {
   543  			t.Errorf("expected error %q, got %q", c.err, err)
   544  		}
   545  		if c.err == "" && err != nil {
   546  			t.Errorf("unexpected error %q", err)
   547  		}
   548  	}
   549  }