51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const validKey = "sha256:0000000000000000000000000000000000000000000000000000000000000000"
|
|
|
|
func setValid(t *testing.T) {
|
|
t.Setenv("MASTER_URL", "wss://10.0.0.5:8090/agent")
|
|
t.Setenv("MASTER_KEY", validKey)
|
|
t.Setenv("AGENT_TOKEN", "pua_abc")
|
|
t.Setenv("SELF_ID", "")
|
|
t.Setenv("LOG_LEVEL", "")
|
|
}
|
|
|
|
func TestLoad_Valid(t *testing.T) {
|
|
setValid(t)
|
|
cfg, err := Load()
|
|
require.NoError(t, err)
|
|
host, _ := os.Hostname()
|
|
assert.Equal(t, &Config{
|
|
MasterURL: "wss://10.0.0.5:8090/agent", MasterKey: validKey, Token: "pua_abc",
|
|
SelfID: host, LogLevel: "info",
|
|
}, cfg)
|
|
}
|
|
|
|
func TestLoad_Invalid(t *testing.T) {
|
|
cases := []struct {
|
|
name, key, value, wantErr string
|
|
}{
|
|
{"missing url", "MASTER_URL", "", "MASTER_URL is required"},
|
|
{"plain ws", "MASTER_URL", "ws://10.0.0.5:8090/agent", "MASTER_URL must start with wss://"},
|
|
{"missing key", "MASTER_KEY", "", "MASTER_KEY is required"},
|
|
{"malformed key", "MASTER_KEY", "abc", "MASTER_KEY must look like sha256:<64 hex>"},
|
|
{"missing token", "AGENT_TOKEN", "", "AGENT_TOKEN is required"},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
setValid(t)
|
|
t.Setenv(c.key, c.value)
|
|
_, err := Load()
|
|
assert.ErrorContains(t, err, c.wantErr)
|
|
})
|
|
}
|
|
}
|