Skip to content

Commit 0f007c4

Browse files
committed
test: add unit test
add ut
1 parent 5214335 commit 0f007c4

File tree

5 files changed

+104
-17
lines changed

5 files changed

+104
-17
lines changed

.github/workflows/linux.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ jobs:
3333
- name: Send coverage
3434
env:
3535
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36-
run: goveralls -coverprofile=profile.cov -service=github -ignore=./pkg/azurefile/azurefile_interface_mock.go,./pkg/azurefile/directvolume_mock.go,./pkg/azurefile/directvolume_interface.go
36+
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

pkg/azurefile/utils_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/apimachinery/pkg/util/wait"
3333
fake "k8s.io/client-go/kubernetes/fake"
3434
utiltesting "k8s.io/client-go/util/testing"
35+
"k8s.io/kubernetes/pkg/volume"
3536
azureconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
3637
)
3738

@@ -909,3 +910,41 @@ func TestGetBackOff(t *testing.T) {
909910
}
910911
}
911912
}
913+
914+
func TestVolumeMounter(t *testing.T) {
915+
path := "/mnt/data"
916+
attributes := volume.Attributes{}
917+
918+
mounter := &VolumeMounter{
919+
path: path,
920+
attributes: attributes,
921+
}
922+
923+
if mounter.GetPath() != path {
924+
t.Errorf("Expected path %s, but got %s", path, mounter.GetPath())
925+
}
926+
927+
if mounter.GetAttributes() != attributes {
928+
t.Errorf("Expected attributes %v, but got %v", attributes, mounter.GetAttributes())
929+
}
930+
931+
if err := mounter.CanMount(); err != nil {
932+
t.Errorf("Unexpected error: %v", err)
933+
}
934+
935+
if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
936+
t.Errorf("Unexpected error: %v", err)
937+
}
938+
939+
if err := mounter.SetUpAt("", volume.MounterArgs{}); err != nil {
940+
t.Errorf("Unexpected error: %v", err)
941+
}
942+
943+
metrics, err := mounter.GetMetrics()
944+
if err != nil {
945+
t.Errorf("Unexpected error: %v", err)
946+
}
947+
if metrics != nil {
948+
t.Errorf("Expected nil metrics, but got %v", metrics)
949+
}
950+
}

pkg/util/util.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ const (
4040
AzcopyJobCompleted AzcopyJobState = "Completed"
4141
)
4242

43-
// control the number of concurrent powershell commands running on Windows node
44-
var powershellCmdSem = make(chan struct{}, 3)
45-
4643
// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB
4744
// in the unit of Bytes
4845
func RoundUpBytes(volumeSizeBytes int64) int64 {
@@ -78,23 +75,11 @@ func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
7875
return roundedUp
7976
}
8077

81-
func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
82-
// acquire a semaphore to limit the number of concurrent operations
83-
powershellCmdSem <- struct{}{}
84-
defer func() { <-powershellCmdSem }()
85-
86-
cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)
87-
cmd.Env = append(os.Environ(), envs...)
88-
klog.V(6).Infof("Executing command: %q", cmd.String())
89-
return cmd.CombinedOutput()
90-
}
91-
9278
type EXEC interface {
9379
RunCommand(string, []string) (string, error)
9480
}
9581

96-
type ExecCommand struct {
97-
}
82+
type ExecCommand struct{}
9883

9984
func (ec *ExecCommand) RunCommand(cmdStr string, authEnv []string) (string, error) {
10085
cmd := exec.Command("sh", "-c", cmdStr)

pkg/util/util_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package util
1919
import (
2020
"fmt"
2121
"reflect"
22+
"strings"
2223
"testing"
2324
"time"
2425

@@ -313,3 +314,24 @@ func TestWaitUntilTimeout(t *testing.T) {
313314
}
314315
}
315316
}
317+
318+
func TestGenerateVolumeName(t *testing.T) {
319+
// Normal operation, no truncate
320+
v1 := GenerateVolumeName("kubernetes", "pv-cinder-abcde", 255)
321+
if v1 != "kubernetes-dynamic-pv-cinder-abcde" {
322+
t.Errorf("Expected kubernetes-dynamic-pv-cinder-abcde, got %s", v1)
323+
}
324+
// Truncate trailing "6789-dynamic"
325+
prefix := strings.Repeat("0123456789", 9) // 90 characters prefix + 8 chars. of "-dynamic"
326+
v2 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
327+
expect := prefix[:84] + "-pv-cinder-abcde"
328+
if v2 != expect {
329+
t.Errorf("Expected %s, got %s", expect, v2)
330+
}
331+
// Truncate really long cluster name
332+
prefix = strings.Repeat("0123456789", 1000) // 10000 characters prefix
333+
v3 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
334+
if v3 != expect {
335+
t.Errorf("Expected %s, got %s", expect, v3)
336+
}
337+
}

pkg/util/util_windows.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2025 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package util
21+
22+
import (
23+
"os"
24+
"os/exec"
25+
26+
"k8s.io/klog/v2"
27+
)
28+
29+
// control the number of concurrent powershell commands running on Windows node
30+
var powershellCmdSem = make(chan struct{}, 3)
31+
32+
func RunPowershellCmd(command string, envs ...string) ([]byte, error) {
33+
// acquire a semaphore to limit the number of concurrent operations
34+
powershellCmdSem <- struct{}{}
35+
defer func() { <-powershellCmdSem }()
36+
37+
cmd := exec.Command("powershell", "-Mta", "-NoProfile", "-Command", command)
38+
cmd.Env = append(os.Environ(), envs...)
39+
klog.V(6).Infof("Executing command: %q", cmd.String())
40+
return cmd.CombinedOutput()
41+
}

0 commit comments

Comments
 (0)