Skip to content

Commit 67758c3

Browse files
authored
Merge pull request #552 from wking/runtimetest-capabilities-cleanup
cmd/runtimetest/main: Loop for DRYer validateCapabilities
2 parents e6a60ee + fb19ae1 commit 67758c3

File tree

1 file changed

+39
-62
lines changed

1 file changed

+39
-62
lines changed

Diff for: cmd/runtimetest/main.go

+39-62
Original file line numberDiff line numberDiff line change
@@ -202,72 +202,49 @@ func validateCapabilities(spec *rspec.Spec) error {
202202
return err
203203
}
204204

205-
expectedCaps1 := make(map[string]bool)
206-
expectedCaps2 := make(map[string]bool)
207-
expectedCaps3 := make(map[string]bool)
208-
expectedCaps4 := make(map[string]bool)
209-
expectedCaps5 := make(map[string]bool)
210-
for _, ec := range spec.Process.Capabilities.Bounding {
211-
expectedCaps1[ec] = true
212-
}
213-
for _, ec := range spec.Process.Capabilities.Effective {
214-
expectedCaps2[ec] = true
215-
}
216-
for _, ec := range spec.Process.Capabilities.Inheritable {
217-
expectedCaps3[ec] = true
218-
}
219-
for _, ec := range spec.Process.Capabilities.Permitted {
220-
expectedCaps4[ec] = true
221-
}
222-
for _, ec := range spec.Process.Capabilities.Ambient {
223-
expectedCaps5[ec] = true
224-
}
225-
226-
for _, cap := range capability.List() {
227-
if cap > last {
228-
continue
205+
for _, capType := range []struct {
206+
capType capability.CapType
207+
config []string
208+
}{
209+
{
210+
capType: capability.BOUNDING,
211+
config: spec.Process.Capabilities.Bounding,
212+
},
213+
{
214+
capType: capability.EFFECTIVE,
215+
config: spec.Process.Capabilities.Effective,
216+
},
217+
{
218+
capType: capability.INHERITABLE,
219+
config: spec.Process.Capabilities.Inheritable,
220+
},
221+
{
222+
capType: capability.PERMITTED,
223+
config: spec.Process.Capabilities.Permitted,
224+
},
225+
{
226+
capType: capability.AMBIENT,
227+
config: spec.Process.Capabilities.Ambient,
228+
},
229+
} {
230+
expectedCaps := make(map[string]bool)
231+
for _, ec := range capType.config {
232+
expectedCaps[ec] = true
229233
}
230234

231-
capKey := fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String()))
232-
expectedSet := expectedCaps1[capKey]
233-
actuallySet := processCaps.Get(capability.BOUNDING, cap)
234-
if expectedSet != actuallySet {
235-
if expectedSet {
236-
return fmt.Errorf("Expected bounding capability %v not set for process", cap.String())
237-
}
238-
return fmt.Errorf("Unexpected bounding capability %v set for process", cap.String())
239-
}
240-
expectedSet = expectedCaps2[capKey]
241-
actuallySet = processCaps.Get(capability.EFFECTIVE, cap)
242-
if expectedSet != actuallySet {
243-
if expectedSet {
244-
return fmt.Errorf("Expected effective capability %v not set for process", cap.String())
235+
for _, cap := range capability.List() {
236+
if cap > last {
237+
continue
245238
}
246-
return fmt.Errorf("Unexpected effective capability %v set for process", cap.String())
247-
}
248-
expectedSet = expectedCaps3[capKey]
249-
actuallySet = processCaps.Get(capability.INHERITABLE, cap)
250-
if expectedSet != actuallySet {
251-
if expectedSet {
252-
return fmt.Errorf("Expected inheritable capability %v not set for process", cap.String())
253-
}
254-
return fmt.Errorf("Unexpected inheritable capability %v set for process", cap.String())
255-
}
256-
expectedSet = expectedCaps4[capKey]
257-
actuallySet = processCaps.Get(capability.PERMITTED, cap)
258-
if expectedSet != actuallySet {
259-
if expectedSet {
260-
return fmt.Errorf("Expected permitted capability %v not set for process", cap.String())
261-
}
262-
return fmt.Errorf("Unexpected permitted capability %v set for process", cap.String())
263-
}
264-
expectedSet = expectedCaps5[capKey]
265-
actuallySet = processCaps.Get(capability.AMBIENT, cap)
266-
if expectedSet != actuallySet {
267-
if expectedSet {
268-
return fmt.Errorf("Expected ambient capability %v not set for process", cap.String())
239+
240+
capKey := fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String()))
241+
expectedSet := expectedCaps[capKey]
242+
actuallySet := processCaps.Get(capType.capType, cap)
243+
if expectedSet && !actuallySet {
244+
return fmt.Errorf("expected %s capability %v not set", capType.capType, capKey)
245+
} else if !expectedSet && actuallySet {
246+
return fmt.Errorf("unexpected %s capability %v set", capType.capType, capKey)
269247
}
270-
return fmt.Errorf("Unexpected ambient capability %v set for process", cap.String())
271248
}
272249
}
273250

0 commit comments

Comments
 (0)