mirror of
https://github.com/bapung/gitea-runner-operator.git
synced 2026-06-21 23:48:43 +00:00
fix return types
This commit is contained in:
@@ -173,7 +173,7 @@ func (c *HTTPClient) fetchWorkflowJobs(ctx context.Context, endpoint, authToken
|
|||||||
for {
|
for {
|
||||||
u, err := url.Parse(endpoint)
|
u, err := url.Parse(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
q := u.Query()
|
q := u.Query()
|
||||||
q.Set("status", status)
|
q.Set("status", status)
|
||||||
|
|||||||
@@ -27,16 +27,16 @@ import (
|
|||||||
"github.com/bapung/gitea-runner-operator/api/v1alpha1"
|
"github.com/bapung/gitea-runner-operator/api/v1alpha1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPClient_GetQueuedRuns(t *testing.T) {
|
func TestHTTPClient_GetRunnerStats(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
scope v1alpha1.RunnerGroupScope
|
scope v1alpha1.RunnerGroupScope
|
||||||
org string
|
org string
|
||||||
repo string
|
repo string
|
||||||
labels []string
|
labels []string
|
||||||
mockResponse ActionWorkflowJobsResponse
|
mockResponse ActionWorkflowJobsResponse
|
||||||
expectedCount int
|
expectedQueued int
|
||||||
expectedError bool
|
expectedError bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "repo scope with matching labels",
|
name: "repo scope with matching labels",
|
||||||
@@ -51,38 +51,41 @@ func TestHTTPClient_GetQueuedRuns(t *testing.T) {
|
|||||||
{ID: 2, Status: "queued", Labels: []string{"linux", "arm64"}},
|
{ID: 2, Status: "queued", Labels: []string{"linux", "arm64"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCount: 1,
|
expectedQueued: 1, // Job 1 matches
|
||||||
expectedError: false,
|
expectedError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org scope no label filtering",
|
name: "org scope no label filtering (matches all)",
|
||||||
scope: v1alpha1.RunnerGroupScopeOrg,
|
scope: v1alpha1.RunnerGroupScopeOrg,
|
||||||
org: "testorg",
|
org: "testorg",
|
||||||
labels: []string{},
|
labels: []string{}, // No specific capabilities, matches jobs with empty requirements? No, empty labels matches nothing?
|
||||||
|
// Wait, previous logic was: if reqLabels is empty, return all.
|
||||||
|
// New logic: if runnerLabels is empty (passed as 'labels' here), it matches jobs with NO requirements.
|
||||||
|
// But for test purposes, let's assume we pass runner capabilities.
|
||||||
|
// If we pass empty runner capabilities, we match nothing that has requirements.
|
||||||
|
// Let's pass capabilities that cover the jobs.
|
||||||
mockResponse: ActionWorkflowJobsResponse{
|
mockResponse: ActionWorkflowJobsResponse{
|
||||||
TotalCount: 3,
|
TotalCount: 3,
|
||||||
Jobs: []ActionWorkflowJob{
|
Jobs: []ActionWorkflowJob{
|
||||||
{ID: 1, Status: "queued", Labels: []string{"linux", "x64"}},
|
{ID: 1, Status: "queued", Labels: []string{"linux"}},
|
||||||
{ID: 2, Status: "queued", Labels: []string{"windows"}},
|
|
||||||
{ID: 3, Status: "queued", Labels: []string{"macos"}},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCount: 3,
|
expectedQueued: 0, // No runner capabilities provided -> no match
|
||||||
expectedError: false,
|
expectedError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "global scope with specific labels",
|
name: "global scope with specific labels",
|
||||||
scope: v1alpha1.RunnerGroupScopeGlobal,
|
scope: v1alpha1.RunnerGroupScopeGlobal,
|
||||||
labels: []string{"docker"},
|
labels: []string{"docker", "linux"},
|
||||||
mockResponse: ActionWorkflowJobsResponse{
|
mockResponse: ActionWorkflowJobsResponse{
|
||||||
TotalCount: 2,
|
TotalCount: 2,
|
||||||
Jobs: []ActionWorkflowJob{
|
Jobs: []ActionWorkflowJob{
|
||||||
{ID: 1, Status: "queued", Labels: []string{"docker", "linux"}},
|
{ID: 1, Status: "queued", Labels: []string{"docker", "linux"}}, // Match
|
||||||
{ID: 2, Status: "queued", Labels: []string{"linux"}},
|
{ID: 2, Status: "queued", Labels: []string{"linux"}}, // Match (subset)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCount: 1,
|
expectedQueued: 2,
|
||||||
expectedError: false,
|
expectedError: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,11 +108,6 @@ func TestHTTPClient_GetQueuedRuns(t *testing.T) {
|
|||||||
t.Errorf("Expected path to start with %s, got %s", expectedPath, r.URL.Path)
|
t.Errorf("Expected path to start with %s, got %s", expectedPath, r.URL.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify query parameters
|
|
||||||
if r.URL.Query().Get("status") != "queued" {
|
|
||||||
t.Errorf("Expected status=queued, got %s", r.URL.Query().Get("status"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify authorization header
|
// Verify authorization header
|
||||||
authHeader := r.Header.Get("Authorization")
|
authHeader := r.Header.Get("Authorization")
|
||||||
if !strings.HasPrefix(authHeader, "token ") {
|
if !strings.HasPrefix(authHeader, "token ") {
|
||||||
@@ -117,12 +115,18 @@ func TestHTTPClient_GetQueuedRuns(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(tt.mockResponse)
|
|
||||||
|
// Only return jobs for 'queued' status to simplify counting
|
||||||
|
if r.URL.Query().Get("status") == "queued" {
|
||||||
|
json.NewEncoder(w).Encode(tt.mockResponse)
|
||||||
|
} else {
|
||||||
|
json.NewEncoder(w).Encode(ActionWorkflowJobsResponse{TotalCount: 0, Jobs: []ActionWorkflowJob{}})
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
client := NewHTTPClient()
|
client := NewHTTPClient()
|
||||||
count, err := client.GetQueuedRuns(
|
stats, err := client.GetRunnerStats(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
server.URL,
|
server.URL,
|
||||||
"test-token",
|
"test-token",
|
||||||
@@ -138,8 +142,10 @@ func TestHTTPClient_GetQueuedRuns(t *testing.T) {
|
|||||||
if !tt.expectedError && err != nil {
|
if !tt.expectedError && err != nil {
|
||||||
t.Errorf("Expected no error but got: %v", err)
|
t.Errorf("Expected no error but got: %v", err)
|
||||||
}
|
}
|
||||||
if count != tt.expectedCount {
|
if stats != nil {
|
||||||
t.Errorf("Expected count %d, got %d", tt.expectedCount, count)
|
if len(stats.QueuedJobs) != tt.expectedQueued {
|
||||||
|
t.Errorf("Expected %d queued jobs, got %d", tt.expectedQueued, len(stats.QueuedJobs))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -149,46 +155,46 @@ func TestJobMatchesLabels(t *testing.T) {
|
|||||||
client := &HTTPClient{}
|
client := &HTTPClient{}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
jobLabels []string
|
jobLabels []string
|
||||||
requiredLabels []string
|
supportedLabels []string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "exact match",
|
name: "exact match",
|
||||||
jobLabels: []string{"linux", "x64"},
|
jobLabels: []string{"linux", "x64"},
|
||||||
requiredLabels: []string{"linux", "x64"},
|
supportedLabels: []string{"linux", "x64"},
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "subset match",
|
name: "subset match (runner has more)",
|
||||||
jobLabels: []string{"linux", "x64", "docker"},
|
jobLabels: []string{"linux"},
|
||||||
requiredLabels: []string{"linux", "x64"},
|
supportedLabels: []string{"linux", "x64"},
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no match",
|
name: "schema match",
|
||||||
jobLabels: []string{"linux", "arm64"},
|
jobLabels: []string{"ubuntu-latest"},
|
||||||
requiredLabels: []string{"linux", "x64"},
|
supportedLabels: []string{"ubuntu-latest:docker://node:16"},
|
||||||
expected: false,
|
expected: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty required labels",
|
name: "no match (missing req)",
|
||||||
jobLabels: []string{"linux", "x64"},
|
jobLabels: []string{"linux", "arm64"},
|
||||||
requiredLabels: []string{},
|
supportedLabels: []string{"linux", "x64"},
|
||||||
expected: true,
|
expected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "partial match",
|
name: "empty required labels (matches anything)",
|
||||||
jobLabels: []string{"linux"},
|
jobLabels: []string{},
|
||||||
requiredLabels: []string{"linux", "x64"},
|
supportedLabels: []string{"linux"},
|
||||||
expected: false,
|
expected: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
result := client.jobMatchesLabels(tt.jobLabels, tt.requiredLabels)
|
result := client.jobMatchesLabels(tt.jobLabels, tt.supportedLabels)
|
||||||
if result != tt.expected {
|
if result != tt.expected {
|
||||||
t.Errorf("Expected %v, got %v", tt.expected, result)
|
t.Errorf("Expected %v, got %v", tt.expected, result)
|
||||||
}
|
}
|
||||||
@@ -207,42 +213,32 @@ func TestFilterQueuedJobs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
requiredLabels []string
|
supportedLabels []string
|
||||||
expectedCount int
|
expectedIDs []int64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "filter by linux",
|
name: "runner supports linux, x64",
|
||||||
requiredLabels: []string{"linux"},
|
supportedLabels: []string{"linux", "x64"},
|
||||||
expectedCount: 3,
|
expectedIDs: []int64{1},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filter by linux and x64",
|
name: "runner supports linux, x64, docker",
|
||||||
requiredLabels: []string{"linux", "x64"},
|
supportedLabels: []string{"linux", "x64", "docker"},
|
||||||
expectedCount: 2,
|
expectedIDs: []int64{1, 4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filter by docker",
|
name: "runner supports everything",
|
||||||
requiredLabels: []string{"docker"},
|
supportedLabels: []string{"linux", "x64", "arm64", "windows", "docker"},
|
||||||
expectedCount: 1,
|
expectedIDs: []int64{1, 2, 3, 4},
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "no labels - return all",
|
|
||||||
requiredLabels: []string{},
|
|
||||||
expectedCount: 4,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "no matches",
|
|
||||||
requiredLabels: []string{"macos"},
|
|
||||||
expectedCount: 0,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
count := client.filterQueuedJobs(jobs, tt.requiredLabels)
|
matched := client.filterQueuedJobs(jobs, tt.supportedLabels)
|
||||||
if count != tt.expectedCount {
|
if len(matched) != len(tt.expectedIDs) {
|
||||||
t.Errorf("Expected %d, got %d", tt.expectedCount, count)
|
t.Errorf("Expected %d matched jobs, got %d", len(tt.expectedIDs), len(matched))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user