From 4322919a22e8a0cb387685ce0c49897dbb06d94c Mon Sep 17 00:00:00 2001 From: Nicolas Bigler Date: Thu, 14 Dec 2023 15:53:29 +0100 Subject: [PATCH] Fix tests Signed-off-by: Nicolas Bigler --- pkg/exoscale/dbaas.go | 11 ++++--- pkg/exoscale/dbaas_test.go | 64 +++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/pkg/exoscale/dbaas.go b/pkg/exoscale/dbaas.go index ac4b7e3..a6b1d27 100644 --- a/pkg/exoscale/dbaas.go +++ b/pkg/exoscale/dbaas.go @@ -3,6 +3,8 @@ package exoscale import ( "context" "fmt" + "time" + egoscale "github.com/exoscale/egoscale/v2" apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" "github.com/vshn/billing-collector-cloudservices/pkg/kubernetes" @@ -13,7 +15,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" k8s "sigs.k8s.io/controller-runtime/pkg/client" - "time" ) const productIdPrefix = "appcat-exoscale-dbaas" @@ -88,7 +89,7 @@ func (ds *DBaaS) GetMetrics(ctx context.Context) ([]odoo.OdooMeteredBillingRecor return nil, fmt.Errorf("fetchDBaaSUsage: %w", err) } - return ds.aggregateDBaaS(ctx, ds.promClient, usage, detail) + return ds.AggregateDBaaS(ctx, usage, detail) } // fetchManagedDBaaSAndNamespaces fetches instances and namespaces from kubernetes cluster @@ -170,8 +171,8 @@ func (ds *DBaaS) fetchDBaaSUsage(ctx context.Context) ([]*egoscale.DatabaseServi return databaseServices, nil } -// aggregateDBaaS aggregates DBaaS services by namespaces and plan -func (ds *DBaaS) aggregateDBaaS(ctx context.Context, promClient apiv1.API, exoscaleDBaaS []*egoscale.DatabaseService, dbaasDetails []Detail) ([]odoo.OdooMeteredBillingRecord, error) { +// AggregateDBaaS aggregates DBaaS services by namespaces and plan +func (ds *DBaaS) AggregateDBaaS(ctx context.Context, exoscaleDBaaS []*egoscale.DatabaseService, dbaasDetails []Detail) ([]odoo.OdooMeteredBillingRecord, error) { logger := log.Logger(ctx) logger.Info("Aggregating DBaaS instances by namespace and plan") @@ -199,7 +200,7 @@ func (ds *DBaaS) aggregateDBaaS(ctx context.Context, promClient apiv1.API, exosc logger.V(1).Info("Found exoscale dbaas usage", "instance", dbaasUsage.Name, "instance created", dbaasUsage.CreatedAt) if ds.salesOrderId == "" { - ds.salesOrderId, err = prom.GetSalesOrderId(ctx, promClient, dbaasDetail.Organization) + ds.salesOrderId, err = prom.GetSalesOrderId(ctx, ds.promClient, dbaasDetail.Organization) if err != nil { logger.Error(err, "Unable to sync DBaaS, cannot get salesOrderId", "namespace", dbaasDetail.Namespace) continue diff --git a/pkg/exoscale/dbaas_test.go b/pkg/exoscale/dbaas_test.go index a744f16..299af7e 100644 --- a/pkg/exoscale/dbaas_test.go +++ b/pkg/exoscale/dbaas_test.go @@ -9,41 +9,47 @@ import ( "github.com/stretchr/testify/assert" "github.com/vshn/billing-collector-cloudservices/pkg/exofixtures" "github.com/vshn/billing-collector-cloudservices/pkg/log" + "github.com/vshn/billing-collector-cloudservices/pkg/odoo" ) func TestDBaaS_aggregatedDBaaS(t *testing.T) { ctx := getTestContext(t) - key1 := NewKey("vshn-xyz", "hobbyist-2", string(exofixtures.PostgresDBaaSType)) - key2 := NewKey("vshn-abc", "business-128", string(exofixtures.PostgresDBaaSType)) + location, _ := time.LoadLocation("Europe/Zurich") - expectedAggregatedDBaaS := map[Key]Aggregated{ - key1: { - Key: key1, - Value: 1, - Source: &exofixtures.DBaaSSourceString{ - Query: "", - Organization: "org1", - Namespace: "vshn-xyz", - Plan: "hobbyist-2", - }, + now := time.Now().In(location) + record1 := odoo.OdooMeteredBillingRecord{ + ProductID: "appcat-exoscale-dbaas-appcat_postgres-hobbyist-2", + InstanceID: "postgres-abc", + ItemDescription: "Exoscale DBaaS", + ItemGroupDescription: "AppCat Exoscale DBaaS", + SalesOrderID: "1234", + UnitID: "", + ConsumedUnits: 1, + TimeRange: odoo.TimeRange{ + From: time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-1, 0, 0, 0, now.Location()), + To: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()), }, - key2: { - Key: key2, - Value: 1, - Source: &exofixtures.DBaaSSourceString{ - Query: "", - Organization: "org2", - Namespace: "vshn-abc", - Plan: "business-128", - }, + } + record2 := odoo.OdooMeteredBillingRecord{ + ProductID: "appcat-exoscale-dbaas-appcat_postgres-business-128", + InstanceID: "postgres-def", ItemDescription: "Exoscale DBaaS", + ItemGroupDescription: "AppCat Exoscale DBaaS", + SalesOrderID: "1234", + UnitID: "", + ConsumedUnits: 1, + TimeRange: odoo.TimeRange{ + From: time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-1, 0, 0, 0, now.Location()), + To: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()), }, } + expectedAggregatedOdooRecords := []odoo.OdooMeteredBillingRecord{record1, record2} + tests := map[string]struct { - dbaasDetails []Detail - exoscaleDBaaS []*egoscale.DatabaseService - expectedAggregatedDBaaS map[Key]Aggregated + dbaasDetails []Detail + exoscaleDBaaS []*egoscale.DatabaseService + expectedAggregatedOdooRecords []odoo.OdooMeteredBillingRecord }{ "given DBaaS details and Exoscale DBaasS, we should get the ExpectedAggregatedDBaasS": { dbaasDetails: []Detail{ @@ -72,7 +78,7 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) { Plan: strToPointer("business-128"), }, }, - expectedAggregatedDBaaS: expectedAggregatedDBaaS, + expectedAggregatedOdooRecords: expectedAggregatedOdooRecords, }, "given DBaaS details and different names in Exoscale DBaasS, we should not get the ExpectedAggregatedDBaasS": { dbaasDetails: []Detail{ @@ -100,14 +106,16 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) { }, }, - expectedAggregatedDBaaS: map[Key]Aggregated{}, + expectedAggregatedOdooRecords: []odoo.OdooMeteredBillingRecord{}, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - aggregatedDBaaS := aggregateDBaaS(ctx, tc.exoscaleDBaaS, tc.dbaasDetails) - assert.Equal(t, tc.expectedAggregatedDBaaS, aggregatedDBaaS) + ds, _ := NewDBaaS(nil, nil, nil, 1, "1234", "c-test1", map[string]string{}) + aggregatedOdooRecords, err := ds.AggregateDBaaS(ctx, tc.exoscaleDBaaS, tc.dbaasDetails) + assert.NoError(t, err) + assert.Equal(t, tc.expectedAggregatedOdooRecords, aggregatedOdooRecords) }) } }