Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Bigler <[email protected]>
  • Loading branch information
TheBigLee committed Dec 19, 2023
1 parent 4c55be9 commit 89ff58c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions pkg/exoscale/dbaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
20 changes: 11 additions & 9 deletions pkg/exoscale/dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ 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{
Expand Down Expand Up @@ -47,9 +47,9 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) {
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{
Expand Down Expand Up @@ -78,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{
Expand Down Expand Up @@ -106,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)
})
}
}
Expand Down

0 comments on commit 89ff58c

Please sign in to comment.