Skip to content

Commit

Permalink
Fix incorrect expiry when using resource153ToLegacyAdapter (#39773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tener authored Mar 27, 2024
1 parent 4fbed24 commit f3a8227
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
14 changes: 13 additions & 1 deletion api/types/resource_153.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ func (r *resource153ToLegacyAdapter) MarshalJSON() ([]byte, error) {
}

func (r *resource153ToLegacyAdapter) Expiry() time.Time {
return r.inner.GetMetadata().Expires.AsTime()
expires := r.inner.GetMetadata().Expires
// return zero time.time{} for zero *timestamppb.Timestamp, instead of 01/01/1970.
if expires == nil {
return time.Time{}
}

return expires.AsTime()
}

func (r *resource153ToLegacyAdapter) GetKind() string {
Expand All @@ -159,7 +165,13 @@ func (r *resource153ToLegacyAdapter) GetKind() string {

func (r *resource153ToLegacyAdapter) GetMetadata() Metadata {
md := r.inner.GetMetadata()

// use zero time.time{} for zero *timestamppb.Timestamp, instead of 01/01/1970.
expires := md.Expires.AsTime()
if md.Expires == nil {
expires = time.Time{}
}

return Metadata{
Name: md.Name,
Namespace: md.Namespace,
Expand Down
62 changes: 62 additions & 0 deletions api/types/resource_153_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,65 @@ func TestResourceMethods(t *testing.T) {
require.Equal(t, "mars", origin)
})
}

// Tests that expiry is consistent across the different types and transformations.
func TestExpiryConsistency(t *testing.T) {
tests := []struct {
name string
expiryTimestamp *timestamppb.Timestamp
expectedExpiry time.Time
}{
{
name: "nil expiry",
expiryTimestamp: nil,
expectedExpiry: time.Time{},
},
{
name: "zero expiry",
expiryTimestamp: timestamppb.New(time.Time{}),
expectedExpiry: time.Time{},
},
{
name: "set expiry",
expiryTimestamp: timestamppb.New(time.Date(2024, 11, 11, 11, 11, 11, 00, time.UTC)),
expectedExpiry: time.Date(2024, 11, 11, 11, 11, 11, 00, time.UTC),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bot := &machineidv1.Bot{
Kind: "bot",
SubKind: "robot",
Metadata: &headerv1.Metadata{Name: "Bernard", Expires: tt.expiryTimestamp},
Spec: &machineidv1.BotSpec{
Roles: []string{"robot", "human"},
},
}

legacyResource := types.Resource153ToLegacy(bot)

// verify expiry time in different ways
t.Run("GetExpiry() resource", func(t *testing.T) {
expiry, err := types.GetExpiry(bot)
require.NoError(t, err)
require.Equal(t, tt.expectedExpiry, expiry)
})

t.Run("GetExpiry() wrapper", func(t *testing.T) {
expiry, err := types.GetExpiry(legacyResource)
require.NoError(t, err)
require.Equal(t, tt.expectedExpiry, expiry)
})

t.Run("wrapper .Expiry()", func(t *testing.T) {
require.Equal(t, tt.expectedExpiry, legacyResource.Expiry())
})

t.Run("wrapper metadata .Expiry()", func(t *testing.T) {
md := legacyResource.GetMetadata()
require.Equal(t, tt.expectedExpiry, md.Expiry())
})
})
}
}

0 comments on commit f3a8227

Please sign in to comment.