Files

36 lines
1.4 KiB
Go

package declaration
import (
"strings"
"testing"
)
// Defends novox/hq ADR 0102: a file written into carries only a JSON object of its keys, in a
// format the host speaks, and a service may name what it is reloaded on.
func TestAFileWrittenIntoIsRefusedUnlessItIsAnObjectOfKeys(t *testing.T) {
for name, c := range map[string]struct{ resource, refusal string }{
"another format": {`{"id":"f","type":"file","path":"/etc/x","into":"toml","content":"a = 1"}`, `into "toml"`},
"not an object": {`{"id":"f","type":"file","path":"/etc/x","into":"json","content":"[1,2]"}`, "JSON object"},
"with create-once": {`{"id":"f","type":"file","path":"/etc/x","into":"json","content":"{}","create-once":true}`, "create-once"},
} {
_, err := Parse([]byte(`{"declaration":1,"resources":[` + c.resource + `]}`))
if err == nil || !strings.Contains(err.Error(), c.refusal) {
t.Errorf("%s: want a refusal naming %q, got %v", name, c.refusal, err)
}
}
d, err := Parse([]byte(`{"declaration":1,"resources":[
{"id":"f","type":"file","path":"/etc/x","into":"json","content":"{\"k\":1}"},
{"id":"s","type":"service","unit":"docker.service","state":"running","reload-on":["f"]}
]}`))
if err != nil {
t.Fatal(err)
}
if f := d.Resources[0].(*File); f.Into != IntoJSON {
t.Errorf("into was read as %q", f.Into)
}
if s := d.Resources[1].(*Service); len(s.ReloadOn) != 1 || s.ReloadOn[0] != "f" {
t.Errorf("reload-on was read as %v", s.ReloadOn)
}
}