From c19494ae87b6fa49bebfc49c2b24f7f85dea1c93 Mon Sep 17 00:00:00 2001 From: Anvesh J Date: Wed, 20 Nov 2024 10:15:47 +0530 Subject: [PATCH] sriov: Add WithRdmaMode method --- pkg/sriov/poolconfig.go | 27 +++++++++++++++++++++++++++ pkg/sriov/poolconfig_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/pkg/sriov/poolconfig.go b/pkg/sriov/poolconfig.go index 175f88704..f6b2b67bd 100644 --- a/pkg/sriov/poolconfig.go +++ b/pkg/sriov/poolconfig.go @@ -255,6 +255,33 @@ func (builder *PoolConfigBuilder) WithMaxUnavailable(maxUnavailable intstrutil.I return builder } +// WithRDMAMode method sets rdmaMode to shared/exclusive. +func (builder *PoolConfigBuilder) WithRDMAMode(mode string) *PoolConfigBuilder { + if valid, _ := builder.validate(); !valid { + return builder + } + + if mode == "" { + glog.V(100).Info("PoolConfig RdmaMode cannot be empty") + + builder.errorMsg = "rdmaMode cannot be empty" + + return builder + } + + if mode != "shared" && mode != "exclusive" { + glog.V(100).Info("Invalid RdmaMode. Acceptable values: shared or exclusive") + + builder.errorMsg = "invalid value for rdmaMode. It should be 'shared' or 'exclusive'" + + return builder + } + + builder.Definition.Spec.RdmaMode = mode + + return builder +} + // PullPoolConfig pulls existing SriovNetworkPoolConfig from cluster. func PullPoolConfig(apiClient *clients.Settings, name, nsname string) (*PoolConfigBuilder, error) { glog.V(100).Infof("Pulling existing SriovNetworkPoolConfig name %s under namespace %s from cluster", name, nsname) diff --git a/pkg/sriov/poolconfig_test.go b/pkg/sriov/poolconfig_test.go index 7e820431b..528cd759d 100644 --- a/pkg/sriov/poolconfig_test.go +++ b/pkg/sriov/poolconfig_test.go @@ -240,6 +240,40 @@ func TestWithMaxUnavailable(t *testing.T) { } } +func TestWithRDMAMode(t *testing.T) { + testCases := []struct { + RdmaMode string + ExpectedError string + }{ + { + RdmaMode: "shared", + ExpectedError: "", + }, + { + RdmaMode: "exclusive", + ExpectedError: "", + }, + { + RdmaMode: "", + ExpectedError: "rdmaMode cannot be empty", + }, + { + RdmaMode: "none", + ExpectedError: "invalid value for rdmaMode. It should be 'shared' or 'exclusive'", + }, + } + + for _, testCase := range testCases { + testSettings := buildTestPoolConfigClientWithDummyObject() + testBuilder := buildValidPoolConfigTestBuilder(testSettings).WithRDMAMode(testCase.RdmaMode) + assert.Equal(t, testCase.ExpectedError, testBuilder.errorMsg) + + if testCase.ExpectedError == "" { + assert.Equal(t, testBuilder.Definition.Spec.RdmaMode, testCase.RdmaMode) + } + } +} + func TestPullPoolConfig(t *testing.T) { testCases := []struct { poolConfigName string