diff --git a/internal/pkg/nodeagent/agent.go b/internal/pkg/nodeagent/agent.go index 2ec4527ec..ab0e6c19e 100644 --- a/internal/pkg/nodeagent/agent.go +++ b/internal/pkg/nodeagent/agent.go @@ -189,12 +189,12 @@ func (a *Agent) beat(ctx context.Context, booted, stopping bool) *models.NodeHea return nil } - var reply models.NodeHeartbeatReply - if err := json.NewDecoder(resp.Body).Decode(&reply); err != nil { + var reply *models.NodeHeartbeatReply + if err := json.NewDecoder(resp.Body).Decode(&reply); err != nil || reply == nil { return nil } a.applyTarget(reply.DesiredVersion) - return &reply + return reply } // applyTarget records the version the control plane wants. Writing a file the diff --git a/internal/pkg/nodeagent/boot_retry_test.go b/internal/pkg/nodeagent/boot_retry_test.go index 6969e572a..5e64abc24 100644 --- a/internal/pkg/nodeagent/boot_retry_test.go +++ b/internal/pkg/nodeagent/boot_retry_test.go @@ -40,6 +40,8 @@ func TestRunRetriesBootUntilAcknowledged(t *testing.T) { status = http.StatusServiceUnavailable case 3: body = "incomplete reply" + case 4: + body = "null" } return &http.Response{StatusCode: status, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil })} @@ -48,10 +50,10 @@ func TestRunRetriesBootUntilAcknowledged(t *testing.T) { defer cancel() done := make(chan struct{}) go func() { a.Run(ctx); close(done) }() - for i := range 5 { + for i := range 6 { if i > 0 { interval := DefaultInterval - if i == 4 { + if i == 5 { interval = 40 * time.Second } time.Sleep(interval) @@ -59,7 +61,7 @@ func TestRunRetriesBootUntilAcknowledged(t *testing.T) { synctest.Wait() select { case beat := <-received: - if beat.Booted != (i < 4) || beat.Stopping { + if beat.Booted != (i < 5) || beat.Stopping { t.Fatalf("heartbeat %d: Booted=%t Stopping=%t", i+1, beat.Booted, beat.Stopping) } default: