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 <rodrigoca@microsoft.com>
This commit is contained in:
Rodrigo Campos 2022-12-30 13:55:28 -03:00
parent b0b28f1d8e
commit a44b356274

View File

@ -895,11 +895,11 @@ func TestUserNamespace(t *testing.T) {
spec, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime) spec, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
if test.err { if test.err {
assert.Error(t, err) require.Error(t, err)
assert.Nil(t, spec) assert.Nil(t, spec)
return return
} }
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, spec.Linux.UIDMappings, test.expUIDMapping) assert.Equal(t, spec.Linux.UIDMappings, test.expUIDMapping)
assert.Equal(t, spec.Linux.GIDMappings, test.expGIDMapping) assert.Equal(t, spec.Linux.GIDMappings, test.expGIDMapping)