Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add resource validation after delete #654

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"

tap "github.com/mndrix/tap-go"
"github.com/mrunalp/fileutils"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/specerror"
"github.com/opencontainers/runtime-tools/validation/util"
uuid "github.com/satori/go.uuid"
)

func main() {
t := tap.New()
t.Header(0)

// Create a cgroup
cgPath := "/sys/fs/cgroup"
testPath := filepath.Join(cgPath, "pids", "cgrouptest")
os.Mkdir(testPath, 0755)
defer os.RemoveAll(testPath)

bundleDir, err := util.PrepareBundle()
if err != nil {
util.Fatal(err)
}
defer os.RemoveAll(bundleDir)

r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
if err != nil {
util.Fatal(err)
}

r.SetID(uuid.NewV4().String())
g, err := util.GetDefaultGenerator()
if err != nil {
util.Fatal(err)
}

err = r.SetConfig(g)
if err != nil {
util.Fatal(err)
}
err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
if err != nil {
util.Fatal(err)
}

err = r.Create()
if err != nil {
util.Fatal(err)
}

state, err := r.State()
if err != nil {
util.Fatal(err)
}
// Add the container to the cgroup
err = ioutil.WriteFile(filepath.Join(testPath, "tasks"), []byte(strconv.Itoa(state.Pid)), 0644)
if err != nil {
util.Fatal(err)
}

err = r.Start()
if err != nil {
util.Fatal(err)
}

err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1)
if err == nil {
err = r.Delete()
}
if err != nil {
t.Fail(err.Error())
}

_, err = os.Stat(testPath)
fmt.Println(err)
util.SpecErrorOK(t, err == nil, specerror.NewError(specerror.DeleteOnlyCreatedRes, fmt.Errorf("Note that resources associated with the container, but not created by this container, MUST NOT be deleted"), rspec.Version), nil)

t.AutoPlan()
}
84 changes: 84 additions & 0 deletions validation/delete_resources/delete_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"
"os"
"path/filepath"
"time"

tap "github.com/mndrix/tap-go"
"github.com/mrunalp/fileutils"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/cgroups"
"github.com/opencontainers/runtime-tools/specerror"
"github.com/opencontainers/runtime-tools/validation/util"
uuid "github.com/satori/go.uuid"
)

func main() {
t := tap.New()
t.Header(0)

bundleDir, err := util.PrepareBundle()
if err != nil {
util.Fatal(err)
}
defer os.RemoveAll(bundleDir)

r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
if err != nil {
util.Fatal(err)
}

r.SetID(uuid.NewV4().String())
g, err := util.GetDefaultGenerator()
if err != nil {
util.Fatal(err)
}

var limit int64 = 1000

g.SetLinuxCgroupsPath(cgroups.AbsCgroupPath)
g.SetLinuxResourcesPidsLimit(limit)

err = r.SetConfig(g)
if err != nil {
util.Fatal(err)
}
err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
if err != nil {
util.Fatal(err)
}

err = r.Create()
if err != nil {
util.Fatal(err)
}

state, err := r.State()
if err != nil {
util.Fatal(err)
}
if err := util.ValidateLinuxResourcesPids(g.Spec(), t, &state); err != nil {
util.Fatal(err)
}

err = r.Start()
if err != nil {
util.Fatal(err)
}

err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1)
if err == nil {
err = r.Delete()
}
if err != nil {
t.Fail(err.Error())
}

path := filepath.Join("/sys/fs/cgroup/pids", cgroups.AbsCgroupPath)
_, err = os.Stat(path)
util.SpecErrorOK(t, os.IsNotExist(err), specerror.NewError(specerror.DeleteResImplement, fmt.Errorf("Deleting a container MUST delete the resources that were created during the `create` step"), rspec.Version), nil)

t.AutoPlan()
}