fix #2 and added some minimal tests

This commit is contained in:
Tobias Trabelsi 2023-10-20 21:34:30 +02:00
parent 9f4e37adec
commit b39d1bf62f
Signed by: lerentis
GPG Key ID: FF0C2839718CAF2E
4 changed files with 62 additions and 0 deletions

View File

@ -1,4 +1,12 @@
steps:
test:
image: golang
commands:
- go test ./...
when:
event:
- push
- pull_request
build:
image: woodpeckerci/plugin-docker-buildx
settings:

25
internal/config_test.go Normal file
View File

@ -0,0 +1,25 @@
package internal
import (
"reflect"
"testing"
)
var defaultConfig = Config{
LogLevel: "Info",
LabelSelector: "kops.k8s.io/instance-role=Node",
Protocol: "http",
HcloudToken: "",
FloatingIPName: "",
DryRun: false,
}
func TestConfigDefaults(t *testing.T) {
cfg, err := GenConfig()
if err != nil {
t.Errorf("%s", err.Error())
}
if !reflect.DeepEqual(&defaultConfig, cfg) {
t.Errorf("got %+v, want %+v", cfg, defaultConfig)
}
}

23
internal/health_test.go Normal file
View File

@ -0,0 +1,23 @@
package internal
import (
"net/http"
"strings"
"testing"
)
func TestHealth(t *testing.T) {
go func() {
StartHealthEndpoint()
}()
request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/health", strings.NewReader(""))
resp, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Health endpoint did not start: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Bad response from health endpoint. Want: %d, got %d", http.StatusOK, resp.StatusCode)
}
}

View File

@ -78,6 +78,12 @@ func AttachFloatingIpToNode(cfg *Config, server hcloud.Server) error {
if err != nil {
return errors.New(fmt.Sprintf("Could not find Floating IP by name: %s", err.Error()))
}
if floatingIP.Server.ID == server.ID {
log.WithFields(log.Fields{
"Caller": "AttachFloatingIpToNode",
}).Info(fmt.Sprintf("Floating IP %s already assigned to Node %s", cfg.FloatingIPName, server.Name))
return nil
}
_, _, err = client.FloatingIP.Assign(context.TODO(), floatingIP, &server)
if err != nil {
log.WithFields(log.Fields{