mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
libct/configs/validate: simplify intelrtd tests
The whole struct intelRdtStatus with its methods and a sync.Once is not needed, since intelrtd.Is*Enabled methods are already run-once (or use run-once and a simple comparison). Yet it is still needed for the test to fake values returned by *Enabled. Simplify to use func pointers which a test case overwrites. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -1,41 +1,12 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer/intelrdt"
|
||||
)
|
||||
|
||||
// Cache the result of intelrdt IsEnabled functions to avoid repeated sysfs
|
||||
// access and enable mocking for unit tests.
|
||||
type intelRdtStatus struct {
|
||||
sync.Once
|
||||
rdtEnabled bool
|
||||
catEnabled bool
|
||||
mbaEnabled bool
|
||||
}
|
||||
|
||||
var intelRdt = &intelRdtStatus{}
|
||||
|
||||
func (i *intelRdtStatus) init() {
|
||||
i.Do(func() {
|
||||
i.rdtEnabled = intelrdt.IsEnabled()
|
||||
i.catEnabled = intelrdt.IsCATEnabled()
|
||||
i.mbaEnabled = intelrdt.IsMBAEnabled()
|
||||
})
|
||||
}
|
||||
|
||||
func (i *intelRdtStatus) isEnabled() bool {
|
||||
i.init()
|
||||
return i.rdtEnabled
|
||||
}
|
||||
|
||||
func (i *intelRdtStatus) isCATEnabled() bool {
|
||||
i.init()
|
||||
return i.catEnabled
|
||||
}
|
||||
|
||||
func (i *intelRdtStatus) isMBAEnabled() bool {
|
||||
i.init()
|
||||
return i.mbaEnabled
|
||||
}
|
||||
// Allow mocking for TestValidateIntelRdt.
|
||||
var (
|
||||
intelRdtIsEnabled = intelrdt.IsEnabled
|
||||
intelRdtIsCATEnabled = intelrdt.IsCATEnabled
|
||||
intelRdtIsMBAEnabled = intelrdt.IsMBAEnabled
|
||||
)
|
||||
|
||||
@@ -4,12 +4,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer/configs"
|
||||
"github.com/opencontainers/runc/libcontainer/intelrdt"
|
||||
)
|
||||
|
||||
func TestValidateIntelRdt(t *testing.T) {
|
||||
// Call init to trigger the sync.Once and enable overriding the rdt status
|
||||
intelRdt.init()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
rdtEnabled bool
|
||||
@@ -90,11 +88,17 @@ func TestValidateIntelRdt(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
intelRdt.rdtEnabled = tc.rdtEnabled
|
||||
intelRdt.catEnabled = tc.catEnabled
|
||||
intelRdt.mbaEnabled = tc.mbaEnabled
|
||||
t.Cleanup(func() {
|
||||
intelRdtIsEnabled = intelrdt.IsEnabled
|
||||
intelRdtIsCATEnabled = intelrdt.IsCATEnabled
|
||||
intelRdtIsMBAEnabled = intelrdt.IsMBAEnabled
|
||||
})
|
||||
intelRdtIsEnabled = func() bool { return tc.rdtEnabled }
|
||||
intelRdtIsCATEnabled = func() bool { return tc.catEnabled }
|
||||
intelRdtIsMBAEnabled = func() bool { return tc.mbaEnabled }
|
||||
|
||||
config := &configs.Config{
|
||||
Rootfs: "/var",
|
||||
|
||||
@@ -309,7 +309,7 @@ func intelrdtCheck(config *configs.Config) error {
|
||||
if config.IntelRdt == nil {
|
||||
return nil
|
||||
}
|
||||
if !intelRdt.isEnabled() {
|
||||
if !intelRdtIsEnabled() {
|
||||
return fmt.Errorf("intelRdt is specified in config, but Intel RDT is not enabled")
|
||||
}
|
||||
|
||||
@@ -317,11 +317,10 @@ func intelrdtCheck(config *configs.Config) error {
|
||||
case clos == ".", clos == "..", len(clos) > 1 && strings.Contains(clos, "/"):
|
||||
return fmt.Errorf("invalid intelRdt.ClosID %q", clos)
|
||||
}
|
||||
|
||||
if !intelRdt.isCATEnabled() && config.IntelRdt.L3CacheSchema != "" {
|
||||
if !intelRdtIsCATEnabled() && config.IntelRdt.L3CacheSchema != "" {
|
||||
return errors.New("intelRdt.l3CacheSchema is specified in config, but Intel RDT/CAT is not enabled")
|
||||
}
|
||||
if !intelRdt.isMBAEnabled() && config.IntelRdt.MemBwSchema != "" {
|
||||
if !intelRdtIsMBAEnabled() && config.IntelRdt.MemBwSchema != "" {
|
||||
return errors.New("intelRdt.memBwSchema is specified in config, but Intel RDT/MBA is not enabled")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user