|
| 1 | +package testhelper |
| 2 | + |
| 3 | +// This file contains helper method for gNOI services such as |
| 4 | +// Reboot, Install etc. |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/openconfig/ondatra" |
| 12 | + "github.com/openconfig/ondatra/gnmi" |
| 13 | + |
| 14 | + healthzpb "github.com/openconfig/gnoi/healthz" |
| 15 | + syspb "github.com/openconfig/gnoi/system" |
| 16 | +) |
| 17 | + |
| 18 | +// Function pointers that interact with the switch. They enable unit testing |
| 19 | +// of methods that interact with the switch. |
| 20 | +var ( |
| 21 | + gnoiSystemClientGet = func(t *testing.T, d *ondatra.DUTDevice) syspb.SystemClient { |
| 22 | + return d.RawAPIs().GNOI(t).System() |
| 23 | + } |
| 24 | + |
| 25 | + gnoiHealthzClientGet = func(t *testing.T, d *ondatra.DUTDevice) healthzpb.HealthzClient { |
| 26 | + return d.RawAPIs().GNOI(t).Healthz() |
| 27 | + } |
| 28 | + |
| 29 | + gnmiSystemBootTimeGet = func(t *testing.T, d *ondatra.DUTDevice) uint64 { |
| 30 | + return gnmi.Get(t, d, gnmi.OC().System().BootTime().State()) |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +// RebootParams specify the reboot parameters used by the Reboot API. |
| 35 | +type RebootParams struct { |
| 36 | + request any |
| 37 | + waitTime time.Duration |
| 38 | + checkInterval time.Duration |
| 39 | + lmTTkrID string // latency measurement testtracker UUID |
| 40 | + lmTitle string // latency measurement title |
| 41 | +} |
| 42 | + |
| 43 | +// NewRebootParams returns RebootParams structure with default values. |
| 44 | +func NewRebootParams() *RebootParams { |
| 45 | + return &RebootParams{ |
| 46 | + waitTime: 4 * time.Minute, |
| 47 | + checkInterval: 20 * time.Second, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// WithWaitTime adds the period of time to wait for the reboot operation to be |
| 52 | +// successful. |
| 53 | +func (p *RebootParams) WithWaitTime(t time.Duration) *RebootParams { |
| 54 | + p.waitTime = t |
| 55 | + return p |
| 56 | +} |
| 57 | + |
| 58 | +// WithCheckInterval adds the time interval to check whether the reboot |
| 59 | +// operation has been successful. |
| 60 | +func (p *RebootParams) WithCheckInterval(t time.Duration) *RebootParams { |
| 61 | + p.checkInterval = t |
| 62 | + return p |
| 63 | +} |
| 64 | + |
| 65 | +// WithRequest adds the reboot request in RebootParams. The reboot request can |
| 66 | +// be one of the following: |
| 67 | +// 1) RebootMethod such as syspb.RebootMethod_COLD. |
| 68 | +// 2) RebootRequest protobuf. |
| 69 | +func (p *RebootParams) WithRequest(r any) *RebootParams { |
| 70 | + p.request = r |
| 71 | + return p |
| 72 | +} |
| 73 | + |
| 74 | +// WithLatencyMeasurement adds testtracker uuid and title for latency measurement. |
| 75 | +func (p *RebootParams) WithLatencyMeasurement(testTrackerID, title string) *RebootParams { |
| 76 | + p.lmTTkrID = testTrackerID |
| 77 | + p.lmTitle = title |
| 78 | + return p |
| 79 | +} |
| 80 | + |
| 81 | +// measureLatency returns true if latency measurement parameters are set and valid. |
| 82 | +func (p *RebootParams) measureLatency() bool { |
| 83 | + return p.waitTime > 0 && p.lmTitle != "" |
| 84 | +} |
| 85 | + |
| 86 | +// GNOIAble returns whether the gNOI server on the specified device is reachable |
| 87 | +// or not. |
| 88 | +func GNOIAble(t *testing.T, d *ondatra.DUTDevice) error { |
| 89 | + // Time() gNOI request is used to verify the gNOI server reachability. |
| 90 | + _, err := gnoiSystemClientGet(t, d).Time(context.Background(), &syspb.TimeRequest{}) |
| 91 | + return err |
| 92 | +} |
| 93 | + |
| 94 | +// HealthzGetPortDebugData returns port debug data given an interface. |
| 95 | +func HealthzGetPortDebugData(t *testing.T, d *ondatra.DUTDevice, intfName string) error { |
| 96 | + return fmt.Errorf("unimplemented method HealthzGetPortDebugData") |
| 97 | +} |
0 commit comments