From b7f51be8ce937fc8a56113789f6906571ae9601d Mon Sep 17 00:00:00 2001 From: Ace-Tang Date: Sun, 28 Apr 2019 15:21:50 +0800 Subject: [PATCH] cli: do not fail on list when some containers bust kata-runtime list command should list all valid container, not fail when some containers information uncorrent, like rootfs not found. Fixes: #1592 Signed-off-by: Ace-Tang --- cli/list.go | 3 ++- cli/list_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/cli/list.go b/cli/list.go index 26dc7391b3..c85024bb6a 100644 --- a/cli/list.go +++ b/cli/list.go @@ -331,7 +331,8 @@ func getContainers(ctx context.Context, context *cli.Context) ([]fullContainerSt uid, err := getDirOwner(container.RootFs) if err != nil { - return nil, err + fmt.Fprintf(os.Stderr, "WARNING: failed to get container %s rootfs: %s\n", ociState.ID, err) + continue } owner := fmt.Sprintf("#%v", uid) diff --git a/cli/list_test.go b/cli/list_test.go index 268ef469cb..62a5a56b5a 100644 --- a/cli/list_test.go +++ b/cli/list_test.go @@ -590,11 +590,6 @@ func TestListCLIFunctionFormatFail(t *testing.T) { ctx.App.Metadata["runtimeConfig"] = runtimeConfig - _ = os.Remove(rootfs) - - err = fn(ctx) - assert.Error(err) - err = os.MkdirAll(rootfs, testDirMode) assert.NoError(err) @@ -730,3 +725,61 @@ func TestListGetDirOwner(t *testing.T) { assert.NoError(err) assert.Equal(dirUID, uid) } + +func TestListWithRootfsMissShouldSuccess(t *testing.T) { + assert := assert.New(t) + + tmpdir, err := ioutil.TempDir(testDir, "") + assert.NoError(err) + defer os.RemoveAll(tmpdir) + + sandbox := &vcmock.Sandbox{ + MockID: testSandboxID, + } + + rootfs := filepath.Join(tmpdir, "rootfs") + err = os.MkdirAll(rootfs, testDirMode) + assert.NoError(err) + + testingImpl.ListSandboxFunc = func(ctx context.Context) ([]vc.SandboxStatus, error) { + return []vc.SandboxStatus{ + { + ID: sandbox.ID(), + ContainersStatus: []vc.ContainerStatus{ + { + ID: sandbox.ID(), + Annotations: map[string]string{ + vcAnnotations.ContainerTypeKey: string(vc.PodSandbox), + }, + RootFs: rootfs, + }, + }, + }, + }, nil + } + + defer func() { + testingImpl.ListSandboxFunc = nil + }() + + set := flag.NewFlagSet("test", 0) + set.String("format", "table", "") + ctx := createCLIContext(set) + ctx.App.Name = "foo" + + runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true) + assert.NoError(err) + + ctx.App.Metadata["runtimeConfig"] = runtimeConfig + + fn, ok := listCLICommand.Action.(func(context *cli.Context) error) + assert.True(ok) + + err = fn(ctx) + assert.NoError(err) + + // remove container rootfs, check list command should also work + assert.NoError(os.RemoveAll(rootfs)) + err = fn(ctx) + assert.NoError(err) +}