github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-utils/containerd/containerd_test.go (about)

     1  package containerd
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/inspektor-gadget/inspektor-gadget/internal/test"
     9  	"github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/testutils"
    10  	containerutilsTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/types"
    11  )
    12  
    13  func TestNamespace(t *testing.T) {
    14  	test.RequireRoot(t)
    15  
    16  	// validate the container in k8s.io (default for ig) namespace
    17  	c1 := testutils.NewContainerdContainer("test-k8s-io", "sleep inf")
    18  	c1.Start(t)
    19  	k8sClient, err := NewContainerdClient("", "", nil)
    20  	t.Cleanup(func() {
    21  		k8sClient.Close()
    22  		c1.Stop(t)
    23  	})
    24  	require.Nil(t, err)
    25  	require.NotNil(t, k8sClient)
    26  
    27  	container, err := k8sClient.GetContainer("test-k8s-io")
    28  	require.Nil(t, err)
    29  	require.NotNil(t, container)
    30  	require.Equal(t, "test-k8s-io", container.Runtime.ContainerID)
    31  	require.Equal(t, "test-k8s-io", container.Runtime.ContainerName)
    32  
    33  	// validate the container in default (default for containerd) namespace
    34  	c2 := testutils.NewContainerdContainer("test-default", "sleep inf", testutils.WithNamespace("default"))
    35  	c2.Start(t)
    36  	config := &containerutilsTypes.ExtraConfig{
    37  		Namespace: "default",
    38  	}
    39  	defaultClient, err := NewContainerdClient("", "", config)
    40  	t.Cleanup(func() {
    41  		defaultClient.Close()
    42  		c2.Stop(t)
    43  	})
    44  	require.Nil(t, err)
    45  	require.NotNil(t, defaultClient)
    46  
    47  	container, err = defaultClient.GetContainer("test-default")
    48  	require.Nil(t, err)
    49  	require.NotNil(t, container)
    50  	require.Equal(t, "test-default", container.Runtime.ContainerID)
    51  	require.Equal(t, "test-default", container.Runtime.ContainerName)
    52  
    53  	// validate we can't see the container in empty-ns namespace
    54  	config = &containerutilsTypes.ExtraConfig{
    55  		Namespace: "empty-ns",
    56  	}
    57  	emptyClient, err := NewContainerdClient("", "", config)
    58  	t.Cleanup(func() {
    59  		emptyClient.Close()
    60  	})
    61  	require.Nil(t, err)
    62  	require.NotNil(t, emptyClient)
    63  
    64  	containers, err := k8sClient.GetContainers()
    65  	require.Nil(t, err)
    66  	require.NotNil(t, containers)
    67  	require.GreaterOrEqual(t, len(containers), 1)
    68  
    69  	containers, err = defaultClient.GetContainers()
    70  	require.Nil(t, err)
    71  	require.NotNil(t, containers)
    72  	require.GreaterOrEqual(t, len(containers), 1)
    73  
    74  	containers, err = emptyClient.GetContainers()
    75  	require.Nil(t, err)
    76  	require.NotNil(t, containers)
    77  	require.Len(t, containers, 0)
    78  }