|
| 1 | +package record_subscribe_idle_timeout_doa_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/openconfig/featureprofiles/internal/fptest" |
| 11 | + "github.com/openconfig/featureprofiles/internal/helpers" |
| 12 | + acctzpb "github.com/openconfig/gnsi/acctz" |
| 13 | + "github.com/openconfig/ondatra" |
| 14 | + "github.com/openconfig/ondatra/gnmi" |
| 15 | + "google.golang.org/grpc/codes" |
| 16 | + "google.golang.org/grpc/status" |
| 17 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + defaultIdleTimeout = 120 * time.Second |
| 22 | + gracePeriod = 60 * time.Second |
| 23 | + gnsiProfileName = "GNSI_PROFILE" |
| 24 | +) |
| 25 | + |
| 26 | +func TestMain(m *testing.M) { |
| 27 | + fptest.RunTests(m) |
| 28 | +} |
| 29 | + |
| 30 | +func isStreamClosedErr(err error) bool { |
| 31 | + if err == nil { |
| 32 | + return false |
| 33 | + } |
| 34 | + if st, ok := status.FromError(err); ok { |
| 35 | + switch st.Code() { |
| 36 | + case codes.Canceled, codes.Unavailable: |
| 37 | + return true |
| 38 | + default: |
| 39 | + return false |
| 40 | + } |
| 41 | + } |
| 42 | + // Non-gRPC error (e.g., EOF) indicates closed stream. |
| 43 | + return true |
| 44 | +} |
| 45 | + |
| 46 | +func prettyPrint(i any) string { |
| 47 | + s, err := json.MarshalIndent(i, "", "\t") |
| 48 | + if err != nil { |
| 49 | + return fmt.Sprintf("<error: %v>", err) |
| 50 | + } |
| 51 | + return string(s) |
| 52 | +} |
| 53 | + |
| 54 | +func TestRecordSubscribeIdleTimeoutDoA(t *testing.T) { |
| 55 | + dut := ondatra.DUT(t, "dut") |
| 56 | + configureGnsiServiceCLI(t, dut) |
| 57 | + |
| 58 | + systemTime := gnmi.Get(t, dut, gnmi.OC().System().CurrentDatetime().State()) |
| 59 | + startTime, err := time.Parse(time.RFC3339, systemTime) |
| 60 | + if err != nil { |
| 61 | + t.Errorf("Failed to parse system time %q: %v", systemTime, err) |
| 62 | + } |
| 63 | + ctx, cancel := context.WithTimeout(t.Context(), defaultIdleTimeout+gracePeriod+30*time.Second) |
| 64 | + defer cancel() |
| 65 | + |
| 66 | + acctzClient := dut.RawAPIs().GNSI(t).AcctzStream() |
| 67 | + acctzSubClient, err := acctzClient.RecordSubscribe(ctx, &acctzpb.RecordRequest{Timestamp: timestamppb.New(startTime)}) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("Failed sending accountz record request, error: %s", err) |
| 70 | + } |
| 71 | + defer acctzSubClient.CloseSend() |
| 72 | + |
| 73 | + t.Log("RecordSubscribe stream established successfully.") |
| 74 | + t.Logf("Waiting %v to trigger idle timeout... %v", defaultIdleTimeout+gracePeriod, time.Now()) |
| 75 | + time.Sleep(defaultIdleTimeout + gracePeriod) |
| 76 | + t.Log("Idle timeout and grace period passed:", time.Now()) |
| 77 | + t.Logf("Try to receive a record after idle timeout: %v", time.Now()) |
| 78 | + recv, recvErr := acctzSubClient.Recv() |
| 79 | + t.Logf("%v Try to log recv: %v", time.Now(), prettyPrint(recv)) |
| 80 | + |
| 81 | + if recvErr == nil { |
| 82 | + t.Errorf("RecordSubscribe stream is still active after the idle timeout period, but it should have been closed by the DUT.") |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if !isStreamClosedErr(recvErr) { |
| 87 | + t.Errorf("RecordSubscribe stream returned unexpected error after idle timeout: %v", recvErr) |
| 88 | + return |
| 89 | + } |
| 90 | + t.Logf("Stream closed as expected after idle timeout: %v", recvErr) |
| 91 | +} |
| 92 | + |
| 93 | +func configureGnsiServiceCLI(t *testing.T, dut *ondatra.DUTDevice) { |
| 94 | + t.Helper() |
| 95 | + t.Log("Configuring gnsi service through CLI") |
| 96 | + gnsiServerConfig := fmt.Sprintf(` |
| 97 | + management api gnmi |
| 98 | + transport grpc default |
| 99 | + ssl profile %s |
| 100 | + ! |
| 101 | + management api gnsi |
| 102 | + transport gnmi default |
| 103 | + service acctz |
| 104 | + ! |
| 105 | +`, gnsiProfileName) |
| 106 | + helpers.GnmiCLIConfig(t, dut, gnsiServerConfig) |
| 107 | +} |
0 commit comments