chore(): increase test coverage and update dependencies
Some checks are pending
ci/woodpecker/pr/pr Pipeline is pending
Some checks are pending
ci/woodpecker/pr/pr Pipeline is pending
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -36,7 +35,7 @@ func GenConfig() (cfg *Config, err error) {
|
||||
Silent: true,
|
||||
AutoReloadInterval: time.Minute}).Load(cfg, "config.json")
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("Error generating Config: %s", err.Error()))
|
||||
return nil, fmt.Errorf("Error generating Config: %s", err.Error())
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package hetzner
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -40,6 +39,8 @@ runcmd:
|
||||
- [ sh, -xc, "cd /root; docker run --rm --privileged multiarch/qemu-user-static --reset -p yes; docker compose up -d" ]
|
||||
`
|
||||
|
||||
var refreshNodeInfo = RefreshNodeInfo
|
||||
|
||||
type UserDataConfig struct {
|
||||
Image string
|
||||
EnvConfig map[string]interface{}
|
||||
@@ -60,12 +61,12 @@ func generateConfig(cfg *config.Config, name string, agentToken string) (string,
|
||||
}
|
||||
tmpl, err := template.New("userdata").Parse(USER_DATA_TEMPLATE)
|
||||
if err != nil {
|
||||
return "", errors.New(fmt.Sprintf("Errors in userdata template: %s", err.Error()))
|
||||
return "", fmt.Errorf("Errors in userdata template: %s", err.Error())
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
err = tmpl.Execute(&buf, &config)
|
||||
if err != nil {
|
||||
return "", errors.New(fmt.Sprintf("Could not render userdata template: %s", err.Error()))
|
||||
return "", fmt.Errorf("Could not render userdata template: %s", err.Error())
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
@@ -113,7 +114,7 @@ func CreateNewAgent(cfg *config.Config, woodpeckerAgent *models.Agent) (*hcloud.
|
||||
})
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
@@ -127,7 +128,7 @@ func ListAgents(cfg *config.Config) ([]hcloud.Server, error) {
|
||||
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
||||
allServers, err := client.Server.All(context.Background())
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("Could not query Server list: %s", err.Error()))
|
||||
return nil, fmt.Errorf("Could not query Server list: %s", err.Error())
|
||||
}
|
||||
myServers := []hcloud.Server{}
|
||||
for _, server := range allServers {
|
||||
@@ -161,7 +162,7 @@ func DecomNode(cfg *config.Config, server *hcloud.Server) (int64, error) {
|
||||
}).Debugf("Deleting %s node", server.Name)
|
||||
_, _, err := client.Server.DeleteWithResult(context.Background(), server)
|
||||
if err != nil {
|
||||
return woodpeckerAgentID, errors.New(fmt.Sprintf("Could not delete Agent: %s", err.Error()))
|
||||
return woodpeckerAgentID, fmt.Errorf("Could not delete Agent: %s", err.Error())
|
||||
}
|
||||
return woodpeckerAgentID, nil
|
||||
}
|
||||
@@ -170,16 +171,16 @@ func RefreshNodeInfo(cfg *config.Config, serverID int) (*hcloud.Server, error) {
|
||||
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
||||
server, _, err := client.Server.GetByID(context.Background(), serverID)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("Could not refresh server info: %s", err.Error()))
|
||||
return nil, fmt.Errorf("Could not refresh server info: %s", err.Error())
|
||||
}
|
||||
return server, nil
|
||||
}
|
||||
|
||||
func CheckRuntime(cfg *config.Config, server *hcloud.Server) (time.Time, error) {
|
||||
server, err := RefreshNodeInfo(cfg, server.ID)
|
||||
server, err := refreshNodeInfo(cfg, server.ID)
|
||||
now := time.Now()
|
||||
if err != nil {
|
||||
return time.Time{}, errors.New(fmt.Sprintf("Could not check Runtime: %s", err.Error()))
|
||||
return time.Time{}, fmt.Errorf("Could not check Runtime: %s", err.Error())
|
||||
}
|
||||
return server.Created.Add(time.Duration(now.Minute())), nil
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package hetzner
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||
"github.com/hetznercloud/hcloud-go/hcloud"
|
||||
)
|
||||
|
||||
func TestGenerateUserData(t *testing.T) {
|
||||
@@ -54,3 +57,78 @@ runcmd:
|
||||
t.Errorf("got:\n%v\n, wanted:\n%v", got, wanted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateUserData_MultipleCases(t *testing.T) {
|
||||
base := config.Config{
|
||||
WoodpeckerGrpc: "grpc-test.woodpecker.test.tld:443",
|
||||
WoodpeckerLabelSelector: "uploadfilter24.eu/instance-role=WoodpeckerTest",
|
||||
WoodpeckerAgentVersion: "latest",
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
cfg config.Config
|
||||
agentName string
|
||||
agentToken string
|
||||
wantContains []string
|
||||
}{
|
||||
{
|
||||
name: "basic",
|
||||
cfg: base,
|
||||
agentName: "test-instance",
|
||||
agentToken: "Geheim1!",
|
||||
wantContains: []string{
|
||||
"image: woodpeckerci/woodpecker-agent:latest",
|
||||
"- WOODPECKER_AGENT_SECRET=Geheim1!",
|
||||
"- WOODPECKER_FILTER_LABELS=uploadfilter24.eu/instance-role=WoodpeckerTest",
|
||||
"- WOODPECKER_SERVER=grpc-test.woodpecker.test.tld:443",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty token",
|
||||
cfg: base,
|
||||
agentName: "no-token",
|
||||
agentToken: "",
|
||||
wantContains: []string{
|
||||
"image: woodpeckerci/woodpecker-agent:latest",
|
||||
"- WOODPECKER_AGENT_SECRET=",
|
||||
"- WOODPECKER_HOSTNAME=no-token",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
got, err := generateConfig(&tc.cfg, tc.agentName, tc.agentToken)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: generateConfig returned error: %v", tc.name, err)
|
||||
}
|
||||
for _, want := range tc.wantContains {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("%s: expected generated userdata to contain %q, got:\n%s", tc.name, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckRuntime_MockedRefresh(t *testing.T) {
|
||||
// Mock refreshNodeInfo to return a server with a known Created time
|
||||
orig := refreshNodeInfo
|
||||
defer func() { refreshNodeInfo = orig }()
|
||||
|
||||
created := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
refreshNodeInfo = func(cfg *config.Config, serverID int) (*hcloud.Server, error) {
|
||||
return &hcloud.Server{Created: created}, nil
|
||||
}
|
||||
|
||||
cfg := config.Config{}
|
||||
// Capture minute before call to avoid flakiness across minute boundary
|
||||
minute := time.Now().Minute()
|
||||
got, err := CheckRuntime(&cfg, &hcloud.Server{ID: 123})
|
||||
if err != nil {
|
||||
t.Fatalf("CheckRuntime returned error: %v", err)
|
||||
}
|
||||
want := created.Add(time.Duration(minute))
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("unexpected runtime: got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
40
internal/logging/logging_test.go
Normal file
40
internal/logging/logging_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestLoggingDebug(t *testing.T) {
|
||||
cfg := config.Config{LogLevel: "Debug"}
|
||||
ConfigureLogger(&cfg)
|
||||
if log.GetLevel() != log.DebugLevel {
|
||||
t.Fatalf("expected DebugLevel, got %v", log.GetLevel())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggingInfo(t *testing.T) {
|
||||
cfg := config.Config{LogLevel: "Info"}
|
||||
ConfigureLogger(&cfg)
|
||||
if log.GetLevel() != log.InfoLevel {
|
||||
t.Fatalf("expected InfoLevel, got %v", log.GetLevel())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggingWarning(t *testing.T) {
|
||||
cfg := config.Config{LogLevel: "Warn"}
|
||||
ConfigureLogger(&cfg)
|
||||
if log.GetLevel() != log.WarnLevel {
|
||||
t.Fatalf("expected WarnLevel, got %v", log.GetLevel())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggingError(t *testing.T) {
|
||||
cfg := config.Config{LogLevel: "Error"}
|
||||
ConfigureLogger(&cfg)
|
||||
if log.GetLevel() != log.ErrorLevel {
|
||||
t.Fatalf("expected ErrorLevel, got %v", log.GetLevel())
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
110
internal/woodpecker/agent_test.go
Normal file
110
internal/woodpecker/agent_test.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
66
internal/woodpecker/metrics_test.go
Normal file
66
internal/woodpecker/metrics_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user