Use t.Run for /pkg/cri tests
A majority of the tests in /pkg/cri are testing/validating multiple things per test (generally spec or options validations). This flow lends itself well to using *testing.T's Run method to run each thing as a subtest so `go test` output can actually display which subtest failed/passed. Some of the tests in the packages in pkg/cri already did this, but a bunch simply logged what sub-testcase was currently running without invoking t.Run. Signed-off-by: Daniel Canter <dcanter@microsoft.com>
This commit is contained in:
parent
c76559a6a9
commit
b5e1b8f619
@ -236,23 +236,24 @@ func TestRedirectLogs(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
rc := io.NopCloser(strings.NewReader(test.input))
|
rc := io.NopCloser(strings.NewReader(test.input))
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
wc := cioutil.NewNopWriteCloser(buf)
|
wc := cioutil.NewNopWriteCloser(buf)
|
||||||
redirectLogs("test-path", rc, wc, test.stream, test.maxLen)
|
redirectLogs("test-path", rc, wc, test.stream, test.maxLen)
|
||||||
output := buf.String()
|
output := buf.String()
|
||||||
lines := strings.Split(output, "\n")
|
lines := strings.Split(output, "\n")
|
||||||
lines = lines[:len(lines)-1] // Discard empty string after last \n
|
lines = lines[:len(lines)-1] // Discard empty string after last \n
|
||||||
assert.Len(t, lines, len(test.content))
|
assert.Len(t, lines, len(test.content))
|
||||||
for i := range lines {
|
for i := range lines {
|
||||||
fields := strings.SplitN(lines[i], string([]byte{delimiter}), 4)
|
fields := strings.SplitN(lines[i], string([]byte{delimiter}), 4)
|
||||||
require.Len(t, fields, 4)
|
require.Len(t, fields, 4)
|
||||||
_, err := time.Parse(timestampFormat, fields[0])
|
_, err := time.Parse(timestampFormat, fields[0])
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, test.stream, fields[1])
|
assert.EqualValues(t, test.stream, fields[1])
|
||||||
assert.Equal(t, string(test.tag[i]), fields[2])
|
assert.Equal(t, string(test.tag[i]), fields[2])
|
||||||
assert.Equal(t, test.content[i], fields[3])
|
assert.Equal(t, test.content[i], fields[3])
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,34 +238,35 @@ func TestContainerCapabilities(t *testing.T) {
|
|||||||
excludes: util.SubtractStringSlice(allCaps, "CAP_SYS_ADMIN"),
|
excludes: util.SubtractStringSlice(allCaps, "CAP_SYS_ADMIN"),
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
containerConfig, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
|
containerConfig, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
|
||||||
ociRuntime := config.Runtime{}
|
ociRuntime := config.Runtime{}
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
c.allCaps = allCaps
|
c.allCaps = allCaps
|
||||||
|
|
||||||
containerConfig.Linux.SecurityContext.Capabilities = test.capability
|
containerConfig.Linux.SecurityContext.Capabilities = test.capability
|
||||||
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)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
if selinux.GetEnabled() {
|
if selinux.GetEnabled() {
|
||||||
assert.NotEqual(t, "", spec.Process.SelinuxLabel)
|
assert.NotEqual(t, "", spec.Process.SelinuxLabel)
|
||||||
assert.NotEqual(t, "", spec.Linux.MountLabel)
|
assert.NotEqual(t, "", spec.Linux.MountLabel)
|
||||||
}
|
}
|
||||||
|
|
||||||
specCheck(t, testID, testSandboxID, testPid, spec)
|
specCheck(t, testID, testSandboxID, testPid, spec)
|
||||||
for _, include := range test.includes {
|
for _, include := range test.includes {
|
||||||
assert.Contains(t, spec.Process.Capabilities.Bounding, include)
|
assert.Contains(t, spec.Process.Capabilities.Bounding, include)
|
||||||
assert.Contains(t, spec.Process.Capabilities.Effective, include)
|
assert.Contains(t, spec.Process.Capabilities.Effective, include)
|
||||||
assert.Contains(t, spec.Process.Capabilities.Permitted, include)
|
assert.Contains(t, spec.Process.Capabilities.Permitted, include)
|
||||||
}
|
}
|
||||||
for _, exclude := range test.excludes {
|
for _, exclude := range test.excludes {
|
||||||
assert.NotContains(t, spec.Process.Capabilities.Bounding, exclude)
|
assert.NotContains(t, spec.Process.Capabilities.Bounding, exclude)
|
||||||
assert.NotContains(t, spec.Process.Capabilities.Effective, exclude)
|
assert.NotContains(t, spec.Process.Capabilities.Effective, exclude)
|
||||||
assert.NotContains(t, spec.Process.Capabilities.Permitted, exclude)
|
assert.NotContains(t, spec.Process.Capabilities.Permitted, exclude)
|
||||||
}
|
}
|
||||||
assert.Empty(t, spec.Process.Capabilities.Inheritable)
|
assert.Empty(t, spec.Process.Capabilities.Inheritable)
|
||||||
assert.Empty(t, spec.Process.Capabilities.Ambient)
|
assert.Empty(t, spec.Process.Capabilities.Ambient)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,17 +426,18 @@ func TestContainerAndSandboxPrivileged(t *testing.T) {
|
|||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
containerConfig.Linux.SecurityContext.Privileged = test.containerPrivileged
|
containerConfig.Linux.SecurityContext.Privileged = test.containerPrivileged
|
||||||
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||||
Privileged: test.sandboxPrivileged,
|
Privileged: test.sandboxPrivileged,
|
||||||
}
|
}
|
||||||
_, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
_, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
||||||
if test.expectError {
|
if test.expectError {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
} else {
|
} else {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -587,20 +589,22 @@ func TestContainerMounts(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
config := &runtime.ContainerConfig{
|
t.Run(desc, func(t *testing.T) {
|
||||||
Metadata: &runtime.ContainerMetadata{
|
config := &runtime.ContainerConfig{
|
||||||
Name: "test-name",
|
Metadata: &runtime.ContainerMetadata{
|
||||||
Attempt: 1,
|
Name: "test-name",
|
||||||
},
|
Attempt: 1,
|
||||||
Mounts: test.criMounts,
|
},
|
||||||
Linux: &runtime.LinuxContainerConfig{
|
Mounts: test.criMounts,
|
||||||
SecurityContext: test.securityContext,
|
Linux: &runtime.LinuxContainerConfig{
|
||||||
},
|
SecurityContext: test.securityContext,
|
||||||
}
|
},
|
||||||
c := newTestCRIService()
|
}
|
||||||
c.os.(*ostesting.FakeOS).StatFn = test.statFn
|
c := newTestCRIService()
|
||||||
mounts := c.containerMounts(testSandboxID, config)
|
c.os.(*ostesting.FakeOS).StatFn = test.statFn
|
||||||
assert.Equal(t, test.expectedMounts, mounts, desc)
|
mounts := c.containerMounts(testSandboxID, config)
|
||||||
|
assert.Equal(t, test.expectedMounts, mounts, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -627,24 +631,24 @@ func TestPrivilegedBindMount(t *testing.T) {
|
|||||||
expectedCgroupFSRO: false,
|
expectedCgroupFSRO: false,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
|
containerConfig.Linux.SecurityContext.Privileged = test.privileged
|
||||||
|
sandboxConfig.Linux.SecurityContext.Privileged = test.privileged
|
||||||
|
|
||||||
containerConfig.Linux.SecurityContext.Privileged = test.privileged
|
spec, err := c.containerSpec(t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
||||||
sandboxConfig.Linux.SecurityContext.Privileged = test.privileged
|
|
||||||
|
|
||||||
spec, err := c.containerSpec(t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
assert.NoError(t, err)
|
||||||
|
if test.expectedSysFSRO {
|
||||||
assert.NoError(t, err)
|
checkMount(t, spec.Mounts, "sysfs", "/sys", "sysfs", []string{"ro"}, []string{"rw"})
|
||||||
if test.expectedSysFSRO {
|
} else {
|
||||||
checkMount(t, spec.Mounts, "sysfs", "/sys", "sysfs", []string{"ro"}, []string{"rw"})
|
checkMount(t, spec.Mounts, "sysfs", "/sys", "sysfs", []string{"rw"}, []string{"ro"})
|
||||||
} else {
|
}
|
||||||
checkMount(t, spec.Mounts, "sysfs", "/sys", "sysfs", []string{"rw"}, []string{"ro"})
|
if test.expectedCgroupFSRO {
|
||||||
}
|
checkMount(t, spec.Mounts, "cgroup", "/sys/fs/cgroup", "cgroup", []string{"ro"}, []string{"rw"})
|
||||||
if test.expectedCgroupFSRO {
|
} else {
|
||||||
checkMount(t, spec.Mounts, "cgroup", "/sys/fs/cgroup", "cgroup", []string{"ro"}, []string{"rw"})
|
checkMount(t, spec.Mounts, "cgroup", "/sys/fs/cgroup", "cgroup", []string{"rw"}, []string{"ro"})
|
||||||
} else {
|
}
|
||||||
checkMount(t, spec.Mounts, "cgroup", "/sys/fs/cgroup", "cgroup", []string{"rw"}, []string{"ro"})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -736,21 +740,22 @@ func TestMountPropagation(t *testing.T) {
|
|||||||
expectErr: true,
|
expectErr: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
c.os.(*ostesting.FakeOS).LookupMountFn = test.fakeLookupMountFn
|
c.os.(*ostesting.FakeOS).LookupMountFn = test.fakeLookupMountFn
|
||||||
config, _, _, _ := getCreateContainerTestData()
|
config, _, _, _ := getCreateContainerTestData()
|
||||||
|
|
||||||
var spec runtimespec.Spec
|
var spec runtimespec.Spec
|
||||||
spec.Linux = &runtimespec.Linux{}
|
spec.Linux = &runtimespec.Linux{}
|
||||||
|
|
||||||
err := opts.WithMounts(c.os, config, []*runtime.Mount{test.criMount}, "")(context.Background(), nil, nil, &spec)
|
err := opts.WithMounts(c.os, config, []*runtime.Mount{test.criMount}, "")(context.Background(), nil, nil, &spec)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
} else {
|
} else {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
checkMount(t, spec.Mounts, test.criMount.HostPath, test.criMount.ContainerPath, "bind", test.optionsCheck, nil)
|
checkMount(t, spec.Mounts, test.criMount.HostPath, test.criMount.ContainerPath, "bind", test.optionsCheck, nil)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -787,11 +792,12 @@ func TestPidNamespace(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
containerConfig.Linux.SecurityContext.NamespaceOptions = &runtime.NamespaceOption{Pid: test.pidNS}
|
containerConfig.Linux.SecurityContext.NamespaceOptions = &runtime.NamespaceOption{Pid: test.pidNS}
|
||||||
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)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Contains(t, spec.Linux.Namespaces, test.expected)
|
assert.Contains(t, spec.Linux.Namespaces, test.expected)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -920,7 +926,7 @@ func TestGenerateSeccompSecurityProfileSpecOpts(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(fmt.Sprintf("TestCase %q", desc), func(t *testing.T) {
|
t.Run(desc, func(t *testing.T) {
|
||||||
cri := &criService{}
|
cri := &criService{}
|
||||||
cri.config.UnsetSeccompProfile = test.defaultProfile
|
cri.config.UnsetSeccompProfile = test.defaultProfile
|
||||||
ssp := test.sp
|
ssp := test.sp
|
||||||
@ -1073,29 +1079,30 @@ func TestGenerateApparmorSpecOpts(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
asp := test.sp
|
asp := test.sp
|
||||||
csp, err := generateApparmorSecurityProfile(test.profile)
|
csp, err := generateApparmorSecurityProfile(test.profile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
assert.NoError(t, err)
|
if asp == nil {
|
||||||
|
asp = csp
|
||||||
|
}
|
||||||
|
specOpts, err := generateApparmorSpecOpts(asp, test.privileged, !test.disable)
|
||||||
|
assert.Equal(t,
|
||||||
|
reflect.ValueOf(test.specOpts).Pointer(),
|
||||||
|
reflect.ValueOf(specOpts).Pointer())
|
||||||
|
if test.expectErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
})
|
||||||
if asp == nil {
|
|
||||||
asp = csp
|
|
||||||
}
|
|
||||||
specOpts, err := generateApparmorSpecOpts(asp, test.privileged, !test.disable)
|
|
||||||
assert.Equal(t,
|
|
||||||
reflect.ValueOf(test.specOpts).Pointer(),
|
|
||||||
reflect.ValueOf(specOpts).Pointer())
|
|
||||||
if test.expectErr {
|
|
||||||
assert.Error(t, err)
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1169,21 +1176,22 @@ func TestMaskedAndReadonlyPaths(t *testing.T) {
|
|||||||
privileged: true,
|
privileged: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c.config.DisableProcMount = test.disableProcMount
|
c.config.DisableProcMount = test.disableProcMount
|
||||||
containerConfig.Linux.SecurityContext.MaskedPaths = test.masked
|
containerConfig.Linux.SecurityContext.MaskedPaths = test.masked
|
||||||
containerConfig.Linux.SecurityContext.ReadonlyPaths = test.readonly
|
containerConfig.Linux.SecurityContext.ReadonlyPaths = test.readonly
|
||||||
containerConfig.Linux.SecurityContext.Privileged = test.privileged
|
containerConfig.Linux.SecurityContext.Privileged = test.privileged
|
||||||
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||||
Privileged: test.privileged,
|
Privileged: test.privileged,
|
||||||
}
|
}
|
||||||
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)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
if !test.privileged { // specCheck presumes an unprivileged container
|
if !test.privileged { // specCheck presumes an unprivileged container
|
||||||
specCheck(t, testID, testSandboxID, testPid, spec)
|
specCheck(t, testID, testSandboxID, testPid, spec)
|
||||||
}
|
}
|
||||||
assert.Equal(t, test.expectedMasked, spec.Linux.MaskedPaths)
|
assert.Equal(t, test.expectedMasked, spec.Linux.MaskedPaths)
|
||||||
assert.Equal(t, test.expectedReadonly, spec.Linux.ReadonlyPaths)
|
assert.Equal(t, test.expectedReadonly, spec.Linux.ReadonlyPaths)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1219,15 +1227,16 @@ func TestHostname(t *testing.T) {
|
|||||||
expectedEnv: "HOSTNAME=real-hostname",
|
expectedEnv: "HOSTNAME=real-hostname",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
sandboxConfig.Hostname = test.hostname
|
sandboxConfig.Hostname = test.hostname
|
||||||
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
|
||||||
NamespaceOptions: &runtime.NamespaceOption{Network: test.networkNs},
|
NamespaceOptions: &runtime.NamespaceOption{Network: test.networkNs},
|
||||||
}
|
}
|
||||||
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)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
specCheck(t, testID, testSandboxID, testPid, spec)
|
specCheck(t, testID, testSandboxID, testPid, spec)
|
||||||
assert.Contains(t, spec.Process.Env, test.expectedEnv)
|
assert.Contains(t, spec.Process.Env, test.expectedEnv)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1363,24 +1372,24 @@ func TestNonRootUserAndDevices(t *testing.T) {
|
|||||||
expectedDeviceGID: *testDevice.GID,
|
expectedDeviceGID: *testDevice.GID,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
|
c.config.DeviceOwnershipFromSecurityContext = test.deviceOwnershipFromSecurityContext
|
||||||
|
containerConfig.Linux.SecurityContext.RunAsUser = test.uid
|
||||||
|
containerConfig.Linux.SecurityContext.RunAsGroup = test.gid
|
||||||
|
containerConfig.Devices = []*runtime.Device{
|
||||||
|
{
|
||||||
|
ContainerPath: testDevice.Path,
|
||||||
|
HostPath: testDevice.Path,
|
||||||
|
Permissions: "r",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
c.config.DeviceOwnershipFromSecurityContext = test.deviceOwnershipFromSecurityContext
|
spec, err := c.containerSpec(t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, config.Runtime{})
|
||||||
containerConfig.Linux.SecurityContext.RunAsUser = test.uid
|
assert.NoError(t, err)
|
||||||
containerConfig.Linux.SecurityContext.RunAsGroup = test.gid
|
|
||||||
containerConfig.Devices = []*runtime.Device{
|
|
||||||
{
|
|
||||||
ContainerPath: testDevice.Path,
|
|
||||||
HostPath: testDevice.Path,
|
|
||||||
Permissions: "r",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
spec, err := c.containerSpec(t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, config.Runtime{})
|
assert.Equal(t, test.expectedDeviceUID, *spec.Linux.Devices[0].UID)
|
||||||
assert.NoError(t, err)
|
assert.Equal(t, test.expectedDeviceGID, *spec.Linux.Devices[0].GID)
|
||||||
|
})
|
||||||
assert.Equal(t, test.expectedDeviceUID, *spec.Linux.Devices[0].UID)
|
|
||||||
assert.Equal(t, test.expectedDeviceGID, *spec.Linux.Devices[0].GID)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1434,38 +1443,37 @@ func TestPrivilegedDevices(t *testing.T) {
|
|||||||
expectAllDevicesAllowed: true,
|
expectAllDevicesAllowed: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
|
containerConfig.Linux.SecurityContext.Privileged = test.privileged
|
||||||
|
sandboxConfig.Linux.SecurityContext.Privileged = test.privileged
|
||||||
|
|
||||||
containerConfig.Linux.SecurityContext.Privileged = test.privileged
|
ociRuntime := config.Runtime{
|
||||||
sandboxConfig.Linux.SecurityContext.Privileged = test.privileged
|
PrivilegedWithoutHostDevices: test.privilegedWithoutHostDevices,
|
||||||
|
PrivilegedWithoutHostDevicesAllDevicesAllowed: test.privilegedWithoutHostDevicesAllDevicesAllowed,
|
||||||
ociRuntime := config.Runtime{
|
|
||||||
PrivilegedWithoutHostDevices: test.privilegedWithoutHostDevices,
|
|
||||||
PrivilegedWithoutHostDevicesAllDevicesAllowed: test.privilegedWithoutHostDevicesAllDevicesAllowed,
|
|
||||||
}
|
|
||||||
spec, err := c.containerSpec(t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
hostDevicesRaw, err := oci.HostDevices()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
var hostDevices = make([]string, 0)
|
|
||||||
for _, dev := range hostDevicesRaw {
|
|
||||||
// https://github.com/containerd/cri/pull/1521#issuecomment-652807951
|
|
||||||
if dev.Major != 0 {
|
|
||||||
hostDevices = append(hostDevices, dev.Path)
|
|
||||||
}
|
}
|
||||||
}
|
spec, err := c.containerSpec(t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
if test.expectHostDevices {
|
hostDevicesRaw, err := oci.HostDevices()
|
||||||
assert.Len(t, spec.Linux.Devices, len(hostDevices))
|
assert.NoError(t, err)
|
||||||
} else {
|
var hostDevices = make([]string, 0)
|
||||||
assert.Empty(t, spec.Linux.Devices)
|
for _, dev := range hostDevicesRaw {
|
||||||
}
|
// https://github.com/containerd/cri/pull/1521#issuecomment-652807951
|
||||||
|
if dev.Major != 0 {
|
||||||
|
hostDevices = append(hostDevices, dev.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assert.Len(t, spec.Linux.Resources.Devices, 1)
|
if test.expectHostDevices {
|
||||||
assert.Equal(t, spec.Linux.Resources.Devices[0].Allow, test.expectAllDevicesAllowed)
|
assert.Len(t, spec.Linux.Devices, len(hostDevices))
|
||||||
assert.Equal(t, spec.Linux.Resources.Devices[0].Access, "rwm")
|
} else {
|
||||||
|
assert.Empty(t, spec.Linux.Devices)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Len(t, spec.Linux.Resources.Devices, 1)
|
||||||
|
assert.Equal(t, spec.Linux.Resources.Devices[0].Allow, test.expectAllDevicesAllowed)
|
||||||
|
assert.Equal(t, spec.Linux.Resources.Devices[0].Access, "rwm")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1628,34 +1636,34 @@ containerEdits:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", test.description)
|
t.Run(test.description, func(t *testing.T) {
|
||||||
|
spec, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
spec, err := c.containerSpec(testID, testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime)
|
specCheck(t, testID, testSandboxID, testPid, spec)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
specCheck(t, testID, testSandboxID, testPid, spec)
|
cdiDir, err := writeFilesToTempDir("containerd-test-CDI-injections-", test.cdiSpecFiles)
|
||||||
|
if cdiDir != "" {
|
||||||
|
defer os.RemoveAll(cdiDir)
|
||||||
|
}
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
cdiDir, err := writeFilesToTempDir("containerd-test-CDI-injections-", test.cdiSpecFiles)
|
injectFun := oci.WithCDI(test.annotations, []string{cdiDir})
|
||||||
if cdiDir != "" {
|
err = injectFun(nil, nil, nil, spec)
|
||||||
defer os.RemoveAll(cdiDir)
|
assert.Equal(t, test.expectError, err != nil)
|
||||||
}
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
injectFun := oci.WithCDI(test.annotations, []string{cdiDir})
|
if err != nil {
|
||||||
err = injectFun(nil, nil, nil, spec)
|
if test.expectEnv != nil {
|
||||||
assert.Equal(t, test.expectError, err != nil)
|
for _, expectedEnv := range test.expectEnv {
|
||||||
|
assert.Contains(t, spec.Process.Env, expectedEnv)
|
||||||
if err != nil {
|
}
|
||||||
if test.expectEnv != nil {
|
}
|
||||||
for _, expectedEnv := range test.expectEnv {
|
if test.expectDevices != nil {
|
||||||
assert.Contains(t, spec.Process.Env, expectedEnv)
|
for _, expectedDev := range test.expectDevices {
|
||||||
|
assert.Contains(t, spec.Linux.Devices, expectedDev)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if test.expectDevices != nil {
|
})
|
||||||
for _, expectedDev := range test.expectDevices {
|
|
||||||
assert.Contains(t, spec.Linux.Devices, expectedDev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,21 +187,22 @@ func TestContainerSpecCommand(t *testing.T) {
|
|||||||
expectErr: true,
|
expectErr: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
t.Run(desc, func(t *testing.T) {
|
||||||
|
config, _, imageConfig, _ := getCreateContainerTestData()
|
||||||
|
config.Command = test.criEntrypoint
|
||||||
|
config.Args = test.criArgs
|
||||||
|
imageConfig.Entrypoint = test.imageEntrypoint
|
||||||
|
imageConfig.Cmd = test.imageArgs
|
||||||
|
|
||||||
config, _, imageConfig, _ := getCreateContainerTestData()
|
var spec runtimespec.Spec
|
||||||
config.Command = test.criEntrypoint
|
err := opts.WithProcessArgs(config, imageConfig)(context.Background(), nil, nil, &spec)
|
||||||
config.Args = test.criArgs
|
if test.expectErr {
|
||||||
imageConfig.Entrypoint = test.imageEntrypoint
|
assert.Error(t, err)
|
||||||
imageConfig.Cmd = test.imageArgs
|
return
|
||||||
|
}
|
||||||
var spec runtimespec.Spec
|
assert.NoError(t, err)
|
||||||
err := opts.WithProcessArgs(config, imageConfig)(context.Background(), nil, nil, &spec)
|
assert.Equal(t, test.expected, spec.Process.Args, desc)
|
||||||
if test.expectErr {
|
})
|
||||||
assert.Error(t, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, test.expected, spec.Process.Args, desc)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,26 +254,27 @@ func TestVolumeMounts(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
config := &imagespec.ImageConfig{
|
config := &imagespec.ImageConfig{
|
||||||
Volumes: test.imageVolumes,
|
Volumes: test.imageVolumes,
|
||||||
}
|
|
||||||
c := newTestCRIService()
|
|
||||||
got := c.volumeMounts(testContainerRootDir, test.criMounts, config)
|
|
||||||
assert.Len(t, got, len(test.expectedMountDest))
|
|
||||||
for _, dest := range test.expectedMountDest {
|
|
||||||
found := false
|
|
||||||
for _, m := range got {
|
|
||||||
if m.ContainerPath == dest {
|
|
||||||
found = true
|
|
||||||
assert.Equal(t,
|
|
||||||
filepath.Dir(m.HostPath),
|
|
||||||
filepath.Join(testContainerRootDir, "volumes"))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
assert.True(t, found)
|
c := newTestCRIService()
|
||||||
}
|
got := c.volumeMounts(testContainerRootDir, test.criMounts, config)
|
||||||
|
assert.Len(t, got, len(test.expectedMountDest))
|
||||||
|
for _, dest := range test.expectedMountDest {
|
||||||
|
found := false
|
||||||
|
for _, m := range got {
|
||||||
|
if m.ContainerPath == dest {
|
||||||
|
found = true
|
||||||
|
assert.Equal(t,
|
||||||
|
filepath.Dir(m.HostPath),
|
||||||
|
filepath.Join(testContainerRootDir, "volumes"))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,8 +149,10 @@ func TestFilterContainers(t *testing.T) {
|
|||||||
expect: []*runtime.Container{testContainers[2]},
|
expect: []*runtime.Container{testContainers[2]},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
filtered := c.filterCRIContainers(testContainers, test.filter)
|
t.Run(desc, func(t *testing.T) {
|
||||||
assert.Equal(t, test.expect, filtered, desc)
|
filtered := c.filterCRIContainers(testContainers, test.filter)
|
||||||
|
assert.Equal(t, test.expect, filtered, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,14 +334,15 @@ func TestListContainers(t *testing.T) {
|
|||||||
expect: expectedContainers[:1],
|
expect: expectedContainers[:1],
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase: %s", testdesc)
|
t.Run(testdesc, func(t *testing.T) {
|
||||||
resp, err := c.ListContainers(context.Background(), &runtime.ListContainersRequest{Filter: testdata.filter})
|
resp, err := c.ListContainers(context.Background(), &runtime.ListContainersRequest{Filter: testdata.filter})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
require.NotNil(t, resp)
|
require.NotNil(t, resp)
|
||||||
containers := resp.GetContainers()
|
containers := resp.GetContainers()
|
||||||
assert.Len(t, containers, len(testdata.expect))
|
assert.Len(t, containers, len(testdata.expect))
|
||||||
for _, cntr := range testdata.expect {
|
for _, cntr := range testdata.expect {
|
||||||
assert.Contains(t, containers, cntr)
|
assert.Contains(t, containers, cntr)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,21 +65,22 @@ func TestSetContainerRemoving(t *testing.T) {
|
|||||||
expectErr: false,
|
expectErr: false,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
container, err := containerstore.NewContainer(
|
container, err := containerstore.NewContainer(
|
||||||
containerstore.Metadata{ID: testID},
|
containerstore.Metadata{ID: testID},
|
||||||
containerstore.WithFakeStatus(test.status),
|
containerstore.WithFakeStatus(test.status),
|
||||||
)
|
)
|
||||||
assert.NoError(t, err)
|
|
||||||
err = setContainerRemoving(container)
|
|
||||||
if test.expectErr {
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Equal(t, test.status, container.Status.Get(), "metadata should not be updated")
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.True(t, container.Status.Get().Removing, "removing should be set")
|
err = setContainerRemoving(container)
|
||||||
assert.NoError(t, resetContainerRemoving(container))
|
if test.expectErr {
|
||||||
assert.False(t, container.Status.Get().Removing, "removing should be reset")
|
assert.Error(t, err)
|
||||||
}
|
assert.Equal(t, test.status, container.Status.Get(), "metadata should not be updated")
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, container.Status.Get().Removing, "removing should be set")
|
||||||
|
assert.NoError(t, resetContainerRemoving(container))
|
||||||
|
assert.False(t, container.Status.Get().Removing, "removing should be reset")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,21 +78,22 @@ func TestSetContainerStarting(t *testing.T) {
|
|||||||
expectErr: true,
|
expectErr: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
container, err := containerstore.NewContainer(
|
container, err := containerstore.NewContainer(
|
||||||
containerstore.Metadata{ID: testID},
|
containerstore.Metadata{ID: testID},
|
||||||
containerstore.WithFakeStatus(test.status),
|
containerstore.WithFakeStatus(test.status),
|
||||||
)
|
)
|
||||||
assert.NoError(t, err)
|
|
||||||
err = setContainerStarting(container)
|
|
||||||
if test.expectErr {
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Equal(t, test.status, container.Status.Get(), "metadata should not be updated")
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.True(t, container.Status.Get().Starting, "starting should be set")
|
err = setContainerStarting(container)
|
||||||
assert.NoError(t, resetContainerStarting(container))
|
if test.expectErr {
|
||||||
assert.False(t, container.Status.Get().Starting, "starting should be reset")
|
assert.Error(t, err)
|
||||||
}
|
assert.Equal(t, test.status, container.Status.Get(), "metadata should not be updated")
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, container.Status.Get().Starting, "starting should be set")
|
||||||
|
assert.NoError(t, resetContainerStarting(container))
|
||||||
|
assert.False(t, container.Status.Get().Starting, "starting should be reset")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,6 @@ func TestContainerMetricsCPU(t *testing.T) {
|
|||||||
expectedFirst *runtime.CpuUsage
|
expectedFirst *runtime.CpuUsage
|
||||||
expectedSecond *runtime.CpuUsage
|
expectedSecond *runtime.CpuUsage
|
||||||
}{
|
}{
|
||||||
|
|
||||||
"v1 metrics": {
|
"v1 metrics": {
|
||||||
firstMetrics: &v1.Metrics{
|
firstMetrics: &v1.Metrics{
|
||||||
CPU: &v1.CPUStat{
|
CPU: &v1.CPUStat{
|
||||||
|
@ -127,29 +127,32 @@ func TestToCRIContainerStatus(t *testing.T) {
|
|||||||
expectedReason: errorExitReason,
|
expectedReason: errorExitReason,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
metadata, status, _, expected := getContainerStatusTestData()
|
t.Run(desc, func(t *testing.T) {
|
||||||
// Update status with test case.
|
|
||||||
status.StartedAt = test.startedAt
|
metadata, status, _, expected := getContainerStatusTestData()
|
||||||
status.FinishedAt = test.finishedAt
|
// Update status with test case.
|
||||||
status.ExitCode = test.exitCode
|
status.StartedAt = test.startedAt
|
||||||
status.Reason = test.reason
|
status.FinishedAt = test.finishedAt
|
||||||
status.Message = test.message
|
status.ExitCode = test.exitCode
|
||||||
container, err := containerstore.NewContainer(
|
status.Reason = test.reason
|
||||||
*metadata,
|
status.Message = test.message
|
||||||
containerstore.WithFakeStatus(*status),
|
container, err := containerstore.NewContainer(
|
||||||
)
|
*metadata,
|
||||||
assert.NoError(t, err)
|
containerstore.WithFakeStatus(*status),
|
||||||
// Set expectation based on test case.
|
)
|
||||||
expected.Reason = test.expectedReason
|
assert.NoError(t, err)
|
||||||
expected.StartedAt = test.startedAt
|
// Set expectation based on test case.
|
||||||
expected.FinishedAt = test.finishedAt
|
expected.Reason = test.expectedReason
|
||||||
expected.ExitCode = test.exitCode
|
expected.StartedAt = test.startedAt
|
||||||
expected.Message = test.message
|
expected.FinishedAt = test.finishedAt
|
||||||
patchExceptedWithState(expected, test.expectedState)
|
expected.ExitCode = test.exitCode
|
||||||
containerStatus := toCRIContainerStatus(container,
|
expected.Message = test.message
|
||||||
expected.Image,
|
patchExceptedWithState(expected, test.expectedState)
|
||||||
expected.ImageRef)
|
containerStatus := toCRIContainerStatus(container,
|
||||||
assert.Equal(t, expected, containerStatus, desc)
|
expected.Image,
|
||||||
|
expected.ImageRef)
|
||||||
|
assert.Equal(t, expected, containerStatus, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,37 +212,38 @@ func TestContainerStatus(t *testing.T) {
|
|||||||
expectErr: true,
|
expectErr: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(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.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(
|
||||||
*metadata,
|
*metadata,
|
||||||
containerstore.WithFakeStatus(*status),
|
containerstore.WithFakeStatus(*status),
|
||||||
)
|
)
|
||||||
assert.NoError(t, err)
|
|
||||||
if test.exist {
|
|
||||||
assert.NoError(t, c.containerStore.Add(container))
|
|
||||||
}
|
|
||||||
if test.imageExist {
|
|
||||||
c.imageStore, err = imagestore.NewFakeStore([]imagestore.Image{*image})
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
if test.exist {
|
||||||
resp, err := c.ContainerStatus(context.Background(), &runtime.ContainerStatusRequest{ContainerId: container.ID})
|
assert.NoError(t, c.containerStore.Add(container))
|
||||||
if test.expectErr {
|
}
|
||||||
assert.Error(t, err)
|
if test.imageExist {
|
||||||
assert.Nil(t, resp)
|
c.imageStore, err = imagestore.NewFakeStore([]imagestore.Image{*image})
|
||||||
continue
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
// Set expectation based on test case.
|
resp, err := c.ContainerStatus(context.Background(), &runtime.ContainerStatusRequest{ContainerId: container.ID})
|
||||||
expected.StartedAt = test.startedAt
|
if test.expectErr {
|
||||||
expected.FinishedAt = test.finishedAt
|
assert.Error(t, err)
|
||||||
expected.Reason = test.reason
|
assert.Nil(t, resp)
|
||||||
patchExceptedWithState(expected, test.expectedState)
|
return
|
||||||
assert.Equal(t, expected, resp.GetStatus())
|
}
|
||||||
|
// Set expectation based on test case.
|
||||||
|
expected.StartedAt = test.startedAt
|
||||||
|
expected.FinishedAt = test.finishedAt
|
||||||
|
expected.Reason = test.reason
|
||||||
|
patchExceptedWithState(expected, test.expectedState)
|
||||||
|
assert.Equal(t, expected, resp.GetStatus())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,25 +61,27 @@ func TestWaitContainerStop(t *testing.T) {
|
|||||||
expectErr: false,
|
expectErr: false,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
c := newTestCRIService()
|
t.Run(desc, func(t *testing.T) {
|
||||||
container, err := containerstore.NewContainer(
|
c := newTestCRIService()
|
||||||
containerstore.Metadata{ID: id},
|
container, err := containerstore.NewContainer(
|
||||||
containerstore.WithFakeStatus(*test.status),
|
containerstore.Metadata{ID: id},
|
||||||
)
|
containerstore.WithFakeStatus(*test.status),
|
||||||
assert.NoError(t, err)
|
)
|
||||||
assert.NoError(t, c.containerStore.Add(container))
|
assert.NoError(t, err)
|
||||||
ctx := context.Background()
|
assert.NoError(t, c.containerStore.Add(container))
|
||||||
if test.cancel {
|
ctx := context.Background()
|
||||||
cancelledCtx, cancel := context.WithCancel(ctx)
|
if test.cancel {
|
||||||
cancel()
|
cancelledCtx, cancel := context.WithCancel(ctx)
|
||||||
ctx = cancelledCtx
|
cancel()
|
||||||
}
|
ctx = cancelledCtx
|
||||||
if test.timeout > 0 {
|
}
|
||||||
timeoutCtx, cancel := context.WithTimeout(ctx, test.timeout)
|
if test.timeout > 0 {
|
||||||
defer cancel()
|
timeoutCtx, cancel := context.WithTimeout(ctx, test.timeout)
|
||||||
ctx = timeoutCtx
|
defer cancel()
|
||||||
}
|
ctx = timeoutCtx
|
||||||
err = c.waitContainerStop(ctx, container)
|
}
|
||||||
assert.Equal(t, test.expectErr, err != nil, desc)
|
err = c.waitContainerStop(ctx, container)
|
||||||
|
assert.Equal(t, test.expectErr, err != nil, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,19 +211,20 @@ func TestUpdateOCILinuxResource(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
config := criconfig.Config{
|
config := criconfig.Config{
|
||||||
PluginConfig: criconfig.PluginConfig{
|
PluginConfig: criconfig.PluginConfig{
|
||||||
TolerateMissingHugetlbController: true,
|
TolerateMissingHugetlbController: true,
|
||||||
DisableHugetlbController: false,
|
DisableHugetlbController: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
got, err := updateOCIResource(context.Background(), test.spec, test.request, config)
|
got, err := updateOCIResource(context.Background(), test.spec, test.request, config)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
} else {
|
} else {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, test.expected, got)
|
assert.Equal(t, test.expected, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,9 +54,10 @@ func TestGetCgroupsPath(t *testing.T) {
|
|||||||
expected: "/test-id",
|
expected: "/test-id",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
got := getCgroupsPath(test.cgroupsParent, testID)
|
got := getCgroupsPath(test.cgroupsParent, testID)
|
||||||
assert.Equal(t, test.expected, got)
|
assert.Equal(t, test.expected, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,10 +75,11 @@ func TestGetUserFromImage(t *testing.T) {
|
|||||||
name: "test",
|
name: "test",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase - %q", c)
|
t.Run(c, func(t *testing.T) {
|
||||||
actualUID, actualName := getUserFromImage(test.user)
|
actualUID, actualName := getUserFromImage(test.user)
|
||||||
assert.Equal(t, test.uid, actualUID)
|
assert.Equal(t, test.uid, actualUID)
|
||||||
assert.Equal(t, test.name, actualName)
|
assert.Equal(t, test.name, actualName)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,12 +113,13 @@ func TestGetRepoDigestAndTag(t *testing.T) {
|
|||||||
expectedRepoTag: "",
|
expectedRepoTag: "",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
named, err := docker.ParseDockerRef(test.ref)
|
named, err := docker.ParseDockerRef(test.ref)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
repoDigest, repoTag := getRepoDigestAndTag(named, digest, test.schema1)
|
repoDigest, repoTag := getRepoDigestAndTag(named, digest, test.schema1)
|
||||||
assert.Equal(t, test.expectedRepoDigest, repoDigest)
|
assert.Equal(t, test.expectedRepoDigest, repoDigest)
|
||||||
assert.Equal(t, test.expectedRepoTag, repoTag)
|
assert.Equal(t, test.expectedRepoTag, repoTag)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,17 +366,18 @@ func TestEnvDeduplication(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
var spec runtimespec.Spec
|
var spec runtimespec.Spec
|
||||||
if len(test.existing) > 0 {
|
if len(test.existing) > 0 {
|
||||||
spec.Process = &runtimespec.Process{
|
spec.Process = &runtimespec.Process{
|
||||||
Env: test.existing,
|
Env: test.existing,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
for _, kv := range test.kv {
|
||||||
for _, kv := range test.kv {
|
oci.WithEnv([]string{kv[0] + "=" + kv[1]})(context.Background(), nil, nil, &spec)
|
||||||
oci.WithEnv([]string{kv[0] + "=" + kv[1]})(context.Background(), nil, nil, &spec)
|
}
|
||||||
}
|
assert.Equal(t, test.expected, spec.Process.Env)
|
||||||
assert.Equal(t, test.expected, spec.Process.Env)
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,11 +102,12 @@ func TestParseAuth(t *testing.T) {
|
|||||||
expectedSecret: testPasswd,
|
expectedSecret: testPasswd,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
u, s, err := ParseAuth(test.auth, test.host)
|
u, s, err := ParseAuth(test.auth, test.host)
|
||||||
assert.Equal(t, test.expectErr, err != nil)
|
assert.Equal(t, test.expectErr, err != nil)
|
||||||
assert.Equal(t, test.expectedUser, u)
|
assert.Equal(t, test.expectedUser, u)
|
||||||
assert.Equal(t, test.expectedSecret, s)
|
assert.Equal(t, test.expectedSecret, s)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,12 +251,13 @@ func TestRegistryEndpoints(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
c.config.Registry.Mirrors = test.mirrors
|
c.config.Registry.Mirrors = test.mirrors
|
||||||
got, err := c.registryEndpoints(test.host)
|
got, err := c.registryEndpoints(test.host)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, test.expected, got)
|
assert.Equal(t, test.expected, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,9 +307,10 @@ func TestDefaultScheme(t *testing.T) {
|
|||||||
expected: "https",
|
expected: "https",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
got := defaultScheme(test.host)
|
got := defaultScheme(test.host)
|
||||||
assert.Equal(t, test.expected, got)
|
assert.Equal(t, test.expected, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,11 +328,12 @@ func TestEncryptedImagePullOpts(t *testing.T) {
|
|||||||
expectedOpts: 0,
|
expectedOpts: 0,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
c.config.ImageDecryption.KeyModel = test.keyModel
|
c.config.ImageDecryption.KeyModel = test.keyModel
|
||||||
got := len(c.encryptedImagesPullOpts())
|
got := len(c.encryptedImagesPullOpts())
|
||||||
assert.Equal(t, test.expectedOpts, got)
|
assert.Equal(t, test.expectedOpts, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,13 +70,15 @@ func TestToCRISandbox(t *testing.T) {
|
|||||||
expectedState: runtime.PodSandboxState_SANDBOX_NOTREADY,
|
expectedState: runtime.PodSandboxState_SANDBOX_NOTREADY,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
status := sandboxstore.Status{
|
t.Run(desc, func(t *testing.T) {
|
||||||
CreatedAt: createdAt,
|
status := sandboxstore.Status{
|
||||||
State: test.state,
|
CreatedAt: createdAt,
|
||||||
}
|
State: test.state,
|
||||||
expect.State = test.expectedState
|
}
|
||||||
s := toCRISandbox(meta, status)
|
expect.State = test.expectedState
|
||||||
assert.Equal(t, expect, s, desc)
|
s := toCRISandbox(meta, status)
|
||||||
|
assert.Equal(t, expect, s, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,8 +203,9 @@ func TestFilterSandboxes(t *testing.T) {
|
|||||||
expect: []*runtime.PodSandbox{testSandboxes[2]},
|
expect: []*runtime.PodSandbox{testSandboxes[2]},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase: %s", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
filtered := c.filterCRISandboxes(testSandboxes, test.filter)
|
filtered := c.filterCRISandboxes(testSandboxes, test.filter)
|
||||||
assert.Equal(t, test.expect, filtered, desc)
|
assert.Equal(t, test.expect, filtered, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -235,26 +235,27 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
c.config.EnableUnprivilegedICMP = true
|
c.config.EnableUnprivilegedICMP = true
|
||||||
c.config.EnableUnprivilegedPorts = true
|
c.config.EnableUnprivilegedPorts = true
|
||||||
config, imageConfig, specCheck := getRunPodSandboxTestData()
|
config, imageConfig, specCheck := getRunPodSandboxTestData()
|
||||||
if test.configChange != nil {
|
if test.configChange != nil {
|
||||||
test.configChange(config)
|
test.configChange(config)
|
||||||
}
|
}
|
||||||
spec, err := c.sandboxContainerSpec(testID, config, imageConfig, nsPath, nil)
|
spec, err := c.sandboxContainerSpec(testID, config, imageConfig, nsPath, nil)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Nil(t, spec)
|
assert.Nil(t, spec)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, spec)
|
assert.NotNil(t, spec)
|
||||||
specCheck(t, testID, spec)
|
specCheck(t, testID, spec)
|
||||||
if test.specCheck != nil {
|
if test.specCheck != nil {
|
||||||
test.specCheck(t, spec)
|
test.specCheck(t, spec)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -426,32 +427,33 @@ options timeout:1
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
c.os.(*ostesting.FakeOS).HostnameFn = func() (string, error) {
|
c.os.(*ostesting.FakeOS).HostnameFn = func() (string, error) {
|
||||||
return realhostname, nil
|
return realhostname, nil
|
||||||
}
|
}
|
||||||
cfg := &runtime.PodSandboxConfig{
|
cfg := &runtime.PodSandboxConfig{
|
||||||
Hostname: test.hostname,
|
Hostname: test.hostname,
|
||||||
DnsConfig: test.dnsConfig,
|
DnsConfig: test.dnsConfig,
|
||||||
Linux: &runtime.LinuxPodSandboxConfig{
|
Linux: &runtime.LinuxPodSandboxConfig{
|
||||||
SecurityContext: &runtime.LinuxSandboxSecurityContext{
|
SecurityContext: &runtime.LinuxSandboxSecurityContext{
|
||||||
NamespaceOptions: &runtime.NamespaceOption{
|
NamespaceOptions: &runtime.NamespaceOption{
|
||||||
Ipc: test.ipcMode,
|
Ipc: test.ipcMode,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
c.setupSandboxFiles(testID, cfg)
|
|
||||||
calls := c.os.(*ostesting.FakeOS).GetCalls()
|
|
||||||
assert.Len(t, calls, len(test.expectedCalls))
|
|
||||||
for i, expected := range test.expectedCalls {
|
|
||||||
if expected.Arguments == nil {
|
|
||||||
// Ignore arguments.
|
|
||||||
expected.Arguments = calls[i].Arguments
|
|
||||||
}
|
}
|
||||||
assert.Equal(t, expected, calls[i])
|
c.setupSandboxFiles(testID, cfg)
|
||||||
}
|
calls := c.os.(*ostesting.FakeOS).GetCalls()
|
||||||
|
assert.Len(t, calls, len(test.expectedCalls))
|
||||||
|
for i, expected := range test.expectedCalls {
|
||||||
|
if expected.Arguments == nil {
|
||||||
|
// Ignore arguments.
|
||||||
|
expected.Arguments = calls[i].Arguments
|
||||||
|
}
|
||||||
|
assert.Equal(t, expected, calls[i])
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -493,14 +495,15 @@ options timeout:1
|
|||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
resolvContent, err := parseDNSOptions(test.servers, test.searches, test.options)
|
resolvContent, err := parseDNSOptions(test.servers, test.searches, test.options)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, resolvContent, test.expectedContent)
|
assert.Equal(t, resolvContent, test.expectedContent)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,29 +92,30 @@ func TestSandboxContainerSpec(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
c := newTestCRIService()
|
c := newTestCRIService()
|
||||||
config, imageConfig, specCheck := getRunPodSandboxTestData()
|
config, imageConfig, specCheck := getRunPodSandboxTestData()
|
||||||
if test.configChange != nil {
|
if test.configChange != nil {
|
||||||
test.configChange(config)
|
test.configChange(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
if test.imageConfigChange != nil {
|
if test.imageConfigChange != nil {
|
||||||
test.imageConfigChange(imageConfig)
|
test.imageConfigChange(imageConfig)
|
||||||
}
|
}
|
||||||
spec, err := c.sandboxContainerSpec(testID, config, imageConfig, nsPath,
|
spec, err := c.sandboxContainerSpec(testID, config, imageConfig, nsPath,
|
||||||
test.podAnnotations)
|
test.podAnnotations)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Nil(t, spec)
|
assert.Nil(t, spec)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, spec)
|
assert.NotNil(t, spec)
|
||||||
specCheck(t, testID, spec)
|
specCheck(t, testID, spec)
|
||||||
if test.specCheck != nil {
|
if test.specCheck != nil {
|
||||||
test.specCheck(t, spec)
|
test.specCheck(t, spec)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,25 +140,26 @@ func TestTypeurlMarshalUnmarshalSandboxMeta(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
meta := &sandboxstore.Metadata{
|
meta := &sandboxstore.Metadata{
|
||||||
ID: "1",
|
ID: "1",
|
||||||
Name: "sandbox_1",
|
Name: "sandbox_1",
|
||||||
NetNSPath: "/home/cloud",
|
NetNSPath: "/home/cloud",
|
||||||
}
|
}
|
||||||
meta.Config, _, _ = getRunPodSandboxTestData()
|
meta.Config, _, _ = getRunPodSandboxTestData()
|
||||||
if test.configChange != nil {
|
if test.configChange != nil {
|
||||||
test.configChange(meta.Config)
|
test.configChange(meta.Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
any, err := typeurl.MarshalAny(meta)
|
any, err := typeurl.MarshalAny(meta)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
data, err := typeurl.UnmarshalAny(any)
|
data, err := typeurl.UnmarshalAny(any)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.IsType(t, &sandboxstore.Metadata{}, data)
|
assert.IsType(t, &sandboxstore.Metadata{}, data)
|
||||||
curMeta, ok := data.(*sandboxstore.Metadata)
|
curMeta, ok := data.(*sandboxstore.Metadata)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.Equal(t, meta, curMeta)
|
assert.Equal(t, meta, curMeta)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,8 +253,9 @@ func TestToCNIPortMappings(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
assert.Equal(t, test.cniPortMappings, toCNIPortMappings(test.criPortMappings))
|
assert.Equal(t, test.cniPortMappings, toCNIPortMappings(test.criPortMappings))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,16 +300,17 @@ func TestSelectPodIP(t *testing.T) {
|
|||||||
expectedAdditionalIPs: []string{"2001:db8:85a3::8a2e:370:7334", "2001:db8:85a3::8a2e:370:7335", "192.168.17.45"},
|
expectedAdditionalIPs: []string{"2001:db8:85a3::8a2e:370:7334", "2001:db8:85a3::8a2e:370:7335", "192.168.17.45"},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
var ipConfigs []*cni.IPConfig
|
var ipConfigs []*cni.IPConfig
|
||||||
for _, ip := range test.ips {
|
for _, ip := range test.ips {
|
||||||
ipConfigs = append(ipConfigs, &cni.IPConfig{
|
ipConfigs = append(ipConfigs, &cni.IPConfig{
|
||||||
IP: net.ParseIP(ip),
|
IP: net.ParseIP(ip),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
ip, additionalIPs := selectPodIPs(context.Background(), ipConfigs, test.pref)
|
ip, additionalIPs := selectPodIPs(context.Background(), ipConfigs, test.pref)
|
||||||
assert.Equal(t, test.expectedIP, ip)
|
assert.Equal(t, test.expectedIP, ip)
|
||||||
assert.Equal(t, test.expectedAdditionalIPs, additionalIPs)
|
assert.Equal(t, test.expectedAdditionalIPs, additionalIPs)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,13 +104,14 @@ func TestPodSandboxStatus(t *testing.T) {
|
|||||||
expectedState: runtime.PodSandboxState_SANDBOX_NOTREADY,
|
expectedState: runtime.PodSandboxState_SANDBOX_NOTREADY,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase: %s", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
status := sandboxstore.Status{
|
status := sandboxstore.Status{
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
State: test.state,
|
State: test.state,
|
||||||
}
|
}
|
||||||
expected.State = test.expectedState
|
expected.State = test.expectedState
|
||||||
got := toCRISandboxStatus(metadata, status, ip, additionalIPs)
|
got := toCRISandboxStatus(metadata, status, ip, additionalIPs)
|
||||||
assert.Equal(t, expected, got)
|
assert.Equal(t, expected, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,23 +51,25 @@ func TestWaitSandboxStop(t *testing.T) {
|
|||||||
expectErr: false,
|
expectErr: false,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
c := newTestCRIService()
|
t.Run(desc, func(t *testing.T) {
|
||||||
sandbox := sandboxstore.NewSandbox(
|
c := newTestCRIService()
|
||||||
sandboxstore.Metadata{ID: id},
|
sandbox := sandboxstore.NewSandbox(
|
||||||
sandboxstore.Status{State: test.state},
|
sandboxstore.Metadata{ID: id},
|
||||||
)
|
sandboxstore.Status{State: test.state},
|
||||||
ctx := context.Background()
|
)
|
||||||
if test.cancel {
|
ctx := context.Background()
|
||||||
cancelledCtx, cancel := context.WithCancel(ctx)
|
if test.cancel {
|
||||||
cancel()
|
cancelledCtx, cancel := context.WithCancel(ctx)
|
||||||
ctx = cancelledCtx
|
cancel()
|
||||||
}
|
ctx = cancelledCtx
|
||||||
if test.timeout > 0 {
|
}
|
||||||
timeoutCtx, cancel := context.WithTimeout(ctx, test.timeout)
|
if test.timeout > 0 {
|
||||||
defer cancel()
|
timeoutCtx, cancel := context.WithTimeout(ctx, test.timeout)
|
||||||
ctx = timeoutCtx
|
defer cancel()
|
||||||
}
|
ctx = timeoutCtx
|
||||||
err := c.waitSandboxStop(ctx, sandbox)
|
}
|
||||||
assert.Equal(t, test.expectErr, err != nil, desc)
|
err := c.waitSandboxStop(ctx, sandbox)
|
||||||
|
assert.Equal(t, test.expectErr, err != nil, desc)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,8 +65,9 @@ func TestContainerState(t *testing.T) {
|
|||||||
state: runtime.ContainerState_CONTAINER_EXITED,
|
state: runtime.ContainerState_CONTAINER_EXITED,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", c)
|
t.Run(c, func(t *testing.T) {
|
||||||
assertlib.Equal(t, test.state, test.status.State())
|
assertlib.Equal(t, test.state, test.status.State())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,28 +221,29 @@ func TestImageStore(t *testing.T) {
|
|||||||
expected: []Image{},
|
expected: []Image{},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", desc)
|
t.Run(desc, func(t *testing.T) {
|
||||||
s, err := NewFakeStore([]Image{image})
|
s, err := NewFakeStore([]Image{image})
|
||||||
assert.NoError(err)
|
|
||||||
assert.NoError(s.update(test.ref, test.image))
|
|
||||||
|
|
||||||
assert.Len(s.List(), len(test.expected))
|
|
||||||
for _, expect := range test.expected {
|
|
||||||
got, err := s.Get(expect.ID)
|
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
equal(got, expect)
|
assert.NoError(s.update(test.ref, test.image))
|
||||||
for _, ref := range expect.References {
|
|
||||||
id, err := s.Resolve(ref)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Equal(expect.ID, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.image == nil {
|
assert.Len(s.List(), len(test.expected))
|
||||||
// Shouldn't be able to index by removed ref.
|
for _, expect := range test.expected {
|
||||||
id, err := s.Resolve(test.ref)
|
got, err := s.Get(expect.ID)
|
||||||
assert.Equal(errdefs.ErrNotFound, err)
|
assert.NoError(err)
|
||||||
assert.Empty(id)
|
equal(got, expect)
|
||||||
}
|
for _, ref := range expect.References {
|
||||||
|
id, err := s.Resolve(ref)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(expect.ID, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if test.image == nil {
|
||||||
|
// Shouldn't be able to index by removed ref.
|
||||||
|
id, err := s.Resolve(test.ref)
|
||||||
|
assert.Equal(errdefs.ErrNotFound, err)
|
||||||
|
assert.Empty(id)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,12 +73,13 @@ func TestNormalizeImageRef(t *testing.T) {
|
|||||||
expect: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
|
expect: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Logf("TestCase %q", test.input)
|
t.Run(test.input, func(t *testing.T) {
|
||||||
normalized, err := NormalizeImageRef(test.input)
|
normalized, err := NormalizeImageRef(test.input)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
output := normalized.String()
|
output := normalized.String()
|
||||||
assert.Equal(t, test.expect, output)
|
assert.Equal(t, test.expect, output)
|
||||||
_, err = reference.Parse(output)
|
_, err = reference.Parse(output)
|
||||||
assert.NoError(t, err, "%q should be containerd supported reference", output)
|
assert.NoError(t, err, "%q should be containerd supported reference", output)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user