github.com/cilium/cilium@v1.16.2/pkg/maps/timestamp/timestamp_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package timestamp
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/cilium/cilium/api/v1/models"
    12  )
    13  
    14  func TestConvert(t *testing.T) {
    15  	tests := []struct {
    16  		name  string
    17  		mode  string
    18  		hertz int64
    19  		err   bool
    20  		inp   uint64
    21  		res   uint64
    22  	}{
    23  		{name: "ktime", mode: models.ClockSourceModeKtime, inp: uint64(0xff00ff0012345678), res: uint64(0xff00ff0012345678)},
    24  		{name: "jiffies_err", mode: models.ClockSourceModeJiffies, err: true},
    25  		{name: "jiffies_ok", mode: models.ClockSourceModeJiffies, hertz: 100, inp: uint64(0x00ffff0012345678), res: uint64(0x28f5999c834108f)},
    26  		{name: "invalid", mode: "", err: true},
    27  	}
    28  
    29  	for _, tt := range tests {
    30  		t.Run(tt.name, func(t *testing.T) {
    31  			clockSource := &models.ClockSource{Mode: tt.mode, Hertz: tt.hertz}
    32  			conv, err := NewCTTimeToSecConverter(clockSource)
    33  			if tt.err {
    34  				require.Error(t, err, "Invalid converter created")
    35  			} else {
    36  				require.NoError(t, err, "Failed to create converter")
    37  				res := conv(tt.inp)
    38  				require.Equal(t, tt.res, res)
    39  			}
    40  		})
    41  	}
    42  }