Skip to content

Commit

Permalink
tests: Improve coverage for virtcontainers/pkg/compatoci/ for Kata 2.0
Browse files Browse the repository at this point in the history
Add test cases for ParseConfigJson function and GetContainerSpec function

Fixes: #258

Signed-off-by: LiuWeijie <weijie.liu@intel.com>
  • Loading branch information
Liu-Weijie committed May 15, 2023
1 parent 4064192 commit 50cc9c5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/runtime/virtcontainers/pkg/compatoci/utils.go
Expand Up @@ -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{}:
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
96 changes: 91 additions & 5 deletions src/runtime/virtcontainers/pkg/compatoci/utils_test.go
Expand Up @@ -7,6 +7,8 @@ package compatoci

import (
"encoding/json"
"os"
"path"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 50cc9c5

Please sign in to comment.