|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/actions/actions-runner-controller/github/actions" |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMetricsWithWorkflowRefParsing(t *testing.T) { |
| 12 | + // Create a test exporter |
| 13 | + exporter := &exporter{ |
| 14 | + scaleSetLabels: prometheus.Labels{ |
| 15 | + labelKeyEnterprise: "test-enterprise", |
| 16 | + labelKeyOrganization: "test-org", |
| 17 | + labelKeyRepository: "test-repo", |
| 18 | + labelKeyRunnerScaleSetName: "test-scale-set", |
| 19 | + labelKeyRunnerScaleSetNamespace: "test-namespace", |
| 20 | + }, |
| 21 | + } |
| 22 | + |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + jobBase actions.JobMessageBase |
| 26 | + wantName string |
| 27 | + wantTarget string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "main branch workflow", |
| 31 | + jobBase: actions.JobMessageBase{ |
| 32 | + OwnerName: "actions", |
| 33 | + RepositoryName: "runner", |
| 34 | + JobDisplayName: "Build and Test", |
| 35 | + JobWorkflowRef: "actions/runner/.github/workflows/build.yml@refs/heads/main", |
| 36 | + EventName: "push", |
| 37 | + }, |
| 38 | + wantName: "build", |
| 39 | + wantTarget: "heads/main", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "feature branch workflow", |
| 43 | + jobBase: actions.JobMessageBase{ |
| 44 | + OwnerName: "myorg", |
| 45 | + RepositoryName: "myrepo", |
| 46 | + JobDisplayName: "CI/CD Pipeline", |
| 47 | + JobWorkflowRef: "myorg/myrepo/.github/workflows/ci-cd-pipeline.yml@refs/heads/feature/new-metrics", |
| 48 | + EventName: "push", |
| 49 | + }, |
| 50 | + wantName: "ci-cd-pipeline", |
| 51 | + wantTarget: "heads/feature/new-metrics", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "pull request workflow", |
| 55 | + jobBase: actions.JobMessageBase{ |
| 56 | + OwnerName: "actions", |
| 57 | + RepositoryName: "runner", |
| 58 | + JobDisplayName: "PR Checks", |
| 59 | + JobWorkflowRef: "actions/runner/.github/workflows/pr-checks.yml@refs/pull/123/merge", |
| 60 | + EventName: "pull_request", |
| 61 | + }, |
| 62 | + wantName: "pr-checks", |
| 63 | + wantTarget: "pull/123", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "tag workflow", |
| 67 | + jobBase: actions.JobMessageBase{ |
| 68 | + OwnerName: "actions", |
| 69 | + RepositoryName: "runner", |
| 70 | + JobDisplayName: "Release", |
| 71 | + JobWorkflowRef: "actions/runner/.github/workflows/release.yml@refs/tags/v1.2.3", |
| 72 | + EventName: "release", |
| 73 | + }, |
| 74 | + wantName: "release", |
| 75 | + wantTarget: "tags/v1.2.3", |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + labels := exporter.jobLabels(&tt.jobBase) |
| 82 | + |
| 83 | + // Build expected labels |
| 84 | + expectedLabels := prometheus.Labels{ |
| 85 | + labelKeyEnterprise: "test-enterprise", |
| 86 | + labelKeyOrganization: tt.jobBase.OwnerName, |
| 87 | + labelKeyRepository: tt.jobBase.RepositoryName, |
| 88 | + labelKeyJobName: tt.jobBase.JobDisplayName, |
| 89 | + labelKeyJobWorkflowRef: tt.jobBase.JobWorkflowRef, |
| 90 | + labelKeyJobWorkflowName: tt.wantName, |
| 91 | + labelKeyJobWorkflowTarget: tt.wantTarget, |
| 92 | + labelKeyEventName: tt.jobBase.EventName, |
| 93 | + } |
| 94 | + |
| 95 | + // Assert all expected labels match |
| 96 | + assert.Equal(t, expectedLabels, labels, "jobLabels() returned unexpected labels for %s", tt.name) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments