Skip to content

Instantly share code, notes, and snippets.

@afrittoli
Last active October 17, 2024 09:50
Show Gist options
  • Save afrittoli/dc3ea2a29b2be96e87e26a7536947ef6 to your computer and use it in GitHub Desktop.
Save afrittoli/dc3ea2a29b2be96e87e26a7536947ef6 to your computer and use it in GitHub Desktop.
diff --git a/pkg/pipelinerunmetrics/metrics_test.go b/pkg/pipelinerunmetrics/metrics_test.go
index 9cefb0310..8199e29db 100644
--- a/pkg/pipelinerunmetrics/metrics_test.go
+++ b/pkg/pipelinerunmetrics/metrics_test.go
@@ -524,11 +524,18 @@ func TestRecordRunningPipelineRunsCount(t *testing.T) {
}
func TestRecordRunningPipelineRunsCountAtPipelineLevel(t *testing.T) {
- unregisterMetrics()
- newPipelineRun := func(status corev1.ConditionStatus, namespace string) *v1.PipelineRun {
+ newPipelineRun := func(status corev1.ConditionStatus, namespace string, name string) *v1.PipelineRun {
+ if name == "" {
+ name = "anonymous"
+ }
return &v1.PipelineRun{
- ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun-"), Namespace: namespace},
+ ObjectMeta: metav1.ObjectMeta{Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pipelinerun"), Namespace: namespace},
+ Spec: v1.PipelineRunSpec{
+ PipelineRef: &v1.PipelineRef{
+ Name: name,
+ },
+ },
Status: v1.PipelineRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{{
@@ -540,36 +547,105 @@ func TestRecordRunningPipelineRunsCountAtPipelineLevel(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", ""),
}
- ctx = getConfigContextRunningPRLevel("pipeline")
- recorder, err := NewRecorder(ctx)
- if err != nil {
- t.Fatalf("NewRecorder: %v", err)
+ 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)
+ }
}
- if err := recorder.RunningPipelineRuns(informer.Lister()); err != nil {
- t.Errorf("RunningPipelineRuns: %v", err)
- }
+ 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()
- 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)
+ 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 = getConfigContextRunningPRLevel(test.metricLevel)
+ 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)
+ }
+
+ for idx, query := range test.measureQueries {
+ checkLastValueDataForTags(t, "running_pipelineruns", query, float64(test.expectedResults[idx]))
+ }
+ })
+ }
}
func TestRecordRunningPipelineRunsResolutionWaitCounts(t *testing.T) {
@@ -675,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)
+ }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment