baseline: P1 契约、agent 核心与中继投递(既有工作首次入库)
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The interesting property is not that a good file parses, it is that a bad one is refused at boot.
|
||||
// An agent that starts half-configured sits there answering nothing, and "why does nothing work" is
|
||||
// much harder to diagnose than a service that will not start.
|
||||
func valid() File {
|
||||
return File{
|
||||
NodeID: "nd_7f3a9c2e",
|
||||
RelayURL: "https://relay.example",
|
||||
AgentToken: "tok",
|
||||
LocalPort: 8788,
|
||||
SignKey: "AAAA",
|
||||
KxKey: "AAAA",
|
||||
AuditPath: "/var/log/msa-agent/audit.log",
|
||||
Devices: []Device{{ID: "ap_5b1d0000", SignKey: "AAAA"}},
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAcceptsACompleteFile(t *testing.T) {
|
||||
if err := valid().Validate(); err != nil {
|
||||
t.Fatalf("a complete file was refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNamesEveryMissingField(t *testing.T) {
|
||||
cases := map[string]func(*File){
|
||||
"nodeId": func(c *File) { c.NodeID = "" },
|
||||
"relayUrl": func(c *File) { c.RelayURL = "" },
|
||||
"token": func(c *File) { c.AgentToken = "" },
|
||||
"signKey": func(c *File) { c.SignKey = "" },
|
||||
"kxKey": func(c *File) { c.KxKey = "" },
|
||||
"auditPath": func(c *File) { c.AuditPath = "" },
|
||||
"devices": func(c *File) { c.Devices = nil },
|
||||
"badNode": func(c *File) { c.NodeID = "node-1" },
|
||||
"badApp": func(c *File) { c.Devices[0].ID = "phone" },
|
||||
"noSignKey": func(c *File) { c.Devices[0].SignKey = "" },
|
||||
"port0": func(c *File) { c.LocalPort = 0 },
|
||||
"portHigh": func(c *File) { c.LocalPort = 70000 },
|
||||
"duplicate": func(c *File) { c.Devices = append(c.Devices, c.Devices[0]) },
|
||||
}
|
||||
for name, mutate := range cases {
|
||||
cfg := valid()
|
||||
mutate(&cfg)
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Errorf("%s: a broken config was accepted", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// There is no host field, and this test is what keeps it that way: a "localHost" entry would be the
|
||||
// one setting able to turn the agent into a LAN pivot.
|
||||
func TestThereIsNoHostField(t *testing.T) {
|
||||
raw := []byte(`{"nodeId":"nd_7f3a9c2e","relayUrl":"https://r","agentToken":"t","localHost":"10.0.0.5",
|
||||
"localPort":8788,"signKey":"AAAA","kxKey":"AAAA","auditPath":"/tmp/a.log",
|
||||
"devices":[{"id":"ap_5b1d0000","signKey":"AAAA"}]}`)
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
if err := os.WriteFile(path, raw, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The unknown field is ignored, and the port is all the agent knows about the local service.
|
||||
if cfg.LocalPort != 8788 {
|
||||
t.Fatalf("localPort is %d", cfg.LocalPort)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadReportsMissingAndMalformedFiles(t *testing.T) {
|
||||
if _, err := Load(filepath.Join(t.TempDir(), "absent.json")); err == nil {
|
||||
t.Fatal("a missing file was accepted")
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "broken.json")
|
||||
if err := os.WriteFile(path, []byte("{"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Load(path); err == nil {
|
||||
t.Fatal("a malformed file was accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user