Skip to content

Commit

Permalink
Merge pull request #2781 from Zhupku/user/mengzezhu/ut2
Browse files Browse the repository at this point in the history
test: add UT for gen-disk-skus-map_test
  • Loading branch information
andyzhangx authored Jan 2, 2025
2 parents ac0cd33 + bdd1e59 commit 0de3521
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/tool/gen-disk-skus-map.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func init() {
klog.InitFlags(nil)
}

// exit is a separate function to handle program termination
var exit = func(code int) {
os.Exit(code)
}

func main() {

boilerPlate := `/*
Expand Down Expand Up @@ -137,7 +142,7 @@ import (
diskSkuInfoMap[account][diskSize], err = getDiskCapabilities(sku)
if err != nil {
klog.Errorf("populateSkuMap: Failed to get disk capabilities for disk %s %s %s. Error: %v", *sku.Name, *sku.Size, *sku.Tier, err)
os.Exit(1)
exit(1)
}
} else if resType == "virtualmachines" {

Expand All @@ -146,7 +151,7 @@ import (
err = populateNodeCapabilities(sku, &nodeInfo)
if err != nil {
klog.Errorf("populateSkuMap: Failed to populate node capabilities. Error: %v", err)
os.Exit(1)
exit(1)
}
vmSkuInfoMap[strings.ToLower(*sku.Name)] = nodeInfo
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/tool/gen-disk-skus-map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2024 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 main

import (
"os"
"testing"
)

func TestMain(t *testing.T) {
// Capture stdout
old := os.Stdout
_, w, _ := os.Pipe()
os.Stdout = w

// Replace exit function with mock function
var exitCode int
exit = func(code int) {
exitCode = code
}

// Call main function
main()

// Restore stdout
w.Close()
os.Stdout = old
exit = func(code int) {
os.Exit(code)
}

if exitCode != 0 {
t.Errorf("Expected exit code 0, but got %d", exitCode)
}
}

0 comments on commit 0de3521

Please sign in to comment.