Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Jan 15, 2025
1 parent 5214335 commit ac6505a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
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 ac6505a

Please sign in to comment.