Adrian Villin | d01a63a | 2024-08-15 12:53:53 +0200 | [diff] [blame] | 1 | package hst |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "runtime" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | . "github.com/onsi/ginkgo/v2" |
| 11 | ) |
| 12 | |
| 13 | // These correspond to names used in yaml config |
| 14 | const ( |
| 15 | ServerLdpInterfaceName = "srv" |
| 16 | ClientLdpInterfaceName = "cln" |
| 17 | ) |
| 18 | |
| 19 | var ldpTests = map[string][]func(s *LdpSuite){} |
| 20 | var ldpSoloTests = map[string][]func(s *LdpSuite){} |
| 21 | |
| 22 | type LdpSuite struct { |
| 23 | HstSuite |
| 24 | } |
| 25 | |
| 26 | func RegisterLdpTests(tests ...func(s *LdpSuite)) { |
| 27 | ldpTests[getTestFilename()] = tests |
| 28 | } |
| 29 | func RegisterSoloLdpTests(tests ...func(s *LdpSuite)) { |
| 30 | ldpSoloTests[getTestFilename()] = tests |
| 31 | } |
| 32 | |
| 33 | func (s *LdpSuite) SetupSuite() { |
| 34 | time.Sleep(1 * time.Second) |
| 35 | s.HstSuite.SetupSuite() |
| 36 | s.ConfigureNetworkTopology("2peerVeth") |
| 37 | s.LoadContainerTopology("2peerVethLdp") |
| 38 | } |
| 39 | |
| 40 | func (s *LdpSuite) SetupTest() { |
| 41 | s.HstSuite.SetupTest() |
| 42 | |
| 43 | // Setup test conditions |
| 44 | var sessionConfig Stanza |
| 45 | sessionConfig. |
| 46 | NewStanza("session"). |
| 47 | Append("enable"). |
| 48 | Append("use-app-socket-api") |
| 49 | |
| 50 | if strings.Contains(CurrentSpecReport().LeafNodeText, "InterruptMode") { |
| 51 | sessionConfig.Append("use-private-rx-mqs").Close() |
| 52 | s.Log("**********************INTERRUPT MODE**********************") |
| 53 | } else { |
| 54 | sessionConfig.Close() |
| 55 | } |
| 56 | |
| 57 | // ... For server |
| 58 | serverContainer := s.GetContainerByName("server-vpp") |
| 59 | |
| 60 | serverVpp, err := serverContainer.newVppInstance(serverContainer.AllocatedCpus, sessionConfig) |
| 61 | s.AssertNotNil(serverVpp, fmt.Sprint(err)) |
| 62 | |
| 63 | s.SetupServerVpp() |
| 64 | |
| 65 | // ... For client |
| 66 | clientContainer := s.GetContainerByName("client-vpp") |
| 67 | |
| 68 | clientVpp, err := clientContainer.newVppInstance(clientContainer.AllocatedCpus, sessionConfig) |
| 69 | s.AssertNotNil(clientVpp, fmt.Sprint(err)) |
| 70 | |
| 71 | s.setupClientVpp() |
| 72 | |
| 73 | serverContainer.AddEnvVar("VCL_CONFIG", serverContainer.GetContainerWorkDir()+"/vcl_srv.conf") |
| 74 | clientContainer.AddEnvVar("VCL_CONFIG", clientContainer.GetContainerWorkDir()+"/vcl_cln.conf") |
| 75 | |
| 76 | for _, container := range s.StartedContainers { |
| 77 | container.AddEnvVar("LD_PRELOAD", "/usr/lib/libvcl_ldpreload.so") |
| 78 | container.AddEnvVar("LDP_DEBUG", "0") |
| 79 | container.AddEnvVar("VCL_DEBUG", "0") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (s *LdpSuite) TearDownTest() { |
| 84 | for _, container := range s.StartedContainers { |
| 85 | delete(container.EnvVars, "LD_PRELOAD") |
| 86 | delete(container.EnvVars, "VCL_CONFIG") |
| 87 | } |
| 88 | s.HstSuite.TearDownTest() |
| 89 | |
| 90 | } |
| 91 | |
| 92 | func (s *LdpSuite) SetupServerVpp() { |
| 93 | var srvVclConf Stanza |
| 94 | serverContainer := s.GetContainerByName("server-vpp") |
| 95 | serverVclFileName := serverContainer.GetHostWorkDir() + "/vcl_srv.conf" |
| 96 | serverVpp := serverContainer.VppInstance |
| 97 | s.AssertNil(serverVpp.Start()) |
| 98 | |
| 99 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
| 100 | idx, err := serverVpp.createAfPacket(serverVeth) |
| 101 | s.AssertNil(err, fmt.Sprint(err)) |
| 102 | s.AssertNotEqual(0, idx) |
| 103 | |
| 104 | serverAppSocketApi := fmt.Sprintf("app-socket-api %s/var/run/app_ns_sockets/default", |
| 105 | serverContainer.GetContainerWorkDir()) |
| 106 | err = srvVclConf. |
| 107 | NewStanza("vcl"). |
| 108 | Append("rx-fifo-size 4000000"). |
| 109 | Append("tx-fifo-size 4000000"). |
| 110 | Append("app-scope-local"). |
| 111 | Append("app-scope-global"). |
| 112 | Append("use-mq-eventfd"). |
| 113 | Append(serverAppSocketApi).Close(). |
| 114 | SaveToFile(serverVclFileName) |
| 115 | s.AssertNil(err, fmt.Sprint(err)) |
| 116 | } |
| 117 | |
| 118 | func (s *LdpSuite) setupClientVpp() { |
| 119 | var clnVclConf Stanza |
| 120 | clientContainer := s.GetContainerByName("client-vpp") |
| 121 | clientVclFileName := clientContainer.GetHostWorkDir() + "/vcl_cln.conf" |
| 122 | clientVpp := clientContainer.VppInstance |
| 123 | s.AssertNil(clientVpp.Start()) |
| 124 | |
| 125 | clientVeth := s.GetInterfaceByName(ClientInterfaceName) |
| 126 | idx, err := clientVpp.createAfPacket(clientVeth) |
| 127 | s.AssertNil(err, fmt.Sprint(err)) |
| 128 | s.AssertNotEqual(0, idx) |
| 129 | |
| 130 | clientAppSocketApi := fmt.Sprintf("app-socket-api %s/var/run/app_ns_sockets/default", |
| 131 | clientContainer.GetContainerWorkDir()) |
| 132 | err = clnVclConf. |
| 133 | NewStanza("vcl"). |
| 134 | Append("rx-fifo-size 4000000"). |
| 135 | Append("tx-fifo-size 4000000"). |
| 136 | Append("app-scope-local"). |
| 137 | Append("app-scope-global"). |
| 138 | Append("use-mq-eventfd"). |
| 139 | Append(clientAppSocketApi).Close(). |
| 140 | SaveToFile(clientVclFileName) |
| 141 | s.AssertNil(err, fmt.Sprint(err)) |
| 142 | } |
| 143 | |
| 144 | var _ = Describe("LdpSuite", Ordered, ContinueOnFailure, func() { |
| 145 | var s LdpSuite |
| 146 | BeforeAll(func() { |
| 147 | s.SetupSuite() |
| 148 | }) |
| 149 | BeforeEach(func() { |
| 150 | s.SetupTest() |
| 151 | }) |
| 152 | AfterAll(func() { |
| 153 | s.TearDownSuite() |
| 154 | |
| 155 | }) |
| 156 | AfterEach(func() { |
| 157 | s.TearDownTest() |
| 158 | }) |
| 159 | |
| 160 | // https://onsi.github.io/ginkgo/#dynamically-generating-specs |
| 161 | for filename, tests := range ldpTests { |
| 162 | for _, test := range tests { |
| 163 | test := test |
| 164 | pc := reflect.ValueOf(test).Pointer() |
| 165 | funcValue := runtime.FuncForPC(pc) |
| 166 | testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2] |
| 167 | It(testName, func(ctx SpecContext) { |
| 168 | s.Log(testName + ": BEGIN") |
| 169 | test(&s) |
| 170 | }, SpecTimeout(SuiteTimeout)) |
| 171 | } |
| 172 | } |
| 173 | }) |
| 174 | |
| 175 | var _ = Describe("LdpSuiteSolo", Ordered, ContinueOnFailure, Serial, func() { |
| 176 | var s LdpSuite |
| 177 | BeforeAll(func() { |
| 178 | s.SetupSuite() |
| 179 | }) |
| 180 | BeforeEach(func() { |
| 181 | s.SetupTest() |
| 182 | }) |
| 183 | AfterAll(func() { |
| 184 | s.TearDownSuite() |
| 185 | }) |
| 186 | AfterEach(func() { |
| 187 | s.TearDownTest() |
| 188 | }) |
| 189 | |
| 190 | // https://onsi.github.io/ginkgo/#dynamically-generating-specs |
| 191 | for filename, tests := range ldpSoloTests { |
| 192 | for _, test := range tests { |
| 193 | test := test |
| 194 | pc := reflect.ValueOf(test).Pointer() |
| 195 | funcValue := runtime.FuncForPC(pc) |
| 196 | testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2] |
| 197 | It(testName, Label("SOLO"), func(ctx SpecContext) { |
| 198 | s.Log(testName + ": BEGIN") |
| 199 | test(&s) |
| 200 | }, SpecTimeout(SuiteTimeout)) |
| 201 | } |
| 202 | } |
| 203 | }) |