gitlab.com/jokerrs1/Sia@v1.3.2/node/api/gateway_test.go (about)

     1  package api
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/build"
     7  	"github.com/NebulousLabs/Sia/modules/gateway"
     8  )
     9  
    10  // TestGatewayStatus checks that the /gateway/status call is returning a corect
    11  // peerlist.
    12  func TestGatewayStatus(t *testing.T) {
    13  	if testing.Short() {
    14  		t.SkipNow()
    15  	}
    16  	t.Parallel()
    17  	st, err := createServerTester(t.Name())
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	defer st.server.panicClose()
    22  
    23  	var info GatewayGET
    24  	st.getAPI("/gateway", &info)
    25  	if len(info.Peers) != 0 {
    26  		t.Fatal("/gateway gave bad peer list:", info.Peers)
    27  	}
    28  }
    29  
    30  // TestGatewayPeerConnect checks that /gateway/connect is adding a peer to the
    31  // gateway's peerlist.
    32  func TestGatewayPeerConnect(t *testing.T) {
    33  	if testing.Short() {
    34  		t.SkipNow()
    35  	}
    36  	t.Parallel()
    37  	st, err := createServerTester(t.Name())
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer st.server.panicClose()
    42  
    43  	peer, err := gateway.New("localhost:0", false, build.TempDir("api", t.Name()+"2", "gateway"))
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	defer func() {
    48  		err := peer.Close()
    49  		if err != nil {
    50  			panic(err)
    51  		}
    52  	}()
    53  	err = st.stdPostAPI("/gateway/connect/"+string(peer.Address()), nil)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	var info GatewayGET
    59  	err = st.getAPI("/gateway", &info)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if len(info.Peers) != 1 || info.Peers[0].NetAddress != peer.Address() {
    64  		t.Fatal("/gateway/connect did not connect to peer", peer.Address())
    65  	}
    66  }
    67  
    68  // TestGatewayPeerDisconnect checks that /gateway/disconnect removes the
    69  // correct peer from the gateway's peerlist.
    70  func TestGatewayPeerDisconnect(t *testing.T) {
    71  	if testing.Short() {
    72  		t.SkipNow()
    73  	}
    74  	t.Parallel()
    75  	st, err := createServerTester(t.Name())
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	defer st.server.panicClose()
    80  
    81  	peer, err := gateway.New("localhost:0", false, build.TempDir("api", t.Name()+"2", "gateway"))
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	defer func() {
    86  		err := peer.Close()
    87  		if err != nil {
    88  			panic(err)
    89  		}
    90  	}()
    91  	err = st.stdPostAPI("/gateway/connect/"+string(peer.Address()), nil)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	var info GatewayGET
    97  	st.getAPI("/gateway", &info)
    98  	if len(info.Peers) != 1 || info.Peers[0].NetAddress != peer.Address() {
    99  		t.Fatal("/gateway/connect did not connect to peer", peer.Address())
   100  	}
   101  
   102  	err = st.stdPostAPI("/gateway/disconnect/"+string(peer.Address()), nil)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	err = st.getAPI("/gateway", &info)
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	if len(info.Peers) != 0 {
   111  		t.Fatal("/gateway/disconnect did not disconnect from peer", peer.Address())
   112  	}
   113  }