ソースを参照

Add Azure Disk Sku Nil Check (#3165)

Signed-off-by: HMetcalfeW <106991365+HMetcalfeW@users.noreply.github.com>
Hunter Metcalfe 11 ヶ月 前
コミット
81f63656e0
3 ファイル変更16 行追加0 行削除
  1. 1 0
      justfile
  2. 5 0
      pkg/cloud/azure/provider.go
  3. 10 0
      pkg/cloud/azure/provider_test.go

+ 1 - 0
justfile

@@ -24,6 +24,7 @@ test-collector-source:
 # run the opencost unit tests 
 test-opencost: 
     {{commonenv}} go test ./... -coverprofile=coverage.out
+    {{commonenv}} go tool cover -html=coverage.out -o coverage.html
     {{commonenv}} go vet ./...
 
 # Run unit tests, merge coverage reports, remove old reports 

+ 5 - 0
pkg/cloud/azure/provider.go

@@ -1450,6 +1450,11 @@ func (az *Azure) findCostForDisk(d *compute.Disk) (float64, error) {
 	if d == nil {
 		return 0.0, fmt.Errorf("disk is empty")
 	}
+
+	if d.Sku == nil {
+		return 0.0, fmt.Errorf("disk sku is nil")
+	}
+
 	storageClass := string(d.Sku.Name)
 	if strings.EqualFold(storageClass, "Premium_LRS") {
 		storageClass = AzureDiskPremiumSSDStorageClass

+ 10 - 0
pkg/cloud/azure/provider_test.go

@@ -223,6 +223,16 @@ func TestAzure_findCostForDisk(t *testing.T) {
 			730.0,
 			nil,
 		},
+		{
+			"nil sku",
+			&compute.Disk{
+				Location:       nil,
+				Sku:            nil,
+				DiskProperties: nil,
+			},
+			0.0,
+			fmt.Errorf("disk sku is nil"),
+		},
 	}
 
 	for _, tc := range testCases {