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
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/intelrdt"
|
"github.com/opencontainers/runc/libcontainer/intelrdt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cache the result of intelrdt IsEnabled functions to avoid repeated sysfs
|
// Allow mocking for TestValidateIntelRdt.
|
||||||
// access and enable mocking for unit tests.
|
var (
|
||||||
type intelRdtStatus struct {
|
intelRdtIsEnabled = intelrdt.IsEnabled
|
||||||
sync.Once
|
intelRdtIsCATEnabled = intelrdt.IsCATEnabled
|
||||||
rdtEnabled bool
|
intelRdtIsMBAEnabled = intelrdt.IsMBAEnabled
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,12 +4,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/configs"
|
"github.com/opencontainers/runc/libcontainer/configs"
|
||||||
|
"github.com/opencontainers/runc/libcontainer/intelrdt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateIntelRdt(t *testing.T) {
|
func TestValidateIntelRdt(t *testing.T) {
|
||||||
// Call init to trigger the sync.Once and enable overriding the rdt status
|
|
||||||
intelRdt.init()
|
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
rdtEnabled bool
|
rdtEnabled bool
|
||||||
@@ -90,11 +88,17 @@ func TestValidateIntelRdt(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
intelRdt.rdtEnabled = tc.rdtEnabled
|
t.Cleanup(func() {
|
||||||
intelRdt.catEnabled = tc.catEnabled
|
intelRdtIsEnabled = intelrdt.IsEnabled
|
||||||
intelRdt.mbaEnabled = tc.mbaEnabled
|
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{
|
config := &configs.Config{
|
||||||
Rootfs: "/var",
|
Rootfs: "/var",
|
||||||
|
|||||||
@@ -309,7 +309,7 @@ func intelrdtCheck(config *configs.Config) error {
|
|||||||
if config.IntelRdt == nil {
|
if config.IntelRdt == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if !intelRdt.isEnabled() {
|
if !intelRdtIsEnabled() {
|
||||||
return fmt.Errorf("intelRdt is specified in config, but Intel RDT is not enabled")
|
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, "/"):
|
case clos == ".", clos == "..", len(clos) > 1 && strings.Contains(clos, "/"):
|
||||||
return fmt.Errorf("invalid intelRdt.ClosID %q", clos)
|
return fmt.Errorf("invalid intelRdt.ClosID %q", clos)
|
||||||
}
|
}
|
||||||
|
if !intelRdtIsCATEnabled() && config.IntelRdt.L3CacheSchema != "" {
|
||||||
if !intelRdt.isCATEnabled() && config.IntelRdt.L3CacheSchema != "" {
|
|
||||||
return errors.New("intelRdt.l3CacheSchema is specified in config, but Intel RDT/CAT is not enabled")
|
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")
|
return errors.New("intelRdt.memBwSchema is specified in config, but Intel RDT/MBA is not enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user