Skip to content

Commit

Permalink
Temp save
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Mar 26, 2019
1 parent 0cf51f3 commit 1e1ba07
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 55 deletions.
2 changes: 1 addition & 1 deletion virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type ContainerStatus struct {
PID int
StartTime time.Time
RootFs string
Spec *vctypes.CompatOCISpec `json:"-"`
Spec *vctypes.CompatOCISpec

// Annotations allow clients to store arbitrary values,
// for example to add additional status values required
Expand Down
3 changes: 0 additions & 3 deletions virtcontainers/pkg/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ const (
// AssetHashType is the hash type used for assets verification
AssetHashType = vcAnnotationsPrefix + "AssetHashType"

// ConfigJSONKey is the annotation key to fetch the OCI configuration.
//ConfigJSONKey = vcAnnotationsPrefix + "pkg.oci.config"

// BundlePathKey is the annotation key to fetch the OCI configuration file path.
BundlePathKey = vcAnnotationsPrefix + "pkg.oci.bundle_path"

Expand Down
16 changes: 0 additions & 16 deletions virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,19 +637,3 @@ func EnvVars(envs []string) ([]types.EnvVar, error) {

return envVars, nil
}

// GetOCIConfig returns an OCI spec configuration from the annotation
// stored into the container status.
/*func GetOCIConfig(status vc.ContainerStatus) (vctypes.CompatOCISpec, error) {
ociConfigStr, ok := status.Annotations[vcAnnotations.ConfigJSONKey]
if !ok {
return vctypes.CompatOCISpec{}, fmt.Errorf("Annotation[%s] not found", vcAnnotations.ConfigJSONKey)
}
var ociSpec vctypes.CompatOCISpec
if err := json.Unmarshal([]byte(ociConfigStr), &ociSpec); err != nil {
return vctypes.CompatOCISpec{}, err
}
return ociSpec, nil
}*/
52 changes: 22 additions & 30 deletions virtcontainers/pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/device/config"
vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
vctypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
"github.com/kata-containers/runtime/virtcontainers/types"
)

Expand Down Expand Up @@ -167,7 +168,7 @@ func TestMinimalSandboxConfig(t *testing.T) {
},
}

var minimalOCISpec CompatOCISpec
var minimalOCISpec vctypes.CompatOCISpec

//Marshal and unmarshall json to compare sandboxConfig and expectedSandboxConfig
if err := json.Unmarshal([]byte(minimalConfig), &minimalOCISpec); err != nil {
Expand All @@ -180,10 +181,6 @@ func TestMinimalSandboxConfig(t *testing.T) {
}
minimalOCISpec.Process.Capabilities = caps
}
ociSpecJSON, err := json.Marshal(minimalOCISpec)
if err != nil {
t.Fatal(err)
}

devInfo := config.DeviceInfo{
ContainerPath: "/dev/vfio/17",
Expand All @@ -204,7 +201,6 @@ func TestMinimalSandboxConfig(t *testing.T) {
ReadonlyRootfs: true,
Cmd: expectedCmd,
Annotations: map[string]string{
vcAnnotations.ConfigJSONKey: string(ociSpecJSON),
vcAnnotations.BundlePathKey: tempBundlePath,
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
},
Expand All @@ -213,6 +209,7 @@ func TestMinimalSandboxConfig(t *testing.T) {
Resources: specs.LinuxResources{Devices: []specs.LinuxDeviceCgroup{
{Allow: false, Type: "", Major: (*int64)(nil), Minor: (*int64)(nil), Access: "rwm"},
}},
Spec: &minimalOCISpec,
}

expectedNetworkConfig := vc.NetworkConfig{}
Expand All @@ -231,7 +228,6 @@ func TestMinimalSandboxConfig(t *testing.T) {
Containers: []vc.ContainerConfig{expectedContainerConfig},

Annotations: map[string]string{
vcAnnotations.ConfigJSONKey: string(ociSpecJSON),
vcAnnotations.BundlePathKey: tempBundlePath,
},

Expand Down Expand Up @@ -276,7 +272,6 @@ func TestStatusToOCIStateSuccessfulWithReadyState(t *testing.T) {
}

containerAnnotations := map[string]string{
vcAnnotations.ConfigJSONKey: minimalConfig,
vcAnnotations.BundlePathKey: tempBundlePath,
}

Expand Down Expand Up @@ -312,7 +307,6 @@ func TestStatusToOCIStateSuccessfulWithRunningState(t *testing.T) {
}

containerAnnotations := map[string]string{
vcAnnotations.ConfigJSONKey: minimalConfig,
vcAnnotations.BundlePathKey: tempBundlePath,
}

Expand Down Expand Up @@ -347,7 +341,6 @@ func TestStatusToOCIStateSuccessfulWithStoppedState(t *testing.T) {
}

containerAnnotations := map[string]string{
vcAnnotations.ConfigJSONKey: minimalConfig,
vcAnnotations.BundlePathKey: tempBundlePath,
}

Expand Down Expand Up @@ -378,7 +371,6 @@ func TestStatusToOCIStateSuccessfulWithNoState(t *testing.T) {
testRootFs := "testRootFs"

containerAnnotations := map[string]string{
vcAnnotations.ConfigJSONKey: minimalConfig,
vcAnnotations.BundlePathKey: tempBundlePath,
}

Expand Down Expand Up @@ -535,8 +527,8 @@ func TestGetContainerTypeFailure(t *testing.T) {
}
}

func testContainerTypeSuccessful(t *testing.T, ociSpec CompatOCISpec, expected vc.ContainerType) {
containerType, err := ociSpec.ContainerType()
func testContainerTypeSuccessful(t *testing.T, ociSpec vctypes.CompatOCISpec, expected vc.ContainerType) {
containerType, err := ContainerType(&ociSpec)
if err != nil {
t.Fatal(err)
}
Expand All @@ -547,7 +539,7 @@ func testContainerTypeSuccessful(t *testing.T, ociSpec CompatOCISpec, expected v
}

func TestContainerTypePodSandbox(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec

ociSpec.Annotations = map[string]string{
annotations.ContainerType: annotations.ContainerTypeSandbox,
Expand All @@ -557,7 +549,7 @@ func TestContainerTypePodSandbox(t *testing.T) {
}

func TestContainerTypePodContainer(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec

ociSpec.Annotations = map[string]string{
annotations.ContainerType: annotations.ContainerTypeContainer,
Expand All @@ -567,19 +559,19 @@ func TestContainerTypePodContainer(t *testing.T) {
}

func TestContainerTypePodSandboxEmptyAnnotation(t *testing.T) {
testContainerTypeSuccessful(t, CompatOCISpec{}, vc.PodSandbox)
testContainerTypeSuccessful(t, vctypes.CompatOCISpec{}, vc.PodSandbox)
}

func TestContainerTypeFailure(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec
expected := vc.UnknownContainerType
unknownType := "unknown_type"

ociSpec.Annotations = map[string]string{
annotations.ContainerType: unknownType,
}

containerType, err := ociSpec.ContainerType()
containerType, err := ContainerType(&ociSpec)
if err == nil {
t.Fatalf("This test should fail because the container type is %s", unknownType)
}
Expand All @@ -590,14 +582,14 @@ func TestContainerTypeFailure(t *testing.T) {
}

func TestSandboxIDSuccessful(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec
testSandboxID := "testSandboxID"

ociSpec.Annotations = map[string]string{
annotations.SandboxID: testSandboxID,
}

sandboxID, err := ociSpec.SandboxID()
sandboxID, err := SandboxID(&ociSpec)
if err != nil {
t.Fatal(err)
}
Expand All @@ -608,9 +600,9 @@ func TestSandboxIDSuccessful(t *testing.T) {
}

func TestSandboxIDFailure(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec

sandboxID, err := ociSpec.SandboxID()
sandboxID, err := SandboxID(&ociSpec)
if err == nil {
t.Fatalf("This test should fail because annotations is empty")
}
Expand Down Expand Up @@ -653,7 +645,7 @@ func TestAddKernelParamInvalid(t *testing.T) {
}

func TestDeviceTypeFailure(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec

invalidDeviceType := "f"
ociSpec.Linux = &specs.Linux{}
Expand All @@ -678,7 +670,7 @@ func TestContains(t *testing.T) {
}

func TestDevicePathEmpty(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec

ociSpec.Linux = &specs.Linux{}
ociSpec.Linux.Devices = []specs.LinuxDevice{
Expand All @@ -694,9 +686,9 @@ func TestDevicePathEmpty(t *testing.T) {
}

func TestContainerCapabilities(t *testing.T) {
var ociSpec CompatOCISpec
var ociSpec vctypes.CompatOCISpec

ociSpec.Process = &CompatOCIProcess{}
ociSpec.Process = &vctypes.CompatOCIProcess{}
ociSpec.Process.Capabilities = map[string]interface{}{
"bounding": []interface{}{"CAP_KILL"},
"effective": []interface{}{"CAP_KILL", "CAP_LEASE"},
Expand Down Expand Up @@ -736,9 +728,9 @@ func TestContainerCapabilities(t *testing.T) {

// use specs.Spec to decode the spec, the content of capabilities is [] string
func TestCompatOCISpecWithArray(t *testing.T) {
compatOCISpec := CompatOCISpec{}
compatOCISpec := vctypes.CompatOCISpec{}
err := json.Unmarshal([]byte(capabilitiesSpecArray), &compatOCISpec)
assert.Nil(t, err, "use CompatOCISpec to decode capabilitiesSpecArray failed")
assert.Nil(t, err, "use vctypes.CompatOCISpec to decode capabilitiesSpecArray failed")

ociSpecJSON, err := json.Marshal(compatOCISpec)
assert.Nil(t, err, "encode compatOCISpec failed")
Expand All @@ -763,9 +755,9 @@ func TestCompatOCISpecWithArray(t *testing.T) {

// use specs.Spec to decode the spec, the content of capabilities is struct
func TestCompatOCISpecWithStruct(t *testing.T) {
compatOCISpec := CompatOCISpec{}
compatOCISpec := vctypes.CompatOCISpec{}
err := json.Unmarshal([]byte(capabilitiesSpecStruct), &compatOCISpec)
assert.Nil(t, err, "use CompatOCISpec to decode capabilitiesSpecStruct failed")
assert.Nil(t, err, "use vctypes.CompatOCISpec to decode capabilitiesSpecStruct failed")

ociSpecJSON, err := json.Marshal(compatOCISpec)
assert.Nil(t, err, "encode compatOCISpec failed")
Expand Down
11 changes: 6 additions & 5 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,11 +1172,12 @@ func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
for id, c := range s.containers {
if id == containerID {
return ContainerStatus{
ID: c.id,
State: c.state,
PID: c.process.Pid,
StartTime: c.process.StartTime,
RootFs: c.config.RootFs,
ID: c.id,
State: c.state,
PID: c.process.Pid,
StartTime: c.process.StartTime,
RootFs: c.config.RootFs,
Annotations: c.config.Annotations,
}, nil
}
}
Expand Down

0 comments on commit 1e1ba07

Please sign in to comment.