Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering get topology count for specific sc key bug fix #126

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 © 2024 Dell Inc. or its subsidiaries. All Rights Reserved.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the copyright to 2025. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Updated

*
* 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
Loading