diff --git a/pkg/testcore/suites/functional-suites.go b/pkg/testcore/suites/functional-suites.go index 0a00150..b2ff5b4 100644 --- a/pkg/testcore/suites/functional-suites.go +++ b/pkg/testcore/suites/functional-suites.go @@ -840,18 +840,28 @@ type CapacityTrackingSuite struct { // Run runs storage capacity tracking test suite func (cts *CapacityTrackingSuite) Run(ctx context.Context, storageClass string, clients *k8sclient.Clients) (delFunc func() error, e error) { - // Get unique topology count from csinode - topologiesCount, err := getTopologyCount() - if err != nil { - return delFunc, err - } - log.Infof("Found %s topology segment(s) in csinode", color.HiYellowString(strconv.Itoa(topologiesCount))) storageClass = cts.StorageClass sc := clients.SCClient.Get(ctx, storageClass) if sc.HasError() { return delFunc, sc.GetError() } + // Get unique topology count from csinode + // Get topology keys to filter for when retrieving topology count + topologyKeys := []string{} + + if len(sc.Object.AllowedTopologies) > 0 { + matchLabelExpressions := sc.Object.AllowedTopologies[0].MatchLabelExpressions + for _, exp := range matchLabelExpressions { + topologyKeys = append(topologyKeys, exp.Key) + } + } + topologiesCount, err := getTopologyCount(topologyKeys) + if err != nil { + return delFunc, err + } + log.Infof("Found %s topology segment(s) in csinode", color.HiYellowString(strconv.Itoa(topologiesCount))) + if cts.Image == "" { cts.Image = "quay.io/centos/centos:latest" log.Infof("Using default image: %s", cts.Image) @@ -939,14 +949,17 @@ func (cts *CapacityTrackingSuite) Run(ctx context.Context, storageClass string, return delFunc, nil } -func getTopologyCount() (int, error) { +func getTopologyCount(topologyKeys []string) (int, error) { exe := []string{"bash", "-c", "kubectl describe csinode | grep 'Topology Keys'"} str, err := FindDriverLogs(exe) - if err != nil { + if len(str) == 0 || err != nil { return 0, err } topologies := strings.Split(strings.TrimSpace(strings.ReplaceAll(str, "Topology Keys:", "")), "\n") topologies = removeDuplicates(topologies) + if len(topologyKeys) > 0 { + topologies = filterArrayForMatches(topologies, topologyKeys) + } topologiesCount := len(topologies) return topologiesCount, nil } @@ -981,6 +994,20 @@ func removeDuplicates(strSlice []string) []string { return list } +func filterArrayForMatches(listToFilter []string, filterValues []string) []string { + filteredList := []string{} + for _, value := range listToFilter { + for _, key := range filterValues { + if strings.Contains(value, key) { + filteredList = append(filteredList, value) + break + } + } + } + + return filteredList +} + // GetName returns storage capacity tracking suite name func (cts *CapacityTrackingSuite) GetName() string { return "CapacityTrackingSuite" diff --git a/pkg/testcore/suites/functional-suites_test.go b/pkg/testcore/suites/functional-suites_test.go new file mode 100644 index 0000000..28801ae --- /dev/null +++ b/pkg/testcore/suites/functional-suites_test.go @@ -0,0 +1,51 @@ +/* + * + * Copyright © 2025 Dell Inc. or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package suites + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetTopologyCount(t *testing.T) { + // Test case: Empty topology keys + FindDriverLogs = func(_ []string) (string, error) { + return "", nil + } + topologyCount, err := getTopologyCount([]string{}) + assert.NoError(t, err) + assert.Equal(t, 0, topologyCount) + + // Test case: Non-empty topology keys + + FindDriverLogs = func(_ []string) (string, error) { + keys := "Topology Keys: [csi-powerstore.dellemc.com/1.2.3.4-iscsi csi-powerstore.dellemc.com/1.2.3.4-nfs]" + return keys, nil + } + topologyCount, err = getTopologyCount([]string{"csi-powerstore.dellemc.com/1.2.3.4-iscsi"}) + assert.NoError(t, err) + assert.Equal(t, 1, topologyCount) + + // Test case: Error in FindDriverLogs + FindDriverLogs = func(_ []string) (string, error) { + return "", errors.New("error in FindDriverLogs") + } + topologyCount, err = getTopologyCount([]string{}) + assert.Error(t, err) + assert.Equal(t, 0, topologyCount) +} diff --git a/pkg/testcore/suites/perf-suites.go b/pkg/testcore/suites/perf-suites.go index 8dafd19..96a3909 100644 --- a/pkg/testcore/suites/perf-suites.go +++ b/pkg/testcore/suites/perf-suites.go @@ -2240,7 +2240,7 @@ type VolumeHealthMetricsSuite struct { } // FindDriverLogs executes command and returns the output -func FindDriverLogs(command []string) (string, error) { +var FindDriverLogs = func(command []string) (string, error) { cmd := exec.Command(command[0], command[1:]...) // #nosec G204 output, err := cmd.Output() if err != nil {