github.com/Axway/agent-sdk@v1.1.101/pkg/compliance/compliance_test.go (about) 1 package compliance 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/Axway/agent-sdk/pkg/agent" 8 "github.com/Axway/agent-sdk/pkg/apic" 9 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 10 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" 11 "github.com/Axway/agent-sdk/pkg/apic/mock" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type mockProcessor struct { 16 results []RuntimeResult 17 collectCalled bool 18 } 19 20 func (m *mockProcessor) CollectRuntimeResult(results RuntimeResults) error { 21 m.collectCalled = true 22 for _, result := range m.results { 23 results.AddRuntimeResult(result) 24 } 25 return nil 26 } 27 28 func TestCompliance(t *testing.T) { 29 apicMockCli := &mock.Client{} 30 patchReqCount := 0 31 32 apicMockCli.PatchSubResourceMock = func(ri v1.Interface, subResourceName string, patches []map[string]interface{}) (*v1.ResourceInstance, error) { 33 patchReqCount++ 34 resInst, _ := ri.AsInstance() 35 instance := management.NewAPIServiceInstance("", "") 36 instance.FromInstance(resInst) 37 for _, patch := range patches { 38 if patch[apic.PatchOperation] != apic.PatchOpBuildObjectTree { 39 c := patch[apic.PatchValue] 40 buf, _ := json.Marshal(c) 41 compliance := &management.ApiServiceInstanceSourceCompliance{} 42 json.Unmarshal(buf, compliance) 43 instance.Source.Compliance = compliance 44 resInst, _ = instance.AsInstance() 45 agent.GetCacheManager().AddAPIServiceInstance(resInst) 46 } 47 } 48 return instance.AsInstance() 49 } 50 51 tests := []struct { 52 name string 53 runtimeResults []RuntimeResult 54 }{ 55 { 56 name: "no collection", 57 }, 58 { 59 name: "collect and publish", 60 runtimeResults: []RuntimeResult{ 61 { 62 APIServiceInstance: "test-1", 63 RiskScore: 10, 64 }, 65 }, 66 }, 67 } 68 for _, tt := range tests { 69 t.Run(tt.name, func(t *testing.T) { 70 patchReqCount = 0 71 agent.InitializeForTest(apicMockCli) 72 processor := &mockProcessor{ 73 results: tt.runtimeResults, 74 } 75 for _, result := range tt.runtimeResults { 76 inst := management.NewAPIServiceInstance(result.APIServiceInstance, "test") 77 inst.Source = &management.ApiServiceInstanceSource{ 78 DataplaneType: &management.ApiServiceInstanceSourceDataplaneType{ 79 Managed: string(apic.Unclassified), 80 }, 81 } 82 ri, _ := inst.AsInstance() 83 agent.GetCacheManager().AddAPIServiceInstance(ri) 84 } 85 cm := GetManager() 86 manager, ok := cm.(*complianceManager) 87 assert.True(t, ok) 88 manager.job = &runtimeComplianceJob{ 89 logger: manager.logger, 90 processor: processor, 91 } 92 manager.job.Execute() 93 assert.True(t, processor.collectCalled) 94 for _, result := range tt.runtimeResults { 95 cacheManager := agent.GetCacheManager() 96 ri, _ := cacheManager.GetAPIServiceInstanceByName(result.APIServiceInstance) 97 instance := &management.APIServiceInstance{} 98 instance.FromInstance(ri) 99 assert.NotNil(t, instance.Source) 100 assert.NotNil(t, instance.Source.Compliance) 101 assert.Equal(t, result.RiskScore, instance.Source.Compliance.Runtime.Result.RiskScore) 102 } 103 assert.Equal(t, len(tt.runtimeResults), patchReqCount) 104 }) 105 } 106 }