cri: fix container status
Signed-off-by: Iceber Gu <wei.cai-nat@daocloud.io>
This commit is contained in:
parent
bb3fcf62f1
commit
92ab1a63b0
@ -93,13 +93,23 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If container is in the created state, not set started and finished unix timestamps
|
||||||
|
var st, ft int64
|
||||||
|
switch status.State() {
|
||||||
|
case runtime.ContainerState_CONTAINER_RUNNING:
|
||||||
|
// If container is in the running state, set started unix timestamps
|
||||||
|
st = status.StartedAt
|
||||||
|
case runtime.ContainerState_CONTAINER_EXITED, runtime.ContainerState_CONTAINER_UNKNOWN:
|
||||||
|
st, ft = status.StartedAt, status.FinishedAt
|
||||||
|
}
|
||||||
|
|
||||||
return &runtime.ContainerStatus{
|
return &runtime.ContainerStatus{
|
||||||
Id: meta.ID,
|
Id: meta.ID,
|
||||||
Metadata: meta.Config.GetMetadata(),
|
Metadata: meta.Config.GetMetadata(),
|
||||||
State: status.State(),
|
State: status.State(),
|
||||||
CreatedAt: status.CreatedAt,
|
CreatedAt: status.CreatedAt,
|
||||||
StartedAt: status.StartedAt,
|
StartedAt: st,
|
||||||
FinishedAt: status.FinishedAt,
|
FinishedAt: ft,
|
||||||
ExitCode: status.ExitCode,
|
ExitCode: status.ExitCode,
|
||||||
Image: spec,
|
Image: spec,
|
||||||
ImageRef: imageRef,
|
ImageRef: imageRef,
|
||||||
|
@ -47,7 +47,6 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
|
|||||||
}
|
}
|
||||||
|
|
||||||
createdAt := time.Now().UnixNano()
|
createdAt := time.Now().UnixNano()
|
||||||
startedAt := time.Now().UnixNano()
|
|
||||||
|
|
||||||
metadata := &containerstore.Metadata{
|
metadata := &containerstore.Metadata{
|
||||||
ID: testID,
|
ID: testID,
|
||||||
@ -60,7 +59,6 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
|
|||||||
status := &containerstore.Status{
|
status := &containerstore.Status{
|
||||||
Pid: 1234,
|
Pid: 1234,
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
StartedAt: startedAt,
|
|
||||||
}
|
}
|
||||||
image := &imagestore.Image{
|
image := &imagestore.Image{
|
||||||
ID: imageID,
|
ID: imageID,
|
||||||
@ -72,9 +70,8 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
|
|||||||
expected := &runtime.ContainerStatus{
|
expected := &runtime.ContainerStatus{
|
||||||
Id: testID,
|
Id: testID,
|
||||||
Metadata: config.GetMetadata(),
|
Metadata: config.GetMetadata(),
|
||||||
State: runtime.ContainerState_CONTAINER_RUNNING,
|
State: runtime.ContainerState_CONTAINER_CREATED,
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
StartedAt: startedAt,
|
|
||||||
Image: &runtime.ImageSpec{Image: "gcr.io/library/busybox:latest"},
|
Image: &runtime.ImageSpec{Image: "gcr.io/library/busybox:latest"},
|
||||||
ImageRef: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
|
ImageRef: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
|
||||||
Reason: completeExitReason,
|
Reason: completeExitReason,
|
||||||
@ -89,6 +86,7 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
|
|||||||
|
|
||||||
func TestToCRIContainerStatus(t *testing.T) {
|
func TestToCRIContainerStatus(t *testing.T) {
|
||||||
for desc, test := range map[string]struct {
|
for desc, test := range map[string]struct {
|
||||||
|
startedAt int64
|
||||||
finishedAt int64
|
finishedAt int64
|
||||||
exitCode int32
|
exitCode int32
|
||||||
reason string
|
reason string
|
||||||
@ -96,10 +94,15 @@ func TestToCRIContainerStatus(t *testing.T) {
|
|||||||
expectedState runtime.ContainerState
|
expectedState runtime.ContainerState
|
||||||
expectedReason string
|
expectedReason string
|
||||||
}{
|
}{
|
||||||
|
"container created": {
|
||||||
|
expectedState: runtime.ContainerState_CONTAINER_CREATED,
|
||||||
|
},
|
||||||
"container running": {
|
"container running": {
|
||||||
|
startedAt: time.Now().UnixNano(),
|
||||||
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
|
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
|
||||||
},
|
},
|
||||||
"container exited with reason": {
|
"container exited with reason": {
|
||||||
|
startedAt: time.Now().UnixNano(),
|
||||||
finishedAt: time.Now().UnixNano(),
|
finishedAt: time.Now().UnixNano(),
|
||||||
exitCode: 1,
|
exitCode: 1,
|
||||||
reason: "test-reason",
|
reason: "test-reason",
|
||||||
@ -108,6 +111,7 @@ func TestToCRIContainerStatus(t *testing.T) {
|
|||||||
expectedReason: "test-reason",
|
expectedReason: "test-reason",
|
||||||
},
|
},
|
||||||
"container exited with exit code 0 without reason": {
|
"container exited with exit code 0 without reason": {
|
||||||
|
startedAt: time.Now().UnixNano(),
|
||||||
finishedAt: time.Now().UnixNano(),
|
finishedAt: time.Now().UnixNano(),
|
||||||
exitCode: 0,
|
exitCode: 0,
|
||||||
message: "test-message",
|
message: "test-message",
|
||||||
@ -115,6 +119,7 @@ func TestToCRIContainerStatus(t *testing.T) {
|
|||||||
expectedReason: completeExitReason,
|
expectedReason: completeExitReason,
|
||||||
},
|
},
|
||||||
"container exited with non-zero exit code without reason": {
|
"container exited with non-zero exit code without reason": {
|
||||||
|
startedAt: time.Now().UnixNano(),
|
||||||
finishedAt: time.Now().UnixNano(),
|
finishedAt: time.Now().UnixNano(),
|
||||||
exitCode: 1,
|
exitCode: 1,
|
||||||
message: "test-message",
|
message: "test-message",
|
||||||
@ -124,6 +129,7 @@ func TestToCRIContainerStatus(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
metadata, status, _, expected := getContainerStatusTestData()
|
metadata, status, _, expected := getContainerStatusTestData()
|
||||||
// Update status with test case.
|
// Update status with test case.
|
||||||
|
status.StartedAt = test.startedAt
|
||||||
status.FinishedAt = test.finishedAt
|
status.FinishedAt = test.finishedAt
|
||||||
status.ExitCode = test.exitCode
|
status.ExitCode = test.exitCode
|
||||||
status.Reason = test.reason
|
status.Reason = test.reason
|
||||||
@ -134,11 +140,12 @@ func TestToCRIContainerStatus(t *testing.T) {
|
|||||||
)
|
)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
// Set expectation based on test case.
|
// Set expectation based on test case.
|
||||||
expected.State = test.expectedState
|
|
||||||
expected.Reason = test.expectedReason
|
expected.Reason = test.expectedReason
|
||||||
|
expected.StartedAt = test.startedAt
|
||||||
expected.FinishedAt = test.finishedAt
|
expected.FinishedAt = test.finishedAt
|
||||||
expected.ExitCode = test.exitCode
|
expected.ExitCode = test.exitCode
|
||||||
expected.Message = test.message
|
expected.Message = test.message
|
||||||
|
patchExceptedWithState(expected, test.expectedState)
|
||||||
containerStatus := toCRIContainerStatus(container,
|
containerStatus := toCRIContainerStatus(container,
|
||||||
expected.Image,
|
expected.Image,
|
||||||
expected.ImageRef)
|
expected.ImageRef)
|
||||||
@ -166,19 +173,27 @@ func TestContainerStatus(t *testing.T) {
|
|||||||
for desc, test := range map[string]struct {
|
for desc, test := range map[string]struct {
|
||||||
exist bool
|
exist bool
|
||||||
imageExist bool
|
imageExist bool
|
||||||
|
startedAt int64
|
||||||
finishedAt int64
|
finishedAt int64
|
||||||
reason string
|
reason string
|
||||||
expectedState runtime.ContainerState
|
expectedState runtime.ContainerState
|
||||||
expectErr bool
|
expectErr bool
|
||||||
}{
|
}{
|
||||||
|
"container created": {
|
||||||
|
exist: true,
|
||||||
|
imageExist: true,
|
||||||
|
expectedState: runtime.ContainerState_CONTAINER_CREATED,
|
||||||
|
},
|
||||||
"container running": {
|
"container running": {
|
||||||
exist: true,
|
exist: true,
|
||||||
imageExist: true,
|
imageExist: true,
|
||||||
|
startedAt: time.Now().UnixNano(),
|
||||||
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
|
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
|
||||||
},
|
},
|
||||||
"container exited": {
|
"container exited": {
|
||||||
exist: true,
|
exist: true,
|
||||||
imageExist: true,
|
imageExist: true,
|
||||||
|
startedAt: time.Now().UnixNano(),
|
||||||
finishedAt: time.Now().UnixNano(),
|
finishedAt: time.Now().UnixNano(),
|
||||||
reason: "test-reason",
|
reason: "test-reason",
|
||||||
expectedState: runtime.ContainerState_CONTAINER_EXITED,
|
expectedState: runtime.ContainerState_CONTAINER_EXITED,
|
||||||
@ -198,6 +213,7 @@ func TestContainerStatus(t *testing.T) {
|
|||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
metadata, status, image, expected := getContainerStatusTestData()
|
metadata, status, image, expected := getContainerStatusTestData()
|
||||||
// Update status with test case.
|
// Update status with test case.
|
||||||
|
status.StartedAt = test.startedAt
|
||||||
status.FinishedAt = test.finishedAt
|
status.FinishedAt = test.finishedAt
|
||||||
status.Reason = test.reason
|
status.Reason = test.reason
|
||||||
container, err := containerstore.NewContainer(
|
container, err := containerstore.NewContainer(
|
||||||
@ -219,9 +235,20 @@ func TestContainerStatus(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Set expectation based on test case.
|
// Set expectation based on test case.
|
||||||
|
expected.StartedAt = test.startedAt
|
||||||
expected.FinishedAt = test.finishedAt
|
expected.FinishedAt = test.finishedAt
|
||||||
expected.Reason = test.reason
|
expected.Reason = test.reason
|
||||||
expected.State = test.expectedState
|
patchExceptedWithState(expected, test.expectedState)
|
||||||
assert.Equal(t, expected, resp.GetStatus())
|
assert.Equal(t, expected, resp.GetStatus())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func patchExceptedWithState(expected *runtime.ContainerStatus, state runtime.ContainerState) {
|
||||||
|
expected.State = state
|
||||||
|
switch state {
|
||||||
|
case runtime.ContainerState_CONTAINER_CREATED:
|
||||||
|
expected.StartedAt, expected.FinishedAt = 0, 0
|
||||||
|
case runtime.ContainerState_CONTAINER_RUNNING:
|
||||||
|
expected.FinishedAt = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user