@@ -783,3 +783,171 @@ func mockResources(t testing.TB) map[string]workloadinterface.IMetadata {
783783 "resource-10" : reporthandling .NewResource (mocks .GetResourceByType (t , "Pod" , mocks .WithName ("resource-10" ))),
784784 }
785785}
786+
787+ // ================================ compliance score tests ================================
788+
789+ func TestGetControlComplianceScore (t * testing.T ) {
790+ var resourceWithFailed , resourceWithPassed helpers.AllLists
791+ resourceWithFailed .Append (apis .StatusFailed , "resource-1" , "resource-2" )
792+ resourceWithFailed .Append (apis .StatusPassed , "resource-3" )
793+ resourceWithPassed .Append (apis .StatusPassed , "resource-4" )
794+
795+ var resourceWithFailed2 , resourceWithPassed2 helpers.AllLists
796+ resourceWithFailed2 .Append (apis .StatusFailed , "resource-5" , "resource-6" )
797+ resourceWithFailed2 .Append (apis .StatusPassed , "resource-7" , "resource-8" )
798+ resourceWithPassed2 .Append (apis .StatusPassed , "resource-9" , "resource-10" )
799+ t .Parallel ()
800+
801+ t .Run ("with empty control report" , func (t * testing.T ) {
802+ t .Parallel ()
803+
804+ resources := mockResources (t )
805+ s := ScoreUtil {isDebugMode : true , resources : resources }
806+ controlReport := reportsummary.ControlSummary {
807+ Name : "empty" ,
808+ ControlID : "empty" ,
809+ ResourceIDs : helpers.AllLists {},
810+ }
811+
812+ require .Equal (t , float32 (0 ), s .GetControlComplianceScore (& controlReport , "" ),
813+ "empty control report should return a score equals to 0" ,
814+ )
815+ })
816+
817+ t .Run ("with control report" , func (t * testing.T ) {
818+ t .Parallel ()
819+
820+ resources := mockResources (t )
821+ s := ScoreUtil {isDebugMode : true , resources : resources }
822+ controlReport := reportsummary.ControlSummary {
823+ Name : "mock-control-1" ,
824+ ControlID : "mock-control-1" ,
825+ ResourceIDs : resourceWithFailed2 ,
826+ }
827+
828+ require .Equal (t , float32 (50 ), s .GetControlComplianceScore (& controlReport , "" ),
829+ "control report should return a score equals to 50" ,
830+ )
831+ })
832+ }
833+
834+ func TestSetPostureReportComplianceScores (t * testing.T ) {
835+ t .Parallel ()
836+
837+ t .Run ("with empty report" , func (t * testing.T ) {
838+ t .Parallel ()
839+
840+ s := NewScore (map [string ]workloadinterface.IMetadata {})
841+ report := & v2.PostureReport {
842+ SummaryDetails : reportsummary.SummaryDetails {Frameworks : []reportsummary.FrameworkSummary {{Name : "empty" , Controls : reportsummary.ControlSummaries {}}}},
843+ Results : []resourcesresults.Result {},
844+ Resources : []reporthandling.Resource {},
845+ }
846+
847+ require .Errorf (t , s .SetPostureReportComplianceScores (report ),
848+ "empty framework should return an error" ,
849+ )
850+
851+ require .Equal (t , float32 (0 ), report .SummaryDetails .Frameworks [0 ].Score ,
852+ "empty framework should return an error and have a score equals to 0" ,
853+ )
854+ })
855+
856+ t .Run ("with skipped report" , func (t * testing.T ) {
857+ t .Parallel ()
858+
859+ s := NewScore (map [string ]workloadinterface.IMetadata {})
860+ report := & v2.PostureReport {
861+ SummaryDetails : reportsummary.SummaryDetails {Frameworks : []reportsummary.FrameworkSummary {{Name : "skipped" , Controls : reportsummary.ControlSummaries {
862+ "skipped1" : reportsummary.ControlSummary {
863+ Name : "skipped1" ,
864+ ControlID : "Skippie1" ,
865+ Description : "skipper" ,
866+ },
867+ "skipped2" : reportsummary.ControlSummary {
868+ Name : "skipped2" ,
869+ ControlID : "Skippie2" ,
870+ Description : "skipper" ,
871+ },
872+ }}}},
873+ Results : []resourcesresults.Result {},
874+ Resources : []reporthandling.Resource {},
875+ }
876+
877+ require .Errorf (t , s .SetPostureReportComplianceScores (report ),
878+ "empty framework should return an error" ,
879+ )
880+
881+ require .Equal (t , float32 (0 ), report .SummaryDetails .Frameworks [0 ].Score ,
882+ "empty framework should return an error and have a score equals to 0" ,
883+ )
884+ })
885+
886+ t .Run ("with mock report" , func (t * testing.T ) {
887+ t .Parallel ()
888+
889+ resources , report := mockPostureReportV2 (t )
890+ s := ScoreUtil {
891+ isDebugMode : true ,
892+ resources : resources ,
893+ }
894+
895+ require .NoErrorf (t , s .SetPostureReportComplianceScores (report ),
896+ "mock framework should not return an error" ,
897+ )
898+
899+ const (
900+ expectedScoreFramework1 = float32 (62.577965 )
901+ expectedScoreFramework2 = float32 (46.42857 )
902+ expectedComplianceScoreFramework1 = float32 (66.66667 )
903+ expectedComplianceScoreFramework2 = float32 (75 )
904+ expectedSummary = float32 (70.833336 )
905+ )
906+
907+ t .Run ("assert control scores" , func (t * testing.T ) {
908+ require .Len (t , report .SummaryDetails .Controls , 4 )
909+ for _ , control := range report .SummaryDetails .Controls {
910+ var expectedForControl float64
911+
912+ switch control .ControlID {
913+ case "control-1" :
914+ expectedForControl = 33.333336
915+ case "control-2" :
916+ expectedForControl = 100 // passed
917+ case "control-3" :
918+ expectedForControl = 50
919+ case "control-4" :
920+ expectedForControl = 100 // passed
921+ }
922+
923+ assert .InDeltaf (t , expectedForControl , control .Score , 1e-6 ,
924+ "unexpected summarized score for control %q" , control .ControlID ,
925+ )
926+ }
927+ })
928+
929+ t .Run ("assert framework scores" , func (t * testing.T ) {
930+ assert .InDeltaf (t , expectedScoreFramework1 , report .SummaryDetails .Frameworks [0 ].Score , 1e-6 ,
931+ "unexpected summarized score for framework[0]" ,
932+ )
933+ assert .InDeltaf (t , expectedScoreFramework2 , report .SummaryDetails .Frameworks [1 ].Score , 1e-6 ,
934+ "unexpected summarized score for framework[1]" ,
935+ )
936+ })
937+
938+ t .Run ("assert framework compliance scores" , func (t * testing.T ) {
939+ assert .InDeltaf (t , expectedComplianceScoreFramework1 , report .SummaryDetails .Frameworks [0 ].ComplianceScore , 1e-6 ,
940+ "unexpected summarized compliance score for framework[0]" ,
941+ )
942+ assert .InDeltaf (t , expectedComplianceScoreFramework2 , report .SummaryDetails .Frameworks [1 ].ComplianceScore , 1e-6 ,
943+ "unexpected summarized compliance score for framework[1]" ,
944+ )
945+ })
946+
947+ t .Run ("assert final score" , func (t * testing.T ) {
948+ assert .InDeltaf (t , expectedSummary , report .SummaryDetails .Score , 1e-6 ,
949+ "unexpected summarized final score" ,
950+ )
951+ })
952+ })
953+ }
0 commit comments