github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/version/version_test.go (about)

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	gover "github.com/hashicorp/go-version"
     8  	"gopkg.in/fatih/set.v0"
     9  )
    10  
    11  func TestCompare(t *testing.T) {
    12  	v1, err := gover.NewVersion(Version)
    13  	if err != nil {
    14  		t.Fatal("Version 1 format error.")
    15  	}
    16  	v2, err := gover.NewVersion(Version + "+f873dfca")
    17  	if err != nil {
    18  		t.Fatal("Version 2 format error.")
    19  	}
    20  	if v1.GreaterThan(v2) || v1.GreaterThan(v2) {
    21  		t.Error("Version comparison error.")
    22  	}
    23  }
    24  
    25  func TestCompatibleWith(t *testing.T) {
    26  	cases := []struct {
    27  		a      string
    28  		b      string
    29  		result bool
    30  	}{
    31  		{
    32  			"1.0.4",
    33  			"1.0.4",
    34  			true,
    35  		},
    36  		{
    37  			"1.0.4",
    38  			"1.0.5",
    39  			true,
    40  		},
    41  		{
    42  			"1.0.4",
    43  			"1.1.5",
    44  			true,
    45  		},
    46  		{
    47  			"1.0.5",
    48  			"1.0.5-90825109",
    49  			true,
    50  		},
    51  		{
    52  			"1.0.5",
    53  			"1.0.5+90825109",
    54  			true,
    55  		},
    56  		{
    57  			"1.0.5",
    58  			"2.0.5",
    59  			false,
    60  		},
    61  		{
    62  			"1.0.5-90825109",
    63  			"1.0.5+90825109",
    64  			true,
    65  		},
    66  	}
    67  
    68  	for i, c := range cases {
    69  		Version = c.a
    70  		if result, _ := CompatibleWith(c.b); c.result != result {
    71  			t.Errorf("case %d: got %t want %t", i, c.result, result)
    72  		}
    73  	}
    74  }
    75  
    76  func TestCheckUpdate(t *testing.T) {
    77  	cases := []struct {
    78  		desc           string
    79  		localVer       string
    80  		remotePeers    []string
    81  		wantStatus     uint16
    82  		wantmaxVerSeen string
    83  		wantNotified   bool
    84  	}{
    85  		{
    86  			desc:           "has large version number update",
    87  			localVer:       "1.0",
    88  			remotePeers:    []string{"1.0", "2.0", "1.0.3"},
    89  			wantStatus:     hasMUpdate,
    90  			wantmaxVerSeen: "2.0",
    91  			wantNotified:   true,
    92  		},
    93  		{
    94  			desc:           "some remote version less than local version, but some remote verison larger than local version",
    95  			localVer:       "1.0",
    96  			remotePeers:    []string{"0.8", "1.1", "1.0.3", "0.9"},
    97  			wantStatus:     hasUpdate,
    98  			wantmaxVerSeen: "1.1",
    99  			wantNotified:   true,
   100  		},
   101  		{
   102  			desc:           "has small version number update",
   103  			localVer:       "1.0",
   104  			remotePeers:    []string{"1.0", "1.0.3", "1.0.2"},
   105  			wantStatus:     hasUpdate,
   106  			wantmaxVerSeen: "1.0.3",
   107  			wantNotified:   true,
   108  		},
   109  		{
   110  			desc:           "the remote equals to local version",
   111  			localVer:       "1.0",
   112  			remotePeers:    []string{"1.0", "1.0", "1.0"},
   113  			wantStatus:     noUpdate,
   114  			wantmaxVerSeen: "1.0",
   115  			wantNotified:   false,
   116  		},
   117  		{
   118  			desc:           "the remote version less than local version",
   119  			localVer:       "1.0",
   120  			remotePeers:    []string{"0.8", "0.8", "0.8"},
   121  			wantStatus:     noUpdate,
   122  			wantmaxVerSeen: "1.0",
   123  			wantNotified:   false,
   124  		},
   125  	}
   126  
   127  	for i, c := range cases {
   128  		status := &UpdateStatus{
   129  			maxVerSeen:    c.localVer,
   130  			notified:      false,
   131  			seedSet:       set.New(),
   132  			versionStatus: noUpdate,
   133  		}
   134  		for i, remoteVer := range c.remotePeers {
   135  			peer := fmt.Sprintf("peer%d", i)
   136  			status.seedSet.Add(peer)
   137  			if err := status.CheckUpdate(c.localVer, remoteVer, peer); err != nil {
   138  				t.Fatal(err)
   139  			}
   140  		}
   141  
   142  		if status.versionStatus != c.wantStatus {
   143  			t.Errorf("#%d(%s) got version status:%d, want version status:%d", i, c.desc, status.versionStatus, c.wantStatus)
   144  		}
   145  
   146  		if status.notified != c.wantNotified {
   147  			t.Errorf("#%d(%s) got notified:%t, want notified:%t", i, c.desc, status.notified, c.wantNotified)
   148  		}
   149  
   150  		if status.maxVerSeen != c.wantmaxVerSeen {
   151  			t.Errorf("#%d(%s) got max version seen%s, want max version seen%s", i, c.desc, status.maxVerSeen, c.wantmaxVerSeen)
   152  		}
   153  	}
   154  }
   155  
   156  // In case someone edit the iota part and have the mapping changed:
   157  // noUpdate: 0
   158  // hasUpdate: 1
   159  // hasMUpdate: 2
   160  func TestFlag(t *testing.T) {
   161  	if noUpdate != 0 {
   162  		t.Error("noUpdate value error")
   163  	}
   164  	if hasUpdate != 1 {
   165  		t.Error("hasUpdate value error")
   166  	}
   167  	if hasMUpdate != 2 {
   168  		t.Error("noUpdate value error")
   169  	}
   170  }