Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 1 | package hst |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | . "github.com/onsi/ginkgo/v2" |
| 6 | "reflect" |
| 7 | "runtime" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | var cpuPinningTests = map[string][]func(s *CpuPinningSuite){} |
| 12 | var cpuPinningSoloTests = map[string][]func(s *CpuPinningSuite){} |
| 13 | |
| 14 | type CpuPinningSuite struct { |
| 15 | HstSuite |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 16 | previousMaxContainerCount int |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | func RegisterCpuPinningTests(tests ...func(s *CpuPinningSuite)) { |
| 20 | cpuPinningTests[getTestFilename()] = tests |
| 21 | } |
| 22 | |
| 23 | func RegisterCpuPinningSoloTests(tests ...func(s *CpuPinningSuite)) { |
| 24 | cpuPinningSoloTests[getTestFilename()] = tests |
| 25 | } |
| 26 | |
| 27 | func (s *CpuPinningSuite) SetupSuite() { |
| 28 | s.HstSuite.SetupSuite() |
| 29 | s.LoadNetworkTopology("tap") |
| 30 | s.LoadContainerTopology("singleCpuPinning") |
| 31 | } |
| 32 | |
| 33 | func (s *CpuPinningSuite) SetupTest() { |
| 34 | // Skip if we cannot allocate 3 CPUs for test container |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 35 | s.previousMaxContainerCount = s.CpuAllocator.maxContainerCount |
| 36 | s.CpuCount = 3 |
| 37 | s.CpuAllocator.maxContainerCount = 1 |
| 38 | s.SkipIfNotEnoughAvailableCpus() |
| 39 | |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 40 | s.HstSuite.SetupTest() |
| 41 | container := s.GetContainerByName(SingleTopoContainerVpp) |
| 42 | vpp, err := container.newVppInstance(container.AllocatedCpus) |
| 43 | s.AssertNotNil(vpp, fmt.Sprint(err)) |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func (s *CpuPinningSuite) TearDownTest() { |
| 47 | // reset vars |
| 48 | s.CpuCount = *NConfiguredCpus |
| 49 | s.CpuAllocator.maxContainerCount = s.previousMaxContainerCount |
| 50 | s.HstSuite.TearDownTest() |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 51 | |
| 52 | } |
| 53 | |
| 54 | var _ = Describe("CpuPinningSuite", Ordered, ContinueOnFailure, func() { |
| 55 | var s CpuPinningSuite |
| 56 | BeforeAll(func() { |
| 57 | s.SetupSuite() |
| 58 | }) |
| 59 | BeforeEach(func() { |
| 60 | s.SetupTest() |
| 61 | }) |
| 62 | AfterAll(func() { |
| 63 | s.TearDownSuite() |
| 64 | |
| 65 | }) |
| 66 | AfterEach(func() { |
| 67 | s.TearDownTest() |
| 68 | }) |
| 69 | |
| 70 | // https://onsi.github.io/ginkgo/#dynamically-generating-specs |
| 71 | for filename, tests := range cpuPinningTests { |
| 72 | for _, test := range tests { |
| 73 | test := test |
| 74 | pc := reflect.ValueOf(test).Pointer() |
| 75 | funcValue := runtime.FuncForPC(pc) |
| 76 | testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2] |
| 77 | It(testName, func(ctx SpecContext) { |
| 78 | s.Log(testName + ": BEGIN") |
| 79 | test(&s) |
| 80 | }, SpecTimeout(SuiteTimeout)) |
| 81 | } |
| 82 | } |
| 83 | }) |
| 84 | |
| 85 | var _ = Describe("CpuPinningSuiteSolo", Ordered, ContinueOnFailure, Serial, func() { |
| 86 | var s CpuPinningSuite |
| 87 | BeforeAll(func() { |
| 88 | s.SetupSuite() |
| 89 | }) |
| 90 | BeforeEach(func() { |
| 91 | s.SetupTest() |
| 92 | }) |
| 93 | AfterAll(func() { |
| 94 | s.TearDownSuite() |
| 95 | }) |
| 96 | AfterEach(func() { |
| 97 | s.TearDownTest() |
| 98 | }) |
| 99 | |
| 100 | for filename, tests := range cpuPinningSoloTests { |
| 101 | for _, test := range tests { |
| 102 | test := test |
| 103 | pc := reflect.ValueOf(test).Pointer() |
| 104 | funcValue := runtime.FuncForPC(pc) |
| 105 | testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2] |
| 106 | It(testName, Label("SOLO"), func(ctx SpecContext) { |
| 107 | s.Log(testName + ": BEGIN") |
| 108 | test(&s) |
| 109 | }, SpecTimeout(SuiteTimeout)) |
| 110 | } |
| 111 | } |
| 112 | }) |