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:
@@ -7,7 +7,7 @@ labels:
|
||||
|
||||
steps:
|
||||
test:
|
||||
image: golang:1.23
|
||||
image: golang:1.25
|
||||
commands:
|
||||
- go test ./...
|
||||
pre-release:
|
||||
|
||||
@@ -6,7 +6,7 @@ labels:
|
||||
|
||||
steps:
|
||||
test:
|
||||
image: golang:1.23
|
||||
image: golang:1.25
|
||||
commands:
|
||||
- go test ./...
|
||||
pr-build:
|
||||
|
||||
@@ -6,7 +6,7 @@ labels:
|
||||
|
||||
steps:
|
||||
test:
|
||||
image: golang:1.23
|
||||
image: golang:1.25
|
||||
commands:
|
||||
- go test ./...
|
||||
release:
|
||||
|
||||
17
CHANGELOG
17
CHANGELOG
@@ -1,4 +1,17 @@
|
||||
CHANGELOG
|
||||
CHANGELOG
|
||||
|
||||
v1.1.1
|
||||
- Updated Dependencies
|
||||
- Added: unit tests
|
||||
- Added `internal/hetzner/hetzneragent_extra_test.go` covering userdata generation and mocked runtime checks.
|
||||
- Added `internal/woodpecker/agent_test.go` covering `CreateWoodpeckerAgent`, `GetAgentIdByName`, `DecomAgent` (httptest-based).
|
||||
- Added `internal/woodpecker/metrics_test.go` covering `QueueInfo`, `CheckPending`, and `CheckRunning`.
|
||||
- Implemented `internal/logging/logging_test.go` assertions for `ConfigureLogger` levels.
|
||||
- Changed: code to improve testability
|
||||
- Introduced `refreshNodeInfo` indirection in `internal/hetzner/hetzneragent.go` to allow mocking in tests.
|
||||
- Updated `hetzner` userdata test expectations and fixed JSON encoding in tests.
|
||||
- Notes:
|
||||
- No functional behavior changes except testability refactor (indirection).
|
||||
|
||||
v1.1.0
|
||||
|
||||
@@ -16,4 +29,4 @@ First stable release
|
||||
|
||||
v0.0.1
|
||||
|
||||
First test release
|
||||
First test release
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.23 as build
|
||||
FROM golang:1.25 AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
||||
23
go.mod
23
go.mod
@@ -1,6 +1,6 @@
|
||||
module git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler
|
||||
|
||||
go 1.23
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/gorilla/mux v1.8.1
|
||||
@@ -10,18 +10,19 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
github.com/BurntSushi/toml v1.6.0 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/klauspost/compress v1.18.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/prometheus/client_golang v1.20.5 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.62.0 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
golang.org/x/net v0.34.0 // indirect
|
||||
golang.org/x/sys v0.29.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.36.4 // indirect
|
||||
github.com/prometheus/client_golang v1.23.2 // indirect
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.67.4 // indirect
|
||||
github.com/prometheus/procfs v0.19.2 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
||||
golang.org/x/net v0.48.0 // indirect
|
||||
golang.org/x/sys v0.39.0 // indirect
|
||||
golang.org/x/text v0.32.0 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
52
go.sum
52
go.sum
@@ -1,6 +1,6 @@
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
@@ -8,16 +8,16 @@ github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/hetznercloud/hcloud-go v1.59.2 h1:NkCPwYiPv85FnOV3IW9/gxfW61TPIUSwyPHRSLwCkHA=
|
||||
github.com/hetznercloud/hcloud-go v1.59.2/go.mod h1:oTebZCjd+osj75jlI76Z+zjN1sTxmMiQ1MWoO8aRl1c=
|
||||
github.com/jinzhu/configor v1.2.2 h1:sLgh6KMzpCmaQB4e+9Fu/29VErtBUqsS2t8C9BNIVsA=
|
||||
github.com/jinzhu/configor v1.2.2/go.mod h1:iFFSfOBKP3kC2Dku0ZGB3t3aulfQgTGJknodhFavsU8=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
|
||||
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
@@ -28,31 +28,35 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
|
||||
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
||||
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||
github.com/prometheus/common v0.67.4 h1:yR3NqWO1/UyO1w2PhUvXlGQs/PtFmoveVO0KZ4+Lvsc=
|
||||
github.com/prometheus/common v0.67.4/go.mod h1:gP0fq6YjjNCLssJCQp0yk4M8W6ikLURwkdd/YKtTbyI=
|
||||
github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4Vws=
|
||||
github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
|
||||
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
|
||||
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
|
||||
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
||||
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
|
||||
@@ -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