Skip to content

Commit

Permalink
Filtering get topology count for specific sc key bug fix (#126)
Browse files Browse the repository at this point in the history
* Fixing  multi-driver-capacity-tracking and adding UT

* Updated year in copyright section

* Update to fix lint warnings

* gofumpt lint fix

* adjust test

* updating 2024 to 2025 copyright

---------

Co-authored-by: Garg <sakshi_garg1@dell.com>
Co-authored-by: sakshi-garg1 <74704849+sakshi-garg1@users.noreply.github.com>
Co-authored-by: Jooseppi Luna <jooseppi_luna@dell.com>
  • Loading branch information
4 people authored Jan 14, 2025
1 parent 9c2d304 commit e1c6a6f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
43 changes: 35 additions & 8 deletions pkg/testcore/suites/functional-suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
Expand Down
51 changes: 51 additions & 0 deletions pkg/testcore/suites/functional-suites_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion pkg/testcore/suites/perf-suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e1c6a6f

Please sign in to comment.