From 0552dbce85961a6530f68d0f94213847e3de0c3d 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/exofixtures/kafka.go | 2 +- pkg/exofixtures/mysql.go | 2 +- pkg/exofixtures/opensearch.go | 2 +- pkg/exofixtures/postgres.go | 2 +- pkg/exofixtures/redis.go | 2 +- pkg/exoscale/dbaas.go | 9 ++++--- pkg/exoscale/dbaas_test.go | 49 ++++++++++++++++++++--------------- 7 files changed, 38 insertions(+), 30 deletions(-) diff --git a/pkg/exofixtures/kafka.go b/pkg/exofixtures/kafka.go index f9be752..c8f9332 100644 --- a/pkg/exofixtures/kafka.go +++ b/pkg/exofixtures/kafka.go @@ -7,7 +7,7 @@ import ( ) // KafkaDBaaSType represents kafka DBaaS type -const KafkaDBaaSType ObjectType = "appcat_kafka" +const KafkaDBaaSType ObjectType = "kafka" // Available plans for Kafka var kafkaProductDBaaS = []productDBaaS{ diff --git a/pkg/exofixtures/mysql.go b/pkg/exofixtures/mysql.go index 3cbbae2..2bcc9e3 100644 --- a/pkg/exofixtures/mysql.go +++ b/pkg/exofixtures/mysql.go @@ -7,7 +7,7 @@ import ( ) // MysqlDBaaSType represents mysql DBaaS type -const MysqlDBaaSType ObjectType = "appcat_mysql" +const MysqlDBaaSType ObjectType = "mysql" // Available plans for MySQL var mysqlProductDBaaS = []productDBaaS{ diff --git a/pkg/exofixtures/opensearch.go b/pkg/exofixtures/opensearch.go index 28f07d6..03b7cb2 100644 --- a/pkg/exofixtures/opensearch.go +++ b/pkg/exofixtures/opensearch.go @@ -7,7 +7,7 @@ import ( ) // OpensearchDBaaSType represents opensearch DBaaS type -const OpensearchDBaaSType ObjectType = "appcat_opensearch" +const OpensearchDBaaSType ObjectType = "opesearch" // Available plans for OpenSearch var opensearchProductDBaaS = []productDBaaS{ diff --git a/pkg/exofixtures/postgres.go b/pkg/exofixtures/postgres.go index 253c730..4cb20b6 100644 --- a/pkg/exofixtures/postgres.go +++ b/pkg/exofixtures/postgres.go @@ -7,7 +7,7 @@ import ( ) // PostgresDBaaSType represents postgres DBaaS type -const PostgresDBaaSType ObjectType = "appcat_postgres" +const PostgresDBaaSType ObjectType = "pg" // Available plans for PostgreSQL var postgresProductDBaaS = []productDBaaS{ diff --git a/pkg/exofixtures/redis.go b/pkg/exofixtures/redis.go index b03e2ca..79980fa 100644 --- a/pkg/exofixtures/redis.go +++ b/pkg/exofixtures/redis.go @@ -7,7 +7,7 @@ import ( ) // RedisDBaaSType represents redis DBaaS type -const RedisDBaaSType ObjectType = "appcat_redis" +const RedisDBaaSType ObjectType = "redis" // Available plans for Redis var redisProductDBaaS = []productDBaaS{ diff --git a/pkg/exoscale/dbaas.go b/pkg/exoscale/dbaas.go index 7f9ddd8..fb4ffb1 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" "github.com/vshn/billing-collector-cloudservices/pkg/controlAPI" "github.com/vshn/billing-collector-cloudservices/pkg/kubernetes" @@ -12,7 +14,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" @@ -95,7 +96,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 @@ -178,8 +179,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") diff --git a/pkg/exoscale/dbaas_test.go b/pkg/exoscale/dbaas_test.go index 914f1ae..c025b7a 100644 --- a/pkg/exoscale/dbaas_test.go +++ b/pkg/exoscale/dbaas_test.go @@ -9,47 +9,48 @@ 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") 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", + ProductID: "appcat-exoscale-dbaas-pg-hobbyist-2", + InstanceID: "ch-gva-2/postgres-abc", + ItemDescription: "Exoscale DBaaS PostgreSQL", + ItemGroupDescription: "APPUiO Managed - Zone: c-test1 / Namespace: vshn-xyz", SalesOrder: "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()), + From: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()).In(time.UTC), + To: time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+1, 0, 0, 0, now.Location()).In(time.UTC), }, } record2 := odoo.OdooMeteredBillingRecord{ - ProductID: "appcat-exoscale-dbaas-appcat_postgres-business-128", - InstanceID: "postgres-def", ItemDescription: "Exoscale DBaaS", - ItemGroupDescription: "AppCat Exoscale DBaaS", + ProductID: "appcat-exoscale-dbaas-pg-business-128", + InstanceID: "ch-gva-2/postgres-def", + ItemDescription: "Exoscale DBaaS PostgreSQL", + ItemGroupDescription: "APPUiO Managed - Zone: c-test1 / Namespace: vshn-uvw", SalesOrder: "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()), + From: time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()).In(time.UTC), + To: time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+1, 0, 0, 0, now.Location()).In(time.UTC), }, } 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{ @@ -58,12 +59,14 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) { DBName: "postgres-abc", Namespace: "vshn-xyz", Zone: "ch-gva-2", + Kind: "PostgreSQLList", }, { Organization: "org2", DBName: "postgres-def", - Namespace: "vshn-abc", + Namespace: "vshn-uvw", Zone: "ch-gva-2", + Kind: "PostgreSQLList", }, }, exoscaleDBaaS: []*egoscale.DatabaseService{ @@ -78,7 +81,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{ @@ -87,12 +90,14 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) { DBName: "postgres-abc", Namespace: "vshn-xyz", Zone: "ch-gva-2", + Kind: "PostgreSQLList", }, { Organization: "org2", DBName: "postgres-def", Namespace: "vshn-abc", Zone: "ch-gva-2", + Kind: "PostgreSQLList", }, }, exoscaleDBaaS: []*egoscale.DatabaseService{ @@ -106,14 +111,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) }) } }