@@ -477,6 +477,111 @@ func Test_textAnalyze(t *testing.T) {
477477 "text-collector-1/cfile-1.txt" : []byte ("value: 2\n other: 10" ),
478478 },
479479 },
480+ // This test ensures that the Outcomes.Pass.Message can be templated using the findings of the regular expression groups.
481+ {
482+ name : "Outcome pass message is templated with regex groups" ,
483+ analyzer : troubleshootv1beta2.TextAnalyze {
484+ Outcomes : []* troubleshootv1beta2.Outcome {
485+ {
486+ Pass : & troubleshootv1beta2.SingleOutcome {
487+ When : `Feature == insert-feature-name-here` ,
488+ Message : "Feature {{ .Feature }} is enabled for CR {{ .CRName }} in namespace {{ .Namespace }}" ,
489+ },
490+ },
491+ },
492+ CollectorName : "text-collector-templated-regex-message" ,
493+ FileName : "cfile-1.txt" ,
494+ RegexGroups : `"name":\s*"(?P<CRName>.*?)".*namespace":\s*"(?P<Namespace>.*?)".*feature":\s*.*"(?P<Feature>insert-feature-name-here.*?)"` ,
495+ },
496+ expectResult : []AnalyzeResult {
497+ {
498+ IsPass : true ,
499+ IsWarn : false ,
500+ IsFail : false ,
501+ Title : "text-collector-templated-regex-message" ,
502+ Message : "Feature insert-feature-name-here is enabled for CR insert-cr-name-here in namespace default" ,
503+ IconKey : "kubernetes_text_analyze" ,
504+ IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16" ,
505+ },
506+ },
507+ files : map [string ][]byte {
508+ "text-collector-templated-regex-message/cfile-1.txt" : []byte (`{"level":"INFO","timestamp":"2022-05-17T20:37:41Z","caller":"controller/controller.go:317","message":"Feature enabled","context":{"name":"insert-cr-name-here","namespace":"default","feature":"insert-feature-name-here"}}` ),
509+ },
510+ },
511+ // This test ensures that the Outcomes.Warn.Message can be templated using the findings of the regular expression groups.
512+ {
513+ name : "Outcome warn message is templated with regex groups" ,
514+ analyzer : troubleshootv1beta2.TextAnalyze {
515+ Outcomes : []* troubleshootv1beta2.Outcome {
516+ {
517+ Pass : & troubleshootv1beta2.SingleOutcome {
518+ When : `Warning == ""` ,
519+ Message : "No warning found" ,
520+ },
521+ },
522+ // The Warn case is triggered if warning != ""
523+ {
524+ Warn : & troubleshootv1beta2.SingleOutcome {
525+ Message : "Warning for CR with name {{ .CRName }} in namespace {{ .Namespace }}" ,
526+ },
527+ },
528+ },
529+ CollectorName : "text-collector-templated-regex-message" ,
530+ FileName : "cfile-1.txt" ,
531+ RegexGroups : `"name":\s*"(?P<CRName>.*?)".*namespace":\s*"(?P<Namespace>.*?)".*warning":\s*.*"(?P<Error>mywarning.*?)"` ,
532+ },
533+ expectResult : []AnalyzeResult {
534+ {
535+ IsPass : false ,
536+ IsWarn : true ,
537+ IsFail : false ,
538+ Title : "text-collector-templated-regex-message" ,
539+ Message : "Warning for CR with name insert-cr-name-here in namespace default" ,
540+ IconKey : "kubernetes_text_analyze" ,
541+ IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16" ,
542+ },
543+ },
544+ files : map [string ][]byte {
545+ "text-collector-templated-regex-message/cfile-1.txt" : []byte (`{"level":"WARN","timestamp":"2022-05-17T20:37:41Z","caller":"controller/controller.go:317","message":"Reconciler error","context":{"name":"insert-cr-name-here","namespace":"default","warning":"mywarning"}}` ),
546+ },
547+ },
548+ // This test ensures that the Outcomes.Fail.Message can be templated using the findings of the regular expression groups.
549+ {
550+ name : "Outcome fail message is templated with regex groups" ,
551+ analyzer : troubleshootv1beta2.TextAnalyze {
552+ Outcomes : []* troubleshootv1beta2.Outcome {
553+ {
554+ Pass : & troubleshootv1beta2.SingleOutcome {
555+ When : `Error == ""` ,
556+ Message : "No error found" ,
557+ },
558+ },
559+ // The Fail case is triggered if warning != ""
560+ {
561+ Fail : & troubleshootv1beta2.SingleOutcome {
562+ Message : "Error for CR with name {{ .CRName }} in namespace {{ .Namespace }}" ,
563+ },
564+ },
565+ },
566+ CollectorName : "text-collector-templated-regex-message" ,
567+ FileName : "cfile-1.txt" ,
568+ RegexGroups : `"name":\s*"(?P<CRName>.*?)".*namespace":\s*"(?P<Namespace>.*?)".*error":\s*.*"(?P<Error>myerror.*?)"` ,
569+ },
570+ expectResult : []AnalyzeResult {
571+ {
572+ IsPass : false ,
573+ IsWarn : false ,
574+ IsFail : true ,
575+ Title : "text-collector-templated-regex-message" ,
576+ Message : "Error for CR with name insert-cr-name-here in namespace default" ,
577+ IconKey : "kubernetes_text_analyze" ,
578+ IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16" ,
579+ },
580+ },
581+ files : map [string ][]byte {
582+ "text-collector-templated-regex-message/cfile-1.txt" : []byte (`{"level":"ERROR","timestamp":"2022-05-17T20:37:41Z","caller":"controller/controller.go:317","message":"Reconciler error","context":{"name":"insert-cr-name-here","namespace":"default","error":"myerror"}}` ),
583+ },
584+ },
480585 }
481586
482587 for _ , test := range tests {
0 commit comments