github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/gnmi/arbitration_test.go (about) 1 // Copyright (c) 2019 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package gnmi 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/aristanetworks/goarista/test" 12 13 "github.com/openconfig/gnmi/proto/gnmi_ext" 14 ) 15 16 func arbitration(role string, id *gnmi_ext.Uint128) *gnmi_ext.Extension { 17 arb := &gnmi_ext.MasterArbitration{ 18 Role: &gnmi_ext.Role{Id: role}, 19 ElectionId: id, 20 } 21 ext := gnmi_ext.Extension_MasterArbitration{MasterArbitration: arb} 22 return &gnmi_ext.Extension{Ext: &ext} 23 } 24 25 func electionID(high, low uint64) *gnmi_ext.Uint128 { 26 return &gnmi_ext.Uint128{High: high, Low: low} 27 } 28 29 func TestArbitrationExt(t *testing.T) { 30 testCases := map[string]struct { 31 s string 32 ext *gnmi_ext.Extension 33 err error 34 }{ 35 "empty": {}, 36 "no_role": { 37 s: "1", 38 ext: arbitration("", electionID(0, 1)), 39 }, 40 "with_role": { 41 s: "admin:1", 42 ext: arbitration("admin", electionID(0, 1)), 43 }, 44 "large_no_role": { 45 s: "9223372036854775807", 46 ext: arbitration("", electionID(0, 9223372036854775807)), 47 }, 48 "large_with_role": { 49 s: "admin:18446744073709551615", 50 ext: arbitration("admin", electionID(0, 18446744073709551615)), 51 }, 52 "invalid": { 53 s: "cat", 54 err: fmt.Errorf("badly formed arbitration id (%s)", "cat"), 55 }, 56 "invalid_too_many_colons": { 57 s: "dog:1:2", 58 err: fmt.Errorf("badly formed arbitration id (%s)", "dog:1:2"), 59 }, 60 } 61 62 for name, tc := range testCases { 63 t.Run(name, func(t *testing.T) { 64 ext, err := ArbitrationExt(tc.s) 65 if !test.DeepEqual(tc.ext, ext) { 66 t.Errorf("Expected %#v, got %#v", tc.ext, ext) 67 } 68 if !test.DeepEqual(tc.err, err) { 69 t.Errorf("Expected %v, got %v", tc.err, err) 70 } 71 }) 72 } 73 }