mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-29 20:24:31 +00:00
tests: Improve coverage for virtcontainers/pkg/compatoci/ for Kata 2.0
Add test cases for ParseConfigJson function and GetContainerSpec function Fixes: #258 Signed-off-by: LiuWeijie <weijie.liu@intel.com>
This commit is contained in:
parent
4064192896
commit
50cc9c582f
@ -74,7 +74,7 @@ func containerCapabilities(s compatOCISpec) (specs.LinuxCapabilities, error) {
|
||||
}
|
||||
|
||||
default:
|
||||
return c, fmt.Errorf("Unexpected format for capabilities: %v", caps)
|
||||
return c, fmt.Errorf("unexpected format for capabilities: %v", caps)
|
||||
}
|
||||
}
|
||||
case []interface{}:
|
||||
@ -94,7 +94,7 @@ func containerCapabilities(s compatOCISpec) (specs.LinuxCapabilities, error) {
|
||||
ociLog.Debug("Empty capabilities have been passed")
|
||||
return c, nil
|
||||
default:
|
||||
return c, fmt.Errorf("Unexpected format for capabilities: %v", caps)
|
||||
return c, fmt.Errorf("unexpected format for capabilities: %v", caps)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
@ -154,5 +154,5 @@ func GetContainerSpec(annotations map[string]string) (specs.Spec, error) {
|
||||
|
||||
ociLog.Errorf("Annotations[%s] not found, cannot find container spec",
|
||||
vcAnnotations.BundlePathKey)
|
||||
return specs.Spec{}, fmt.Errorf("Could not find container spec")
|
||||
return specs.Spec{}, fmt.Errorf("could not find container spec")
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ package compatoci
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@ -78,6 +80,18 @@ func TestContainerCapabilities(t *testing.T) {
|
||||
assert.Equal(t, c.Inheritable, []string{"CAP_KILL", "CAP_LEASE", "CAP_SYS_ADMIN"})
|
||||
assert.Equal(t, c.Ambient, []string{""})
|
||||
|
||||
ociSpec.Process.Capabilities = map[string]interface{}{
|
||||
"unexpected": interface{}(""),
|
||||
}
|
||||
|
||||
c, err = ContainerCapabilities(ociSpec)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, len(c.Bounding), 0)
|
||||
assert.Equal(t, len(c.Effective), 0)
|
||||
assert.Equal(t, len(c.Permitted), 0)
|
||||
assert.Equal(t, len(c.Inheritable), 0)
|
||||
assert.Equal(t, len(c.Ambient), 0)
|
||||
|
||||
ociSpec.Process.Capabilities = []interface{}{"CAP_LEASE", "CAP_SETUID"}
|
||||
|
||||
c, err = ContainerCapabilities(ociSpec)
|
||||
@ -92,11 +106,31 @@ func TestContainerCapabilities(t *testing.T) {
|
||||
|
||||
c, err = ContainerCapabilities(ociSpec)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, c.Bounding, []string(nil))
|
||||
assert.Equal(t, c.Effective, []string(nil))
|
||||
assert.Equal(t, c.Permitted, []string(nil))
|
||||
assert.Equal(t, c.Inheritable, []string(nil))
|
||||
assert.Equal(t, c.Ambient, []string(nil))
|
||||
assert.Equal(t, len(c.Bounding), 0)
|
||||
assert.Equal(t, len(c.Effective), 0)
|
||||
assert.Equal(t, len(c.Permitted), 0)
|
||||
assert.Equal(t, len(c.Inheritable), 0)
|
||||
assert.Equal(t, len(c.Ambient), 0)
|
||||
|
||||
ociSpec.Process.Capabilities = interface{}("")
|
||||
|
||||
c, err = ContainerCapabilities(ociSpec)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, len(c.Bounding), 0)
|
||||
assert.Equal(t, len(c.Effective), 0)
|
||||
assert.Equal(t, len(c.Permitted), 0)
|
||||
assert.Equal(t, len(c.Inheritable), 0)
|
||||
assert.Equal(t, len(c.Ambient), 0)
|
||||
|
||||
ociSpec.Process = nil
|
||||
|
||||
c, err = ContainerCapabilities(ociSpec)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, len(c.Bounding), 0)
|
||||
assert.Equal(t, len(c.Effective), 0)
|
||||
assert.Equal(t, len(c.Permitted), 0)
|
||||
assert.Equal(t, len(c.Inheritable), 0)
|
||||
assert.Equal(t, len(c.Ambient), 0)
|
||||
}
|
||||
|
||||
// use specs.Spec to decode the spec, the content of capabilities is [] string
|
||||
@ -145,3 +179,55 @@ func TestGetConfigPath(t *testing.T) {
|
||||
configPath := getConfigPath(tempBundlePath)
|
||||
assert.Equal(t, configPath, expected)
|
||||
}
|
||||
|
||||
func TestParseConfigJSON(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
var ociSpec compatOCISpec
|
||||
var configByte []byte
|
||||
|
||||
ociSpec.Spec.Version = "1.0.0"
|
||||
ociSpec.Process = &compatOCIProcess{}
|
||||
ociSpec.Process.Capabilities = map[string]interface{}{
|
||||
"bounding": []interface{}{"CAP_KILL"},
|
||||
"effective": []interface{}{"CAP_KILL", "CAP_LEASE"},
|
||||
"permitted": []interface{}{"CAP_SETUID"},
|
||||
"inheritable": []interface{}{"CAP_KILL", "CAP_LEASE", "CAP_SYS_ADMIN"},
|
||||
"ambient": []interface{}{""},
|
||||
}
|
||||
|
||||
file, err := os.Create(path.Join(tmpDir, "config.json"))
|
||||
if err != nil {
|
||||
t.Error("cannot create file")
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if configByte, err = json.Marshal(ociSpec); err != nil {
|
||||
t.Error("cannot marshal compat oci spec")
|
||||
}
|
||||
|
||||
_, err = file.Write(configByte)
|
||||
if err != nil {
|
||||
t.Error("cannot write config data into file")
|
||||
}
|
||||
|
||||
spec, err := ParseConfigJSON(tmpDir)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, spec.Version, "1.0.0")
|
||||
}
|
||||
|
||||
func TestGetContainerSpec(t *testing.T) {
|
||||
annotations := map[string]string{
|
||||
"io.katacontainers.pkg.oci.bundle_path": "",
|
||||
}
|
||||
|
||||
_, err := GetContainerSpec(annotations)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
annotations = map[string]string{
|
||||
"io.katacontainers.pkg.oci.wrong_path": "",
|
||||
}
|
||||
|
||||
_, err = GetContainerSpec(annotations)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user