@@ -23,6 +23,7 @@ import (
2323
2424 configv1 "github.com/openshift/api/config/v1"
2525
26+ "github.com/openshift/cluster-version-operator/pkg/featuregates"
2627 "github.com/openshift/cluster-version-operator/pkg/internal"
2728)
2829
@@ -667,6 +668,7 @@ func Test_operatorMetrics_Collect(t *testing.T) {
667668 }
668669 for _ , tt := range tests {
669670 t .Run (tt .name , func (t * testing.T ) {
671+ tt .optr .enabledCVOFeatureGates = featuregates .DefaultCvoGates ("version" )
670672 tt .optr .eventRecorder = record .NewFakeRecorder (100 )
671673 if tt .optr .cvLister == nil {
672674 tt .optr .cvLister = & cvLister {}
@@ -973,6 +975,116 @@ func TestCollectUnknownConditionalUpdates(t *testing.T) {
973975 }
974976}
975977
978+ func Test_collectConditionalUpdateRisks (t * testing.T ) {
979+ type valueWithLabels struct {
980+ value float64
981+ labels map [string ]string
982+ }
983+ testCases := []struct {
984+ name string
985+ risks []configv1.ConditionalUpdateRisk
986+ expected []valueWithLabels
987+ }{
988+ {
989+ name : "no conditional updates" ,
990+ expected : []valueWithLabels {},
991+ },
992+ {
993+ name : "unknown type" ,
994+ risks : []configv1.ConditionalUpdateRisk {
995+ {
996+ Name : "RiskX" ,
997+ Conditions : []metav1.Condition {{
998+ Type : internal .ConditionalUpdateConditionTypeRecommended ,
999+ Status : metav1 .ConditionFalse ,
1000+ Reason : "ReasonA" ,
1001+ Message : "Risk does not apply" ,
1002+ }},
1003+ },
1004+ },
1005+ },
1006+ {
1007+ name : "apply false" ,
1008+ risks : []configv1.ConditionalUpdateRisk {
1009+ {
1010+ Name : "RiskX" ,
1011+ Conditions : []metav1.Condition {{
1012+ Type : internal .ConditionalUpdateRiskConditionTypeApplies ,
1013+ Status : metav1 .ConditionFalse ,
1014+ Reason : "ReasonA" ,
1015+ Message : "Risk does not apply" ,
1016+ }},
1017+ },
1018+ },
1019+ expected : []valueWithLabels {{
1020+ labels : map [string ]string {"name" : "version" , "condition" : "Applies" , "risk" : "RiskX" },
1021+ }},
1022+ },
1023+ {
1024+ name : "apply true" ,
1025+ risks : []configv1.ConditionalUpdateRisk {
1026+ {
1027+ Name : "RiskX" ,
1028+ Conditions : []metav1.Condition {{
1029+ Type : internal .ConditionalUpdateRiskConditionTypeApplies ,
1030+ Status : metav1 .ConditionTrue ,
1031+ Reason : "ReasonA" ,
1032+ Message : "Risk does not apply" ,
1033+ }},
1034+ },
1035+ },
1036+ expected : []valueWithLabels {{
1037+ value : 1 ,
1038+ labels : map [string ]string {"name" : "version" , "condition" : "Applies" , "risk" : "RiskX" },
1039+ }},
1040+ },
1041+ {
1042+ name : "apply unknown" ,
1043+ risks : []configv1.ConditionalUpdateRisk {
1044+ {
1045+ Name : "RiskX" ,
1046+ Conditions : []metav1.Condition {{
1047+ Type : internal .ConditionalUpdateRiskConditionTypeApplies ,
1048+ Status : metav1 .ConditionUnknown ,
1049+ Reason : "ReasonA" ,
1050+ Message : "Risk does not apply" ,
1051+ }},
1052+ },
1053+ },
1054+ expected : []valueWithLabels {{
1055+ labels : map [string ]string {"name" : "version" , "condition" : "Applies" , "risk" : "RiskX" },
1056+ }},
1057+ },
1058+ }
1059+
1060+ for _ , tc := range testCases {
1061+ tc := tc
1062+ t .Run (tc .name , func (t * testing.T ) {
1063+ optr := & Operator {}
1064+ m := newOperatorMetrics (optr )
1065+ ch := make (chan prometheus.Metric )
1066+
1067+ go func () {
1068+ m .collectConditionalUpdateRisks (ch , tc .risks )
1069+ close (ch )
1070+ }()
1071+
1072+ var collected []prometheus.Metric
1073+ for item := range ch {
1074+ collected = append (collected , item )
1075+ }
1076+
1077+ if lenC , lenE := len (collected ), len (tc .expected ); lenC != lenE {
1078+
1079+ t .Fatalf ("Expected %d metrics, got %d metrics\n Got metrics: %s" , lenE , lenC , spew .Sdump (collected ))
1080+ }
1081+ for i := range tc .expected {
1082+ expectMetric (t , collected [i ], tc .expected [i ].value , tc .expected [i ].labels )
1083+ }
1084+ })
1085+ }
1086+ }
1087+
9761088func expectMetric (t * testing.T , metric prometheus.Metric , value float64 , labels map [string ]string ) {
9771089 t .Helper ()
9781090 var d dto.Metric
0 commit comments