Skip to content

Commit d07223b

Browse files
committed
Add unit tests for RunAsUser.MustRunAsRange strategy.
1 parent 3d4c343 commit d07223b

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package user
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
securityapi "github.com/openshift/origin/pkg/security/apis/security"
9+
)
10+
11+
func TestMustRunAsRangeOptions(t *testing.T) {
12+
var uid int64 = 1
13+
tests := map[string]struct {
14+
opts *securityapi.RunAsUserStrategyOptions
15+
pass bool
16+
}{
17+
"invalid opts, required min and max": {
18+
opts: &securityapi.RunAsUserStrategyOptions{},
19+
pass: false,
20+
},
21+
"invalid opts, required max": {
22+
opts: &securityapi.RunAsUserStrategyOptions{UIDRangeMin: &uid},
23+
pass: false,
24+
},
25+
"invalid opts, required min": {
26+
opts: &securityapi.RunAsUserStrategyOptions{UIDRangeMax: &uid},
27+
pass: false,
28+
},
29+
"valid opts": {
30+
opts: &securityapi.RunAsUserStrategyOptions{UIDRangeMin: &uid, UIDRangeMax: &uid},
31+
pass: true,
32+
},
33+
}
34+
for name, tc := range tests {
35+
_, err := NewMustRunAsRange(tc.opts)
36+
if err != nil && tc.pass {
37+
t.Errorf("%s expected to pass but received error %v", name, err)
38+
}
39+
if err == nil && !tc.pass {
40+
t.Errorf("%s expected to fail but did not receive an error", name)
41+
}
42+
}
43+
}
44+
45+
func TestMustRunAsRangeGenerate(t *testing.T) {
46+
var uidMin int64 = 1
47+
var uidMax int64 = 10
48+
opts := &securityapi.RunAsUserStrategyOptions{UIDRangeMin: &uidMin, UIDRangeMax: &uidMax}
49+
mustRunAsRange, err := NewMustRunAsRange(opts)
50+
if err != nil {
51+
t.Fatalf("unexpected error initializing NewMustRunAsRange %v", err)
52+
}
53+
generated, err := mustRunAsRange.Generate(nil, nil)
54+
if err != nil {
55+
t.Fatalf("unexpected error generating uid %v", err)
56+
}
57+
if *generated != uidMin {
58+
t.Errorf("generated uid does not equal expected uid")
59+
}
60+
}
61+
62+
func TestMustRunAsRangeValidate(t *testing.T) {
63+
var uidMin int64 = 1
64+
var uidMax int64 = 10
65+
opts := &securityapi.RunAsUserStrategyOptions{UIDRangeMin: &uidMin, UIDRangeMax: &uidMax}
66+
mustRunAsRange, err := NewMustRunAsRange(opts)
67+
if err != nil {
68+
t.Fatalf("unexpected error initializing NewMustRunAsRange %v", err)
69+
}
70+
71+
errs := mustRunAsRange.Validate(nil, nil, nil, nil, nil)
72+
expectedMessage := "runAsUser: Required value"
73+
if len(errs) == 0 {
74+
t.Errorf("expected errors from nil runAsUser but got none")
75+
} else if !strings.Contains(errs[0].Error(), expectedMessage) {
76+
t.Errorf("expected error to contain %q but it did not: %v", expectedMessage, errs)
77+
}
78+
79+
var lowUid int64 = 0
80+
errs = mustRunAsRange.Validate(nil, nil, nil, nil, &lowUid)
81+
expectedMessage = fmt.Sprintf("runAsUser: Invalid value: %d: must be in the ranges: [%d, %d]", lowUid, uidMin, uidMax)
82+
if len(errs) == 0 {
83+
t.Errorf("expected errors from mismatch uid but got none")
84+
} else if !strings.Contains(errs[0].Error(), expectedMessage) {
85+
t.Errorf("expected error to contain %q but it did not: %v", expectedMessage, errs)
86+
}
87+
88+
var highUid int64 = 11
89+
errs = mustRunAsRange.Validate(nil, nil, nil, nil, &highUid)
90+
expectedMessage = fmt.Sprintf("runAsUser: Invalid value: %d: must be in the ranges: [%d, %d]", highUid, uidMin, uidMax)
91+
if len(errs) == 0 {
92+
t.Errorf("expected errors from mismatch uid but got none")
93+
} else if !strings.Contains(errs[0].Error(), expectedMessage) {
94+
t.Errorf("expected error to contain %q but it did not: %v", expectedMessage, errs)
95+
}
96+
97+
var goodUid int64 = 5
98+
errs = mustRunAsRange.Validate(nil, nil, nil, nil, &goodUid)
99+
if len(errs) != 0 {
100+
t.Errorf("expected no errors from matching uid but got %v", errs)
101+
}
102+
}

0 commit comments

Comments
 (0)