Skip to content

Commit

Permalink
APIGOV-28984 Added more unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Kasu committed Nov 13, 2024
1 parent 974d838 commit 058c26d
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 101 deletions.
37 changes: 33 additions & 4 deletions pkg/customunit/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"
"log"
"math/rand/v2"

Check failure on line 7 in pkg/customunit/client_test.go

View workflow job for this annotation

GitHub Actions / test

package math/rand/v2 is not in std (/opt/hostedtoolcache/go/1.21.13/x64/src/math/rand/v2)
"net"
"testing"
"time"

"github.com/Axway/agent-sdk/pkg/agent/cache"
"github.com/Axway/agent-sdk/pkg/amplify/agent/customunits"
Expand All @@ -27,6 +27,33 @@ type fakeCustomUnitMetricReportingServer struct {
customunits.UnimplementedMetricReportingServiceServer
}

func (s *fakeCustomUnitMetricReportingServer) MetricReporting(metricServiceInit *customunits.MetricServiceInit, server customunits.MetricReportingService_MetricReportingServer) error {

apiID, appID, unit := "fsdfsf2342r2ferge", "fsdfsdfsf234235fgdgd", "x-ai-tokens"
count := rand.Int64N(50)
metricReport := &customunits.MetricReport{
ApiService: &customunits.APIServiceLookup{
Type: customunits.APIServiceLookupType_ExternalAPIID,
Value: apiID,
},
ManagedApp: &customunits.AppLookup{
Type: customunits.AppLookupType_ManagedAppID,
Value: appID,
},
PlanUnit: &customunits.UnitLookup{
UnitName: unit,
},
Count: count,
}

err := server.Send(metricReport)
if err != nil {
return err
}

return nil
}

func Test_QuotaEnforcementInfo(t *testing.T) {

ctx := context.Background()
Expand Down Expand Up @@ -75,9 +102,11 @@ func Test_MetricReporting(t *testing.T) {
client, _ := createMRConnection(fakeServer, ctx)
metricReportChan := make(chan *customunits.MetricReport, 100)
go client.StartMetricReporting(metricReportChan)

time.Sleep(5 * time.Second)
client.Stop()
metricReport := <-metricReportChan
assert.NotNil(t, metricReport)
assert.Equal(t, metricReport.ApiService.Value, "fsdfsf2342r2ferge")
assert.Equal(t, metricReport.ManagedApp.Value, "fsdfsdfsf234235fgdgd")
assert.Equal(t, metricReport.PlanUnit.UnitName, "x-ai-tokens")
}

func createMRConnection(fakeServer *fakeCustomUnitMetricReportingServer, _ context.Context) (*customUnitClient, error) {
Expand Down
File renamed without changes.
267 changes: 267 additions & 0 deletions pkg/customunit/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package customunit

import (
"math/rand/v2"
"testing"
"time"

agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
"github.com/Axway/agent-sdk/pkg/amplify/agent/customunits"
v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
defs "github.com/Axway/agent-sdk/pkg/apic/definitions"
prov "github.com/Axway/agent-sdk/pkg/apic/provisioning"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/transaction/models"
"github.com/stretchr/testify/assert"
)

const instRefID = "inst-id-1"
const instRefName = "inst-name-1"
const managedAppRefName = "managed-app-name"

func Test_NewCustomUnitHandler(t *testing.T) {
metricServicesConfigs := config.NewAgentFeaturesConfiguration().GetMetricServicesConfigs()
cm := agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false)
handler := NewCustomUnitHandler(metricServicesConfigs, cm, config.DiscoveryAgent)

assert.NotNil(t, handler)
}

type fakeMetricCollector struct {
}

type mockAgentCache struct {
}

func (c *mockAgentCache) GetAPIServiceWithAPIID(id string) *v1.ResourceInstance {
return &v1.ResourceInstance{
ResourceMeta: v1.ResourceMeta{
Name: "service",
Metadata: v1.Metadata{
ID: "svc-" + "fsdfsf2342r2ferge",
},
SubResources: map[string]interface{}{
defs.XAgentDetails: map[string]interface{}{
defs.AttrExternalAPIID: "fsdfsf2342r2ferge",
defs.AttrExternalAPIPrimaryKey: "primary-" + "fsdfsf2342r2ferge",
defs.AttrExternalAPIName: "test",
},
},
},
}
}

func (c *mockAgentCache) GetAPIServiceWithName(name string) *v1.ResourceInstance {
return &v1.ResourceInstance{
ResourceMeta: v1.ResourceMeta{
Name: "service",
Metadata: v1.Metadata{
ID: "svc-" + "fsdfsf2342r2ferge",
},
SubResources: map[string]interface{}{
defs.XAgentDetails: map[string]interface{}{
defs.AttrExternalAPIID: "fsdfsf2342r2ferge",
defs.AttrExternalAPIPrimaryKey: "primary-" + "fsdfsf2342r2ferge",
defs.AttrExternalAPIName: "test",
},
},
},
}
}

func (c *mockAgentCache) GetAPIServiceInstanceByID(string) (*v1.ResourceInstance, error) {
return &v1.ResourceInstance{
ResourceMeta: v1.ResourceMeta{
Name: "instance",
Metadata: v1.Metadata{
ID: "instance-" + "fsdfsf2342r2ferge",
},
SubResources: map[string]interface{}{
defs.XAgentDetails: map[string]interface{}{
defs.AttrExternalAPIID: "fsdfsf2342r2ferge",
defs.AttrExternalAPIPrimaryKey: "primary-" + "fsdfsf2342r2ferge",
defs.AttrExternalAPIName: "test",
},
},
},
}, nil
}

func (c *mockAgentCache) GetAPIServiceKeys() []string {
keys := make([]string, 0)
return keys
}
func (c *mockAgentCache) GetManagedApplication(string) *v1.ResourceInstance {
return &v1.ResourceInstance{
ResourceMeta: v1.ResourceMeta{
Metadata: v1.Metadata{
ID: "fsdfsdfsf234235fgdgd",
},
Name: "app",
},
}
}
func (c *mockAgentCache) GetManagedApplicationByName(string) *v1.ResourceInstance {
return &v1.ResourceInstance{
ResourceMeta: v1.ResourceMeta{
Metadata: v1.Metadata{
ID: "fsdfsdfsf234235fgdgd",
},
Name: "app",
},
}
}

func (c *fakeMetricCollector) AddCustomMetricDetail(detail models.CustomMetricDetail) {

}

func GetMetricReport() *customunits.MetricReport {
apiID, appID, unit := "fsdfsf2342r2ferge", "fsdfsdfsf234235fgdgd", "x-ai-tokens"
count := rand.Int64N(50)
metricReport := &customunits.MetricReport{
ApiService: &customunits.APIServiceLookup{
Type: customunits.APIServiceLookupType_ExternalAPIID,
Value: apiID,
},
ManagedApp: &customunits.AppLookup{
Type: customunits.AppLookupType_ManagedAppID,
Value: appID,
},
PlanUnit: &customunits.UnitLookup{
UnitName: unit,
},
Count: count,
}

return metricReport
}

func Test_HandleQuotaEnforcementInfo(t *testing.T) {
metricServicesConfigs := []config.MetricServiceConfiguration{
{
Enable: true,
URL: "https://mockserver:8080",
RejectOnFail: true,
},
}
cm := agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false)
accessReq := &management.AccessRequest{
ResourceMeta: v1.ResourceMeta{
Metadata: v1.Metadata{
ID: "11",
References: []v1.Reference{
{
Group: management.APIServiceInstanceGVK().Group,
Kind: management.APIServiceInstanceGVK().Kind,
ID: instRefID,
Name: instRefName,
},
},
Scope: v1.MetadataScope{
Kind: management.EnvironmentGVK().Kind,
Name: "env-1",
},
},
SubResources: map[string]interface{}{
defs.XAgentDetails: map[string]interface{}{
"sub_access_request_key": "sub_access_request_val",
},
},
},
Spec: management.AccessRequestSpec{
ApiServiceInstance: instRefName,
ManagedApplication: managedAppRefName,
Data: map[string]interface{}{},
},
Status: &v1.ResourceStatus{
Level: prov.Pending.String(),
},
}
managedAppForTest := &management.ManagedApplication{
ResourceMeta: v1.ResourceMeta{
Name: "app-test",
Metadata: v1.Metadata{
ID: "11",
Scope: v1.MetadataScope{
Kind: management.EnvironmentGVK().Kind,
Name: "env-1",
},
},
SubResources: map[string]interface{}{
defs.XAgentDetails: map[string]interface{}{
"sub_manage_app_key": "sub_manage_app_val",
},
},
},
Owner: &v1.Owner{
Type: 0,
},
Spec: management.ManagedApplicationSpec{},
Status: &v1.ResourceStatus{
Level: prov.Pending.String(),
},
}

handler := NewCustomUnitHandler(metricServicesConfigs, cm, config.DiscoveryAgent)
err := handler.HandleQuotaEnforcement(accessReq, managedAppForTest)

assert.Nil(t, err)
}

func Test_HandleMetricReporting(t *testing.T) {

metricCollector := &fakeMetricCollector{}
cm := agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false)
metricServicesConfigs := []config.MetricServiceConfiguration{
{
Enable: true,
URL: "bufnet",
RejectOnFail: true,
},
}

handler := NewCustomUnitHandler(metricServicesConfigs, cm, config.TraceabilityAgent)

go handler.HandleMetricReporting(metricCollector)
time.Sleep(10 * time.Second)
handler.Stop()
}

func Test_ReceiveMetrics(t *testing.T) {
metricReport := GetMetricReport()
metricServicesConfigs := []config.MetricServiceConfiguration{
{
Enable: true,
URL: "bufnet",
RejectOnFail: true,
},
}
handler := NewCustomUnitHandler(metricServicesConfigs, &mockAgentCache{}, config.TraceabilityAgent)

go handler.receiveMetrics(&fakeMetricCollector{})
handler.metricReportChan <- metricReport
time.Sleep(15 * time.Second)
handler.Stop()
}

func Test_BuildCustomMetricDetail(t *testing.T) {
metricReport := GetMetricReport()
metricServicesConfigs := []config.MetricServiceConfiguration{
{
Enable: true,
URL: "bufnet",
RejectOnFail: true,
},
}
handler := NewCustomUnitHandler(metricServicesConfigs, &mockAgentCache{}, config.TraceabilityAgent)

customMetricDetail, err := handler.buildCustomMetricDetail(metricReport)

assert.Nil(t, err)
assert.NotNil(t, customMetricDetail)
assert.Equal(t, customMetricDetail.APIDetails.ID, "remoteApiId_fsdfsf2342r2ferge")
assert.Equal(t, customMetricDetail.AppDetails.ID, "fsdfsdfsf234235fgdgd")
assert.Equal(t, customMetricDetail.UnitDetails.Name, "x-ai-tokens")
}
Loading

0 comments on commit 058c26d

Please sign in to comment.