github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/tools/validatormon/validatormon_test.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  )
     7  
     8  type mockSR struct{}
     9  
    10  func (m *mockSR) StatusRead(accountID string) (*Status, error) {
    11  	switch accountID {
    12  	case "GCWJKM4EGTGJUVSWUJDPCQEOEP5LHSOFKSA4HALBTOO4T4H3HCHOM6UX":
    13  		return &Status{Ledger: 9999, Phase: "EXTERNALIZE"}, nil
    14  	case "GDKWELGJURRKXECG3HHFHXMRX64YWQPUHKCVRESOX3E5PM6DM4YXLZJM":
    15  		return &Status{Ledger: 9999, Phase: "Sync"}, nil
    16  	case "GA35T3723UP2XJLC2H7MNL6VMKZZIFL2VW7XHMFFJKKIA2FJCYTLKFBW":
    17  		return &Status{Ledger: 9980, Phase: "EXTERNALIZE"}, nil
    18  	case "GCGB2S2KGYARPVIA37HYZXVRM2YZUEXA6S33ZU5BUDC6THSB62LZSTYH":
    19  		return &Status{Ledger: 9999}, nil
    20  	case "GCM6QMP3DLRPTAZW2UZPCPX2LF3SXWXKPMP3GKFZBDSF3QZGV2G5QSTK":
    21  		return &Status{Ledger: 10000}, nil
    22  	case "GABMKJM6I25XI4K7U6XWMULOUQIQ27BCTMLS6BYYSOWKTBUXVRJSXHYQ":
    23  		return &Status{Ledger: 9997}, nil
    24  	default:
    25  		return nil, errors.New("unknown account id")
    26  	}
    27  }
    28  
    29  func TestCompareLedger(t *testing.T) {
    30  	n := CompareLedger(new(mockSR))
    31  	if n != 10000 {
    32  		t.Errorf("CompareLedger: %d, expected 10000", n)
    33  	}
    34  }
    35  
    36  func TestAnalyzeNode(t *testing.T) {
    37  	a, err := AnalyzeNode(new(mockSR), "keybase1")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if !a.Ok {
    42  		t.Errorf("keybase1 not ok, expected ok: %+v", a)
    43  	}
    44  
    45  	a, err = AnalyzeNode(new(mockSR), "keybase2")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	// this was a bad phase check, which we are ignoring now
    50  	/*
    51  		if a.Ok {
    52  			t.Errorf("keybase2 ok, expected not ok: %+v", a)
    53  		}
    54  	*/
    55  	if !a.Ok {
    56  		t.Errorf("keybase2 not ok, expected ok: %+v", a)
    57  	}
    58  
    59  	a, err = AnalyzeNode(new(mockSR), "keybase3")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if a.Ok {
    64  		t.Errorf("keybase3 ok, expected not ok: %+v", a)
    65  	}
    66  }
    67  
    68  func TestStatusFromJSON(t *testing.T) {
    69  	s, err := statusFromJSON([]byte(lbRes))
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	if s.Node != "lobstr3" {
    74  		t.Errorf("node: %q, expected lobstr3", s.Node)
    75  	}
    76  	if s.Ledger != 25889124 {
    77  		t.Errorf("ledger: %d, expected 25889124", s.Ledger)
    78  	}
    79  	if s.Phase != "expired" {
    80  		t.Errorf("phase: %q, expected expired", s.Phase)
    81  	}
    82  	if len(s.Missing) != 0 {
    83  		t.Errorf("missing len: %d, expected 0", len(s.Missing))
    84  	}
    85  
    86  	s, err = statusFromJSON([]byte(kbRes))
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if s.Node != "keybase2" {
    91  		t.Errorf("node: %q, expected keybase2", s.Node)
    92  	}
    93  	if s.Ledger != 25889136 {
    94  		t.Errorf("ledger: %d, expected 25889136", s.Ledger)
    95  	}
    96  	if s.Phase != "EXTERNALIZE" {
    97  		t.Errorf("phase: %q, expected EXTERNALIZE", s.Phase)
    98  	}
    99  	if len(s.Missing) != 1 {
   100  		t.Errorf("missing len: %d, expected 1", len(s.Missing))
   101  	}
   102  }
   103  
   104  const (
   105  	lbRes = `{
   106     "node" : "lobstr3",
   107     "qset" : {
   108        "delayed" : null,
   109        "disagree" : null,
   110        "ledger" : 25889124,
   111        "missing" : null,
   112        "phase" : "expired",
   113        "validated" : true
   114     }
   115  }
   116  `
   117  	kbRes = `{
   118     "node" : "keybase2",
   119     "qset" : {
   120        "agree" : 16,
   121        "delayed" : null,
   122        "disagree" : null,
   123        "fail_at" : 4,
   124        "fail_with" : [ "sdf3", "sdf1", "coinqvest.FI", "coinqvest.HK" ],
   125        "hash" : "b69f17",
   126        "ledger" : 25889136,
   127        "missing" : [ "lobstr3" ],
   128        "phase" : "EXTERNALIZE",
   129        "validated" : true,
   130        "value" : {
   131           "t" : 4,
   132           "v" : [
   133              {
   134                 "t" : 2,
   135                 "v" : [ "sdf3", "sdf1", "sdf2" ]
   136              },
   137              {
   138                 "t" : 2,
   139                 "v" : [ "coinqvest.FI", "coinqvest.HK", "coinqvest.DE" ]
   140              },
   141              {
   142                 "t" : 2,
   143                 "v" : [ "satoshipay.US", "satoshipay.SG", "satoshipay.DE" ]
   144              },
   145              {
   146                 "t" : 2,
   147                 "v" : [ "keybase3", "keybase1", "keybase2" ]
   148              },
   149              {
   150                 "t" : 3,
   151                 "v" : [ "lobstr5", "lobstr4", "lobstr1", "lobstr2", "lobstr3" ]
   152              }
   153           ]
   154        }
   155     }
   156  }
   157  `
   158  )