Files
worker/internal/installer/install_test.go
Gleb Tv e987f24903
Все проверки выполнены успешно
CI / test (push) Successful in 2m32s
Docker / Build and publish worker image (push) Successful in 18m17s
fix(worker): harden control-plane lifecycle
- reconnect safely after token rotation and retry leased results
- reject malformed tasks and remove production cluster debug mutation
- validate environment files and require immutable container images

BREAKING CHANGE: Docker install, deploy, and Compose now require an
immutable repository@sha256 image reference.
2026-07-19 23:11:43 +03:00

132 строки
5.0 KiB
Go

package installer
import (
"os"
"path/filepath"
"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 TestValidateEnvironmentFile(t *testing.T) {
tests := []struct {
name string
contents string
valid bool
}{
{name: "valid", contents: "# Worker credentials\nRSMON_URL=https://rsmon.ru\nRSMON_TOKEN=secret\n\n", valid: true},
{name: "missing URL", contents: "RSMON_TOKEN=secret\n"},
{name: "missing token", contents: "RSMON_URL=https://rsmon.ru\n"},
{name: "invalid URL", contents: "RSMON_URL=file:///tmp/worker\nRSMON_TOKEN=secret\n"},
{name: "additional settings", contents: "RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=secret\nWORKER_HOST=0.0.0.0\nWORKER_URL=\n", valid: true},
{name: "dotenv interpolation", contents: "RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=${TOKEN}\n"},
{name: "dotenv export", contents: "export RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=secret\n"},
{name: "YAML assignment", contents: "RSMON_URL: https://rsmon.ru\nRSMON_TOKEN=secret\n"},
{name: "malformed key", contents: "RSMON-URL=https://rsmon.ru\nRSMON_TOKEN=secret\n"},
{name: "missing assignment", contents: "RSMON_URL=https://rsmon.ru\nRSMON_TOKEN\n"},
{name: "quoted value", contents: "RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=\"secret\"\n"},
{name: "whitespace", contents: "RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=secret value\n"},
{name: "duplicate URL", contents: "RSMON_URL=https://rsmon.ru\nRSMON_URL=https://evil.test\nRSMON_TOKEN=secret\n"},
{name: "duplicate empty token", contents: "RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=\nRSMON_TOKEN=secret\n"},
{name: "carriage return", contents: "RSMON_URL=https://rsmon.ru\r\nRSMON_TOKEN=secret\r\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "worker.env")
if err := os.WriteFile(path, []byte(tt.contents), 0600); err != nil {
t.Fatal(err)
}
err := ValidateEnvironmentFile(path)
if tt.valid && err != nil {
t.Fatalf("ValidateEnvironmentFile() error = %v", err)
}
if !tt.valid && err == nil {
t.Fatal("ValidateEnvironmentFile() succeeded")
}
})
}
}
func TestValidateEnvironmentFileInputErrors(t *testing.T) {
if err := ValidateEnvironmentFile(filepath.Join(t.TempDir(), "missing")); err == nil {
t.Fatal("missing environment file accepted")
}
if err := ValidateEnvironmentFile(t.TempDir()); err == nil {
t.Fatal("directory accepted as an environment file")
}
if os.Geteuid() == 0 {
t.Skip("root can read mode-000 files")
}
path := filepath.Join(t.TempDir(), "unreadable")
if err := os.WriteFile(path, []byte("RSMON_URL=https://rsmon.ru\nRSMON_TOKEN=secret\n"), 0000); err != nil {
t.Fatal(err)
}
if err := ValidateEnvironmentFile(path); err == nil {
t.Fatal("unreadable environment file accepted")
}
}
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) {
const image = "reg.rsxx.ru/rsmon/rsmon-worker@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
if err := ValidateImage(image); err != nil {
t.Fatal(err)
}
for _, image := range []string{"", "-bad", "image name", "image%stest", `image"test`, "reg.rsxx.ru/rsmon/rsmon-worker:latest", "reg.rsxx.ru/rsmon/rsmon-worker@sha256:short", "reg.rsxx.ru/rsmon/rsmon-worker@sha256:0123456789ABCDEF0123456789abcdef0123456789abcdef0123456789abcdef"} {
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")
}
const image = "reg.rsxx.ru/rsmon/rsmon-worker@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
unit := DockerUnit(image)
for _, want := range []string{"ExecStartPre=-docker rm -f rsmon-worker", "docker run --rm", image} {
if !strings.Contains(unit, want) {
t.Fatalf("Docker systemd unit missing %q", want)
}
}
}