Skip to content

Commit f163b31

Browse files
committed
docker runtime healthcheck: initialize LastUpdated to the current time
1 parent 89c8635 commit f163b31

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

agent/doctor/docker_runtime_healthcheck.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525

2626
const systemPingTimeout = time.Second * 2
2727

28+
var timeNow = time.Now
29+
2830
type dockerRuntimeHealthcheck struct {
2931
// HealthcheckType is the reported healthcheck type
3032
HealthcheckType string `json:"HealthcheckType,omitempty"`
@@ -45,12 +47,13 @@ type dockerRuntimeHealthcheck struct {
4547
}
4648

4749
func NewDockerRuntimeHealthcheck(client dockerapi.DockerClient) *dockerRuntimeHealthcheck {
48-
nowTime := time.Now()
50+
nowTime := timeNow()
4951
return &dockerRuntimeHealthcheck{
5052
HealthcheckType: doctor.HealthcheckTypeContainerRuntime,
5153
Status: doctor.HealthcheckStatusInitializing,
5254
TimeStamp: nowTime,
5355
StatusChangeTime: nowTime,
56+
LastTimeStamp: nowTime,
5457
client: client,
5558
}
5659
}

agent/doctor/docker_runtime_healthcheck_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,23 @@ func TestNewDockerRuntimeHealthCheck(t *testing.T) {
1919
ctrl := gomock.NewController(t)
2020
defer ctrl.Finish()
2121
mockDockerClient := mock_dockerapi.NewMockDockerClient(ctrl)
22-
dockerRuntimeHealthCheck := NewDockerRuntimeHealthcheck(mockDockerClient)
23-
assert.Equal(t, doctor.HealthcheckStatusInitializing, dockerRuntimeHealthCheck.Status)
22+
23+
// Mock the time to have predictable values
24+
mockTime := time.Now()
25+
originalTimeNow := timeNow
26+
timeNow = func() time.Time { return mockTime }
27+
defer func() { timeNow = originalTimeNow }()
28+
29+
expectedDockerRuntimeHealthcheck := &dockerRuntimeHealthcheck{
30+
HealthcheckType: doctor.HealthcheckTypeContainerRuntime,
31+
Status: doctor.HealthcheckStatusInitializing,
32+
TimeStamp: mockTime,
33+
StatusChangeTime: mockTime,
34+
LastTimeStamp: mockTime,
35+
client: mockDockerClient,
36+
}
37+
actualDockerRuntimeHealthcheck := NewDockerRuntimeHealthcheck(mockDockerClient)
38+
assert.Equal(t, expectedDockerRuntimeHealthcheck, actualDockerRuntimeHealthcheck)
2439
}
2540

2641
func TestRunCheck(t *testing.T) {

0 commit comments

Comments
 (0)