Skip to content

Commit

Permalink
Fix unit tests for pkg/apiserver/registry/stats (#5363)
Browse files Browse the repository at this point in the history
Some unit tests were assuming that the clock for the test machine was
using the UTC time zone. In my case, the unit tests were failing when
run locally.

Signed-off-by: Antonin Bas <[email protected]>
  • Loading branch information
antoninbas authored Aug 8, 2023
1 parent eeec2c0 commit c9770d7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions)

var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()

func formatTimestamp(t metav1.Time) string {
return t.UTC().Format(time.RFC3339)
}

func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
table := &metav1.Table{
ColumnDefinitions: tableColumnDefinitions,
Expand All @@ -131,7 +135,7 @@ func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOpti
var err error
table.Rows, err = metatable.MetaToTableRow(obj, func(obj runtime.Object, m metav1.Object, name, age string) ([]interface{}, error) {
stats := obj.(*statsv1alpha1.AntreaClusterNetworkPolicyStats)
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, m.GetCreationTimestamp().Time.UTC().Format(time.RFC3339)}, nil
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, formatTimestamp(m.GetCreationTimestamp())}, nil
})
return table, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func TestRESTConvertToTable(t *testing.T) {
Sessions: 5,
},
}
expectedFormattedCreationTimestamp := stats.CreationTimestamp.UTC().Format(time.RFC3339)
tests := []struct {
name string
object runtime.Object
Expand All @@ -290,7 +291,7 @@ func TestRESTConvertToTable(t *testing.T) {
ColumnDefinitions: tableColumnDefinitions,
Rows: []metav1.TableRow{
{
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), stats.CreationTimestamp.Format("2006-01-02T15:04:05Z")},
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), expectedFormattedCreationTimestamp},
Object: runtime.RawExtension{Object: stats},
},
},
Expand All @@ -303,7 +304,7 @@ func TestRESTConvertToTable(t *testing.T) {
ColumnDefinitions: tableColumnDefinitions,
Rows: []metav1.TableRow{
{
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), stats.CreationTimestamp.Format("2006-01-02T15:04:05Z")},
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), expectedFormattedCreationTimestamp},
Object: runtime.RawExtension{Object: stats},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions)

var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()

func formatTimestamp(t metav1.Time) string {
return t.UTC().Format(time.RFC3339)
}

func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
table := &metav1.Table{
ColumnDefinitions: tableColumnDefinitions,
Expand All @@ -137,7 +141,7 @@ func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOpti
var err error
table.Rows, err = metatable.MetaToTableRow(obj, func(obj runtime.Object, m metav1.Object, name, age string) ([]interface{}, error) {
stats := obj.(*statsv1alpha1.AntreaNetworkPolicyStats)
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, m.GetCreationTimestamp().Time.UTC().Format(time.RFC3339)}, nil
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, formatTimestamp(m.GetCreationTimestamp())}, nil
})
return table, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func TestRESTConvertToTable(t *testing.T) {
Sessions: 5,
},
}
expectedFormattedCreationTimestamp := stats.CreationTimestamp.UTC().Format(time.RFC3339)
tests := []struct {
name string
object runtime.Object
Expand All @@ -367,7 +368,7 @@ func TestRESTConvertToTable(t *testing.T) {
ColumnDefinitions: tableColumnDefinitions,
Rows: []metav1.TableRow{
{
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), stats.CreationTimestamp.Format("2006-01-02T15:04:05Z")},
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), expectedFormattedCreationTimestamp},
Object: runtime.RawExtension{Object: stats},
},
},
Expand All @@ -380,7 +381,7 @@ func TestRESTConvertToTable(t *testing.T) {
ColumnDefinitions: tableColumnDefinitions,
Rows: []metav1.TableRow{
{
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), stats.CreationTimestamp.Format("2006-01-02T15:04:05Z")},
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), expectedFormattedCreationTimestamp},
Object: runtime.RawExtension{Object: stats},
},
},
Expand Down
6 changes: 5 additions & 1 deletion pkg/apiserver/registry/stats/networkpolicystats/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions)

var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()

func formatTimestamp(t metav1.Time) string {
return t.UTC().Format(time.RFC3339)
}

func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
table := &metav1.Table{
ColumnDefinitions: tableColumnDefinitions,
Expand All @@ -131,7 +135,7 @@ func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOpti
var err error
table.Rows, err = metatable.MetaToTableRow(obj, func(obj runtime.Object, m metav1.Object, name, age string) ([]interface{}, error) {
stats := obj.(*statsv1alpha1.NetworkPolicyStats)
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, m.GetCreationTimestamp().Time.UTC().Format(time.RFC3339)}, nil
return []interface{}{name, stats.TrafficStats.Sessions, stats.TrafficStats.Packets, stats.TrafficStats.Bytes, formatTimestamp(m.GetCreationTimestamp())}, nil
})
return table, err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/apiserver/registry/stats/networkpolicystats/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func TestRESTConvertToTable(t *testing.T) {
Sessions: 5,
},
}
expectedFormattedCreationTimestamp := stats.CreationTimestamp.UTC().Format(time.RFC3339)
tests := []struct {
name string
object runtime.Object
Expand All @@ -338,7 +339,7 @@ func TestRESTConvertToTable(t *testing.T) {
ColumnDefinitions: tableColumnDefinitions,
Rows: []metav1.TableRow{
{
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), stats.CreationTimestamp.Format("2006-01-02T15:04:05Z")},
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), expectedFormattedCreationTimestamp},
Object: runtime.RawExtension{Object: stats},
},
},
Expand All @@ -351,7 +352,7 @@ func TestRESTConvertToTable(t *testing.T) {
ColumnDefinitions: tableColumnDefinitions,
Rows: []metav1.TableRow{
{
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), stats.CreationTimestamp.Format("2006-01-02T15:04:05Z")},
Cells: []interface{}{"bar", int64(5), int64(10), int64(2000), expectedFormattedCreationTimestamp},
Object: runtime.RawExtension{Object: stats},
},
},
Expand Down

0 comments on commit c9770d7

Please sign in to comment.