Все проверки выполнены успешно
CI / test (push) Successful in 2m24s
Docker / Build and publish worker image (push) Successful in 13m24s
69 строки
1.9 KiB
Go
69 строки
1.9 KiB
Go
package installer
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateURL(t *testing.T) {
|
|
for _, raw := range []string{"https://rsmon.ru", "http://localhost:7401"} {
|
|
if err := ValidateURL(raw); err != nil {
|
|
t.Fatalf("ValidateURL(%q): %v", raw, err)
|
|
}
|
|
}
|
|
for _, raw := range []string{"", "rsmon.ru", "file:///tmp/x"} {
|
|
if err := ValidateURL(raw); err == nil {
|
|
t.Fatalf("ValidateURL(%q) succeeded", raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateToken(t *testing.T) {
|
|
if err := ValidateToken("token"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, token := range []string{"", " ", "token\nRSMON_URL=https://evil.test"} {
|
|
if err := ValidateToken(token); err == nil {
|
|
t.Fatalf("ValidateToken(%q) succeeded", token)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEnvironment(t *testing.T) {
|
|
got := string(Environment("https://example.test", "secret"))
|
|
for _, want := range []string{"RSMON_URL=https://example.test\n", "RSMON_TOKEN=secret\n", "WORKER_HOST=127.0.0.1\n"} {
|
|
if !strings.Contains(got, want) {
|
|
t.Fatalf("environment missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestShellQuote(t *testing.T) {
|
|
if got, want := shellQuote("a'b"), `'a'\''b'`; got != want {
|
|
t.Fatalf("shellQuote() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestValidateImage(t *testing.T) {
|
|
if err := ValidateImage(DefaultImage); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, image := range []string{"", "-bad", "image name", "image%stest", `image"test`} {
|
|
if err := ValidateImage(image); err == nil {
|
|
t.Fatalf("ValidateImage(%q) succeeded", image)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSystemdUnits(t *testing.T) {
|
|
if !strings.Contains(systemdUnit, "Type=simple\nUser=root\n") || !strings.Contains(systemdUnit, "ExecStart=/usr/local/bin/rsmon-worker\n") {
|
|
t.Fatal("binary systemd unit is not the simple root service")
|
|
}
|
|
unit := DockerUnit(DefaultImage)
|
|
for _, want := range []string{"ExecStartPre=-docker rm -f rsmon-worker", "docker run --rm", DefaultImage} {
|
|
if !strings.Contains(unit, want) {
|
|
t.Fatalf("Docker systemd unit missing %q", want)
|
|
}
|
|
}
|
|
}
|