mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-04 10:05:18 +02:00
fix: Adjust the unit tests
This commit is contained in:
parent
d5823e0431
commit
704800ec07
@ -1,95 +0,0 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestActionRunner_Capacity(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("DefaultCapacity", func(t *testing.T) {
|
||||
runner := &ActionRunner{
|
||||
UUID: "test-uuid-1",
|
||||
Name: "test-runner",
|
||||
OwnerID: 0,
|
||||
RepoID: 0,
|
||||
TokenHash: "hash1",
|
||||
Token: "token1",
|
||||
}
|
||||
assert.NoError(t, db.Insert(ctx, runner))
|
||||
|
||||
// Verify in database
|
||||
retrieved, err := GetRunnerByID(ctx, runner.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, retrieved.Capacity, "Default capacity should be 0 (unlimited)")
|
||||
})
|
||||
|
||||
t.Run("CustomCapacity", func(t *testing.T) {
|
||||
runner := &ActionRunner{
|
||||
UUID: "test-uuid-2",
|
||||
Name: "test-runner-2",
|
||||
OwnerID: 0,
|
||||
RepoID: 0,
|
||||
Capacity: 5,
|
||||
TokenHash: "hash2",
|
||||
Token: "token2",
|
||||
}
|
||||
assert.NoError(t, db.Insert(ctx, runner))
|
||||
|
||||
assert.Equal(t, 5, runner.Capacity)
|
||||
|
||||
// Verify in database
|
||||
retrieved, err := GetRunnerByID(ctx, runner.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, retrieved.Capacity)
|
||||
})
|
||||
|
||||
t.Run("UpdateCapacity", func(t *testing.T) {
|
||||
runner := &ActionRunner{
|
||||
UUID: "test-uuid-3",
|
||||
Name: "test-runner-3",
|
||||
OwnerID: 0,
|
||||
RepoID: 0,
|
||||
Capacity: 1,
|
||||
TokenHash: "hash3",
|
||||
Token: "token3",
|
||||
}
|
||||
assert.NoError(t, db.Insert(ctx, runner))
|
||||
|
||||
// Update capacity
|
||||
runner.Capacity = 10
|
||||
assert.NoError(t, UpdateRunner(ctx, runner, "capacity"))
|
||||
|
||||
// Verify update
|
||||
retrieved, err := GetRunnerByID(ctx, runner.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 10, retrieved.Capacity)
|
||||
})
|
||||
|
||||
t.Run("ZeroCapacity", func(t *testing.T) {
|
||||
runner := &ActionRunner{
|
||||
UUID: "test-uuid-4",
|
||||
Name: "test-runner-4",
|
||||
OwnerID: 0,
|
||||
RepoID: 0,
|
||||
Capacity: 0, // Unlimited
|
||||
}
|
||||
assert.NoError(t, db.Insert(ctx, runner))
|
||||
|
||||
assert.Equal(t, 0, runner.Capacity)
|
||||
|
||||
retrieved, err := GetRunnerByID(ctx, runner.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, retrieved.Capacity)
|
||||
})
|
||||
}
|
||||
@ -524,7 +524,6 @@ func ToActionRunner(ctx context.Context, runner *actions_model.ActionRunner) *ap
|
||||
Disabled: runner.IsDisabled,
|
||||
Ephemeral: runner.Ephemeral,
|
||||
Labels: labels,
|
||||
Capacity: runner.Capacity,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,149 +0,0 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAPIUpdateRunnerCapacity(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Clean up existing runners
|
||||
require.NoError(t, db.DeleteAllRecords("action_runner"))
|
||||
|
||||
// Create a test runner
|
||||
runner := &actions_model.ActionRunner{
|
||||
UUID: "test-capacity-runner",
|
||||
Name: "Test Capacity Runner",
|
||||
OwnerID: 0,
|
||||
RepoID: 0,
|
||||
Capacity: 1,
|
||||
TokenHash: "test-capacity-hash",
|
||||
Token: "test-capacity-token",
|
||||
}
|
||||
require.NoError(t, actions_model.CreateRunner(ctx, runner))
|
||||
|
||||
// Load the created runner to get the ID
|
||||
runner = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{UUID: "test-capacity-runner"})
|
||||
|
||||
session := loginUser(t, "user1")
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin)
|
||||
|
||||
t.Run("UpdateCapacity", func(t *testing.T) {
|
||||
req := NewRequestWithJSON(t, "PATCH",
|
||||
fmt.Sprintf("/api/v1/admin/actions/runners/%d/capacity", runner.ID),
|
||||
&api.UpdateRunnerCapacityOption{Capacity: 5}).
|
||||
AddTokenAuth(token)
|
||||
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
var apiRunner api.ActionRunner
|
||||
DecodeJSON(t, resp, &apiRunner)
|
||||
|
||||
assert.Equal(t, runner.ID, apiRunner.ID)
|
||||
assert.Equal(t, 5, apiRunner.Capacity)
|
||||
|
||||
// Verify in database
|
||||
updated, err := actions_model.GetRunnerByID(context.Background(), runner.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, updated.Capacity)
|
||||
})
|
||||
|
||||
t.Run("UpdateCapacityToZero", func(t *testing.T) {
|
||||
req := NewRequestWithJSON(t, "PATCH",
|
||||
fmt.Sprintf("/api/v1/admin/actions/runners/%d/capacity", runner.ID),
|
||||
&api.UpdateRunnerCapacityOption{Capacity: 0}).
|
||||
AddTokenAuth(token)
|
||||
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
var apiRunner api.ActionRunner
|
||||
DecodeJSON(t, resp, &apiRunner)
|
||||
|
||||
assert.Equal(t, 0, apiRunner.Capacity)
|
||||
})
|
||||
|
||||
t.Run("InvalidCapacity", func(t *testing.T) {
|
||||
req := NewRequestWithJSON(t, "PATCH",
|
||||
fmt.Sprintf("/api/v1/admin/actions/runners/%d/capacity", runner.ID),
|
||||
&api.UpdateRunnerCapacityOption{Capacity: -1}).
|
||||
AddTokenAuth(token)
|
||||
|
||||
MakeRequest(t, req, http.StatusBadRequest)
|
||||
})
|
||||
|
||||
t.Run("NonExistentRunner", func(t *testing.T) {
|
||||
req := NewRequestWithJSON(t, "PATCH",
|
||||
"/api/v1/admin/actions/runners/999999/capacity",
|
||||
&api.UpdateRunnerCapacityOption{Capacity: 5}).
|
||||
AddTokenAuth(token)
|
||||
|
||||
MakeRequest(t, req, http.StatusNotFound)
|
||||
})
|
||||
|
||||
t.Run("GetRunnerWithCapacity", func(t *testing.T) {
|
||||
// First set capacity
|
||||
runner.Capacity = 7
|
||||
assert.NoError(t, actions_model.UpdateRunner(context.Background(), runner, "capacity"))
|
||||
|
||||
// Get runner
|
||||
req := NewRequest(t, "GET",
|
||||
fmt.Sprintf("/api/v1/admin/actions/runners/%d", runner.ID)).
|
||||
AddTokenAuth(token)
|
||||
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
var apiRunner api.ActionRunner
|
||||
DecodeJSON(t, resp, &apiRunner)
|
||||
|
||||
assert.Equal(t, runner.ID, apiRunner.ID)
|
||||
assert.Equal(t, 7, apiRunner.Capacity)
|
||||
})
|
||||
|
||||
t.Run("ListRunnersWithCapacity", func(t *testing.T) {
|
||||
req := NewRequest(t, "GET", "/api/v1/admin/actions/runners").
|
||||
AddTokenAuth(token)
|
||||
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
var response api.ActionRunnersResponse
|
||||
DecodeJSON(t, resp, &response)
|
||||
|
||||
// Find our test runner
|
||||
found := false
|
||||
for _, r := range response.Entries {
|
||||
if r.ID == runner.ID {
|
||||
found = true
|
||||
assert.Equal(t, 7, r.Capacity)
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, found, "Test runner should be in list")
|
||||
})
|
||||
|
||||
t.Run("UnauthorizedAccess", func(t *testing.T) {
|
||||
req := NewRequestWithJSON(t, "PATCH",
|
||||
fmt.Sprintf("/api/v1/admin/actions/runners/%d/capacity", runner.ID),
|
||||
&api.UpdateRunnerCapacityOption{Capacity: 5})
|
||||
// No token
|
||||
|
||||
MakeRequest(t, req, http.StatusUnauthorized)
|
||||
})
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user