|
|
@@ -7,11 +7,12 @@ import (
|
|
|
"testing"
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/profiles/2020-09-01/commerce/mgmt/commerce"
|
|
|
+ "github.com/opencost/opencost/pkg/cloud/types"
|
|
|
"github.com/stretchr/testify/require"
|
|
|
)
|
|
|
|
|
|
func TestDownloader(t *testing.T) {
|
|
|
- d := Downloader[fakePricing]{
|
|
|
+ d := PriceSheetDownloader{
|
|
|
TenantID: "test-tenant-id",
|
|
|
ClientID: "test-client-id",
|
|
|
ClientSecret: "test-client-secret",
|
|
|
@@ -26,11 +27,11 @@ func TestDownloader(t *testing.T) {
|
|
|
|
|
|
// Units and prices are normalised.
|
|
|
// Info for saving plans and other offers is skipped.
|
|
|
- expected := map[string]*fakePricing{
|
|
|
- "DC96as_v4": {price: "10.505", unit: "1 Hour"},
|
|
|
- "DC2as_v4": {price: "0.219", unit: "1 Hour"},
|
|
|
- "VM1": {price: "1.0", unit: "1 Hour"},
|
|
|
- "VM2": {price: "2.0", unit: "1 Hour"},
|
|
|
+ expected := map[string]*AzurePricing{
|
|
|
+ "DC96as_v4 1 Hour": {Node: &types.Node{Cost: "10.505"}},
|
|
|
+ "DC2as_v4 1 Hour": {Node: &types.Node{Cost: "0.219"}},
|
|
|
+ "VM1 1 Hour": {Node: &types.Node{Cost: "1.0"}},
|
|
|
+ "VM2 1 Hour": {Node: &types.Node{Cost: "2.0"}},
|
|
|
}
|
|
|
require.Equal(t, expected, results)
|
|
|
})
|
|
|
@@ -48,13 +49,13 @@ func TestDownloader(t *testing.T) {
|
|
|
})
|
|
|
|
|
|
t.Run("no matching prices", func(t *testing.T) {
|
|
|
- d := Downloader[fakePricing]{
|
|
|
+ d := PriceSheetDownloader{
|
|
|
TenantID: "test-tenant-id",
|
|
|
ClientID: "test-client-id",
|
|
|
ClientSecret: "test-client-secret",
|
|
|
BillingAccount: "test-billing-account",
|
|
|
OfferID: "my-offer-id",
|
|
|
- ConvertMeterInfo: func(commerce.MeterInfo) (map[string]*fakePricing, error) {
|
|
|
+ ConvertMeterInfo: func(commerce.MeterInfo) (map[string]*AzurePricing, error) {
|
|
|
return nil, nil
|
|
|
},
|
|
|
}
|
|
|
@@ -63,29 +64,26 @@ func TestDownloader(t *testing.T) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-func convertMeter(info commerce.MeterInfo) (map[string]*fakePricing, error) {
|
|
|
+func convertMeter(info commerce.MeterInfo) (map[string]*AzurePricing, error) {
|
|
|
switch *info.MeterName {
|
|
|
case "skip-this":
|
|
|
return nil, nil
|
|
|
case "multiple-prices":
|
|
|
- return map[string]*fakePricing{
|
|
|
- "VM1": {price: "1.0", unit: "1 Hour"},
|
|
|
- "VM2": {price: "2.0", unit: "1 Hour"},
|
|
|
+ return map[string]*AzurePricing{
|
|
|
+ "VM1 1 Hour": {Node: &types.Node{Cost: "1.0"}},
|
|
|
+ "VM2 1 Hour": {Node: &types.Node{Cost: "2.0"}},
|
|
|
}, nil
|
|
|
case "error":
|
|
|
return nil, fmt.Errorf("there was an error handling this row!")
|
|
|
default:
|
|
|
- return map[string]*fakePricing{
|
|
|
- *info.MeterName: {price: fmt.Sprintf("%0.3f", *info.MeterRates["0"]), unit: *info.Unit},
|
|
|
+ return map[string]*AzurePricing{
|
|
|
+ *info.MeterName + " " + *info.Unit: {
|
|
|
+ Node: &types.Node{Cost: fmt.Sprintf("%0.3f", *info.MeterRates["0"])},
|
|
|
+ },
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type fakePricing struct {
|
|
|
- price string
|
|
|
- unit string
|
|
|
-}
|
|
|
-
|
|
|
const pricesheetData = `Price Sheet Report for billing period - 202304
|
|
|
|
|
|
Meter ID,Meter name,Meter category,Meter sub-category,Meter region,Unit,Unit of measure,Part number,Unit price,Currency code,Included quantity,Offer Id,Term,Price type
|