Skip to content

Commit 0ace550

Browse files
author
Zhou Hao
authored
Merge pull request #654 from q384566678/resources-validation
add resource validation after delete
2 parents 0bf8b92 + caa32a1 commit 0ace550

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"strconv"
9+
"time"
10+
11+
tap "github.com/mndrix/tap-go"
12+
"github.com/mrunalp/fileutils"
13+
rspec "github.com/opencontainers/runtime-spec/specs-go"
14+
"github.com/opencontainers/runtime-tools/specerror"
15+
"github.com/opencontainers/runtime-tools/validation/util"
16+
uuid "github.com/satori/go.uuid"
17+
)
18+
19+
func main() {
20+
t := tap.New()
21+
t.Header(0)
22+
23+
// Create a cgroup
24+
cgPath := "/sys/fs/cgroup"
25+
testPath := filepath.Join(cgPath, "pids", "cgrouptest")
26+
os.Mkdir(testPath, 0755)
27+
defer os.RemoveAll(testPath)
28+
29+
bundleDir, err := util.PrepareBundle()
30+
if err != nil {
31+
util.Fatal(err)
32+
}
33+
defer os.RemoveAll(bundleDir)
34+
35+
r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
36+
if err != nil {
37+
util.Fatal(err)
38+
}
39+
40+
r.SetID(uuid.NewV4().String())
41+
g, err := util.GetDefaultGenerator()
42+
if err != nil {
43+
util.Fatal(err)
44+
}
45+
46+
err = r.SetConfig(g)
47+
if err != nil {
48+
util.Fatal(err)
49+
}
50+
err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
51+
if err != nil {
52+
util.Fatal(err)
53+
}
54+
55+
err = r.Create()
56+
if err != nil {
57+
util.Fatal(err)
58+
}
59+
60+
state, err := r.State()
61+
if err != nil {
62+
util.Fatal(err)
63+
}
64+
// Add the container to the cgroup
65+
err = ioutil.WriteFile(filepath.Join(testPath, "tasks"), []byte(strconv.Itoa(state.Pid)), 0644)
66+
if err != nil {
67+
util.Fatal(err)
68+
}
69+
70+
err = r.Start()
71+
if err != nil {
72+
util.Fatal(err)
73+
}
74+
75+
err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1)
76+
if err == nil {
77+
err = r.Delete()
78+
}
79+
if err != nil {
80+
t.Fail(err.Error())
81+
}
82+
83+
_, err = os.Stat(testPath)
84+
fmt.Println(err)
85+
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)
86+
87+
t.AutoPlan()
88+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"time"
8+
9+
tap "github.com/mndrix/tap-go"
10+
"github.com/mrunalp/fileutils"
11+
rspec "github.com/opencontainers/runtime-spec/specs-go"
12+
"github.com/opencontainers/runtime-tools/cgroups"
13+
"github.com/opencontainers/runtime-tools/specerror"
14+
"github.com/opencontainers/runtime-tools/validation/util"
15+
uuid "github.com/satori/go.uuid"
16+
)
17+
18+
func main() {
19+
t := tap.New()
20+
t.Header(0)
21+
22+
bundleDir, err := util.PrepareBundle()
23+
if err != nil {
24+
util.Fatal(err)
25+
}
26+
defer os.RemoveAll(bundleDir)
27+
28+
r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
29+
if err != nil {
30+
util.Fatal(err)
31+
}
32+
33+
r.SetID(uuid.NewV4().String())
34+
g, err := util.GetDefaultGenerator()
35+
if err != nil {
36+
util.Fatal(err)
37+
}
38+
39+
var limit int64 = 1000
40+
41+
g.SetLinuxCgroupsPath(cgroups.AbsCgroupPath)
42+
g.SetLinuxResourcesPidsLimit(limit)
43+
44+
err = r.SetConfig(g)
45+
if err != nil {
46+
util.Fatal(err)
47+
}
48+
err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
49+
if err != nil {
50+
util.Fatal(err)
51+
}
52+
53+
err = r.Create()
54+
if err != nil {
55+
util.Fatal(err)
56+
}
57+
58+
state, err := r.State()
59+
if err != nil {
60+
util.Fatal(err)
61+
}
62+
if err := util.ValidateLinuxResourcesPids(g.Spec(), t, &state); err != nil {
63+
util.Fatal(err)
64+
}
65+
66+
err = r.Start()
67+
if err != nil {
68+
util.Fatal(err)
69+
}
70+
71+
err = util.WaitingForStatus(r, util.LifecycleStatusStopped, time.Second*10, time.Second*1)
72+
if err == nil {
73+
err = r.Delete()
74+
}
75+
if err != nil {
76+
t.Fail(err.Error())
77+
}
78+
79+
path := filepath.Join("/sys/fs/cgroup/pids", cgroups.AbsCgroupPath)
80+
_, err = os.Stat(path)
81+
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)
82+
83+
t.AutoPlan()
84+
}

0 commit comments

Comments
 (0)