Skip to content

Commit 92fc2fe

Browse files
afrittolitekton-robot
authored andcommitted
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 a758ca4 commit 92fc2fe

File tree

1 file changed

+102
-174
lines changed

1 file changed

+102
-174
lines changed

pkg/pipelinerunmetrics/metrics_test.go

+102-174
Original file line numberDiff line numberDiff line change
@@ -523,114 +523,18 @@ 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()
528-
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)
526+
func TestRecordRunningPipelineRunsCountAtAllLevels(t *testing.T) {
527+
newPipelineRun := func(status corev1.ConditionStatus, namespace string, name string) *v1.PipelineRun {
528+
if name == "" {
529+
name = "anonymous"
557530
}
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 {
583531
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-
}},
532+
ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun"), Namespace: namespace},
533+
Spec: v1.PipelineRunSpec{
534+
PipelineRef: &v1.PipelineRef{
535+
Name: name,
591536
},
592537
},
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},
634538
Status: v1.PipelineRunStatus{
635539
Status: duckv1.Status{
636540
Conditions: duckv1.Conditions{{
@@ -642,83 +546,105 @@ func TestRecordRunningPipelineRunsCountAtNamespaceLevel(t *testing.T) {
642546
}
643547
}
644548

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)
549+
pipelineRuns := []*v1.PipelineRun{
550+
newPipelineRun(corev1.ConditionUnknown, "testns1", ""),
551+
newPipelineRun(corev1.ConditionUnknown, "testns1", "another"),
552+
newPipelineRun(corev1.ConditionUnknown, "testns1", "another"),
553+
newPipelineRun(corev1.ConditionFalse, "testns1", "another"),
554+
newPipelineRun(corev1.ConditionTrue, "testns1", ""),
555+
newPipelineRun(corev1.ConditionUnknown, "testns2", ""),
556+
newPipelineRun(corev1.ConditionUnknown, "testns2", ""),
557+
newPipelineRun(corev1.ConditionUnknown, "testns2", "another"),
558+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
559+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
560+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
561+
newPipelineRun(corev1.ConditionUnknown, "testns3", ""),
562+
newPipelineRun(corev1.ConditionUnknown, "testns3", "another"),
563+
newPipelineRun(corev1.ConditionFalse, "testns3", ""),
564+
}
565+
566+
pipelineRunMeasureQueries := []map[string]string{}
567+
pipelineRunExpectedResults := []int64{}
568+
for _, pipelineRun := range pipelineRuns {
569+
if pipelineRun.Status.Conditions[0].Status == corev1.ConditionUnknown {
570+
pipelineRunMeasureQueries = append(pipelineRunMeasureQueries, map[string]string{
571+
"namespace": pipelineRun.Namespace,
572+
"pipeline": pipelineRun.Spec.PipelineRef.Name,
573+
"pipelinerun": pipelineRun.Name,
574+
})
575+
pipelineRunExpectedResults = append(pipelineRunExpectedResults, 1)
659576
}
660577
}
661578

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()
579+
for _, test := range []struct {
580+
name string
581+
metricLevel string
582+
pipelineRuns []*v1.PipelineRun
583+
measureQueries []map[string]string
584+
expectedResults []int64
585+
}{{
586+
name: "pipelinerun at pipeline level",
587+
metricLevel: "pipeline",
588+
pipelineRuns: pipelineRuns,
589+
measureQueries: []map[string]string{
590+
{"namespace": "testns1", "pipeline": "anonymous"},
591+
{"namespace": "testns2", "pipeline": "anonymous"},
592+
{"namespace": "testns3", "pipeline": "anonymous"},
593+
{"namespace": "testns1", "pipeline": "another"},
594+
},
595+
expectedResults: []int64{1, 2, 4, 2},
596+
}, {
597+
name: "pipelinerun at namespace level",
598+
metricLevel: "namespace",
599+
pipelineRuns: pipelineRuns,
600+
measureQueries: []map[string]string{
601+
{"namespace": "testns1"},
602+
{"namespace": "testns2"},
603+
{"namespace": "testns3"},
604+
},
605+
expectedResults: []int64{3, 3, 5},
606+
}, {
607+
name: "pipelinerun at cluster level",
608+
metricLevel: "",
609+
pipelineRuns: pipelineRuns,
610+
measureQueries: []map[string]string{
611+
{},
612+
},
613+
expectedResults: []int64{11},
614+
}, {
615+
name: "pipelinerun at pipelinerun level",
616+
metricLevel: "pipelinerun",
617+
pipelineRuns: pipelineRuns,
618+
measureQueries: pipelineRunMeasureQueries,
619+
expectedResults: pipelineRunExpectedResults,
620+
}} {
621+
t.Run(test.name, func(t *testing.T) {
622+
unregisterMetrics()
679623

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-
}
624+
ctx, _ := ttesting.SetupFakeContext(t)
625+
informer := fakepipelineruninformer.Get(ctx)
626+
// Add N randomly-named PipelineRuns with differently-succeeded statuses.
627+
for _, pipelineRun := range test.pipelineRuns {
628+
if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil {
629+
t.Fatalf("Adding TaskRun to informer: %v", err)
630+
}
631+
}
693632

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-
}
633+
ctx = getConfigContextRunningPRLevel(test.metricLevel)
634+
recorder, err := NewRecorder(ctx)
635+
if err != nil {
636+
t.Fatalf("NewRecorder: %v", err)
637+
}
710638

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

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

724650
func TestRecordRunningPipelineRunsResolutionWaitCounts(t *testing.T) {
@@ -824,7 +750,9 @@ func checkLastValueDataForTags(t *testing.T, name string, wantTags map[string]st
824750
continue
825751
}
826752
val := getLastValueData(data, wantTags)
827-
if expected != val.Value {
753+
if val == nil {
754+
t.Error("Found no data for ", name, wantTags)
755+
} else if expected != val.Value {
828756
t.Error("Value did not match for ", name, wantTags, ", expected", expected, "got", val.Value)
829757
}
830758
}

0 commit comments

Comments
 (0)