Skip to content

Commit

Permalink
Merge pull request #2343 from kubernetes-sigs/add-ut
Browse files Browse the repository at this point in the history
test: add unit test
  • Loading branch information
andyzhangx authored Jan 16, 2025
2 parents 5214335 + 0f007c4 commit cdb292c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- name: Send coverage
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=profile.cov -service=github -ignore=./pkg/azurefile/azurefile_interface_mock.go,./pkg/azurefile/directvolume_mock.go,./pkg/azurefile/directvolume_interface.go
run: goveralls -coverprofile=profile.cov -service=github -ignore=pkg/azurefile/azurefile_interface_mock.go,pkg/azurefile/directvolume_mock.go,pkg/azurefile/directvolume_interface.go,pkg/azurefile/resolver_interface.go
39 changes: 39 additions & 0 deletions pkg/azurefile/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
fake "k8s.io/client-go/kubernetes/fake"
utiltesting "k8s.io/client-go/util/testing"
"k8s.io/kubernetes/pkg/volume"
azureconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
)

Expand Down Expand Up @@ -909,3 +910,41 @@ func TestGetBackOff(t *testing.T) {
}
}
}

func TestVolumeMounter(t *testing.T) {
path := "/mnt/data"
attributes := volume.Attributes{}

mounter := &VolumeMounter{
path: path,
attributes: attributes,
}

if mounter.GetPath() != path {
t.Errorf("Expected path %s, but got %s", path, mounter.GetPath())
}

if mounter.GetAttributes() != attributes {
t.Errorf("Expected attributes %v, but got %v", attributes, mounter.GetAttributes())
}

if err := mounter.CanMount(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
t.Errorf("Unexpected error: %v", err)
}

if err := mounter.SetUpAt("", volume.MounterArgs{}); err != nil {
t.Errorf("Unexpected error: %v", err)
}

metrics, err := mounter.GetMetrics()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if metrics != nil {
t.Errorf("Expected nil metrics, but got %v", metrics)
}
}
17 changes: 1 addition & 16 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const (
AzcopyJobCompleted AzcopyJobState = "Completed"
)

// control the number of concurrent powershell commands running on Windows node
var powershellCmdSem = make(chan struct{}, 3)

// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB
// in the unit of Bytes
func RoundUpBytes(volumeSizeBytes int64) int64 {
Expand Down Expand Up @@ -78,23 +75,11 @@ func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
return roundedUp
}

func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
// acquire a semaphore to limit the number of concurrent operations
powershellCmdSem <- struct{}{}
defer func() { <-powershellCmdSem }()

cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)
cmd.Env = append(os.Environ(), envs...)
klog.V(6).Infof("Executing command: %q", cmd.String())
return cmd.CombinedOutput()
}

type EXEC interface {
RunCommand(string, []string) (string, error)
}

type ExecCommand struct {
}
type ExecCommand struct{}

func (ec *ExecCommand) RunCommand(cmdStr string, authEnv []string) (string, error) {
cmd := exec.Command("sh", "-c", cmdStr)
Expand Down
22 changes: 22 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package util
import (
"fmt"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -313,3 +314,24 @@ func TestWaitUntilTimeout(t *testing.T) {
}
}
}

func TestGenerateVolumeName(t *testing.T) {
// Normal operation, no truncate
v1 := GenerateVolumeName("kubernetes", "pv-cinder-abcde", 255)
if v1 != "kubernetes-dynamic-pv-cinder-abcde" {
t.Errorf("Expected kubernetes-dynamic-pv-cinder-abcde, got %s", v1)
}
// Truncate trailing "6789-dynamic"
prefix := strings.Repeat("0123456789", 9) // 90 characters prefix + 8 chars. of "-dynamic"
v2 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
expect := prefix[:84] + "-pv-cinder-abcde"
if v2 != expect {
t.Errorf("Expected %s, got %s", expect, v2)
}
// Truncate really long cluster name
prefix = strings.Repeat("0123456789", 1000) // 10000 characters prefix
v3 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
if v3 != expect {
t.Errorf("Expected %s, got %s", expect, v3)
}
}
41 changes: 41 additions & 0 deletions pkg/util/util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build windows
// +build windows

/*
Copyright 2025 The Kubernetes Authors.
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 util

import (
"os"
"os/exec"

"k8s.io/klog/v2"
)

// control the number of concurrent powershell commands running on Windows node
var powershellCmdSem = make(chan struct{}, 3)

func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
// acquire a semaphore to limit the number of concurrent operations
powershellCmdSem <- struct{}{}
defer func() { <-powershellCmdSem }()

cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)
cmd.Env = append(os.Environ(), envs...)
klog.V(6).Infof("Executing command: %q", cmd.String())
return cmd.CombinedOutput()
}

0 comments on commit cdb292c

Please sign in to comment.