@@ -973,6 +973,116 @@ func TestCollectUnknownConditionalUpdates(t *testing.T) {
973973 }
974974}
975975
976+ func Test_collectConditionalUpdateRisks (t * testing.T ) {
977+ type valueWithLabels struct {
978+ value float64
979+ labels map [string ]string
980+ }
981+ testCases := []struct {
982+ name string
983+ risks []configv1.ConditionalUpdateRisk
984+ expected []valueWithLabels
985+ }{
986+ {
987+ name : "no conditional updates" ,
988+ expected : []valueWithLabels {},
989+ },
990+ {
991+ name : "unknown type" ,
992+ risks : []configv1.ConditionalUpdateRisk {
993+ {
994+ Name : "RiskX" ,
995+ Conditions : []metav1.Condition {{
996+ Type : internal .ConditionalUpdateConditionTypeRecommended ,
997+ Status : metav1 .ConditionFalse ,
998+ Reason : "ReasonA" ,
999+ Message : "Risk does not apply" ,
1000+ }},
1001+ },
1002+ },
1003+ },
1004+ {
1005+ name : "apply false" ,
1006+ risks : []configv1.ConditionalUpdateRisk {
1007+ {
1008+ Name : "RiskX" ,
1009+ Conditions : []metav1.Condition {{
1010+ Type : internal .ConditionalUpdateRiskConditionTypeApplies ,
1011+ Status : metav1 .ConditionFalse ,
1012+ Reason : "ReasonA" ,
1013+ Message : "Risk does not apply" ,
1014+ }},
1015+ },
1016+ },
1017+ expected : []valueWithLabels {{
1018+ labels : map [string ]string {"name" : "version" , "condition" : "Applies" , "risk" : "RiskX" },
1019+ }},
1020+ },
1021+ {
1022+ name : "apply true" ,
1023+ risks : []configv1.ConditionalUpdateRisk {
1024+ {
1025+ Name : "RiskX" ,
1026+ Conditions : []metav1.Condition {{
1027+ Type : internal .ConditionalUpdateRiskConditionTypeApplies ,
1028+ Status : metav1 .ConditionTrue ,
1029+ Reason : "ReasonA" ,
1030+ Message : "Risk does not apply" ,
1031+ }},
1032+ },
1033+ },
1034+ expected : []valueWithLabels {{
1035+ value : 1 ,
1036+ labels : map [string ]string {"name" : "version" , "condition" : "Applies" , "risk" : "RiskX" },
1037+ }},
1038+ },
1039+ {
1040+ name : "apply unknown" ,
1041+ risks : []configv1.ConditionalUpdateRisk {
1042+ {
1043+ Name : "RiskX" ,
1044+ Conditions : []metav1.Condition {{
1045+ Type : internal .ConditionalUpdateRiskConditionTypeApplies ,
1046+ Status : metav1 .ConditionUnknown ,
1047+ Reason : "ReasonA" ,
1048+ Message : "Risk does not apply" ,
1049+ }},
1050+ },
1051+ },
1052+ expected : []valueWithLabels {{
1053+ labels : map [string ]string {"name" : "version" , "condition" : "Applies" , "risk" : "RiskX" },
1054+ }},
1055+ },
1056+ }
1057+
1058+ for _ , tc := range testCases {
1059+ tc := tc
1060+ t .Run (tc .name , func (t * testing.T ) {
1061+ optr := & Operator {}
1062+ m := newOperatorMetrics (optr )
1063+ ch := make (chan prometheus.Metric )
1064+
1065+ go func () {
1066+ m .collectConditionalUpdateRisks (ch , tc .risks )
1067+ close (ch )
1068+ }()
1069+
1070+ var collected []prometheus.Metric
1071+ for item := range ch {
1072+ collected = append (collected , item )
1073+ }
1074+
1075+ if lenC , lenE := len (collected ), len (tc .expected ); lenC != lenE {
1076+
1077+ t .Fatalf ("Expected %d metrics, got %d metrics\n Got metrics: %s" , lenE , lenC , spew .Sdump (collected ))
1078+ }
1079+ for i := range tc .expected {
1080+ expectMetric (t , collected [i ], tc .expected [i ].value , tc .expected [i ].labels )
1081+ }
1082+ })
1083+ }
1084+ }
1085+
9761086func expectMetric (t * testing.T , metric prometheus.Metric , value float64 , labels map [string ]string ) {
9771087 t .Helper ()
9781088 var d dto.Metric
0 commit comments