chore(): increase test coverage and update dependencies
All checks were successful
ci/woodpecker/pr/pr Pipeline was successful

This commit is contained in:
2025-12-18 23:12:27 +01:00
parent e779c5a38b
commit 2d1aa62c61
15 changed files with 384 additions and 74 deletions

View File

@@ -3,7 +3,6 @@ package woodpecker
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -18,7 +17,7 @@ func DecomAgent(cfg *config.Config, agentId int64) error {
apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId)
req, err := http.NewRequest("DELETE", apiRoute, nil)
if err != nil {
return errors.New(fmt.Sprintf("Could not create delete request: %s", err.Error()))
return fmt.Errorf("Could not create delete request: %s", err.Error())
}
req.Header.Set("Accept", "text/plain")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
@@ -29,7 +28,7 @@ func DecomAgent(cfg *config.Config, agentId int64) error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New(fmt.Sprintf("Could not delete agent: %s", err.Error()))
return fmt.Errorf("Could not delete agent: %s", err.Error())
}
defer resp.Body.Close()
return nil
@@ -39,24 +38,24 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error()))
return 0, fmt.Errorf("Could not create agent query request: %s", err.Error())
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
return 0, fmt.Errorf("Could not query agent list: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
return 0, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
}
agentList := new(models.AgentList)
err = json.NewDecoder(resp.Body).Decode(agentList)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
return 0, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
}
for _, agent := range agentList.Agents {
@@ -67,7 +66,7 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
return int(agent.ID), nil
}
}
return 0, errors.New(fmt.Sprintf("Agent with name %s is not in server", name))
return 0, fmt.Errorf("Agent with name %s is not in server", name)
}
func ListAgents(cfg *config.Config) (*models.AgentList, error) {
@@ -75,23 +74,23 @@ func ListAgents(cfg *config.Config) (*models.AgentList, error) {
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
if err != nil {
return agentList, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error()))
return agentList, fmt.Errorf("Could not create agent query request: %s", err.Error())
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return agentList, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
return agentList, fmt.Errorf("Could not query agent list: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return agentList, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
return agentList, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(agentList)
if err != nil {
return agentList, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
return agentList, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
}
return agentList, nil
}
@@ -111,24 +110,24 @@ func CreateWoodpeckerAgent(cfg *config.Config) (*models.Agent, error) {
}).Debugf("Sending the following data to %s: %s", apiRoute, jsonBody)
req, err := http.NewRequest(http.MethodPost, apiRoute, bodyReader)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not create agent request: %s", err.Error()))
return nil, fmt.Errorf("Could not create agent request: %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not create new Agent: %s", err.Error()))
return nil, fmt.Errorf("Could not create new Agent: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
return nil, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
}
newAgent := new(models.Agent)
err = json.NewDecoder(resp.Body).Decode(newAgent)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
return nil, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
}
return newAgent, nil

View File

@@ -0,0 +1,110 @@
package woodpecker
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
)
func TestCreateAndGetAndDeleteAgent(t *testing.T) {
// prepare a fake agent to return
createdAgent := models.Agent{
ID: 42,
Name: "woodpecker-autoscaler-agent-abcde",
Token: "tok",
}
mux := http.NewServeMux()
mux.HandleFunc("/api/agents", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// ensure content-type
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("expected json content-type, got %s", ct)
}
body, _ := io.ReadAll(r.Body)
defer r.Body.Close()
if !strings.Contains(string(body), "woodpecker-autoscaler-agent-") {
t.Fatalf("unexpected agent request body: %s", string(body))
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(createdAgent)
return
}
// For GET listing, return an AgentList
w.WriteHeader(http.StatusOK)
list := models.AgentList{Agents: []models.Agent{createdAgent}}
_ = json.NewEncoder(w).Encode(list)
})
mux.HandleFunc("/api/agents?page=1&perPage=100", func(w http.ResponseWriter, r *http.Request) {
// return list in expected format for GetAgentIdByName
w.WriteHeader(http.StatusOK)
// GetAgentIdByName expects a models.AgentList; encode accordingly
list := models.AgentList{Agents: []models.Agent{createdAgent}}
_ = json.NewEncoder(w).Encode(list)
})
// handle delete
mux.HandleFunc(fmt.Sprintf("/api/agents/%d", createdAgent.ID), func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Fatalf("expected DELETE, got %s", r.Method)
}
w.WriteHeader(http.StatusOK)
})
srv := httptest.NewServer(mux)
defer srv.Close()
cfg := config.Config{
WoodpeckerInstance: srv.URL,
WoodpeckerApiToken: "testtoken",
}
// Test CreateWoodpeckerAgent
a, err := CreateWoodpeckerAgent(&cfg)
if err != nil {
t.Fatalf("CreateWoodpeckerAgent failed: %v", err)
}
if a == nil || !strings.HasPrefix(a.Name, "woodpecker-autoscaler-agent-") {
t.Fatalf("unexpected agent returned: %#v", a)
}
// Test GetAgentIdByName
id, err := GetAgentIdByName(&cfg, a.Name)
if err != nil {
t.Fatalf("GetAgentIdByName failed: %v", err)
}
if id != int(a.ID) {
t.Fatalf("unexpected id: got %d want %d", id, a.ID)
}
// Test DecomAgent
if err := DecomAgent(&cfg, a.ID); err != nil {
t.Fatalf("DecomAgent failed: %v", err)
}
}
func TestGetAgentIdByName_NotFound(t *testing.T) {
// server returns empty list
mux := http.NewServeMux()
mux.HandleFunc("/api/agents?page=1&perPage=100", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
list := models.AgentList{Agents: []models.Agent{{ID: 1, Name: "other"}}}
_ = json.NewEncoder(w).Encode(list)
})
srv := httptest.NewServer(mux)
defer srv.Close()
cfg := config.Config{WoodpeckerInstance: srv.URL, WoodpeckerApiToken: "t"}
_, err := GetAgentIdByName(&cfg, "nonexistent")
if err == nil {
t.Fatalf("expected error for unknown agent name")
}
}

View File

@@ -2,7 +2,6 @@ package woodpecker
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
@@ -17,19 +16,19 @@ func QueueInfo(cfg *config.Config, target interface{}) error {
apiRoute := fmt.Sprintf("%s/api/queue/info", cfg.WoodpeckerInstance)
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
if err != nil {
return errors.New(fmt.Sprintf("Could not create queue request: %s", err.Error()))
return fmt.Errorf("Could not create queue request: %s", err.Error())
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New(fmt.Sprintf("Could not query queue info: %s", err.Error()))
return fmt.Errorf("Could not query queue info: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(fmt.Sprintf("Error from queue info api: %s", resp.Status))
return fmt.Errorf("Error from queue info api: %s", resp.Status)
}
return json.NewDecoder(resp.Body).Decode(target)
@@ -40,7 +39,7 @@ func CheckPending(cfg *config.Config) (int, error) {
queueInfo := new(models.QueueInfo)
err := QueueInfo(cfg, queueInfo)
if err != nil {
return 0, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
return 0, fmt.Errorf("Error from QueueInfo: %s", err.Error())
}
count := 0
if queueInfo.Stats.PendingCount > 0 {
@@ -64,7 +63,7 @@ func CheckRunning(cfg *config.Config) (int, error) {
queueInfo := new(models.QueueInfo)
err := QueueInfo(cfg, queueInfo)
if err != nil {
return 0, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
return 0, fmt.Errorf("Error from QueueInfo: %s", err.Error())
}
count := 0
if queueInfo.Stats.RunningCount > 0 {

View File

@@ -0,0 +1,66 @@
package woodpecker
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
)
func TestQueueInfoAndChecks(t *testing.T) {
// Create queue info with one pending job matching label and one running matching
qi := models.QueueInfo{
Pending: []models.JobInformation{
{ID: "1", Labels: map[string]string{"role": "worker"}},
},
Running: []models.JobInformation{
{ID: "2", Labels: map[string]string{"role": "worker"}},
},
Stats: models.Stats{PendingCount: 1, RunningCount: 1},
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/queue/info" {
w.WriteHeader(http.StatusNotFound)
return
}
_ = json.NewEncoder(w).Encode(qi)
}))
defer srv.Close()
cfg := config.Config{
WoodpeckerInstance: srv.URL,
WoodpeckerApiToken: "t",
WoodpeckerLabelSelector: "role=worker",
}
// Test QueueInfo
var got models.QueueInfo
if err := QueueInfo(&cfg, &got); err != nil {
t.Fatalf("QueueInfo failed: %v", err)
}
if got.Stats.PendingCount != 1 || got.Stats.RunningCount != 1 {
t.Fatalf("unexpected stats: %#v", got.Stats)
}
// Test CheckPending
pending, err := CheckPending(&cfg)
if err != nil {
t.Fatalf("CheckPending error: %v", err)
}
if pending != 1 {
t.Fatalf("expected 1 pending, got %d", pending)
}
// Test CheckRunning
running, err := CheckRunning(&cfg)
if err != nil {
t.Fatalf("CheckRunning error: %v", err)
}
if running != 1 {
t.Fatalf("expected 1 running, got %d", running)
}
}