mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-11-05 06:40:07 +08:00
The test context is cancelled when the test ends, so anything still holding it stops instead of leaking into the next test. Uses inside `t.Cleanup`, `defer` and `go func` blocks are left alone, as the context is already cancelled there. Stacked on https://gitea.com/gitea/runner/pulls/1305. Assisted-by: Codet Co-authored-by: bircni <bircni@icloud.com> Reviewed-on: https://gitea.com/gitea/runner/pulls/1306 Reviewed-by: bircni <bircni@icloud.com>
112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package metrics
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
runnerv1 "gitea.dev/actionslib/runner/v1"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
dto "github.com/prometheus/client_model/go"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestResultToStatusLabel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
result runnerv1.Result
|
|
want string
|
|
}{
|
|
{"success", runnerv1.Result_RESULT_SUCCESS, LabelStatusSuccess},
|
|
{"failure", runnerv1.Result_RESULT_FAILURE, LabelStatusFailure},
|
|
{"cancelled", runnerv1.Result_RESULT_CANCELLED, LabelStatusCancelled},
|
|
{"skipped", runnerv1.Result_RESULT_SKIPPED, LabelStatusSkipped},
|
|
{"unspecified", runnerv1.Result_RESULT_UNSPECIFIED, LabelStatusUnknown},
|
|
{"out of range", runnerv1.Result(999), LabelStatusUnknown},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.want, ResultToStatusLabel(tt.result))
|
|
})
|
|
}
|
|
expired, cancel := context.WithDeadline(t.Context(), time.Time{})
|
|
defer cancel()
|
|
require.Equal(t, LabelStatusTimeout, JobStatus(expired, runnerv1.Result_RESULT_FAILURE))
|
|
require.Equal(t, LabelStatusSuccess, JobStatus(expired, runnerv1.Result_RESULT_SUCCESS))
|
|
require.Equal(t, LabelStatusCancelled, JobStatus(t.Context(), runnerv1.Result_RESULT_CANCELLED))
|
|
}
|
|
|
|
func TestInitAndDynamicMetricRegistration(t *testing.T) {
|
|
oldRegistry := Registry
|
|
t.Cleanup(func() {
|
|
Registry = oldRegistry
|
|
})
|
|
|
|
Registry = prometheus.NewRegistry()
|
|
initOnce = sync.Once{}
|
|
|
|
Init()
|
|
Init()
|
|
RunnerInfo.WithLabelValues("test", "runner").Set(1)
|
|
RegisterUptimeFunc(time.Now().Add(-time.Second))
|
|
RegisterRunningJobsFunc(func() int64 { return 2 }, 4)
|
|
RegisterStateFunc(func() string { return LabelStateIdle })
|
|
RegisterStateFunc(func() string { return LabelStateBusy })
|
|
|
|
metrics, err := Registry.Gather()
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, hasMetric(metrics, "gitea_runner_info"))
|
|
require.True(t, hasMetric(metrics, "gitea_runner_uptime_seconds"))
|
|
require.True(t, hasMetric(metrics, "gitea_runner_job_running"))
|
|
require.True(t, hasMetric(metrics, "gitea_runner_job_capacity_utilization_ratio"))
|
|
states := map[string]float64{}
|
|
for _, mf := range metrics {
|
|
if mf.GetName() == "gitea_runner_state" {
|
|
for _, m := range mf.GetMetric() {
|
|
states[m.GetLabel()[0].GetValue()] = m.GetGauge().GetValue()
|
|
}
|
|
}
|
|
}
|
|
require.Equal(t, map[string]float64{LabelStateBusy: 1, LabelStateIdle: 0, LabelStateUnavailable: 0}, states)
|
|
}
|
|
|
|
func TestRegisterRunningJobsFuncZeroCapacity(t *testing.T) {
|
|
oldRegistry := Registry
|
|
t.Cleanup(func() { Registry = oldRegistry })
|
|
Registry = prometheus.NewRegistry()
|
|
|
|
RegisterRunningJobsFunc(func() int64 { return 3 }, 0)
|
|
|
|
metrics, err := Registry.Gather()
|
|
require.NoError(t, err)
|
|
for _, mf := range metrics {
|
|
if mf.GetName() == "gitea_runner_job_capacity_utilization_ratio" {
|
|
require.Len(t, mf.GetMetric(), 1)
|
|
require.InDelta(t, 0, mf.GetMetric()[0].GetGauge().GetValue(), 0)
|
|
return
|
|
}
|
|
}
|
|
t.Fatal("capacity utilization metric not gathered")
|
|
}
|
|
|
|
func TestStartServerCanBeCancelled(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
StartServer(ctx, "127.0.0.1:0", nil)
|
|
cancel()
|
|
}
|
|
|
|
func hasMetric(metrics []*dto.MetricFamily, name string) bool {
|
|
for _, mf := range metrics {
|
|
if strings.EqualFold(mf.GetName(), name) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|