From c813d3f12776532e9209d1848e39a3da73f83b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paolo=20Chil=C3=A0?= Date: Thu, 16 Nov 2023 17:54:48 +0100 Subject: [PATCH] Fix comparison of upgrade metadata (#3779) --- .../pkg/agent/application/dispatcher/dispatcher.go | 2 +- .../agent/application/dispatcher/dispatcher_test.go | 5 +++-- .../agent/application/upgrade/details/details.go | 13 ++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/pkg/agent/application/dispatcher/dispatcher.go b/internal/pkg/agent/application/dispatcher/dispatcher.go index eda450d6a0b..63d00aeeea3 100644 --- a/internal/pkg/agent/application/dispatcher/dispatcher.go +++ b/internal/pkg/agent/application/dispatcher/dispatcher.go @@ -321,7 +321,7 @@ func reportNextScheduledUpgrade(input []fleetapi.Action, detailsSetter details.O upgradeDetails := details.NewDetails(nextUpgrade.Version, details.StateScheduled, nextUpgrade.ID()) startTime, _ := nextUpgrade.StartTime() - upgradeDetails.Metadata.ScheduledAt = startTime + upgradeDetails.Metadata.ScheduledAt = &startTime detailsSetter(upgradeDetails) } diff --git a/internal/pkg/agent/application/dispatcher/dispatcher_test.go b/internal/pkg/agent/application/dispatcher/dispatcher_test.go index 212c50a3068..bf69c76ce74 100644 --- a/internal/pkg/agent/application/dispatcher/dispatcher_test.go +++ b/internal/pkg/agent/application/dispatcher/dispatcher_test.go @@ -493,6 +493,7 @@ func Test_ActionDispatcher_scheduleRetry(t *testing.T) { func TestReportNextScheduledUpgrade(t *testing.T) { now := time.Now().UTC() later := now.Add(3 * time.Hour) + laterTruncate := later.Truncate(time.Second) muchLater := later.Add(3 * time.Hour) cases := map[string]struct { @@ -522,7 +523,7 @@ func TestReportNextScheduledUpgrade(t *testing.T) { State: details.StateScheduled, ActionID: "action2", Metadata: details.Metadata{ - ScheduledAt: later.Truncate(time.Second), + ScheduledAt: &laterTruncate, }, }, }, @@ -544,7 +545,7 @@ func TestReportNextScheduledUpgrade(t *testing.T) { State: details.StateScheduled, ActionID: "action4", Metadata: details.Metadata{ - ScheduledAt: later.Truncate(time.Second), + ScheduledAt: &laterTruncate, }, }, }, diff --git a/internal/pkg/agent/application/upgrade/details/details.go b/internal/pkg/agent/application/upgrade/details/details.go index dcc990d33cd..dae27923ecf 100644 --- a/internal/pkg/agent/application/upgrade/details/details.go +++ b/internal/pkg/agent/application/upgrade/details/details.go @@ -167,13 +167,24 @@ func (d *Details) notifyObserver(observer Observer) { } func (m Metadata) Equals(otherM Metadata) bool { - return m.ScheduledAt.Equal(otherM.ScheduledAt) && + return equalTimePointers(m.ScheduledAt, otherM.ScheduledAt) && m.FailedState == otherM.FailedState && m.ErrorMsg == otherM.ErrorMsg && m.DownloadPercent == otherM.DownloadPercent && m.DownloadRate == otherM.DownloadRate } +func equalTimePointers(t, otherT *time.Time) bool { + if t == otherT { + return true + } + if t == nil || otherT == nil { + return false + } + + return t.Equal(*otherT) +} + func (dr *downloadRate) MarshalJSON() ([]byte, error) { downloadRateBytesPerSecond := float64(*dr) if math.IsInf(downloadRateBytesPerSecond, 0) {