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: refine UT for gen-disk-skus-map_test #2787

Merged
merged 1 commit into from
Jan 6, 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
27 changes: 16 additions & 11 deletions pkg/tool/gen-disk-skus-map.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
UncachedDiskBytesPerSecondCapability = "uncacheddiskbytespersecond"
MaxDataDiskCountCapability = "maxdatadiskcount"
VCPUsCapability = "vcpus"
SkusFullfilePath = "skus-full.json"
)

func init() {
Expand Down Expand Up @@ -87,21 +88,20 @@ import (
nodeInfo := ` "%s": NodeInfo{SkuName: "%s", MaxDataDiskCount: %s, VCpus: %s, MaxBurstIops: %s, MaxIops: %s, MaxBwMbps: %s, MaxBurstBwMbps: %s},`
nodeMapEnd := " }"

skusFullfilePath := "skus-full.json"
defer os.Remove(skusFullfilePath)
defer os.Remove(SkusFullfilePath)
skusFilePath := "pkg/azuredisk/azure_skus_map.go"

skuMap := map[string]bool{}

var resources []*armcompute.ResourceSKU

if err := getAllSkus(skusFullfilePath); err != nil {
if err := getAllSkus(); err != nil {
klog.Errorf("Could not get skus. Error: %v", err)
}

diskSkusFullJSON, err := os.Open(skusFullfilePath)
diskSkusFullJSON, err := os.Open(SkusFullfilePath)
if err != nil {
klog.Errorf("Could not read file. Error: %v, FilePath: %s", err, skusFullfilePath)
klog.Errorf("Could not read file. Error: %v, FilePath: %s", err, SkusFullfilePath)
return
}
defer diskSkusFullJSON.Close()
Expand All @@ -110,7 +110,7 @@ import (

err = json.Unmarshal([]byte(byteValue), &resources)
if err != nil {
klog.Errorf("Could not parse json file file. Error: %v, FilePath: %s", err, skusFullfilePath)
klog.Errorf("Could not parse json file file. Error: %v, FilePath: %s", err, SkusFullfilePath)
return
}

Expand Down Expand Up @@ -145,7 +145,6 @@ import (
exit(1)
}
} else if resType == "virtualmachines" {

nodeInfo := optimization.NodeInfo{}
nodeInfo.SkuName = *sku.Name
err = populateNodeCapabilities(sku, &nodeInfo)
Expand Down Expand Up @@ -215,13 +214,19 @@ func formatInt(value int) string {

return strconv.Itoa(value)
}
func getAllSkus(filePath string) (err error) {
klog.V(2).Infof("Getting skus and writing to %s", filePath)
func getAllSkus() (err error) {
if os.Getenv("TEST_SCENARIO") == "true" {
if _, err := os.Stat("skus-sample.json"); err == nil {
SkusFullfilePath = "skus-sample.json"
return nil
}
}
klog.V(2).Infof("Getting skus and writing to %s", SkusFullfilePath)
cmd := exec.Command("az", "vm", "list-skus")
outfile, err := os.Create(filePath)
outfile, err := os.Create(SkusFullfilePath)
var errorBuf bytes.Buffer
if err != nil {
klog.Errorf("Could dnot create file. Error: %v File: %s", err, filePath)
klog.Errorf("Could not create file. Error: %v File: %s", err, SkusFullfilePath)
return err
}
defer outfile.Close()
Expand Down
4 changes: 4 additions & 0 deletions pkg/tool/gen-disk-skus-map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
)

func TestMain(t *testing.T) {
// Set environment variable for test scenario
os.Setenv("TEST_SCENARIO", "true")
defer os.Unsetenv("TEST_SCENARIO")

// Capture stdout
old := os.Stdout
_, w, _ := os.Pipe()
Expand Down
Loading
Loading