Skip to content

Commit a8724fb

Browse files
committed
Refactor pipelinerun metrics tests
Refactor the tests for the new pipelinerun metric as a matrix test. Ensure we test the case of non-running pipelineruns. Signed-off-by: Andrea Frittoli <[email protected]>
1 parent 0350a6e commit a8724fb

File tree

1 file changed

+105
-174
lines changed

1 file changed

+105
-174
lines changed

pkg/pipelinerunmetrics/metrics_test.go

+105-174
Original file line numberDiff line numberDiff line change
@@ -523,114 +523,19 @@ func TestRecordRunningPipelineRunsCount(t *testing.T) {
523523
metricstest.CheckLastValueData(t, "running_pipelineruns", map[string]string{}, 1)
524524
}
525525

526-
func TestRecordRunningPipelineRunsCountAtPipelineRunLevel(t *testing.T) {
527-
unregisterMetrics()
526+
func TestRecordRunningPipelineRunsCountAtAllLevels(t *testing.T) {
528527

529-
newPipelineRun := func(status corev1.ConditionStatus, pipelineRun, namespace string) *v1.PipelineRun {
530-
return &v1.PipelineRun{
531-
ObjectMeta: metav1.ObjectMeta{Name: pipelineRun, Namespace: namespace},
532-
Status: v1.PipelineRunStatus{
533-
Status: duckv1.Status{
534-
Conditions: duckv1.Conditions{{
535-
Type: apis.ConditionSucceeded,
536-
Status: status,
537-
}},
538-
},
539-
},
540-
}
541-
}
542-
543-
ctx, _ := ttesting.SetupFakeContext(t)
544-
informer := fakepipelineruninformer.Get(ctx)
545-
// Add N randomly-named PipelineRuns with differently-succeeded statuses.
546-
for _, pipelineRun := range []*v1.PipelineRun{
547-
newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns1"),
548-
newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns2"),
549-
newPipelineRun(corev1.ConditionUnknown, "testpr2", "testns2"),
550-
newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns3"),
551-
newPipelineRun(corev1.ConditionUnknown, "testpr2", "testns3"),
552-
newPipelineRun(corev1.ConditionUnknown, "testpr3", "testns3"),
553-
newPipelineRun(corev1.ConditionUnknown, "testpr4", "testns3"),
554-
} {
555-
if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil {
556-
t.Fatalf("Adding TaskRun to informer: %v", err)
528+
newPipelineRun := func(status corev1.ConditionStatus, namespace string, name string) *v1.PipelineRun {
529+
if name == "" {
530+
name = "anonymous"
557531
}
558-
}
559-
560-
ctx = getConfigContextRunningPRLevel("pipelinerun")
561-
recorder, err := NewRecorder(ctx)
562-
if err != nil {
563-
t.Fatalf("NewRecorder: %v", err)
564-
}
565-
566-
if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil {
567-
t.Errorf("RunningPipelineRuns: %v", err)
568-
}
569-
570-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1)
571-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1)
572-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous", "pipelinerun": "testpr2"}, 1)
573-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1)
574-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr2"}, 1)
575-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr3"}, 1)
576-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr4"}, 1)
577-
}
578-
579-
func TestRecordRunningPipelineRunsCountAtPipelineLevel(t *testing.T) {
580-
unregisterMetrics()
581-
582-
newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun {
583532
return &v1.PipelineRun{
584-
ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace},
585-
Status: v1.PipelineRunStatus{
586-
Status: duckv1.Status{
587-
Conditions: duckv1.Conditions{{
588-
Type: apis.ConditionSucceeded,
589-
Status: status,
590-
}},
533+
ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun"), Namespace: namespace},
534+
Spec: v1.PipelineRunSpec{
535+
PipelineRef: &v1.PipelineRef{
536+
Name: name,
591537
},
592538
},
593-
}
594-
}
595-
596-
ctx, _ := ttesting.SetupFakeContext(t)
597-
informer := fakepipelineruninformer.Get(ctx)
598-
// Add N randomly-named PipelineRuns with differently-succeeded statuses.
599-
for _, pipelineRun := range []*v1.PipelineRun{
600-
newPipelineRun(corev1.ConditionUnknown, "testns1"),
601-
newPipelineRun(corev1.ConditionUnknown, "testns2"),
602-
newPipelineRun(corev1.ConditionUnknown, "testns2"),
603-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
604-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
605-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
606-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
607-
} {
608-
if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil {
609-
t.Fatalf("Adding TaskRun to informer: %v", err)
610-
}
611-
}
612-
613-
ctx = getConfigContextRunningPRLevel("pipeline")
614-
recorder, err := NewRecorder(ctx)
615-
if err != nil {
616-
t.Fatalf("NewRecorder: %v", err)
617-
}
618-
619-
if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil {
620-
t.Errorf("RunningPipelineRuns: %v", err)
621-
}
622-
623-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1", "pipeline": "anonymous"}, 1)
624-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous"}, 2)
625-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous"}, 4)
626-
}
627-
628-
func TestRecordRunningPipelineRunsCountAtNamespaceLevel(t *testing.T) {
629-
unregisterMetrics()
630-
631-
newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun {
632-
return &v1.PipelineRun{
633-
ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace},
634539
Status: v1.PipelineRunStatus{
635540
Status: duckv1.Status{
636541
Conditions: duckv1.Conditions{{
@@ -642,83 +547,105 @@ func TestRecordRunningPipelineRunsCountAtNamespaceLevel(t *testing.T) {
642547
}
643548
}
644549

645-
ctx, _ := ttesting.SetupFakeContext(t)
646-
informer := fakepipelineruninformer.Get(ctx)
647-
// Add N randomly-named PipelineRuns with differently-succeeded statuses.
648-
for _, pipelineRun := range []*v1.PipelineRun{
649-
newPipelineRun(corev1.ConditionUnknown, "testns1"),
650-
newPipelineRun(corev1.ConditionUnknown, "testns2"),
651-
newPipelineRun(corev1.ConditionUnknown, "testns2"),
652-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
653-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
654-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
655-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
656-
} {
657-
if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil {
658-
t.Fatalf("Adding TaskRun to informer: %v", err)
550+
pipelineRuns := []*v1.PipelineRun{
551+
newPipelineRun(corev1.ConditionUnknown, "testns1", ""),
552+
newPipelineRun(corev1.ConditionUnknown, "testns1", "another"),
553+
newPipelineRun(corev1.ConditionUnknown, "testns1", "another"),
554+
newPipelineRun(corev1.ConditionFalse, "testns1", "another"),
555+
newPipelineRun(corev1.ConditionTrue, "testns1", ""),
556+
newPipelineRun(corev1.ConditionUnknown, "testns2", ""),
557+
newPipelineRun(corev1.ConditionUnknown, "testns2", ""),
558+
newPipelineRun(corev1.ConditionUnknown, "testns2", "another"),
559+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
560+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
561+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
562+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
563+
newPipelineRun(corev1.ConditionUnknown, "testns3", "another"),
564+
newPipelineRun(corev1.ConditionFalse, "testns3", ""),
565+
}
566+
567+
pipelineRunMeasureQueries := []map[string]string{}
568+
pipelineRunExpectedResults := []int64{}
569+
for _, pipelineRun := range pipelineRuns {
570+
if pipelineRun.Status.Conditions[0].Status == corev1.ConditionUnknown {
571+
pipelineRunMeasureQueries = append(pipelineRunMeasureQueries, map[string]string{
572+
"namespace": pipelineRun.Namespace,
573+
"pipeline": pipelineRun.Spec.PipelineRef.Name,
574+
"pipelinerun": pipelineRun.Name,
575+
})
576+
pipelineRunExpectedResults = append(pipelineRunExpectedResults, 1)
659577
}
660578
}
661579

662-
ctx = getConfigContextRunningPRLevel("namespace")
663-
recorder, err := NewRecorder(ctx)
664-
if err != nil {
665-
t.Fatalf("NewRecorder: %v", err)
666-
}
667-
668-
if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil {
669-
t.Errorf("RunningPipelineRuns: %v", err)
670-
}
671-
672-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1"}, 1)
673-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2"}, 2)
674-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3"}, 4)
675-
}
676-
677-
func TestRecordRunningPipelineRunsCountAtClusterLevel(t *testing.T) {
678-
unregisterMetrics()
580+
for _, test := range []struct {
581+
name string
582+
metricLevel string
583+
pipelineRuns []*v1.PipelineRun
584+
measureQueries []map[string]string
585+
expectedResults []int64
586+
}{{
587+
name: "pipelinerun at pipeline level",
588+
metricLevel: "pipeline",
589+
pipelineRuns: pipelineRuns,
590+
measureQueries: []map[string]string{
591+
map[string]string{"namespace": "testns1", "pipeline": "anonymous"},
592+
map[string]string{"namespace": "testns2", "pipeline": "anonymous"},
593+
map[string]string{"namespace": "testns3", "pipeline": "anonymous"},
594+
map[string]string{"namespace": "testns1", "pipeline": "another"},
595+
},
596+
expectedResults: []int64{1, 2, 4, 2},
597+
}, {
598+
name: "pipelinerun at namespace level",
599+
metricLevel: "namespace",
600+
pipelineRuns: pipelineRuns,
601+
measureQueries: []map[string]string{
602+
map[string]string{"namespace": "testns1"},
603+
map[string]string{"namespace": "testns2"},
604+
map[string]string{"namespace": "testns3"},
605+
},
606+
expectedResults: []int64{3, 3, 5},
607+
}, {
608+
name: "pipelinerun at cluster level",
609+
metricLevel: "",
610+
pipelineRuns: pipelineRuns,
611+
measureQueries: []map[string]string{
612+
map[string]string{},
613+
},
614+
expectedResults: []int64{11},
615+
}, {
616+
name: "pipelinerun at pipelinerun level",
617+
metricLevel: "pipelinerun",
618+
pipelineRuns: pipelineRuns,
619+
measureQueries: pipelineRunMeasureQueries,
620+
expectedResults: pipelineRunExpectedResults,
621+
}} {
622+
t.Run(test.name, func(t *testing.T) {
623+
unregisterMetrics()
679624

680-
newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun {
681-
return &v1.PipelineRun{
682-
ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace},
683-
Status: v1.PipelineRunStatus{
684-
Status: duckv1.Status{
685-
Conditions: duckv1.Conditions{{
686-
Type: apis.ConditionSucceeded,
687-
Status: status,
688-
}},
689-
},
690-
},
691-
}
692-
}
625+
ctx, _ := ttesting.SetupFakeContext(t)
626+
informer := fakepipelineruninformer.Get(ctx)
627+
// Add N randomly-named PipelineRuns with differently-succeeded statuses.
628+
for _, pipelineRun := range test.pipelineRuns {
629+
if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil {
630+
t.Fatalf("Adding TaskRun to informer: %v", err)
631+
}
632+
}
693633

694-
ctx, _ := ttesting.SetupFakeContext(t)
695-
informer := fakepipelineruninformer.Get(ctx)
696-
// Add N randomly-named PipelineRuns with differently-succeeded statuses.
697-
for _, pipelineRun := range []*v1.PipelineRun{
698-
newPipelineRun(corev1.ConditionUnknown, "testns1"),
699-
newPipelineRun(corev1.ConditionUnknown, "testns2"),
700-
newPipelineRun(corev1.ConditionUnknown, "testns2"),
701-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
702-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
703-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
704-
newPipelineRun(corev1.ConditionUnknown, "testns3"),
705-
} {
706-
if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil {
707-
t.Fatalf("Adding TaskRun to informer: %v", err)
708-
}
709-
}
634+
ctx = getConfigContextRunningPRLevel(test.metricLevel)
635+
recorder, err := NewRecorder(ctx)
636+
if err != nil {
637+
t.Fatalf("NewRecorder: %v", err)
638+
}
710639

711-
ctx = getConfigContextRunningPRLevel("")
712-
recorder, err := NewRecorder(ctx)
713-
if err != nil {
714-
t.Fatalf("NewRecorder: %v", err)
715-
}
640+
if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil {
641+
t.Errorf("RunningPipelineRuns: %v", err)
642+
}
716643

717-
if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil {
718-
t.Errorf("RunningPipelineRuns: %v", err)
644+
for idx, query := range test.measureQueries {
645+
checkLastValueDataForTags(t, "running_pipelineruns", query, float64(test.expectedResults[idx]))
646+
}
647+
})
719648
}
720-
721-
checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{}, 7)
722649
}
723650

724651
func TestRecordRunningPipelineRunsResolutionWaitCounts(t *testing.T) {
@@ -824,8 +751,12 @@ func checkLastValueDataForTags(t *testing.T, name string, wantTags map[string]st
824751
continue
825752
}
826753
val := getLastValueData(data, wantTags)
827-
if expected != val.Value {
828-
t.Error("Value did not match for ", name, wantTags, ", expected", expected, "got", val.Value)
754+
if val == nil {
755+
t.Error("Found no data for ", name, wantTags)
756+
} else {
757+
if expected != val.Value {
758+
t.Error("Value did not match for ", name, wantTags, ", expected", expected, "got", val.Value)
759+
}
829760
}
830761
}
831762
}

0 commit comments

Comments
 (0)