hub v0.181.0 (not deployed): root files are 'behind' only when the files differ (R-1392, decision 304); Go 1.27.4 (R-1391)
gates / gates (push) Successful in 1m55s
gates / gates (push) Successful in 1m55s
This commit is contained in:
1 parent
561a4d98e9
commit
83316fc986
15 files changed
+442
-11
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package bundlefiles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func bundle(version, wrapperSHA string) []byte {
|
||||
return []byte(`{
|
||||
"agent_version": "` + version + `",
|
||||
"files": [
|
||||
{"check": "", "content_b64": "QQ==", "mode": "0o755", "path": "/usr/local/sbin/felhom-os-apply", "policy": "replace", "sha256": "` + wrapperSHA + `"}
|
||||
],
|
||||
"format": 1
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
func sha(b []byte) string { s := sha256.Sum256(b); return hex.EncodeToString(s[:]) }
|
||||
|
||||
type reg struct {
|
||||
files map[string][]byte
|
||||
calls int
|
||||
err error
|
||||
}
|
||||
|
||||
func (g *reg) fetch(_ context.Context, v string) ([]byte, error) {
|
||||
g.calls++
|
||||
if g.err != nil {
|
||||
return nil, g.err
|
||||
}
|
||||
b, ok := g.files[v]
|
||||
if !ok {
|
||||
return nil, errors.New("404")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// R-1392: the three verdicts, and the fail direction.
|
||||
func TestSame(t *testing.T) {
|
||||
old, relabel, changed := bundle("0.172.1", "aa"), bundle("0.173.0", "aa"), bundle("0.174.0", "bb")
|
||||
g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel, "0.174.0": changed}}
|
||||
r := &Resolver{Fetch: g.fetch}
|
||||
|
||||
if sha(old) == sha(relabel) {
|
||||
t.Fatal("fixture: the label must move the bundle's sha256")
|
||||
}
|
||||
if !r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("same files under a new label must be the same")
|
||||
}
|
||||
if r.Same("0.172.1", sha(old), "0.174.0", sha(changed)) {
|
||||
t.Fatal("one file differs: must NOT be the same")
|
||||
}
|
||||
if r.Same("none", "", "0.173.0", sha(relabel)) {
|
||||
t.Fatal("a box that never got a bundle must NOT be the same")
|
||||
}
|
||||
if r.Same("", "", "", "") {
|
||||
t.Fatal("nothing vouched and nothing on the box is not 'the same'")
|
||||
}
|
||||
// The cache: every bundle was read once.
|
||||
n := g.calls
|
||||
r.Same("0.172.1", sha(old), "0.173.0", sha(relabel))
|
||||
if g.calls != n {
|
||||
t.Fatalf("a verified bundle was read again (%d -> %d)", n, g.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// A bundle counts only when its bytes hash to the sha256 the caller trusts.
|
||||
func TestSame_UnverifiedBytesNeverCount(t *testing.T) {
|
||||
old, relabel := bundle("0.172.1", "aa"), bundle("0.173.0", "aa")
|
||||
g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel}}
|
||||
r := &Resolver{Fetch: g.fetch}
|
||||
wrong := sha([]byte("something else"))
|
||||
if r.Same("0.172.1", wrong, "0.173.0", sha(relabel)) {
|
||||
t.Fatal("the box's sha256 does not match the published file: must NOT be the same")
|
||||
}
|
||||
if r.Same("0.172.1", sha(old), "0.173.0", wrong) {
|
||||
t.Fatal("the vouched sha256 does not match the published file: must NOT be the same")
|
||||
}
|
||||
if r.Same("../../x", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("a version that is not a version must never reach the registry")
|
||||
}
|
||||
}
|
||||
|
||||
// A registry that is away answers "not the same", is not hammered, and is asked again later.
|
||||
func TestSame_RegistryAway(t *testing.T) {
|
||||
old, relabel := bundle("0.172.1", "aa"), bundle("0.173.0", "aa")
|
||||
g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel}, err: errors.New("dial tcp: refused")}
|
||||
now := time.Date(2026, 11, 14, 10, 0, 0, 0, time.UTC)
|
||||
r := &Resolver{Fetch: g.fetch, Now: func() time.Time { return now }}
|
||||
if r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("unreadable must NOT be the same")
|
||||
}
|
||||
n := g.calls
|
||||
r.Same("0.172.1", sha(old), "0.173.0", sha(relabel))
|
||||
if g.calls != n {
|
||||
t.Fatal("a failed read was retried at once")
|
||||
}
|
||||
g.err = nil
|
||||
now = now.Add(11 * time.Minute)
|
||||
if !r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("the registry is back after the retry wait: must be the same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesDigest_NoFilesIsAnError(t *testing.T) {
|
||||
for _, b := range []string{`{}`, `{"files":[]}`, `{"files":"x"}`, `not json`} {
|
||||
if _, err := FilesDigest([]byte(b)); err == nil {
|
||||
t.Fatalf("%q: no error", b)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user