Skip to content

Commit 51f985c

Browse files
committed
oci: move cap UT to _linux_test.go
No substantial code change Signed-off-by: Akihiro Suda <[email protected]>
1 parent a2d1a8a commit 51f985c

2 files changed

Lines changed: 108 additions & 84 deletions

File tree

oci/spec_opts_linux_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package oci
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
specs "github.com/opencontainers/runtime-spec/specs-go"
24+
)
25+
26+
func TestAddCaps(t *testing.T) {
27+
t.Parallel()
28+
29+
var s specs.Spec
30+
31+
if err := WithAddedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
32+
t.Fatal(err)
33+
}
34+
for i, cl := range [][]string{
35+
s.Process.Capabilities.Bounding,
36+
s.Process.Capabilities.Effective,
37+
s.Process.Capabilities.Permitted,
38+
s.Process.Capabilities.Inheritable,
39+
} {
40+
if !capsContain(cl, "CAP_CHOWN") {
41+
t.Errorf("cap list %d does not contain added cap", i)
42+
}
43+
}
44+
}
45+
46+
func TestDropCaps(t *testing.T) {
47+
t.Parallel()
48+
49+
var s specs.Spec
50+
51+
if err := WithAllKnownCapabilities(context.Background(), nil, nil, &s); err != nil {
52+
t.Fatal(err)
53+
}
54+
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
for i, cl := range [][]string{
59+
s.Process.Capabilities.Bounding,
60+
s.Process.Capabilities.Effective,
61+
s.Process.Capabilities.Permitted,
62+
s.Process.Capabilities.Inheritable,
63+
} {
64+
if capsContain(cl, "CAP_CHOWN") {
65+
t.Errorf("cap list %d contains dropped cap", i)
66+
}
67+
}
68+
69+
// Add all capabilities back and drop a different cap.
70+
if err := WithAllKnownCapabilities(context.Background(), nil, nil, &s); err != nil {
71+
t.Fatal(err)
72+
}
73+
if err := WithDroppedCapabilities([]string{"CAP_FOWNER"})(context.Background(), nil, nil, &s); err != nil {
74+
t.Fatal(err)
75+
}
76+
77+
for i, cl := range [][]string{
78+
s.Process.Capabilities.Bounding,
79+
s.Process.Capabilities.Effective,
80+
s.Process.Capabilities.Permitted,
81+
s.Process.Capabilities.Inheritable,
82+
} {
83+
if capsContain(cl, "CAP_FOWNER") {
84+
t.Errorf("cap list %d contains dropped cap", i)
85+
}
86+
if !capsContain(cl, "CAP_CHOWN") {
87+
t.Errorf("cap list %d doesn't contain non-dropped cap", i)
88+
}
89+
}
90+
91+
// Drop all duplicated caps.
92+
if err := WithCapabilities([]string{"CAP_CHOWN", "CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
93+
t.Fatal(err)
94+
}
95+
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
96+
t.Fatal(err)
97+
}
98+
for i, cl := range [][]string{
99+
s.Process.Capabilities.Bounding,
100+
s.Process.Capabilities.Effective,
101+
s.Process.Capabilities.Permitted,
102+
s.Process.Capabilities.Inheritable,
103+
} {
104+
if len(cl) != 0 {
105+
t.Errorf("cap list %d is not empty", i)
106+
}
107+
}
108+
}

oci/spec_opts_test.go

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -549,90 +549,6 @@ func TestWithImageConfigArgs(t *testing.T) {
549549
}
550550
}
551551

552-
func TestAddCaps(t *testing.T) {
553-
t.Parallel()
554-
555-
var s specs.Spec
556-
557-
if err := WithAddedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
558-
t.Fatal(err)
559-
}
560-
for i, cl := range [][]string{
561-
s.Process.Capabilities.Bounding,
562-
s.Process.Capabilities.Effective,
563-
s.Process.Capabilities.Permitted,
564-
s.Process.Capabilities.Inheritable,
565-
} {
566-
if !capsContain(cl, "CAP_CHOWN") {
567-
t.Errorf("cap list %d does not contain added cap", i)
568-
}
569-
}
570-
}
571-
572-
func TestDropCaps(t *testing.T) {
573-
t.Parallel()
574-
575-
var s specs.Spec
576-
577-
if err := WithAllKnownCapabilities(context.Background(), nil, nil, &s); err != nil {
578-
t.Fatal(err)
579-
}
580-
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
581-
t.Fatal(err)
582-
}
583-
584-
for i, cl := range [][]string{
585-
s.Process.Capabilities.Bounding,
586-
s.Process.Capabilities.Effective,
587-
s.Process.Capabilities.Permitted,
588-
s.Process.Capabilities.Inheritable,
589-
} {
590-
if capsContain(cl, "CAP_CHOWN") {
591-
t.Errorf("cap list %d contains dropped cap", i)
592-
}
593-
}
594-
595-
// Add all capabilities back and drop a different cap.
596-
if err := WithAllKnownCapabilities(context.Background(), nil, nil, &s); err != nil {
597-
t.Fatal(err)
598-
}
599-
if err := WithDroppedCapabilities([]string{"CAP_FOWNER"})(context.Background(), nil, nil, &s); err != nil {
600-
t.Fatal(err)
601-
}
602-
603-
for i, cl := range [][]string{
604-
s.Process.Capabilities.Bounding,
605-
s.Process.Capabilities.Effective,
606-
s.Process.Capabilities.Permitted,
607-
s.Process.Capabilities.Inheritable,
608-
} {
609-
if capsContain(cl, "CAP_FOWNER") {
610-
t.Errorf("cap list %d contains dropped cap", i)
611-
}
612-
if !capsContain(cl, "CAP_CHOWN") {
613-
t.Errorf("cap list %d doesn't contain non-dropped cap", i)
614-
}
615-
}
616-
617-
// Drop all duplicated caps.
618-
if err := WithCapabilities([]string{"CAP_CHOWN", "CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
619-
t.Fatal(err)
620-
}
621-
if err := WithDroppedCapabilities([]string{"CAP_CHOWN"})(context.Background(), nil, nil, &s); err != nil {
622-
t.Fatal(err)
623-
}
624-
for i, cl := range [][]string{
625-
s.Process.Capabilities.Bounding,
626-
s.Process.Capabilities.Effective,
627-
s.Process.Capabilities.Permitted,
628-
s.Process.Capabilities.Inheritable,
629-
} {
630-
if len(cl) != 0 {
631-
t.Errorf("cap list %d is not empty", i)
632-
}
633-
}
634-
}
635-
636552
func TestDevShmSize(t *testing.T) {
637553
t.Parallel()
638554
var (

0 commit comments

Comments
 (0)