github.com/koko1123/flow-go-1@v0.29.6/model/bootstrap/node_info_test.go (about)

     1  package bootstrap_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/koko1123/flow-go-1/model/bootstrap"
    12  	"github.com/koko1123/flow-go-1/model/flow/order"
    13  	"github.com/koko1123/flow-go-1/utils/unittest"
    14  )
    15  
    16  func TestSort(t *testing.T) {
    17  	nodes := unittest.NodeInfosFixture(20)
    18  	nodes = bootstrap.Sort(nodes, order.Canonical)
    19  	require.True(t, bootstrap.ToIdentityList(nodes).Sorted(order.Canonical))
    20  }
    21  
    22  func TestNodeConfigEncodingJSON(t *testing.T) {
    23  	t.Run("normal node config", func(t *testing.T) {
    24  		conf := unittest.NodeConfigFixture()
    25  		enc, err := json.Marshal(conf)
    26  		require.NoError(t, err)
    27  		var dec bootstrap.NodeConfig
    28  		err = json.Unmarshal(enc, &dec)
    29  		require.NoError(t, err)
    30  		assert.Equal(t, conf, dec)
    31  	})
    32  	t.Run("compat: should accept old files using Stake field", func(t *testing.T) {
    33  		conf := unittest.NodeConfigFixture()
    34  		enc, err := json.Marshal(conf)
    35  		require.NoError(t, err)
    36  		// emulate the old encoding by replacing the new field with old field name
    37  		enc = []byte(strings.Replace(string(enc), "Weight", "Stake", 1))
    38  		var dec bootstrap.NodeConfig
    39  		err = json.Unmarshal(enc, &dec)
    40  		require.NoError(t, err)
    41  		assert.Equal(t, conf, dec)
    42  	})
    43  }
    44  
    45  func TestNodeInfoPubEncodingJSON(t *testing.T) {
    46  	t.Run("normal node info", func(t *testing.T) {
    47  		conf := unittest.NodeInfoFixture().Public()
    48  		enc, err := json.Marshal(conf)
    49  		require.NoError(t, err)
    50  		var dec bootstrap.NodeInfoPub
    51  		err = json.Unmarshal(enc, &dec)
    52  		require.NoError(t, err)
    53  		assert.Equal(t, conf, dec)
    54  	})
    55  	t.Run("compat: should accept old files using Stake field", func(t *testing.T) {
    56  		conf := unittest.NodeInfoFixture().Public()
    57  		enc, err := json.Marshal(conf)
    58  		require.NoError(t, err)
    59  		// emulate the old encoding by replacing the new field with old field name
    60  		enc = []byte(strings.Replace(string(enc), "Weight", "Stake", 1))
    61  		var dec bootstrap.NodeInfoPub
    62  		err = json.Unmarshal(enc, &dec)
    63  		require.NoError(t, err)
    64  		assert.Equal(t, conf, dec)
    65  	})
    66  }