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

test: add UT for gen-disk-skus-map_test #2781

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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)
}
}
Loading