diff --git a/pkg/pipelinerunmetrics/metrics_test.go b/pkg/pipelinerunmetrics/metrics_test.go index 418563e09f4..6589a9a16da 100644 --- a/pkg/pipelinerunmetrics/metrics_test.go +++ b/pkg/pipelinerunmetrics/metrics_test.go @@ -523,114 +523,19 @@ func TestRecordRunningPipelineRunsCount(t *testing.T) { metricstest.CheckLastValueData(t, "running_pipelineruns", map[string]string{}, 1) } -func TestRecordRunningPipelineRunsCountAtPipelineRunLevel(t *testing.T) { - unregisterMetrics() +func TestRecordRunningPipelineRunsCountAtAllLevels(t *testing.T) { - newPipelineRun := func(status corev1.ConditionStatus, pipelineRun, namespace string) *v1.PipelineRun { - return &v1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: pipelineRun, Namespace: namespace}, - Status: v1.PipelineRunStatus{ - Status: duckv1.Status{ - Conditions: duckv1.Conditions{{ - Type: apis.ConditionSucceeded, - Status: status, - }}, - }, - }, - } - } - - ctx, _ := ttesting.SetupFakeContext(t) - informer := fakepipelineruninformer.Get(ctx) - // Add N randomly-named PipelineRuns with differently-succeeded statuses. - for _, pipelineRun := range []*v1.PipelineRun{ - newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns1"), - newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testpr2", "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testpr1", "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testpr2", "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testpr3", "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testpr4", "testns3"), - } { - if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { - t.Fatalf("Adding TaskRun to informer: %v", err) + newPipelineRun := func(status corev1.ConditionStatus, namespace string, name string) *v1.PipelineRun { + if name == "" { + name = "anonymous" } - } - - ctx = getConfigContextRunningPRLevel("pipelinerun") - recorder, err := NewRecorder(ctx) - if err != nil { - t.Fatalf("NewRecorder: %v", err) - } - - if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { - t.Errorf("RunningPipelineRuns: %v", err) - } - - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous", "pipelinerun": "testpr2"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr1"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr2"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr3"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous", "pipelinerun": "testpr4"}, 1) -} - -func TestRecordRunningPipelineRunsCountAtPipelineLevel(t *testing.T) { - unregisterMetrics() - - newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun { return &v1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace}, - Status: v1.PipelineRunStatus{ - Status: duckv1.Status{ - Conditions: duckv1.Conditions{{ - Type: apis.ConditionSucceeded, - Status: status, - }}, + ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun"), Namespace: namespace}, + Spec: v1.PipelineRunSpec{ + PipelineRef: &v1.PipelineRef{ + Name: name, }, }, - } - } - - ctx, _ := ttesting.SetupFakeContext(t) - informer := fakepipelineruninformer.Get(ctx) - // Add N randomly-named PipelineRuns with differently-succeeded statuses. - for _, pipelineRun := range []*v1.PipelineRun{ - newPipelineRun(corev1.ConditionUnknown, "testns1"), - newPipelineRun(corev1.ConditionUnknown, "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - } { - if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { - t.Fatalf("Adding TaskRun to informer: %v", err) - } - } - - ctx = getConfigContextRunningPRLevel("pipeline") - recorder, err := NewRecorder(ctx) - if err != nil { - t.Fatalf("NewRecorder: %v", err) - } - - if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { - t.Errorf("RunningPipelineRuns: %v", err) - } - - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1", "pipeline": "anonymous"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2", "pipeline": "anonymous"}, 2) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3", "pipeline": "anonymous"}, 4) -} - -func TestRecordRunningPipelineRunsCountAtNamespaceLevel(t *testing.T) { - unregisterMetrics() - - newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun { - return &v1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace}, Status: v1.PipelineRunStatus{ Status: duckv1.Status{ Conditions: duckv1.Conditions{{ @@ -642,83 +547,105 @@ func TestRecordRunningPipelineRunsCountAtNamespaceLevel(t *testing.T) { } } - ctx, _ := ttesting.SetupFakeContext(t) - informer := fakepipelineruninformer.Get(ctx) - // Add N randomly-named PipelineRuns with differently-succeeded statuses. - for _, pipelineRun := range []*v1.PipelineRun{ - newPipelineRun(corev1.ConditionUnknown, "testns1"), - newPipelineRun(corev1.ConditionUnknown, "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - } { - if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { - t.Fatalf("Adding TaskRun to informer: %v", err) + pipelineRuns := []*v1.PipelineRun{ + newPipelineRun(corev1.ConditionUnknown, "testns1", ""), + newPipelineRun(corev1.ConditionUnknown, "testns1", "another"), + newPipelineRun(corev1.ConditionUnknown, "testns1", "another"), + newPipelineRun(corev1.ConditionFalse, "testns1", "another"), + newPipelineRun(corev1.ConditionTrue, "testns1", ""), + newPipelineRun(corev1.ConditionUnknown, "testns2", ""), + newPipelineRun(corev1.ConditionUnknown, "testns2", ""), + newPipelineRun(corev1.ConditionUnknown, "testns2", "another"), + newPipelineRun(corev1.ConditionUnknown, "testns3", ""), + newPipelineRun(corev1.ConditionUnknown, "testns3", ""), + newPipelineRun(corev1.ConditionUnknown, "testns3", ""), + newPipelineRun(corev1.ConditionUnknown, "testns3", ""), + newPipelineRun(corev1.ConditionUnknown, "testns3", "another"), + newPipelineRun(corev1.ConditionFalse, "testns3", ""), + } + + pipelineRunMeasureQueries := []map[string]string{} + pipelineRunExpectedResults := []int64{} + for _, pipelineRun := range pipelineRuns { + if pipelineRun.Status.Conditions[0].Status == corev1.ConditionUnknown { + pipelineRunMeasureQueries = append(pipelineRunMeasureQueries, map[string]string{ + "namespace": pipelineRun.Namespace, + "pipeline": pipelineRun.Spec.PipelineRef.Name, + "pipelinerun": pipelineRun.Name, + }) + pipelineRunExpectedResults = append(pipelineRunExpectedResults, 1) } } - ctx = getConfigContextRunningPRLevel("namespace") - recorder, err := NewRecorder(ctx) - if err != nil { - t.Fatalf("NewRecorder: %v", err) - } - - if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { - t.Errorf("RunningPipelineRuns: %v", err) - } - - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns1"}, 1) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns2"}, 2) - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{"namespace": "testns3"}, 4) -} - -func TestRecordRunningPipelineRunsCountAtClusterLevel(t *testing.T) { - unregisterMetrics() + for _, test := range []struct { + name string + metricLevel string + pipelineRuns []*v1.PipelineRun + measureQueries []map[string]string + expectedResults []int64 + }{{ + name: "pipelinerun at pipeline level", + metricLevel: "pipeline", + pipelineRuns: pipelineRuns, + measureQueries: []map[string]string{ + map[string]string{"namespace": "testns1", "pipeline": "anonymous"}, + map[string]string{"namespace": "testns2", "pipeline": "anonymous"}, + map[string]string{"namespace": "testns3", "pipeline": "anonymous"}, + map[string]string{"namespace": "testns1", "pipeline": "another"}, + }, + expectedResults: []int64{1, 2, 4, 2}, + }, { + name: "pipelinerun at namespace level", + metricLevel: "namespace", + pipelineRuns: pipelineRuns, + measureQueries: []map[string]string{ + map[string]string{"namespace": "testns1"}, + map[string]string{"namespace": "testns2"}, + map[string]string{"namespace": "testns3"}, + }, + expectedResults: []int64{3, 3, 5}, + }, { + name: "pipelinerun at cluster level", + metricLevel: "", + pipelineRuns: pipelineRuns, + measureQueries: []map[string]string{ + map[string]string{}, + }, + expectedResults: []int64{11}, + }, { + name: "pipelinerun at pipelinerun level", + metricLevel: "pipelinerun", + pipelineRuns: pipelineRuns, + measureQueries: pipelineRunMeasureQueries, + expectedResults: pipelineRunExpectedResults, + }} { + t.Run(test.name, func(t *testing.T) { + unregisterMetrics() - newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun { - return &v1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace}, - Status: v1.PipelineRunStatus{ - Status: duckv1.Status{ - Conditions: duckv1.Conditions{{ - Type: apis.ConditionSucceeded, - Status: status, - }}, - }, - }, - } - } + ctx, _ := ttesting.SetupFakeContext(t) + informer := fakepipelineruninformer.Get(ctx) + // Add N randomly-named PipelineRuns with differently-succeeded statuses. + for _, pipelineRun := range test.pipelineRuns { + if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { + t.Fatalf("Adding TaskRun to informer: %v", err) + } + } - ctx, _ := ttesting.SetupFakeContext(t) - informer := fakepipelineruninformer.Get(ctx) - // Add N randomly-named PipelineRuns with differently-succeeded statuses. - for _, pipelineRun := range []*v1.PipelineRun{ - newPipelineRun(corev1.ConditionUnknown, "testns1"), - newPipelineRun(corev1.ConditionUnknown, "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testns2"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - newPipelineRun(corev1.ConditionUnknown, "testns3"), - } { - if err := informer.Informer().GetIndexer().Add(pipelineRun); err != nil { - t.Fatalf("Adding TaskRun to informer: %v", err) - } - } + ctx = getConfigContextRunningPRLevel(test.metricLevel) + recorder, err := NewRecorder(ctx) + if err != nil { + t.Fatalf("NewRecorder: %v", err) + } - ctx = getConfigContextRunningPRLevel("") - recorder, err := NewRecorder(ctx) - if err != nil { - t.Fatalf("NewRecorder: %v", err) - } + if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { + t.Errorf("RunningPipelineRuns: %v", err) + } - if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil { - t.Errorf("RunningPipelineRuns: %v", err) + for idx, query := range test.measureQueries { + checkLastValueDataForTags(t, "running_pipelineruns", query, float64(test.expectedResults[idx])) + } + }) } - - checkLastValueDataForTags(t, "running_pipelineruns", map[string]string{}, 7) } func TestRecordRunningPipelineRunsResolutionWaitCounts(t *testing.T) { @@ -824,8 +751,12 @@ func checkLastValueDataForTags(t *testing.T, name string, wantTags map[string]st continue } val := getLastValueData(data, wantTags) - if expected != val.Value { - t.Error("Value did not match for ", name, wantTags, ", expected", expected, "got", val.Value) + if val == nil { + t.Error("Found no data for ", name, wantTags) + } else { + if expected != val.Value { + t.Error("Value did not match for ", name, wantTags, ", expected", expected, "got", val.Value) + } } } }