Skip to content

Commit

Permalink
Adding Tests for better coverage of different running-pipelinerun use…
Browse files Browse the repository at this point in the history
… cases.
  • Loading branch information
pramodbindal committed Oct 17, 2024
1 parent d1757a6 commit b7756ef
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/pipelinerunmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func (r *Recorder) RunningPipelineRuns(lister listers.PipelineRunLister) error {
mutators := []tag.Mutator{
tag.Insert(namespaceTag, pr.Namespace),
tag.Insert(pipelineTag, pipelineName),
tag.Insert(pipelinerunTag, pr.Name),
}
if r.cfg != nil {
switch r.cfg.RunningPipelinerunLevel {
Expand Down
150 changes: 150 additions & 0 deletions pkg/pipelinerunmetrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,59 @@ func TestRecordRunningPipelineRunsCount(t *testing.T) {
metricstest.CheckLastValueData(t, "running_pipelineruns", map[string]string{}, 1)
}

func TestRecordRunningPipelineRunsCountAtPipelineRunLevel(t *testing.T) {
unregisterMetrics()

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)
}
}

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()

Expand Down Expand Up @@ -572,6 +625,103 @@ func TestRecordRunningPipelineRunsCountAtPipelineLevel(t *testing.T) {
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{{
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, "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("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()

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 []*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("")
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{}, 7)

}

func TestRecordRunningPipelineRunsResolutionWaitCounts(t *testing.T) {
multiplier := 3
for _, tc := range []struct {
Expand Down

0 comments on commit b7756ef

Please sign in to comment.