From a44b356274bf2a5f672734698519f2bb93d10efb Mon Sep 17 00:00:00 2001 From: Rodrigo Campos Date: Fri, 30 Dec 2022 13:55:28 -0300 Subject: [PATCH] cri: Fix assert vs require in tests Currently we require that c.containerSpec() does not return an error if test.err is not set. However, if the require fails (i.e. it indeed returned an error) the rest of the code is executed anyways. The rest of the code assumes it did not return an error (so code assumes spec is not nil). This fails miserably if it indeed returned an error, as spec is nil and go crashes while running the unit tests. Let's require it is not an error, so code does not continue to execute if that fails and go doesn't crash. In the test.err case is not harmful the bug of using assert, but let's switch it to require too as that is what we really want. Signed-off-by: Rodrigo Campos --- pkg/cri/server/container_create_linux_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cri/server/container_create_linux_test.go b/pkg/cri/server/container_create_linux_test.go index 8a963bf1a..6fd1e0e8d 100644 --- a/pkg/cri/server/container_create_linux_test.go +++ b/pkg/cri/server/container_create_linux_test.go @@ -895,11 +895,11 @@ func TestUserNamespace(t *testing.T) { spec, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime) if test.err { - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, spec) return } - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, spec.Linux.UIDMappings, test.expUIDMapping) assert.Equal(t, spec.Linux.GIDMappings, test.expGIDMapping)