Archived
Compare commits
377
Commits
aa9da94260
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56e89d62f1 | ||
|
|
5970367173 | ||
|
|
4964ad1037 | ||
|
|
5c281ad99b | ||
|
|
29dfb28f38 | ||
|
|
88b16e9be2 | ||
|
|
b564c315d0 | ||
|
|
5bd03b3984 | ||
|
|
e02fcad739 | ||
|
|
4a92ec6102 | ||
|
|
909cc25169 | ||
|
|
272d5ebd61 | ||
|
|
a6386ff23a | ||
|
|
b51105908a | ||
|
|
8b4d536a11 | ||
|
|
dc90b3f5bd | ||
|
|
0e9c4c8299 | ||
|
|
14d10051e5 | ||
|
|
c58b311ea7 | ||
|
|
7a169eae76 | ||
|
|
a07621d761 | ||
|
|
d791052f77 | ||
|
|
22935b9202 | ||
|
|
efe745f2be | ||
|
|
ff912d0972 | ||
|
|
4c9be8efc8 | ||
|
|
62c05c681c | ||
|
|
bfb728730f | ||
|
|
159db37618 | ||
|
|
b7c3868980 | ||
|
|
ddae6729c1 | ||
|
|
a4426190b0 | ||
|
|
2154ea8e3c | ||
|
|
812c528479 | ||
|
|
8b32e04e2f | ||
|
|
79f44386d2 | ||
|
|
6fe9efa712 | ||
|
|
d96542cd25 | ||
|
|
80dc6980b7 | ||
|
|
80df1b6361 | ||
|
|
816cf96459 | ||
|
|
3f05df5d1a | ||
|
|
e04846e9fa | ||
|
|
020c6a99b0 | ||
|
|
5a9766d4a4 | ||
|
|
f46a08772f | ||
|
|
1c2adfdca9 | ||
|
|
0fbb5ef359 | ||
|
|
8f84731896 | ||
|
|
fbaa463a38 | ||
|
|
2dafbe60e9 | ||
|
|
8d58afea6c | ||
|
|
b611f7375a | ||
|
|
253bbcea2d | ||
|
|
2c6382346f | ||
|
|
b8cafccaf6 | ||
|
|
82d212a2a1 | ||
|
|
df8ebf6a77 | ||
|
|
dc414fbfef | ||
|
|
fd891688ac | ||
|
|
9d8a5a7ede | ||
|
|
4e9e5c27d4 | ||
|
|
7c8a2e03dd | ||
|
|
89f52a6504 | ||
|
|
fb2f5a2962 | ||
|
|
2ecfbb3a29 | ||
|
|
9ad8715871 | ||
|
|
e1a55f099b | ||
|
|
ccc7ba334c | ||
|
|
65d5ed3367 | ||
|
|
08a261ac1a | ||
|
|
23226384e5 | ||
|
|
350f8ebdfe | ||
|
|
09d2cc5056 | ||
|
|
47e64f0284 | ||
|
|
6927b02d4d | ||
|
|
fda4859a95 | ||
|
|
a81717e53c | ||
|
|
d01839c29c | ||
|
|
f67d15536b | ||
|
|
7cd4d18bd5 | ||
|
|
fbf14b68db | ||
|
|
79ac62ffbc | ||
|
|
40034be479 | ||
|
|
045f25bd13 | ||
|
|
ff3cc47027 | ||
|
|
31a2bcdb3c | ||
|
|
2df47d7b1c | ||
|
|
666d1b682e | ||
|
|
e40b1e70db | ||
|
|
6b116586fc | ||
|
|
bf20d9d8cf | ||
|
|
81e335e42d | ||
|
|
2407bfb9ff | ||
|
|
850f4b4d26 | ||
|
|
e7673e3bd3 | ||
|
|
3e39ccf958 | ||
|
|
94bb74966f | ||
|
|
bfb271a91b | ||
|
|
9ff47f6aaa | ||
|
|
ca5f74eadb | ||
|
|
c3db00ceac | ||
|
|
8e02c4726e | ||
|
|
cc5c9ae888 | ||
|
|
c7e31640cf | ||
|
|
c75d726205 | ||
|
|
a4cd5ba375 | ||
|
|
62802cd966 | ||
|
|
e9a885cda9 | ||
|
|
2814262ac0 | ||
|
|
0a536d7b6d | ||
|
|
2bcd85d2df | ||
|
|
ac34bce49f | ||
|
|
af2b0844a1 | ||
|
|
3b4cb69e84 | ||
|
|
c47a590463 | ||
|
|
f35aa35541 | ||
|
|
8114cee2af | ||
|
|
baa8f58473 | ||
|
|
0470869fbb | ||
|
|
8a3c0cd022 | ||
|
|
0fff3efd4e | ||
|
|
eafd1883f6 | ||
|
|
e67916bbd8 | ||
|
|
7039c1b4b3 | ||
|
|
3dadc46ee6 | ||
|
|
d8aa565fee | ||
|
|
82c89bec65 | ||
|
|
b1f758ed71 | ||
|
|
ba4d156573 | ||
|
|
9c38aed789 | ||
|
|
a3f1d0460d | ||
|
|
0af12b369b | ||
|
|
e338b1f0f4 | ||
|
|
e42818a0ab | ||
|
|
bdb8193456 | ||
|
|
ca25e4ab45 | ||
|
|
7a64075702 | ||
|
|
9abe0b59fa | ||
|
|
183d7a99ff | ||
|
|
fad3d7ee71 | ||
|
|
3c50e80972 | ||
|
|
6e4bca7fce | ||
|
|
f4998a7266 | ||
|
|
53d9c4de19 | ||
|
|
9372733861 | ||
|
|
d640ef15ec | ||
|
|
28e1b29e0c | ||
|
|
f0815c4f18 | ||
|
|
72bbdeed35 | ||
|
|
dfd81d8daf | ||
|
|
8c54139905 | ||
|
|
6ed5de186d | ||
|
|
8fab4d8b22 | ||
|
|
b879128efb | ||
|
|
9641b88725 | ||
|
|
36704abc08 | ||
|
|
7fccba4658 | ||
|
|
98e948e30e | ||
|
|
f18bac4533 | ||
|
|
e05481e992 | ||
|
|
dd58fbcb2c | ||
|
|
1ed5afe679 | ||
|
|
38523c6509 | ||
|
|
a78f244621 | ||
|
|
4ab4529440 | ||
|
|
370673195c | ||
|
|
3ad12f8ec7 | ||
|
|
f6e78c837f | ||
|
|
35e330c7f0 | ||
|
|
aa7e53112b | ||
|
|
af6bcea41e | ||
|
|
49fc8b6d7d | ||
|
|
f20d221a60 | ||
|
|
bd8ea8ad4f | ||
|
|
1350c428ed | ||
|
|
76c48fa074 | ||
|
|
df6aab6f44 | ||
|
|
7f8e5a821c | ||
|
|
3db472ce56 | ||
|
|
3ae2d6e17c | ||
|
|
29ef458d89 | ||
|
|
9d0798a1c0 | ||
|
|
0e85230769 | ||
|
|
ad0aabc2a9 | ||
|
|
fe3faaef83 | ||
|
|
34b105ea88 | ||
|
|
8f90d3ecd6 | ||
|
|
b4a62e9e6e | ||
|
|
03894e5e51 | ||
|
|
565de8c46a | ||
|
|
c19d4516ef | ||
|
|
a8d63653fb | ||
|
|
aaadb5695c | ||
|
|
73a31eb205 | ||
|
|
f56458da32 | ||
|
|
66650e315c | ||
|
|
c40e15268e | ||
|
|
0f8c47842a | ||
|
|
aee0a2718f | ||
|
|
4f95be7e08 | ||
|
|
788fb681ec | ||
|
|
96d7784bce | ||
|
|
dc456d5660 | ||
|
|
44d9d40582 | ||
|
|
74bed5a800 | ||
|
|
ecbb317dc3 | ||
|
|
dd2d8b91be | ||
|
|
1b0fd8b442 | ||
|
|
c06f75d083 | ||
|
|
5f2da0af69 | ||
|
|
16ea033124 | ||
|
|
9088db9a04 | ||
|
|
d0f5ae26de | ||
|
|
e8b66a2eb3 | ||
|
|
f2da7d0540 | ||
|
|
5b03b2e26b | ||
|
|
a5b874a2a0 | ||
|
|
491cbe2548 | ||
|
|
b2bf14ab01 | ||
|
|
9db1459cb0 | ||
|
|
3cfcf5e3e4 | ||
|
|
f99e499288 | ||
|
|
3c9a4d56c0 | ||
|
|
e57224c25a | ||
|
|
bb2073b6bb | ||
|
|
9131ef0dbc | ||
|
|
8831c74f70 | ||
|
|
fecfb40ed4 | ||
|
|
a1969403c6 | ||
|
|
893aa85349 | ||
|
|
2ba9eef58d | ||
|
|
3b51d3f86e | ||
|
|
5ee08cb5e1 | ||
|
|
443ba6b265 | ||
|
|
a96bd2ff38 | ||
|
|
64fbef7713 | ||
|
|
a77aa8520e | ||
|
|
a9de578cb9 | ||
|
|
d951aa954e | ||
|
|
ab9a7771f4 | ||
|
|
1c50608246 | ||
|
|
760351b4ae | ||
|
|
899389c710 | ||
|
|
a203f605d4 | ||
|
|
0741880d31 | ||
|
|
fa734d3098 | ||
|
|
f13d42abb4 | ||
|
|
fdf26a1fe2 | ||
|
|
343e271ec7 | ||
|
|
05c872485a | ||
|
|
12dd2bb3f0 | ||
|
|
5831eec770 | ||
|
|
c9a7102bc5 | ||
|
|
67279199e9 | ||
|
|
9d2a9e62d1 | ||
|
|
b345d10bec | ||
|
|
13842a7234 | ||
|
|
4238318601 | ||
|
|
758c8e84e5 | ||
|
|
9222575ae9 | ||
|
|
c5b8fca8bd | ||
|
|
ff543d3def | ||
|
|
ef211faefb | ||
|
|
3c247e80af | ||
|
|
bb0d5e17db | ||
|
|
24ad859629 | ||
|
|
5ccc87f4d0 | ||
|
|
68c1220e87 | ||
|
|
5b5cd6160a | ||
|
|
d7d70cd539 | ||
|
|
df70ca43a7 | ||
|
|
f60c742d93 | ||
|
|
2db5dcdcb4 | ||
|
|
d9cdb3615d | ||
|
|
dec3b83e66 | ||
|
|
2e36a63096 | ||
|
|
eb8286fc68 | ||
|
|
5540fd4f85 | ||
|
|
e419d0182f | ||
|
|
5ca4f14479 | ||
|
|
c43883910d | ||
|
|
548681cc4e | ||
|
|
9c4bf1f08b | ||
|
|
6300a5b31c | ||
|
|
505694c424 | ||
|
|
72186815e3 | ||
|
|
526eed99fa | ||
|
|
b506852388 | ||
|
|
791786d5b8 | ||
|
|
0b312218f9 | ||
|
|
c834e4f821 | ||
|
|
149f89e8f9 | ||
|
|
b2af3d654f | ||
|
|
7676e67c5e | ||
|
|
0173d9c70e | ||
|
|
89abce5df9 | ||
|
|
185d0294eb | ||
|
|
4d4d51eb01 | ||
|
|
b7d996878b | ||
|
|
219fdcd7f0 | ||
|
|
55ba7f2cb8 | ||
|
|
102f8dc267 | ||
|
|
296e11d499 | ||
|
|
eed4976483 | ||
|
|
bbadbb2af0 | ||
|
|
1414ed7f7b | ||
|
|
f67342f0d8 | ||
|
|
fae7710bed | ||
|
|
289bbf68e4 | ||
|
|
f2a4363a1a | ||
|
|
d51324df45 | ||
|
|
58512a9c6d | ||
|
|
1fd833bfe8 | ||
|
|
fd4d26b42c | ||
|
|
f9014452d3 | ||
|
|
61449249a8 | ||
|
|
61f7e89ad5 | ||
|
|
9713ceb84d | ||
|
|
93fc2f48a1 | ||
|
|
acaf8830ec | ||
|
|
d5d53893a4 | ||
|
|
a0b2d98002 | ||
|
|
bc80683b9e | ||
|
|
96441ea28a | ||
|
|
420ae3d0e7 | ||
|
|
7c0e1d223f | ||
|
|
a10ef8995b | ||
|
|
dfc2f2aa98 | ||
|
|
ab33f015ec | ||
|
|
791c2a3d48 | ||
|
|
d36e0ea278 | ||
|
|
b6ed18197b | ||
|
|
45d091778c | ||
|
|
3a1937914e | ||
|
|
25fdf9920e | ||
|
|
323e1270cc | ||
|
|
e7e23c57cc | ||
|
|
a8387ebad8 | ||
|
|
1963d84a7a | ||
|
|
b71425867f | ||
|
|
c7039d81e1 | ||
|
|
f76da2ce9c | ||
|
|
7f6d418205 | ||
|
|
0aec975d19 | ||
|
|
9da79ef8c3 | ||
|
|
7743bd0d4b | ||
|
|
62268cf43d | ||
|
|
ea3c8ad7f6 | ||
|
|
d984f0a55d | ||
|
|
f00b6aff8b | ||
|
|
eff0d5cbf4 | ||
|
|
1ed7e34155 | ||
|
|
abe356a262 | ||
|
|
41c9628757 | ||
|
|
3e3400f510 | ||
|
|
e022a3fd6f | ||
|
|
684a8783e5 | ||
|
|
11e364bd43 | ||
|
|
1e9b87e03a | ||
|
|
45a09f90e3 | ||
|
|
4538d05732 | ||
|
|
980d396d40 | ||
|
|
b8f77577fe | ||
|
|
28a278c146 | ||
|
|
c34ab0e43a | ||
|
|
771cc24fcb | ||
|
|
b3786cc67e | ||
|
|
b7d03b6897 | ||
|
|
21954ad1bc | ||
|
|
485eb44de4 | ||
|
|
8d562f9b22 | ||
|
|
8f2d22db0c | ||
|
|
cc8a74fce4 | ||
|
|
01f615aa0a | ||
|
|
707bfba4ea | ||
|
|
499ff5c218 |
+3
-1
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
# A built binary. The lab writes one here when pointed at the repository root by mistake;
|
# A built binary. The lab writes one here when pointed at the repository root by mistake;
|
||||||
# built artifacts belong in build/, which is already ignored.
|
# built artifacts belong in build/, which is already ignored.
|
||||||
/mesh-builder
|
|
||||||
/mesh-controller
|
/mesh-controller
|
||||||
/postgres-provisioner
|
/postgres-provisioner
|
||||||
/redis-provisioner
|
/redis-provisioner
|
||||||
# And where `go build` in cmd/mesh-controller leaves it (novox/hq issue 233).
|
# And where `go build` in cmd/mesh-controller leaves it (novox/hq issue 233).
|
||||||
/cmd/mesh-controller/mesh-controller
|
/cmd/mesh-controller/mesh-controller
|
||||||
|
|
||||||
|
# No repository commits its dependencies (issue 1305).
|
||||||
|
vendor/
|
||||||
|
|||||||
-45
@@ -1,45 +0,0 @@
|
|||||||
# The Go it builds with, pinned here because genesis builds this file with no arguments (novox/hq
|
|
||||||
# issue 223) — the Makefile passes the same digest. A tag older than go.mod asks for is how
|
|
||||||
# `make image` broke once before (issue 146).
|
|
||||||
ARG GO_BASE=golang@sha256:8ac98ca534ac3f51e1f420a1dd2c15e74c75cfa0f23f3ad27eb5d7236c349a0c
|
|
||||||
# The control plane's image — for genesis and the lab only. The mesh runs the controller as a Go
|
|
||||||
# bundle the host starts as a process (module.json; novox/hq issue 213), and builds no image of it.
|
|
||||||
# Genesis builds this file and raises it as the container the process replaces on the first push
|
|
||||||
# (mesh-host internal/bootstrap, novox/hq issue 223).
|
|
||||||
#
|
|
||||||
# novox/hq ADR 0006: this image is pinned by digest in the bundle the host carries, fetched on a
|
|
||||||
# machine where no mesh exists yet, and run before there is anything to check it against. So it
|
|
||||||
# holds the program and nothing else — no shell, no package manager, no libc, nothing with a CVE
|
|
||||||
# feed of its own. What a person has to audit before trusting a first node is one binary.
|
|
||||||
#
|
|
||||||
# There are no CA certificates in here on purpose. Nothing it does today makes an outbound TLS
|
|
||||||
# connection to a public name: it reaches PostgreSQL on the machine it was raised on, and the
|
|
||||||
# broker is verified against a fingerprint pinned in a token rather than against a public root
|
|
||||||
# (novox/hq ADR 0004). Adding them "just in case" would put a trust store in the one image whose
|
|
||||||
# whole argument is that it contains nothing to reason about.
|
|
||||||
|
|
||||||
FROM ${GO_BASE} AS build
|
|
||||||
WORKDIR /src
|
|
||||||
|
|
||||||
# **Nothing is fetched** (novox/hq to-be 45 Phase 1): every dependency is in vendor/, committed, so
|
|
||||||
# the image builds from this repository alone — the host's validator among them, whose module no
|
|
||||||
# public proxy is asked for. Dependencies first, so a change to the source does not re-copy them.
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
COPY vendor/ vendor/
|
|
||||||
|
|
||||||
# `COPY . .` brings `.git` along, so Go is told not to stamp the commit: the version is said by -X, and a
|
|
||||||
# rebuild of an unchanged program is the same bytes (novox/hq ADR 0267 rule 6, issue 363).
|
|
||||||
COPY . .
|
|
||||||
ARG VERSION=development
|
|
||||||
RUN CGO_ENABLED=0 GOFLAGS=-mod=vendor GOPROXY=off go build -trimpath -buildvcs=false \
|
|
||||||
-ldflags "-s -w -X main.version=${VERSION}" \
|
|
||||||
-o /mesh-controller ./cmd/mesh-controller
|
|
||||||
|
|
||||||
FROM scratch
|
|
||||||
COPY --from=build /mesh-controller /mesh-controller
|
|
||||||
|
|
||||||
# Numeric because there is no /etc/passwd to look a name up in. Nothing here needs to be root:
|
|
||||||
# it opens outbound connections and writes nothing to its own filesystem.
|
|
||||||
USER 65534:65534
|
|
||||||
|
|
||||||
ENTRYPOINT ["/mesh-controller"]
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
# novox/hq ADR 0006 — the control plane, in Go.
|
|
||||||
#
|
|
||||||
# The image the bundle pins holds the program and nothing else, so the build is static and the
|
|
||||||
# container is built FROM scratch. That is not a size optimisation: this image is fetched by
|
|
||||||
# digest and run on a machine where no mesh exists to check anything, and everything in it is
|
|
||||||
# something a person would have to audit.
|
|
||||||
|
|
||||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo development)
|
|
||||||
LDFLAGS := -s -w -X main.version=$(VERSION)
|
|
||||||
|
|
||||||
# Where `make check` raises PostgreSQL. A high port and a throwaway container: nothing here
|
|
||||||
# touches a database anybody else is using. Override PG_PORT if this one is taken -- the first
|
|
||||||
# port chosen was already serving something that had been up for six days.
|
|
||||||
PG_PORT ?= 55532
|
|
||||||
PG_CONTAINER ?= mesh-controller-check
|
|
||||||
PG_IMAGE ?= postgres:17-alpine
|
|
||||||
export MESH_TEST_POSTGRES ?= postgres://postgres:check@127.0.0.1:$(PG_PORT)/postgres?sslmode=disable
|
|
||||||
|
|
||||||
.PHONY: build image check test vet fmt postgres postgres-stop clean
|
|
||||||
|
|
||||||
build:
|
|
||||||
CGO_ENABLED=0 go build -trimpath -ldflags '$(LDFLAGS)' -o build/mesh-controller ./cmd/mesh-controller
|
|
||||||
|
|
||||||
# Tagged 'development' as well as by version, because the lab places images by name and a
|
|
||||||
# scenario naming a version would have to be edited on every build. The version tag is what a
|
|
||||||
# real bundle pins.
|
|
||||||
IMAGE ?= mesh-controller:$(VERSION)
|
|
||||||
DEV_TAG ?= mesh-controller:development
|
|
||||||
|
|
||||||
# The Go base the image is built on.
|
|
||||||
#
|
|
||||||
# **`make image` was broken and stayed broken**, because the Dockerfile's fallback base was a Go
|
|
||||||
# older than go.mod asks for: every build died at `go mod download` with "go.mod requires go >=
|
|
||||||
# 1.26.0", and the pipeline never saw it because the pipeline passes the declared base in. Anybody
|
|
||||||
# building the image by hand hit it and had to find the digest themselves (novox/hq 04-ISSUES/146,
|
|
||||||
# what it cost).
|
|
||||||
#
|
|
||||||
# **Pinned here since the manifest stopped building an image** (novox/hq issue 213): the mesh builds
|
|
||||||
# the controller as a Go bundle with its own toolchain, and only `make image` — genesis and the lab —
|
|
||||||
# still needs a Go base. The digest is the one the manifest declared until then.
|
|
||||||
GO_BASE ?= golang@sha256:8ac98ca534ac3f51e1f420a1dd2c15e74c75cfa0f23f3ad27eb5d7236c349a0c
|
|
||||||
|
|
||||||
image:
|
|
||||||
@test -n "$(GO_BASE)" || { echo "no GO_BASE; pass GO_BASE=<image>"; exit 1; }
|
|
||||||
docker build --build-arg GO_BASE=$(GO_BASE) --build-arg VERSION=$(VERSION) -t $(IMAGE) -t $(DEV_TAG) .
|
|
||||||
@echo
|
|
||||||
@docker image inspect $(IMAGE) --format 'built {{.RepoTags}} {{.Size}} bytes'
|
|
||||||
|
|
||||||
# The builder ships as an image too, because it is a module the mesh assigns rather than a program
|
|
||||||
# somebody starts on a machine by hand.
|
|
||||||
BUILDER_IMAGE ?= mesh-builder:$(VERSION)
|
|
||||||
BUILDER_DEV_TAG ?= mesh-builder:development
|
|
||||||
|
|
||||||
builder-image:
|
|
||||||
@test -n "$(GO_BASE)" || { echo "no GO_BASE; pass GO_BASE=<image>"; exit 1; }
|
|
||||||
docker build --build-arg GO_BASE=$(GO_BASE) -f cmd/mesh-builder/Dockerfile -t $(BUILDER_IMAGE) -t $(BUILDER_DEV_TAG) .
|
|
||||||
@echo
|
|
||||||
@docker image inspect $(BUILDER_IMAGE) --format 'built {{.RepoTags}} {{.Size}} bytes'
|
|
||||||
|
|
||||||
# The provisioner ships as an image too, because it is the thing that makes a sealed credential
|
|
||||||
# true on a machine -- and the mesh cannot, having discarded the plaintext.
|
|
||||||
PROVISIONER_IMAGE ?= mesh-provision-postgres:$(VERSION)
|
|
||||||
PROVISIONER_DEV_TAG ?= mesh-provision-postgres:development
|
|
||||||
|
|
||||||
provisioner-image:
|
|
||||||
docker build --build-arg GO_BASE=$(GO_BASE) -f examples/postgres-provisioner/Dockerfile \
|
|
||||||
-t $(PROVISIONER_IMAGE) -t $(PROVISIONER_DEV_TAG) .
|
|
||||||
@echo
|
|
||||||
@docker image inspect $(PROVISIONER_IMAGE) --format 'built {{.RepoTags}} {{.Size}} bytes'
|
|
||||||
|
|
||||||
# The object store's provisioner, for the same reason: a bucket and a policy are not files, and
|
|
||||||
# the mesh cannot make them -- it discarded the credential it would have to use.
|
|
||||||
OBJECTSTORE_IMAGE ?= mesh-provision-objectstore:$(VERSION)
|
|
||||||
OBJECTSTORE_DEV_TAG ?= mesh-provision-objectstore:development
|
|
||||||
|
|
||||||
objectstore-image:
|
|
||||||
docker build --build-arg GO_BASE=$(GO_BASE) -f examples/objectstore-provisioner/Dockerfile \
|
|
||||||
-t $(OBJECTSTORE_IMAGE) -t $(OBJECTSTORE_DEV_TAG) .
|
|
||||||
@echo
|
|
||||||
@docker image inspect $(OBJECTSTORE_IMAGE) --format 'built {{.RepoTags}} {{.Size}} bytes'
|
|
||||||
|
|
||||||
# The cache's provisioner, for the same reason as the database's: an ACL user is not a file,
|
|
||||||
# and the mesh cannot make one -- it discarded the credential it would have to use.
|
|
||||||
REDIS_PROVISIONER_IMAGE ?= mesh-provision-redis:$(VERSION)
|
|
||||||
REDIS_PROVISIONER_DEV_TAG ?= mesh-provision-redis:development
|
|
||||||
|
|
||||||
redis-provisioner-image:
|
|
||||||
docker build --build-arg GO_BASE=$(GO_BASE) -f examples/redis-provisioner/Dockerfile \
|
|
||||||
-t $(REDIS_PROVISIONER_IMAGE) -t $(REDIS_PROVISIONER_DEV_TAG) .
|
|
||||||
@echo
|
|
||||||
@docker image inspect $(REDIS_PROVISIONER_IMAGE) --format 'built {{.RepoTags}} {{.Size}} bytes'
|
|
||||||
|
|
||||||
# The proxy that turns a route grant into traffic reaching a workload.
|
|
||||||
PROXY_IMAGE ?= mesh-route-proxy:$(VERSION)
|
|
||||||
PROXY_DEV_TAG ?= mesh-route-proxy:development
|
|
||||||
|
|
||||||
proxy-image:
|
|
||||||
docker build --build-arg GO_BASE=$(GO_BASE) -f modules/route-proxy/Dockerfile -t $(PROXY_IMAGE) -t $(PROXY_DEV_TAG) .
|
|
||||||
@echo
|
|
||||||
@docker image inspect $(PROXY_IMAGE) --format 'built {{.RepoTags}} {{.Size}} bytes'
|
|
||||||
|
|
||||||
# The whole check. Raises a database, runs the repository's own check against it -- merge-check.sh,
|
|
||||||
# the very script `mesh/repo-check` runs -- and takes the database down again, including when the
|
|
||||||
# check fails, which is why the teardown is not conditional and the script's exit status is the
|
|
||||||
# target's.
|
|
||||||
#
|
|
||||||
# **It calls the script rather than restating it** (novox/hq issue 431): this target used to list its
|
|
||||||
# own steps, its formatting step walked vendor/ where the script's does not, and it failed on
|
|
||||||
# third-party code no change could fix -- so a developer's check and the gate disagreed, and the steps
|
|
||||||
# after formatting never ran through it. The script is the one list of steps; this target only gives it
|
|
||||||
# a database (MESH_TEST_POSTGRES, exported above).
|
|
||||||
#
|
|
||||||
# **Two levels** (issue 895): the unit level, `go test ./...` untagged, timed and held to a bound; and the integration
|
|
||||||
# level, `-tags integration`, the tests that start a bus, a store or another process. Each is said with its time.
|
|
||||||
#
|
|
||||||
# **Packages in parallel, under the race detector, each test on a bus of its own** (internal/testbus),
|
|
||||||
# as the script runs them: a bus per test, of the release the mesh runs, makes the suite the same in any
|
|
||||||
# order, and the timeout bounds a hang to a failure with a stack, never a stalled gate.
|
|
||||||
check: postgres
|
|
||||||
@sh merge-check.sh ; status=$$? ; docker rm -f -v $(PG_CONTAINER) >/dev/null 2>&1 || true ; exit $$status
|
|
||||||
|
|
||||||
# The unit level alone (untagged: no bus, no store, no other process), which a developer runs continuously. The tests
|
|
||||||
# that need a bus or the store carry `//go:build integration` and run in `check`, not here; without a database the
|
|
||||||
# store's tests would skip rather than fail anyway.
|
|
||||||
test:
|
|
||||||
go test -timeout 15m ./...
|
|
||||||
|
|
||||||
vet:
|
|
||||||
go vet ./...
|
|
||||||
|
|
||||||
# Quick steps for a developer, not part of `check`. The directories gofmt reads must be the ones
|
|
||||||
# merge-check.sh lists, never `.`, which walks vendor/ (novox/hq issue 431).
|
|
||||||
fmt:
|
|
||||||
@unformatted=$$(gofmt -l cmd internal examples) ; \
|
|
||||||
if [ -n "$$unformatted" ] ; then echo "not gofmt'd:" ; echo "$$unformatted" ; exit 1 ; fi
|
|
||||||
|
|
||||||
postgres:
|
|
||||||
@docker rm -f -v $(PG_CONTAINER) >/dev/null 2>&1 || true
|
|
||||||
@docker run -d --name $(PG_CONTAINER) -e POSTGRES_PASSWORD=check \
|
|
||||||
-p 127.0.0.1:$(PG_PORT):5432 $(PG_IMAGE) >/dev/null
|
|
||||||
@printf 'waiting for postgres'
|
|
||||||
@for i in $$(seq 1 60) ; do \
|
|
||||||
if docker exec $(PG_CONTAINER) pg_isready -U postgres >/dev/null 2>&1 ; then \
|
|
||||||
echo ' — ready' ; exit 0 ; fi ; \
|
|
||||||
printf '.' ; sleep 1 ; \
|
|
||||||
done ; \
|
|
||||||
echo ' — never came up' ; docker logs $(PG_CONTAINER) | tail -20 ; exit 1
|
|
||||||
|
|
||||||
postgres-stop:
|
|
||||||
@docker rm -f -v $(PG_CONTAINER) >/dev/null 2>&1 || true
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf build/
|
|
||||||
@@ -1,218 +1,7 @@
|
|||||||
# mesh-controller
|
# mesh-controller
|
||||||
|
|
||||||
**Tier 2 of Novox Mesh — the control plane.** Everything that needs to know about more than one
|
The controller ended under issue 1126. Its jobs moved to the parts that ADR 0590 and ADR 0593 name: the indexer, the
|
||||||
node.
|
assigner, the provisioner, the grants part and the node-engine. Its last module, route-proxy, moved to mesh-networking.
|
||||||
|
|
||||||
That is the whole test, and it draws the line the host cannot: the host applies and does not
|
This repository holds no code and is archived once the delivery of the group that emptied it (release sdk1) is done.
|
||||||
decide, *because deciding needs knowledge one machine does not have*. Which nodes should run the
|
Its history stays here for reading.
|
||||||
store, which peers belong in an overlay, whether a node has been unreachable for a week — nobody
|
|
||||||
on a single machine can answer any of them.
|
|
||||||
|
|
||||||
The reasoning lives in [novox/hq](https://git.novox.be/novox/hq); this repository carries no
|
|
||||||
argument that is not settled there.
|
|
||||||
|
|
||||||
## What it is not
|
|
||||||
|
|
||||||
- **Not the thing that changes machines.** It decides; the host applies. It never reaches into a
|
|
||||||
node except through the host, over the link, in a bounded vocabulary.
|
|
||||||
- **Not a database.** There is no mesh database. Each context owns its own store and nothing
|
|
||||||
outside a context touches it — including nodes, which hold no credential to any of them.
|
|
||||||
- **Not privileged.** It has no more access to a machine than a declaration can express.
|
|
||||||
|
|
||||||
## What exists today
|
|
||||||
|
|
||||||
**One context of seven, and one of the things it will do.**
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| `inventory` | node records and enrolment tokens — **built, as far as identity** |
|
|
||||||
| `identity` | the control plane's signing key, and the keys nodes are known by — **built** |
|
|
||||||
| `config`, `connectivity`, `provisioning`, `delivery`, `observability` | not built |
|
|
||||||
| the interface every surface speaks to | not built; its shape is not decided |
|
|
||||||
|
|
||||||
```
|
|
||||||
mesh-controller migrate bring each context's schema up to date
|
|
||||||
mesh-controller node add <name> create a node record
|
|
||||||
mesh-controller node list the nodes this mesh knows about
|
|
||||||
mesh-controller token issue --node <name> a one-time right to join, for an existing record
|
|
||||||
mesh-controller token issue --new <name> create the record and issue for it
|
|
||||||
mesh-controller identity show this control plane's signing key
|
|
||||||
mesh-controller broker show where the broker is, and what to expect there
|
|
||||||
mesh-controller version what this binary is
|
|
||||||
```
|
|
||||||
|
|
||||||
`migrate` is **step 3 of the foundation bootstrap** — the step the first node cannot get past, run
|
|
||||||
against a database raised moments earlier from the bundle the host carries.
|
|
||||||
|
|
||||||
### Tokens, and what they are missing
|
|
||||||
|
|
||||||
A token is **a one-time right to join, issued for a node record** — which is where re-enrolment is
|
|
||||||
decided, since what an identity binds to is settled when the token is made rather than when it is
|
|
||||||
presented.
|
|
||||||
|
|
||||||
What is built: the secret is 256 bits from the system's random source, shown once, and **stored
|
|
||||||
only as a hash**, so a copy of this database is not a set of working credentials. It is usable
|
|
||||||
exactly once and only before it expires, and both are read from the row rather than from a status
|
|
||||||
something would have had to write. Issuing again for the same node invalidates the outstanding
|
|
||||||
one — two live tokens are two machines able to join as the same node.
|
|
||||||
|
|
||||||
Redemption is a single statement that both finds a live token and spends it, so eight concurrent
|
|
||||||
attempts on one secret produce exactly one winner. There is a test that runs them.
|
|
||||||
|
|
||||||
**A token now carries three of its four parts**, and is one line of base64 a person can copy. The
|
|
||||||
signing key is real: an Ed25519 key this control plane generates once and keeps, whose public half
|
|
||||||
travels in every token. A node believes a declaration because it carries a signature that key made
|
|
||||||
— and pinning only the broker would not do, because it would make the control plane's authority
|
|
||||||
transitive, so a compromised broker could forge declarations, and since the host applies whatever
|
|
||||||
the link delivers that is the whole machine.
|
|
||||||
|
|
||||||
**All four parts are built.** Given `MESH_BROKER_ADDRESS` and `MESH_BROKER_CERTIFICATE`, a token
|
|
||||||
carries everything ADR 0004 requires. Without them it carries two, and `token issue` prints it
|
|
||||||
**and names what is missing** rather than producing something that looks complete and cannot be
|
|
||||||
used.
|
|
||||||
|
|
||||||
**The fingerprint is derived from the certificate, never configured.** A configured one can drift
|
|
||||||
from the certificate it describes, and a drifted pin is worse than none: every node issued a token
|
|
||||||
during the drift refuses to connect, and the failure looks like an attack. It is computed over the
|
|
||||||
DER bytes — what a client actually sees on the wire — so the same certificate re-wrapped with
|
|
||||||
different line endings still produces the same pin.
|
|
||||||
|
|
||||||
### Two contexts, and the rule between them is real
|
|
||||||
|
|
||||||
`identity` is the second context and it exists partly to test a claim this repository had made and
|
|
||||||
never checked: that a context reaches only its own store. It holds `MESH_STORE_IDENTITY`;
|
|
||||||
`inventory` holds `MESH_STORE_INVENTORY`; there is no setting that reaches both and no way to ask
|
|
||||||
for one. Run `migrate` with only one and it stops, naming the grant it does not have.
|
|
||||||
|
|
||||||
A token needs a node record from one and a signing key from the other. Neither reads the other's
|
|
||||||
store — the process holding both grants asks each for its part.
|
|
||||||
|
|
||||||
### How a node is known
|
|
||||||
|
|
||||||
**The node generates a keypair; the mesh records the public half.** The same principle as SSH, and
|
|
||||||
the same rule `08-connectivity` already applies to the overlay keys — which is where this was
|
|
||||||
settled all along, though it took being asked directly to notice.
|
|
||||||
|
|
||||||
Only the public half is ever stored, and that is the property worth having: **a copy of this
|
|
||||||
database grants nothing.** It is a list of who to believe, not a set of credentials, which is what
|
|
||||||
makes *compromise of a node is compromise of that node* literally true.
|
|
||||||
|
|
||||||
Exactly one key is live per node. Re-enrolment revokes the one it replaced, in the same
|
|
||||||
transaction — two live identities for one node record is the stolen-laptop case, with the replaced
|
|
||||||
machine still believed. The database enforces it as well as the code, and there is a test running
|
|
||||||
six concurrent enrolments that fails when the constraint is removed.
|
|
||||||
|
|
||||||
**Not the machine's SSH host key**, though that was the obvious economy. Host keys are regenerated
|
|
||||||
by reinstalls and image clones, which would silently un-enrol a node; their lifecycle belongs to
|
|
||||||
sshd rather than the mesh; and a partial host has no SSH daemon at all.
|
|
||||||
|
|
||||||
## Reaching a store
|
|
||||||
|
|
||||||
**A context is granted only what it exclusively owns.** No shared writes, no read-only role on
|
|
||||||
another context's store, and no connection string that reaches more than one.
|
|
||||||
|
|
||||||
That is a rule about credentials, so it is built as one. There is no mesh-wide connection setting
|
|
||||||
and no way to ask for one:
|
|
||||||
|
|
||||||
```
|
|
||||||
MESH_STORE_INVENTORY=postgres://…/inventory
|
|
||||||
```
|
|
||||||
|
|
||||||
A process granted `inventory` holds that variable and no other. Reaching another context's store
|
|
||||||
is not a matter of restraint — it has no address for it and no credential to present. And it is
|
|
||||||
how the rule is *checked*: what a context can reach is visible in the declaration that runs it,
|
|
||||||
as the list of variables it was given.
|
|
||||||
|
|
||||||
Each context's database is named after the context. There is deliberately no database named for
|
|
||||||
the mesh as a whole.
|
|
||||||
|
|
||||||
## Migrations
|
|
||||||
|
|
||||||
Numbered, embedded in the binary, applied in order, each in a transaction with the row recording
|
|
||||||
it. The applying is four lines; the rest is refusals, and the refusals are the point:
|
|
||||||
|
|
||||||
| it stops when | because |
|
|
||||||
|---|---|
|
|
||||||
| a migration that ran has since been edited | the database holds the old version, the repository holds the new one, and nothing holds the difference |
|
|
||||||
| a migration is numbered below one that already ran | usually two branches taking the same next number — applying it now runs the schema in an order nobody tested |
|
|
||||||
| two migrations share a number | order is the entire guarantee, and two files with one number have none |
|
|
||||||
| a file in the directory is not a valid migration name | a misnamed migration would otherwise never run and nothing would say so |
|
|
||||||
| a migration is empty | it records that something happened and changes nothing, which cannot be told from a mistake |
|
|
||||||
|
|
||||||
All of them stop rather than warn. At the moment any of them is true, nobody knows what the
|
|
||||||
database contains, and there is no correct guess about a schema.
|
|
||||||
|
|
||||||
Running it again does nothing. Two copies running at once take an advisory lock, so a restart
|
|
||||||
during a slow migration does not become two runners racing.
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
```
|
|
||||||
make build the binary
|
|
||||||
make image the container image
|
|
||||||
make check gofmt, vet, and every test against a real PostgreSQL
|
|
||||||
```
|
|
||||||
|
|
||||||
`make check` raises a throwaway PostgreSQL in a container and takes it down afterwards, including
|
|
||||||
when the tests fail. Without one the tests that need a database **skip and say so** rather than
|
|
||||||
passing quietly — `make test` is the honest subset, not the gate.
|
|
||||||
|
|
||||||
Tests run against a real database rather than a fake because what is being tested *is* the
|
|
||||||
database's behaviour: that DDL is transactional, that an advisory lock serialises, that a
|
|
||||||
checksum mismatch is caught against a record PostgreSQL actually kept. A fake would assert that
|
|
||||||
the fake behaves as expected.
|
|
||||||
|
|
||||||
Every test here has been confirmed to fail when the behaviour it defends is removed. Two did not,
|
|
||||||
when first written, and both are now commented with what they were missing.
|
|
||||||
|
|
||||||
## Checks that cross into the host's repository
|
|
||||||
|
|
||||||
Two things are agreed between this repository and `novox/mesh-host`, and each is a separate struct
|
|
||||||
on each side. A field renamed on one of them fails **silently** — the crossing succeeds and
|
|
||||||
something is simply absent — so both are checked by handing one side's real output to the other's
|
|
||||||
real parser. Neither runs by default; each skips with a reason, because a repository that fails
|
|
||||||
without its neighbour checked out is a repository nobody can build.
|
|
||||||
|
|
||||||
**What this mesh sends, read by the host that receives it:**
|
|
||||||
|
|
||||||
```
|
|
||||||
mesh-controller: ./build/mesh-controller plan <node> --json > /tmp/d.json
|
|
||||||
mesh-host: MESH_EMITTED=/tmp/d.json go test ./internal/declaration/ -v
|
|
||||||
```
|
|
||||||
|
|
||||||
**What a node says when it joins, read by this mesh:**
|
|
||||||
|
|
||||||
```
|
|
||||||
mesh-host: MESH_ENROL_OUT=/tmp/enrol.json go test ./internal/link/
|
|
||||||
mesh-controller: MESH_ENROL=/tmp/enrol.json make check
|
|
||||||
```
|
|
||||||
|
|
||||||
The second does more than compare shapes: it seals something to the key that arrived and opens it
|
|
||||||
with the private half the host kept. A key that is present, correctly named and simply *wrong*
|
|
||||||
passes every check that only looks at the message.
|
|
||||||
|
|
||||||
## The image
|
|
||||||
|
|
||||||
**The mesh no longer runs the controller from it** (novox/hq issue 213). The module declares a Go
|
|
||||||
bundle, `controller`, which the host on the controller's machine unpacks and runs as the process
|
|
||||||
`mesh-controller` under the account of the same name (ADR 0188 §1, §3). The image stays for what
|
|
||||||
still runs a container of the controller: genesis, which raises the first controller from it and
|
|
||||||
installs the module from its manifest (mesh-host `internal/bootstrap`), and the lab. Neither is the
|
|
||||||
mesh's own build any more — `make image` builds it.
|
|
||||||
|
|
||||||
`FROM scratch`, holding one statically linked binary and nothing else — no shell, no package
|
|
||||||
manager, no libc, no CA certificates.
|
|
||||||
|
|
||||||
Not a size optimisation. The bundle a host carries pins this image by digest, and it is fetched
|
|
||||||
and run on a machine where no mesh exists to check anything and a person is expected to have read
|
|
||||||
the bundle and believed it. Everything in the image is something that person would have to audit.
|
|
||||||
|
|
||||||
## Where the reasoning lives
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| what the control plane is | novox/hq ADR 0006 |
|
|
||||||
| what it takes to run one, and why Go | novox/hq ADR 0006 |
|
|
||||||
| a context owns its store, exclusively | novox/hq ADR 0008 |
|
|
||||||
| schema changes are numbered migrations | novox/hq ADR 0013 |
|
|
||||||
| a test defends a decision | novox/hq ADR 0017 |
|
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
# Release 57: config and provisioning
|
|
||||||
|
|
||||||
This branch carries the work of hq design 57 (ADR 0348, ADR 0349) in this repository. Each package
|
|
||||||
merges here through its own reviewed pull request; the branch reaches main as one pull request, in one
|
|
||||||
delivery group with the branch of the same name in mesh-controller, mesh-host, mesh-sdk, mesh-catalog
|
|
||||||
and hq, after a rehearsal in the lab. This file is deleted by the last commit before that pull request.
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# The unit tests of every Go module of its own below this repository's root, run from the root, for merge-check.sh
|
|
||||||
# and for the test that tries it on a tree (merge_check_nested_test.go). Its own file so that the test needs no
|
|
||||||
# particular merge-check.sh: the check that judges a change to merge-check.sh is main's.
|
|
||||||
set -eu
|
|
||||||
cgo="${cgo:-0}"
|
|
||||||
race="${race:-}"
|
|
||||||
|
|
||||||
# nested_modules vets and tests every Go module of its own below the root, `modules/<name>/go.mod` (novox/hq ADR 0420
|
|
||||||
# §5, issue 1025): `./...` stops at a nested go.mod, so a module that lives whole in this repository would have its
|
|
||||||
# unit tests run by nobody. None is a pass. Each runs in its own directory, outside the vendor directory this
|
|
||||||
# repository's own packages are built from; a nested module vendors what the forge holds (its own `vendor/`, so the
|
|
||||||
# check runs offline), as this repository's root module does (issue 1019). A failing module fails the check; the others are still run.
|
|
||||||
nested_modules() {
|
|
||||||
nested=0
|
|
||||||
for gomod in modules/*/go.mod; do
|
|
||||||
[ -e "$gomod" ] || continue
|
|
||||||
dir=$(dirname "$gomod")
|
|
||||||
echo "nested module $dir:"
|
|
||||||
if ! (cd "$dir" && GOFLAGS= CGO_ENABLED=0 go vet ./... &&
|
|
||||||
GOFLAGS= CGO_ENABLED="${cgo:-0}" go test ${race:-} -count=1 -timeout 30m ./...); then
|
|
||||||
echo "nested module $dir FAILED"
|
|
||||||
nested=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
return $nested
|
|
||||||
}
|
|
||||||
|
|
||||||
nested_modules
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
ARG ALPINE_BASE=alpine:3
|
|
||||||
ARG GO_BASE=golang:1.25-alpine
|
|
||||||
# The builder, as a module ships one.
|
|
||||||
#
|
|
||||||
# Not FROM scratch, unlike the control plane: this one runs git and a container client, so it
|
|
||||||
# needs a filesystem with them on it. That is the honest cost of a machine whose job is to build —
|
|
||||||
# and it is why building is a MODULE on a machine that has a runtime rather than something the
|
|
||||||
# control plane does (novox/hq ADR 0005).
|
|
||||||
FROM ${GO_BASE} AS build
|
|
||||||
WORKDIR /src
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
RUN go mod download
|
|
||||||
COPY . .
|
|
||||||
RUN CGO_ENABLED=0 go build -trimpath -buildvcs=false -ldflags '-s -w' -o /mesh-builder ./cmd/mesh-builder
|
|
||||||
|
|
||||||
FROM ${ALPINE_BASE}
|
|
||||||
# git to clone what it is asked to build, and the docker client to build and push it. The daemon
|
|
||||||
# is the machine's, reached through its socket — a build machine shares the runtime it was given
|
|
||||||
# rather than running one inside itself.
|
|
||||||
RUN apk add --no-cache git docker-cli
|
|
||||||
COPY --from=build /mesh-builder /usr/local/bin/mesh-builder
|
|
||||||
ENTRYPOINT ["/usr/local/bin/mesh-builder"]
|
|
||||||
@@ -1,235 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **The build agent says whether it is busy, and being asked drains it** (novox/hq ADR 0335 §1, §5). Its
|
|
||||||
// container declares `busy` as `mesh-builder busy`: the node-engine runs it inside the container before it
|
|
||||||
// replaces it. It exits 0 while a build is in hand and 1 when none is; and from the first ask the agent takes
|
|
||||||
// no new build request, so its answer ends once the build in hand ends — a replaced builder finishes the check
|
|
||||||
// it runs, instead of the build queue handing it out again from the start (issue 594).
|
|
||||||
//
|
|
||||||
// Two marks in the container's own temporary directory, as the ready mark (ready.go): a new container starts
|
|
||||||
// without them, and a restarted one removes them before it takes work again.
|
|
||||||
//
|
|
||||||
// - in hand: written by the agent when it claims a request, before it reads whether it is drained; removed
|
|
||||||
// when the build ends.
|
|
||||||
// - drained: written by the ask, before it reads whether a build is in hand.
|
|
||||||
//
|
|
||||||
// Each side writes its own mark before it reads the other's, so an ask racing a fetch sees the build in hand,
|
|
||||||
// or the agent sees the drain and hands the request back; never neither.
|
|
||||||
|
|
||||||
func inHandMark() string { return filepath.Join(os.TempDir(), "mesh-builder.in-hand") }
|
|
||||||
func drainMark() string { return filepath.Join(os.TempDir(), "mesh-builder.drained") }
|
|
||||||
|
|
||||||
// The drain lapses when no ask has come for drainLapse: the node-engine asks every 20 seconds for as long as it
|
|
||||||
// really waits, so an agent whose replacement was withdrawn — the declaration put back, the walk stopped —
|
|
||||||
// takes work again rather than staying drained for ever (review of ADR 0335). And an agent drained for longer
|
|
||||||
// than the node-engine's bound and the slack says it is not ready: the replacement it was drained for never
|
|
||||||
// came, which is a fault to see.
|
|
||||||
const (
|
|
||||||
drainLapse = 2 * time.Minute
|
|
||||||
drainTooLong = 15*time.Minute + 5*time.Minute
|
|
||||||
)
|
|
||||||
|
|
||||||
// drainedAt is when the drain began and when it was last asked, from its mark; false when there is no mark.
|
|
||||||
func drainedAt() (began, asked time.Time, ok bool) {
|
|
||||||
info, err := os.Stat(drainMark())
|
|
||||||
if err != nil {
|
|
||||||
return time.Time{}, time.Time{}, false
|
|
||||||
}
|
|
||||||
asked = info.ModTime()
|
|
||||||
began = asked
|
|
||||||
if raw, err := os.ReadFile(drainMark()); err == nil {
|
|
||||||
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(raw))); err == nil && !t.After(asked) {
|
|
||||||
began = t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return began, asked, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// drained says the agent was asked whether it is busy within drainLapse: it takes no new build request.
|
|
||||||
func drained() bool { return drainedSince(time.Now()) }
|
|
||||||
|
|
||||||
func drainedSince(now time.Time) bool {
|
|
||||||
_, asked, ok := drainedAt()
|
|
||||||
return ok && now.Sub(asked) < drainLapse
|
|
||||||
}
|
|
||||||
|
|
||||||
// drainedTooLong is the health check's second half: nil unless the agent has been drained, asked all along, for
|
|
||||||
// longer than its node-engine's bound and the slack.
|
|
||||||
func drainedTooLong(now time.Time) error {
|
|
||||||
began, asked, ok := drainedAt()
|
|
||||||
if !ok || now.Sub(asked) >= drainLapse || now.Sub(began) <= drainTooLong {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return fmt.Errorf("drained since %s and still asked: the replacement its node-engine waits for has not come "+
|
|
||||||
"within %s (novox/hq ADR 0335)", began.UTC().Format(time.RFC3339), drainTooLong)
|
|
||||||
}
|
|
||||||
|
|
||||||
// inHand is true while this process has claimed a build and not yet released it. The mark in the temporary directory
|
|
||||||
// is only believed while it is: a mark with no build of this process behind it is stale (a crash, a fault in an
|
|
||||||
// earlier path) and is removed, because left alone it hands back every ask for ever (issue 1768).
|
|
||||||
var inHand atomic.Bool
|
|
||||||
|
|
||||||
// markRetry is how long an agent that cannot mark a build in hand takes no work, and markFault when it last failed to.
|
|
||||||
// Taking nothing is the point: an ask fetched and handed back at once, many times a second, spends all its deliveries.
|
|
||||||
const markRetry = 30 * time.Second
|
|
||||||
|
|
||||||
var (
|
|
||||||
markMu sync.Mutex
|
|
||||||
markFault time.Time
|
|
||||||
markSaid string
|
|
||||||
)
|
|
||||||
|
|
||||||
// markFaulty says the agent could not mark a build in hand and has not yet been able to since: it takes no work. Once
|
|
||||||
// markRetry has passed it tries the mark itself, with no ask in hand, so that an ask is fetched only when marking works.
|
|
||||||
func markFaulty(now time.Time) bool {
|
|
||||||
markMu.Lock()
|
|
||||||
failed := markFault
|
|
||||||
markMu.Unlock()
|
|
||||||
if failed.IsZero() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if now.Sub(failed) < markRetry {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
f, err := os.OpenFile(inHandMark(), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
|
||||||
if err != nil && errors.Is(err, os.ErrExist) && !inHand.Load() {
|
|
||||||
_ = os.Remove(inHandMark())
|
|
||||||
f, err = os.OpenFile(inHandMark(), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
markFailed(err)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
_ = f.Close()
|
|
||||||
_ = os.Remove(inHandMark())
|
|
||||||
markMu.Lock()
|
|
||||||
markFault, markSaid = time.Time{}, ""
|
|
||||||
markMu.Unlock()
|
|
||||||
fmt.Fprintf(os.Stderr, "the mark of a build in hand can be made again (%s): taking builds\n", inHandMark())
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// markFailed notes that a build could not be marked in hand, and says it when it is a new reason.
|
|
||||||
func markFailed(err error) {
|
|
||||||
markMu.Lock()
|
|
||||||
defer markMu.Unlock()
|
|
||||||
markFault = time.Now()
|
|
||||||
if said := err.Error(); said != markSaid {
|
|
||||||
markSaid = said
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot mark a build in hand (%s), so this agent takes no build for %s and tries again: %v\n",
|
|
||||||
inHandMark(), markRetry, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// claim marks a request in hand, unless the agent is drained: then nothing is in hand, and the request goes
|
|
||||||
// back to the queue.
|
|
||||||
func claim() bool {
|
|
||||||
if !inHand.Load() {
|
|
||||||
// Nothing of this process is in hand, so a mark already there is stale.
|
|
||||||
if err := os.Remove(inHandMark()); err == nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "removed the mark of a build in hand (%s): no build of this agent is in hand\n", inHandMark())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Made, never written over: one build at a time, and a mark already there is a build in hand.
|
|
||||||
f, err := os.OpenFile(inHandMark(), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
// Unmarked, an ask would say idle while this build runs: the build is not taken.
|
|
||||||
markFailed(err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
_, err = f.WriteString(time.Now().UTC().Format(time.RFC3339) + "\n")
|
|
||||||
if closeErr := f.Close(); err == nil {
|
|
||||||
err = closeErr
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
_ = os.Remove(inHandMark())
|
|
||||||
markFailed(err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if drained() {
|
|
||||||
_ = os.Remove(inHandMark())
|
|
||||||
fmt.Fprintln(os.Stderr, "asked whether busy, so this agent takes no new build: handed back")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
inHand.Store(true)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// holding says what is in hand, for the ask to print: written once the request is read.
|
|
||||||
func holding(what string) {
|
|
||||||
_ = os.WriteFile(inHandMark(), []byte(time.Now().UTC().Format(time.RFC3339)+" "+what+"\n"), 0o644)
|
|
||||||
}
|
|
||||||
|
|
||||||
// released ends the build in hand.
|
|
||||||
func released() {
|
|
||||||
inHand.Store(false)
|
|
||||||
if err := os.Remove(inHandMark()); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot remove the mark of the build in hand (%s): %v\n", inHandMark(), err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// undrained clears both marks at the start of a holder: a container started again is asked afresh.
|
|
||||||
func undrained() error {
|
|
||||||
for _, mark := range []string{drainMark(), inHandMark()} {
|
|
||||||
if err := os.Remove(mark); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
||||||
return fmt.Errorf("cannot remove %s: %w", mark, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// busy is the ask: it drains the agent, then says whether a build is in hand. Nil — exit 0 — while one is.
|
|
||||||
func busy() (string, error) {
|
|
||||||
// The mark keeps when the drain began while it is asked within drainLapse, and is written again on every
|
|
||||||
// ask: its time is the last ask.
|
|
||||||
began := time.Now().UTC()
|
|
||||||
if was, _, ok := drainedAt(); ok && drained() {
|
|
||||||
began = was.UTC()
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(drainMark(), []byte(began.Format(time.RFC3339)+"\n"), 0o644); err != nil {
|
|
||||||
// Not drained, it could take the next build: said, and answered busy only for what is in hand now.
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot mark this agent drained (%s): %v\n", drainMark(), err)
|
|
||||||
}
|
|
||||||
held, err := os.ReadFile(inHandMark())
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.New("idle: no build in hand, and this agent takes no new one")
|
|
||||||
}
|
|
||||||
since, what, _ := strings.Cut(strings.TrimSpace(string(held)), " ")
|
|
||||||
if what == "" {
|
|
||||||
what = "a build"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("busy: %s, in hand since %s; taking no new build", what, since), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
roomMu sync.Mutex
|
|
||||||
roomSaid bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// noRoom says this node is under the floor of free space (novox/hq issue 1834): it takes no work, so another build node
|
|
||||||
// takes the ask instead of this one taking it and ending it in an error. Said once when it starts and again when it ends.
|
|
||||||
func noRoom(workspace string) bool {
|
|
||||||
err := builder.RoomToWork("/", workspace)
|
|
||||||
roomMu.Lock()
|
|
||||||
defer roomMu.Unlock()
|
|
||||||
if err != nil && !roomSaid {
|
|
||||||
fmt.Fprintf(os.Stderr, "taking no builds: %v\n", err)
|
|
||||||
} else if err == nil && roomSaid {
|
|
||||||
fmt.Fprintf(os.Stderr, "there is room again: taking builds\n")
|
|
||||||
}
|
|
||||||
roomSaid = err != nil
|
|
||||||
return err != nil
|
|
||||||
}
|
|
||||||
@@ -1,239 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// `mesh-builder busy` answers 0 while a build is in hand and non-zero when idle, and after its first ask the
|
|
||||||
// agent takes no new build request (novox/hq ADR 0335 §5).
|
|
||||||
func TestBusyAnswersWhileABuildIsInHandAndDrainsTheAgent(t *testing.T) {
|
|
||||||
t.Setenv("TMPDIR", t.TempDir())
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if drained() {
|
|
||||||
t.Fatal("an agent never asked is drained")
|
|
||||||
}
|
|
||||||
// A build is claimed and runs.
|
|
||||||
if !claim() {
|
|
||||||
t.Fatal("an agent never asked refused a build")
|
|
||||||
}
|
|
||||||
holding("novox/mesh-controller (check-280)")
|
|
||||||
said, err := busy()
|
|
||||||
if err != nil || !strings.Contains(said, "novox/mesh-controller (check-280)") {
|
|
||||||
t.Fatalf("an agent with a build in hand did not answer busy with what it holds: %q %v", said, err)
|
|
||||||
}
|
|
||||||
// From that ask on it takes nothing new: the fetch is not made, and one fetched is handed back.
|
|
||||||
if !drained() {
|
|
||||||
t.Fatal("the ask did not drain the agent")
|
|
||||||
}
|
|
||||||
if claim() {
|
|
||||||
t.Fatal("a drained agent took a new build")
|
|
||||||
}
|
|
||||||
// The build in hand is still in hand after the refused claim, and busy goes on saying so.
|
|
||||||
if _, err := busy(); err != nil {
|
|
||||||
t.Fatalf("a refused claim ended the build in hand: %v", err)
|
|
||||||
}
|
|
||||||
// It ends: idle, and still drained.
|
|
||||||
released()
|
|
||||||
if _, err := busy(); err == nil {
|
|
||||||
t.Fatal("an agent with nothing in hand answered busy")
|
|
||||||
}
|
|
||||||
if claim() {
|
|
||||||
t.Fatal("a drained agent took a build after its last one ended")
|
|
||||||
}
|
|
||||||
// Started again, it is asked afresh.
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if drained() || !claim() {
|
|
||||||
t.Fatal("a holder started again is still drained")
|
|
||||||
}
|
|
||||||
released()
|
|
||||||
}
|
|
||||||
|
|
||||||
// An ask racing a fetch sees the build in hand, or the agent sees the drain and hands the request back: never
|
|
||||||
// neither, which would replace the container under a build just taken.
|
|
||||||
func TestAnAskRacingAFetchNeverMissesTheBuild(t *testing.T) {
|
|
||||||
t.Setenv("TMPDIR", t.TempDir())
|
|
||||||
for i := 0; i < 200; i++ {
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var claimed bool
|
|
||||||
var askErr error
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(2)
|
|
||||||
go func() { defer wg.Done(); claimed = claim() }()
|
|
||||||
go func() { defer wg.Done(); _, askErr = busy() }()
|
|
||||||
wg.Wait()
|
|
||||||
if claimed && askErr != nil {
|
|
||||||
t.Fatalf("round %d: the agent took a build and the ask said idle", i)
|
|
||||||
}
|
|
||||||
released()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A drain no longer asked lapses: an agent whose replacement was withdrawn after the first ask (a put-back, a
|
|
||||||
// revert, a stopped walk) takes work again after drainLapse, rather than staying drained with its health green.
|
|
||||||
func TestADrainNoLongerAskedLapses(t *testing.T) {
|
|
||||||
t.Setenv("TMPDIR", t.TempDir())
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := busy(); err == nil {
|
|
||||||
t.Fatal("an idle agent answered busy")
|
|
||||||
}
|
|
||||||
if !drained() || claim() {
|
|
||||||
t.Fatal("an agent just asked takes work")
|
|
||||||
}
|
|
||||||
// The last ask was three minutes ago.
|
|
||||||
old := time.Now().Add(-3 * time.Minute)
|
|
||||||
if err := os.Chtimes(drainMark(), old, old); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if drained() {
|
|
||||||
t.Fatal("a drain no longer asked for three minutes still holds")
|
|
||||||
}
|
|
||||||
if !claim() {
|
|
||||||
t.Fatal("an agent whose drain lapsed takes no work")
|
|
||||||
}
|
|
||||||
released()
|
|
||||||
}
|
|
||||||
|
|
||||||
// An agent drained, and asked all along, for longer than its node-engine's bound and the slack is not ready: the
|
|
||||||
// replacement it was drained for never came. One asked again after a lapse starts a new drain.
|
|
||||||
func TestAnAgentDrainedTooLongIsNotReady(t *testing.T) {
|
|
||||||
t.Setenv("TMPDIR", t.TempDir())
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
taking("mesh-build-machine")
|
|
||||||
if _, err := busy(); err == nil {
|
|
||||||
t.Fatal("an idle agent answered busy")
|
|
||||||
}
|
|
||||||
now := time.Now()
|
|
||||||
if err := drainedTooLong(now); err != nil {
|
|
||||||
t.Fatalf("an agent drained a moment ago is not ready: %v", err)
|
|
||||||
}
|
|
||||||
// The drain began 25 minutes ago, and the node-engine asked a moment ago.
|
|
||||||
began := now.Add(-25 * time.Minute).UTC().Format(time.RFC3339)
|
|
||||||
if err := os.WriteFile(drainMark(), []byte(began+"\n"), 0o644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
err := drainedTooLong(time.Now())
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "has not come") {
|
|
||||||
t.Fatalf("an agent drained for 25 minutes reads ready: %v", err)
|
|
||||||
}
|
|
||||||
// Each ask keeps when the drain began.
|
|
||||||
if _, err := busy(); err == nil {
|
|
||||||
t.Fatal("an idle agent answered busy")
|
|
||||||
}
|
|
||||||
if err := drainedTooLong(time.Now()); err == nil {
|
|
||||||
t.Fatal("an ask started the drain again")
|
|
||||||
}
|
|
||||||
// The asks stopped: the drain lapsed, and so did the fault.
|
|
||||||
old := time.Now().Add(-3 * time.Minute)
|
|
||||||
if err := os.Chtimes(drainMark(), old, old); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := drainedTooLong(time.Now()); err != nil {
|
|
||||||
t.Fatalf("a lapsed drain still reads not ready: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// An ask this build does not know exits non-zero: an older image asked `busy` reads idle, not busy for the
|
|
||||||
// node-engine's whole bound.
|
|
||||||
func TestAnUnknownCommandFails(t *testing.T) {
|
|
||||||
was := os.Args
|
|
||||||
t.Cleanup(func() { os.Args = was })
|
|
||||||
os.Args = []string{"mesh-builder", "some-later-word"}
|
|
||||||
if err := run(); err == nil {
|
|
||||||
t.Fatal("an unknown command exited 0")
|
|
||||||
}
|
|
||||||
os.Args = []string{"mesh-builder", "help"}
|
|
||||||
if err := run(); err != nil {
|
|
||||||
t.Fatalf("help failed: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1768/AC2: a mark naming no build of this agent (left by a crash or a fault) is stale: the next ask removes it and is
|
|
||||||
// run, never fetched and handed back in a loop.
|
|
||||||
func TestAStaleInHandMarkIsRemovedAndTheAskRuns(t *testing.T) {
|
|
||||||
t.Setenv("TMPDIR", t.TempDir())
|
|
||||||
inHand.Store(false)
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(inHandMark(), []byte("2026-10-21T18:45:06Z\n"), 0o644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !claim() {
|
|
||||||
t.Fatal("a stale in-hand mark made the agent hand back an ask")
|
|
||||||
}
|
|
||||||
// A live one — this agent's own build in hand — is never taken over.
|
|
||||||
if claim() {
|
|
||||||
t.Fatal("a second build was taken while one is in hand")
|
|
||||||
}
|
|
||||||
if _, err := busy(); err != nil {
|
|
||||||
t.Fatalf("the build in hand was lost: %v", err)
|
|
||||||
}
|
|
||||||
released()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1768/AC3: an agent that cannot mark a build in hand for another reason takes no work, instead of fetching asks and
|
|
||||||
// handing them back; it takes work again once marking works.
|
|
||||||
func TestAnAgentThatCannotMarkABuildTakesNoWorkUntilItCan(t *testing.T) {
|
|
||||||
dir := t.TempDir()
|
|
||||||
t.Setenv("TMPDIR", dir)
|
|
||||||
inHand.Store(false)
|
|
||||||
markFault, markSaid = time.Time{}, ""
|
|
||||||
t.Cleanup(func() { markFault, markSaid = time.Time{}, "" })
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// The mark's place is a directory that cannot be removed or made over: a directory named as the mark.
|
|
||||||
if err := os.MkdirAll(inHandMark()+"/x", 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if claim() {
|
|
||||||
t.Fatal("an agent that cannot mark a build took it")
|
|
||||||
}
|
|
||||||
if !markFaulty(time.Now()) {
|
|
||||||
t.Fatal("an agent that cannot mark a build still takes work")
|
|
||||||
}
|
|
||||||
// Still failing after the retry time: still takes nothing, and no ask was needed to find out.
|
|
||||||
if !markFaulty(time.Now().Add(2 * markRetry)) {
|
|
||||||
t.Fatal("an agent whose mark still cannot be made takes work")
|
|
||||||
}
|
|
||||||
if err := os.RemoveAll(inHandMark()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if markFaulty(time.Now().Add(4 * markRetry)) {
|
|
||||||
t.Fatal("an agent whose mark can be made again takes no work")
|
|
||||||
}
|
|
||||||
if !claim() {
|
|
||||||
t.Fatal("an agent that can mark takes no build")
|
|
||||||
}
|
|
||||||
released()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1834/AC4: given a node under the floor of free space, when the taking loop asks, then it takes nothing, and takes again
|
|
||||||
// once there is room.
|
|
||||||
func TestANodeUnderTheFloorOfFreeSpaceTakesNoWork(t *testing.T) {
|
|
||||||
was := builder.FreeBytes
|
|
||||||
t.Cleanup(func() { builder.FreeBytes = was })
|
|
||||||
builder.FreeBytes = func(string) (uint64, error) { return 2 << 30, nil }
|
|
||||||
if !noRoom(t.TempDir()) {
|
|
||||||
t.Error("a node with 2 GiB free took work")
|
|
||||||
}
|
|
||||||
builder.FreeBytes = func(string) (uint64, error) { return 200 << 30, nil }
|
|
||||||
if noRoom(t.TempDir()) {
|
|
||||||
t.Error("a node with 200 GiB free refused work")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The build seat hands the check the judge cache the build agent declares (MESH_JUDGE_CACHE), never a path of its own.
|
|
||||||
func TestTheBuildSeatHandsTheCheckTheJudgeCacheItDeclares(t *testing.T) {
|
|
||||||
t.Setenv("MESH_JUDGE_CACHE", "/var/lib/mesh/build-agent/judge-cache")
|
|
||||||
if got := judgeCache(); got != "/var/lib/mesh/build-agent/judge-cache" {
|
|
||||||
t.Fatalf("the judge cache the manifest declares is read as %q", got)
|
|
||||||
}
|
|
||||||
t.Setenv("MESH_JUDGE_CACHE", "")
|
|
||||||
if got := judgeCache(); got != "" {
|
|
||||||
t.Fatalf("with no judge cache declared, %q is used", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The siblings a check is asked to read reach the build seat's spec and, as read, the outcome (novox/hq ADR 0382).
|
|
||||||
func TestTheSiblingsAnAskNamesReachTheSpecAndTheOutcome(t *testing.T) {
|
|
||||||
spec := checkSpecOf(link.BuildRequest{Check: &link.CheckRequest{Owner: "novox", Repo: "mesh-host", Base: "main",
|
|
||||||
Siblings: []link.CheckSibling{{Repository: "novox/mesh-controller", Number: 346, Head: "a", Base: "main", Dir: "mesh-controller"}},
|
|
||||||
SiblingsUnknown: "why"}})
|
|
||||||
if len(spec.Siblings) != 1 || spec.Siblings[0] != (builder.Sibling{Repository: "novox/mesh-controller", Number: 346,
|
|
||||||
Head: "a", Base: "main", Dir: "mesh-controller"}) || spec.SiblingsUnknown != "why" {
|
|
||||||
t.Fatalf("the spec reads %+v", spec)
|
|
||||||
}
|
|
||||||
out := siblingsOut([]builder.JudgedSibling{{Repository: "novox/mesh-controller", Number: 346, Head: "a", Base: "main", BaseCommit: "b"}})
|
|
||||||
if len(out) != 1 || out[0].BaseCommit != "b" || out[0].Number != 346 {
|
|
||||||
t.Fatalf("the outcome reads %+v", out)
|
|
||||||
}
|
|
||||||
if plain := checkSpecOf(link.BuildRequest{Check: &link.CheckRequest{Owner: "o", Repo: "r"}}); plain.Siblings != nil {
|
|
||||||
t.Fatalf("a request that names none has siblings %+v", plain.Siblings)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1826/AC2: the held values a check request carries reach the spec; a request that carries none leaves it empty.
|
|
||||||
func TestTheHeldValuesAnAskCarriesReachTheSpec(t *testing.T) {
|
|
||||||
spec := checkSpecOf(link.BuildRequest{Check: &link.CheckRequest{Owner: "novox", Repo: "mesh-catalog",
|
|
||||||
HeldConfig: []link.HeldConfig{{Pull: "novox/mesh-catalog#434", Head: "a", Node: "ace", Module: "n8n", Key: "media-gid",
|
|
||||||
Value: []byte("2000"), Approved: "the operator"}}}})
|
|
||||||
if len(spec.Held) != 1 || spec.Held[0].Node != "ace" || spec.Held[0].Key != "media-gid" || string(spec.Held[0].Value) != "2000" ||
|
|
||||||
spec.Held[0].Approved != "the operator" {
|
|
||||||
t.Fatalf("the spec reads %+v", spec.Held)
|
|
||||||
}
|
|
||||||
if plain := checkSpecOf(link.BuildRequest{Check: &link.CheckRequest{Owner: "o", Repo: "r"}}); plain.Held != nil {
|
|
||||||
t.Fatalf("a request that carries none has %+v", plain.Held)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,433 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go/micro"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// What a holder of the build seat answers for, on its own machine (novox/hq ADR 0219).
|
|
||||||
//
|
|
||||||
// **The queue is the controller's; the build running here is this machine's.** The controller can
|
|
||||||
// see and change what waits in the seat's queue, but an ask a holder already took is a process tree
|
|
||||||
// on this machine and containers in this machine's runtime, and only this machine can end them. So
|
|
||||||
// the holder serves four verbs on the seat's subjects for this machine: what it is building, kill
|
|
||||||
// it, pause, resume.
|
|
||||||
//
|
|
||||||
// **One build at a time** (ADR 0190), which is also what builder.Said assumes — a package global
|
|
||||||
// set per build — so "the build running here" is one or none, and kill names it by id so a call
|
|
||||||
// that arrives as one build ends and the next begins cannot end the wrong one.
|
|
||||||
|
|
||||||
// holder is this machine's state as a holder of the build seat.
|
|
||||||
type holder struct {
|
|
||||||
on, seat string
|
|
||||||
// workspace is where the paused flag is kept, so a holder restarted while paused stays paused
|
|
||||||
// rather than silently taking work again.
|
|
||||||
workspace string
|
|
||||||
// say publishes this machine's state: whether it takes work (link.HolderState).
|
|
||||||
say func(link.HolderState) error
|
|
||||||
// remove runs what a kill needs outside the build: the containers left behind.
|
|
||||||
remove builder.Runner
|
|
||||||
// scratch is where check builds are kept (novox/hq ADR 0350 §5): what the week's sweep lets go of. Nil where
|
|
||||||
// this machine publishes to no artifact store.
|
|
||||||
scratch builder.Scratch
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
paused bool
|
|
||||||
running *running
|
|
||||||
}
|
|
||||||
|
|
||||||
// running is the build this machine is doing.
|
|
||||||
type running struct {
|
|
||||||
request link.BuildRequest
|
|
||||||
step string
|
|
||||||
started time.Time
|
|
||||||
cancel context.CancelFunc
|
|
||||||
killed bool
|
|
||||||
// returned is the build's own work having ended, before a kill or not; outcome is what was then
|
|
||||||
// announced, and announced whether it went out.
|
|
||||||
returned bool
|
|
||||||
outcome string
|
|
||||||
announced bool
|
|
||||||
done chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// pausedFile is where the flag lives in the workspace.
|
|
||||||
func pausedFile(workspace string) string { return filepath.Join(workspace, ".mesh-builder-paused") }
|
|
||||||
|
|
||||||
// newHolder reads the paused flag the workspace keeps.
|
|
||||||
func newHolder(on, seat, workspace string, say func(link.HolderState) error) *holder {
|
|
||||||
h := &holder{on: on, seat: seat, workspace: workspace, say: say, remove: plainRun}
|
|
||||||
if _, err := os.Stat(pausedFile(workspace)); err == nil {
|
|
||||||
h.paused = true
|
|
||||||
}
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
|
|
||||||
// Paused is asked by the taking loop before every fetch.
|
|
||||||
func (h *holder) Paused() bool {
|
|
||||||
h.mu.Lock()
|
|
||||||
defer h.mu.Unlock()
|
|
||||||
return h.paused
|
|
||||||
}
|
|
||||||
|
|
||||||
// setPaused records the flag in the workspace first and then in memory, so what this holder says
|
|
||||||
// it is and what it would be after a restart never differ.
|
|
||||||
func (h *holder) setPaused(paused bool) error {
|
|
||||||
path := pausedFile(h.workspace)
|
|
||||||
if paused {
|
|
||||||
if err := os.MkdirAll(h.workspace, 0o755); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(path, []byte(time.Now().UTC().Format(time.RFC3339)+"\n"), 0o644); err != nil {
|
|
||||||
return fmt.Errorf("cannot keep the paused flag in %s: %w", path, err)
|
|
||||||
}
|
|
||||||
} else if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
||||||
return fmt.Errorf("cannot remove the paused flag %s: %w", path, err)
|
|
||||||
}
|
|
||||||
h.mu.Lock()
|
|
||||||
h.paused = paused
|
|
||||||
h.mu.Unlock()
|
|
||||||
h.announce()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// announce says whether this machine takes work. Never fatal: the flag is kept either way, and the
|
|
||||||
// controller reading an older state is a plan read as late rather than a build lost.
|
|
||||||
func (h *holder) announce() {
|
|
||||||
if h.say == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := h.say(link.HolderState{On: h.on, Paused: h.Paused(), At: time.Now().UTC().Format(time.RFC3339Nano)}); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot say whether this machine takes builds: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// stateWhilePaused says this machine's state at start, and again every while it stays paused, so
|
|
||||||
// a pause outlives the events stream's retention.
|
|
||||||
func (h *holder) stateWhilePaused(ctx context.Context, every time.Duration) {
|
|
||||||
h.announce()
|
|
||||||
tick := time.NewTicker(every)
|
|
||||||
defer tick.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-tick.C:
|
|
||||||
if h.Paused() {
|
|
||||||
h.announce()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// expireChecks lets go of every check build older than a week (novox/hq ADR 0350 §5), at start and every while
|
|
||||||
// after. Every holder sweeps the one store; what one let go of, another finds gone.
|
|
||||||
func (h *holder) expireChecks(ctx context.Context, every time.Duration, now func() time.Time) {
|
|
||||||
sweep := func() {
|
|
||||||
if h.scratch == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
n, err := h.scratch.Expire(ctx, builder.CheckBuildsKept, now())
|
|
||||||
switch {
|
|
||||||
case err != nil:
|
|
||||||
fmt.Fprintf(os.Stderr, "check builds older than %s: %d let go of, and not all: %v\n", builder.CheckBuildsKept, n, err)
|
|
||||||
case n > 0:
|
|
||||||
fmt.Fprintf(os.Stderr, "check builds older than %s: %d check(s)' builds let go of\n", builder.CheckBuildsKept, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sweep()
|
|
||||||
tick := time.NewTicker(every)
|
|
||||||
defer tick.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-tick.C:
|
|
||||||
sweep()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// begin records the build this machine took, with the cancel that ends it.
|
|
||||||
func (h *holder) begin(request link.BuildRequest, cancel context.CancelFunc) *running {
|
|
||||||
r := &running{request: request, started: time.Now(), cancel: cancel, done: make(chan struct{})}
|
|
||||||
h.mu.Lock()
|
|
||||||
h.running = r
|
|
||||||
h.mu.Unlock()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// end clears it, and lets a kill waiting on it know it is over.
|
|
||||||
func (h *holder) end(r *running) {
|
|
||||||
h.mu.Lock()
|
|
||||||
if h.running == r {
|
|
||||||
h.running = nil
|
|
||||||
}
|
|
||||||
h.mu.Unlock()
|
|
||||||
close(r.done)
|
|
||||||
}
|
|
||||||
|
|
||||||
// stepped records the step a build is at, for `current`.
|
|
||||||
func (h *holder) stepped(r *running, step string) {
|
|
||||||
h.mu.Lock()
|
|
||||||
r.step = step
|
|
||||||
h.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// currentBuild is what `current` answers.
|
|
||||||
type currentBuild struct {
|
|
||||||
On string `json:"on"`
|
|
||||||
Paused bool `json:"paused"`
|
|
||||||
Running *struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Repository string `json:"repository"`
|
|
||||||
Path string `json:"path,omitempty"`
|
|
||||||
Ref string `json:"ref,omitempty"`
|
|
||||||
Step string `json:"step,omitempty"`
|
|
||||||
Started string `json:"started"`
|
|
||||||
Elapsed string `json:"elapsed"`
|
|
||||||
} `json:"running,omitempty"`
|
|
||||||
Said string `json:"said"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *holder) current() currentBuild {
|
|
||||||
h.mu.Lock()
|
|
||||||
defer h.mu.Unlock()
|
|
||||||
out := currentBuild{On: h.on, Paused: h.paused}
|
|
||||||
taking := "taking builds"
|
|
||||||
if h.paused {
|
|
||||||
taking = "paused, taking no new build"
|
|
||||||
}
|
|
||||||
if h.running == nil {
|
|
||||||
out.Said = fmt.Sprintf("%s is building nothing; %s", h.on, taking)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
r := h.running
|
|
||||||
elapsed := time.Since(r.started).Round(time.Second)
|
|
||||||
out.Running = &struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Repository string `json:"repository"`
|
|
||||||
Path string `json:"path,omitempty"`
|
|
||||||
Ref string `json:"ref,omitempty"`
|
|
||||||
Step string `json:"step,omitempty"`
|
|
||||||
Started string `json:"started"`
|
|
||||||
Elapsed string `json:"elapsed"`
|
|
||||||
}{r.request.ID, r.request.Repository, r.request.Path, r.request.Ref, r.step,
|
|
||||||
r.started.UTC().Format(time.RFC3339), elapsed.String()}
|
|
||||||
out.Said = fmt.Sprintf("%s is building %s (%s) at %s for %s; %s",
|
|
||||||
h.on, r.request.Repository, r.request.ID, orNothing(r.step), elapsed, taking)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// The bounds a kill keeps: each pass removing containers, and the wait for the build to end between
|
|
||||||
// them. The worst case — 15s, 20s, 15s — is inside what the controller waits for the answer
|
|
||||||
// (killAnswer in the controller's queue.go, 75s).
|
|
||||||
var (
|
|
||||||
killRemoves = 15 * time.Second
|
|
||||||
killWaits = 20 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
// kill ends the build with this id, if it is the one running here and still working: its context
|
|
||||||
// cancelled — which kills each command's process group — and the containers it started removed by
|
|
||||||
// their label, once at once and again after the build has ended, so one created while it was being
|
|
||||||
// killed is not left. The build's own goroutine announces it failed, killed by hand, and settles the
|
|
||||||
// ask so it is not redelivered; the answer says whether that happened.
|
|
||||||
func (h *holder) kill(id string) (string, error) {
|
|
||||||
h.mu.Lock()
|
|
||||||
r := h.running
|
|
||||||
if r == nil || r.request.ID != id {
|
|
||||||
doing := "nothing"
|
|
||||||
if r != nil {
|
|
||||||
doing = r.request.ID
|
|
||||||
}
|
|
||||||
h.mu.Unlock()
|
|
||||||
return "", fmt.Errorf("%s is not building %s; it is building %s. `queue` says where an ask is", h.on, id, doing)
|
|
||||||
}
|
|
||||||
if r.returned {
|
|
||||||
h.mu.Unlock()
|
|
||||||
return "", fmt.Errorf("%s on %s has already ended on its own and is saying how; `builds` shows it", id, h.on)
|
|
||||||
}
|
|
||||||
r.killed = true
|
|
||||||
cancel, done := r.cancel, r.done
|
|
||||||
h.mu.Unlock()
|
|
||||||
|
|
||||||
cancel()
|
|
||||||
removed, removeErr := h.removeContainers(id)
|
|
||||||
ended := false
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
ended = true
|
|
||||||
case <-time.After(killWaits):
|
|
||||||
}
|
|
||||||
again, againErr := h.removeContainers(id)
|
|
||||||
removed += again
|
|
||||||
if removeErr == nil {
|
|
||||||
removeErr = againErr
|
|
||||||
}
|
|
||||||
containers := fmt.Sprintf("%d container(s) it started removed", removed)
|
|
||||||
if removeErr != nil {
|
|
||||||
containers = "its containers could not all be listed or removed: " + removeErr.Error()
|
|
||||||
}
|
|
||||||
if !ended {
|
|
||||||
return fmt.Sprintf("killed %s on %s: %s; the build has not finished ending yet — `builds` says when "+
|
|
||||||
"its outcome is in", id, h.on, containers), nil
|
|
||||||
}
|
|
||||||
h.mu.Lock()
|
|
||||||
outcome, announced := r.outcome, r.announced
|
|
||||||
h.mu.Unlock()
|
|
||||||
if !announced {
|
|
||||||
return fmt.Sprintf("killed %s (%s) on %s: its commands ended and %s, and its outcome could not be "+
|
|
||||||
"announced — the ask is not settled and will be handed out again", id, r.request.Repository, h.on,
|
|
||||||
containers), nil
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("killed %s (%s) on %s: its commands ended, %s, and its outcome announced as failed, %s — "+
|
|
||||||
"settled, so it is not handed to another machine", id, r.request.Repository, h.on, containers, outcome), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeContainers is one pass of removing what the build left, bounded.
|
|
||||||
func (h *holder) removeContainers(id string) (int, error) {
|
|
||||||
cleanup, stop := context.WithTimeout(context.Background(), killRemoves)
|
|
||||||
defer stop()
|
|
||||||
return builder.RemoveContainersOf(cleanup, h.remove, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeLeftBehind removes, once, what earlier holders on this machine left — called before anything is
|
|
||||||
// taken, so none of it is this holder's — and says what it did. A runtime that cannot be asked is said
|
|
||||||
// and the holder takes work all the same: what is left is waste, not a reason to stop building.
|
|
||||||
func (h *holder) removeLeftBehind() int {
|
|
||||||
cleanup, stop := context.WithTimeout(context.Background(), killRemoves)
|
|
||||||
defer stop()
|
|
||||||
n, err := builder.RemoveLeftBehind(cleanup, h.remove)
|
|
||||||
switch {
|
|
||||||
case err != nil:
|
|
||||||
fmt.Fprintf(os.Stderr, "could not look for containers earlier builds left on %s: %v\n", h.on, err)
|
|
||||||
case n > 0:
|
|
||||||
fmt.Fprintf(os.Stderr, "removed %d container(s) earlier builds left on %s\n", n, h.on)
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// returned records that the build's work ended, and says whether a kill came first — only then is
|
|
||||||
// the build killed; an error it ended with on its own is its own outcome.
|
|
||||||
func (h *holder) returned(r *running) bool {
|
|
||||||
h.mu.Lock()
|
|
||||||
defer h.mu.Unlock()
|
|
||||||
r.returned = true
|
|
||||||
return r.killed
|
|
||||||
}
|
|
||||||
|
|
||||||
// said records the outcome announced, and whether it went out, for a kill to answer with.
|
|
||||||
func (h *holder) said(r *running, outcome string, announced bool) {
|
|
||||||
h.mu.Lock()
|
|
||||||
r.outcome, r.announced = outcome, announced
|
|
||||||
h.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// handlers are the seat's verbs, as this machine answers them.
|
|
||||||
func (h *holder) handlers() map[string]link.ToolHandler {
|
|
||||||
return map[string]link.ToolHandler{
|
|
||||||
"current": func(context.Context, json.RawMessage) (any, error) { return h.current(), nil },
|
|
||||||
"kill": func(_ context.Context, raw json.RawMessage) (any, error) {
|
|
||||||
var args struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(raw, &args); err != nil || strings.TrimSpace(args.ID) == "" {
|
|
||||||
return nil, errors.New("kill needs the build's id")
|
|
||||||
}
|
|
||||||
said, err := h.kill(strings.TrimSpace(args.ID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return map[string]any{"said": said}, nil
|
|
||||||
},
|
|
||||||
"pause": func(context.Context, json.RawMessage) (any, error) {
|
|
||||||
if err := h.setPaused(true); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
said := h.on + " is paused: it takes no new build until resumed"
|
|
||||||
if c := h.current(); c.Running != nil {
|
|
||||||
said += "; " + c.Running.ID + " runs on and finishes"
|
|
||||||
}
|
|
||||||
return map[string]any{"said": said, "paused": true}, nil
|
|
||||||
},
|
|
||||||
"resume": func(context.Context, json.RawMessage) (any, error) {
|
|
||||||
if err := h.setPaused(false); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return map[string]any{"said": h.on + " takes builds again", "paused": false}, nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func orNothing(s string) string {
|
|
||||||
if s == "" {
|
|
||||||
return "its start"
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// plainRun runs a command outside any build: no line reaches a build's log, because the build whose
|
|
||||||
// log it would be is the one being ended.
|
|
||||||
func plainRun(ctx context.Context, dir, name string, args ...string) (string, error) {
|
|
||||||
cmd := exec.CommandContext(ctx, name, args...)
|
|
||||||
cmd.Dir = dir
|
|
||||||
out, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return string(out), fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, strings.TrimSpace(string(out)))
|
|
||||||
}
|
|
||||||
return string(out), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// announcement is what this holder answers discovery with (novox/hq ADR 0195, ADR 0197): this
|
|
||||||
// machine's verbs of the seat, in the shape every tool runtime announces a seat's verb — kind seat,
|
|
||||||
// the module answering, the seat, scope node, the machine, its description and argument schema — so
|
|
||||||
// the console finds `<node>/node-build-agent.kill` by searching, as it finds any seat's verb.
|
|
||||||
//
|
|
||||||
// One service per machine, named for the seat and identified by the machine, so the answer is this
|
|
||||||
// machine's four verbs and nothing more. The verbs are the compiled seat row's: a build machine has no
|
|
||||||
// store, and what it serves is what this binary was built to serve.
|
|
||||||
func announcement(seat, module, node string) micro.Info {
|
|
||||||
s, _ := catalogue.SeatNamed(seat)
|
|
||||||
var endpoints []micro.EndpointInfo
|
|
||||||
for _, v := range s.Serves {
|
|
||||||
schema, _ := json.Marshal(v.Input)
|
|
||||||
endpoints = append(endpoints, micro.EndpointInfo{
|
|
||||||
Name: seat + "__" + v.Name,
|
|
||||||
Subject: link.NodeSeatToolSubject(seat, v.Name, node),
|
|
||||||
// The queue group the verbs are served in, as every runtime announces its own.
|
|
||||||
QueueGroup: "seat." + seat,
|
|
||||||
Metadata: map[string]string{
|
|
||||||
"kind": "seat", "module": module, "tool": v.Name, "seat": seat, "scope": "node",
|
|
||||||
"node": node, "interchangeable": "false", "description": v.Description, "schema": string(schema),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return micro.Info{
|
|
||||||
ServiceIdentity: micro.ServiceIdentity{Name: seat, ID: node, Version: "0.1.0",
|
|
||||||
Metadata: map[string]string{"seat": seat, "scope": "node", "node": node, "module": module}},
|
|
||||||
Description: "what the build running on " + node + " is, and ending, pausing and resuming it (novox/hq ADR 0219)",
|
|
||||||
Endpoints: endpoints,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// moduleOf is the module a credential was issued for: its user is `<node>.<module>`.
|
|
||||||
func moduleOf(user string) string {
|
|
||||||
if _, module, ok := strings.Cut(user, "."); ok && module != "" {
|
|
||||||
return module
|
|
||||||
}
|
|
||||||
return "build-agent"
|
|
||||||
}
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A holder paused stays paused across its restart: the flag is kept in its workspace (novox/hq ADR
|
|
||||||
// 0219), and what it says about itself follows.
|
|
||||||
func TestAPausedHolderStaysPausedAcrossARestart(t *testing.T) {
|
|
||||||
workspace := t.TempDir()
|
|
||||||
var said []link.HolderState
|
|
||||||
h := newHolder("ace", link.TheBuildMachine, workspace, func(s link.HolderState) error {
|
|
||||||
said = append(said, s)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if h.Paused() {
|
|
||||||
t.Fatal("a new holder starts paused")
|
|
||||||
}
|
|
||||||
if _, err := h.handlers()["pause"](context.Background(), json.RawMessage(`{}`)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !h.Paused() || len(said) != 1 || !said[0].Paused || said[0].On != "ace" {
|
|
||||||
t.Fatalf("paused: %v, said %+v", h.Paused(), said)
|
|
||||||
}
|
|
||||||
again := newHolder("ace", link.TheBuildMachine, workspace, nil)
|
|
||||||
if !again.Paused() {
|
|
||||||
t.Fatal("restarted, the holder forgot it was paused")
|
|
||||||
}
|
|
||||||
if _, err := again.handlers()["resume"](context.Background(), json.RawMessage(`{}`)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if newHolder("ace", link.TheBuildMachine, workspace, nil).Paused() {
|
|
||||||
t.Fatal("resumed, the holder came back paused")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// kill ends the build with that id — its context, which ends its commands — removes what it left by
|
|
||||||
// label, and refuses an id it is not building.
|
|
||||||
func TestKillEndsTheBuildRunningHereAndNoOther(t *testing.T) {
|
|
||||||
h := newHolder("ace", link.TheBuildMachine, t.TempDir(), nil)
|
|
||||||
var removed []string
|
|
||||||
h.remove = func(_ context.Context, _ string, name string, args ...string) (string, error) {
|
|
||||||
removed = append(removed, name+" "+strings.Join(args, " "))
|
|
||||||
if args[0] == "ps" {
|
|
||||||
return "c1\n", nil
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
kill := h.handlers()["kill"]
|
|
||||||
if _, err := kill(context.Background(), json.RawMessage(`{"id":"build-1"}`)); err == nil {
|
|
||||||
t.Fatal("killed a build while none ran")
|
|
||||||
}
|
|
||||||
|
|
||||||
building, cancel := context.WithCancel(context.Background())
|
|
||||||
r := h.begin(link.BuildRequest{ID: "build-1", Repository: "novox/a"}, cancel)
|
|
||||||
h.stepped(r, "image")
|
|
||||||
if c := h.current(); c.Running == nil || c.Running.ID != "build-1" || c.Running.Step != "image" {
|
|
||||||
t.Fatalf("current says %+v", c)
|
|
||||||
}
|
|
||||||
if _, err := kill(context.Background(), json.RawMessage(`{"id":"build-2"}`)); err == nil ||
|
|
||||||
!strings.Contains(err.Error(), "build-1") {
|
|
||||||
t.Fatalf("killed another id: %v", err)
|
|
||||||
}
|
|
||||||
// The build's own goroutine: it ends when its context does, as a build's commands do, and
|
|
||||||
// announces what came of it.
|
|
||||||
go func() {
|
|
||||||
<-building.Done()
|
|
||||||
if h.returned(r) {
|
|
||||||
h.said(r, link.KilledByHand, true)
|
|
||||||
}
|
|
||||||
h.end(r)
|
|
||||||
}()
|
|
||||||
answer, err := kill(context.Background(), json.RawMessage(`{"id":"build-1"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if building.Err() == nil {
|
|
||||||
t.Fatal("the build's context was not cancelled")
|
|
||||||
}
|
|
||||||
if !r.killed {
|
|
||||||
t.Error("the build is not marked killed, so it would be announced as an ordinary failure")
|
|
||||||
}
|
|
||||||
said := answer.(map[string]any)["said"].(string)
|
|
||||||
if !strings.Contains(said, "2 container(s)") || !strings.Contains(said, "announced as failed") || !strings.Contains(said, link.KilledByHand) {
|
|
||||||
t.Errorf("kill said %q", said)
|
|
||||||
}
|
|
||||||
// Removed at the kill and again once the build had ended: a container made in between is caught.
|
|
||||||
if len(removed) != 6 || !strings.Contains(removed[0], "label=mesh.build=build-1") || !strings.Contains(removed[3], "label=mesh.build=build-1") {
|
|
||||||
t.Errorf("removed %v", removed)
|
|
||||||
}
|
|
||||||
if c := h.current(); c.Running != nil {
|
|
||||||
t.Errorf("after the kill current says %+v", c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A holder announces this machine's verbs of the seat as the console reads a seat's verb, and no more.
|
|
||||||
func TestAHolderAnnouncesItsMachinesVerbsForTheConsole(t *testing.T) {
|
|
||||||
info := announcement(link.TheBuildMachine, moduleOf("ace.build-agent"), "ace")
|
|
||||||
if info.Name != "node-build-agent" || info.ID != "ace" || len(info.Endpoints) != 4 {
|
|
||||||
t.Fatalf("announced %s/%s with %d endpoints", info.Name, info.ID, len(info.Endpoints))
|
|
||||||
}
|
|
||||||
kill := info.Endpoints[1]
|
|
||||||
md := kill.Metadata
|
|
||||||
if kill.Subject != "mesh.seat.node-build-agent.tool.kill.ace" || kill.QueueGroup != "seat.node-build-agent" || md["kind"] != "seat" || md["seat"] != "node-build-agent" ||
|
|
||||||
md["scope"] != "node" || md["node"] != "ace" || md["tool"] != "kill" || md["module"] != "build-agent" ||
|
|
||||||
!strings.Contains(md["description"], "killed by hand") || !strings.Contains(md["schema"], `"id"`) {
|
|
||||||
t.Fatalf("kill is announced as %+v", kill)
|
|
||||||
}
|
|
||||||
body, err := json.Marshal(info)
|
|
||||||
if err != nil || len(body) > 8*1024 {
|
|
||||||
t.Fatalf("the answer is %d bytes (%v)", len(body), err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A build that ended on its own as the kill arrived says what it did: the kill is refused, and its
|
|
||||||
// own error is its outcome. One whose outcome could not be announced is not said to be settled.
|
|
||||||
func TestAKillArrivingAfterTheBuildEndedIsRefusedAndAnUnannouncedKillSaysSo(t *testing.T) {
|
|
||||||
h := newHolder("ace", link.TheBuildMachine, t.TempDir(), nil)
|
|
||||||
h.remove = func(context.Context, string, string, ...string) (string, error) { return "", nil }
|
|
||||||
_, cancel := context.WithCancel(context.Background())
|
|
||||||
r := h.begin(link.BuildRequest{ID: "build-1"}, cancel)
|
|
||||||
if killed := h.returned(r); killed {
|
|
||||||
t.Fatal("a build nobody killed reads as killed")
|
|
||||||
}
|
|
||||||
if _, err := h.kill("build-1"); err == nil || !strings.Contains(err.Error(), "ended on its own") {
|
|
||||||
t.Fatalf("killed a build that had ended: %v", err)
|
|
||||||
}
|
|
||||||
h.end(r)
|
|
||||||
|
|
||||||
building, cancel := context.WithCancel(context.Background())
|
|
||||||
r = h.begin(link.BuildRequest{ID: "build-2"}, cancel)
|
|
||||||
go func() {
|
|
||||||
<-building.Done()
|
|
||||||
if h.returned(r) {
|
|
||||||
h.said(r, link.KilledByHand, false)
|
|
||||||
}
|
|
||||||
h.end(r)
|
|
||||||
}()
|
|
||||||
said, err := h.kill("build-2")
|
|
||||||
if err != nil || strings.Contains(said, "announced as failed") || !strings.Contains(said, "could not be announced") {
|
|
||||||
t.Fatalf("kill said %q (%v)", said, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **Issue 306**: a holder starting removes the containers earlier builds left on its machine, through
|
|
||||||
// the runner a kill uses, and a runtime that cannot be asked does not stop it.
|
|
||||||
func TestAHolderStartingRemovesWhatEarlierBuildsLeftHere(t *testing.T) {
|
|
||||||
h := newHolder("novox", link.TheBuildMachine, t.TempDir(), nil)
|
|
||||||
var removed []string
|
|
||||||
h.remove = func(_ context.Context, _ string, name string, args ...string) (string, error) {
|
|
||||||
removed = append(removed, name+" "+strings.Join(args, " "))
|
|
||||||
if args[0] == "ps" {
|
|
||||||
return "c1 build-1\nc2 build-1\nc3 check-here-2\n", nil
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
if n := h.removeLeftBehind(); n != 2 || len(removed) != 3 || removed[2] != "docker rm -f -v c1 c2" {
|
|
||||||
t.Fatalf("removed %d: %v", n, removed)
|
|
||||||
}
|
|
||||||
h.remove = func(context.Context, string, string, ...string) (string, error) {
|
|
||||||
return "", errors.New("no runtime")
|
|
||||||
}
|
|
||||||
if n := h.removeLeftBehind(); n != 0 {
|
|
||||||
t.Errorf("a runtime that cannot be asked removed %d", n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,946 +0,0 @@
|
|||||||
// mesh-builder — the thing a build machine runs.
|
|
||||||
//
|
|
||||||
// It takes work from the mesh, turns a repository into artifacts, publishes them, and says what
|
|
||||||
// came out. It is **not** the control plane and it is **not** the host:
|
|
||||||
//
|
|
||||||
// - the control plane decides and never touches a machine. Building runs commands on one, and
|
|
||||||
// what the control plane may send a machine is bounded by the declaration language
|
|
||||||
// (novox/hq ADR 0005). "Run this build" is not in it, and widening the language so it could
|
|
||||||
// be would make the control plane able to run anything anywhere.
|
|
||||||
// - the host applies declarations and holds no opinion about what they contain. A host that
|
|
||||||
// also built things would need a container runtime and git, on every machine, to do something
|
|
||||||
// almost none of them will ever do.
|
|
||||||
//
|
|
||||||
// So it is a module: a program a machine runs because the mesh told it to, holding its own broker
|
|
||||||
// credential and nothing else. Compromise of a build machine is compromise of a build machine.
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// version is set at build time.
|
|
||||||
var version = "development"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if err := run(); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "mesh-builder: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const usage = `mesh-builder — builds modules for the mesh
|
|
||||||
|
|
||||||
It consumes build requests and answers with what it made. Nothing is listened on and nothing
|
|
||||||
is dialled except the broker.
|
|
||||||
|
|
||||||
MESH_BROKER_FILE a file the mesh sealed to this machine holding the same
|
|
||||||
MESH_REGISTRY host:port to publish artifacts to, when the mesh has not said
|
|
||||||
MESH_BINDING a file the mesh wrote saying where the artifact store is
|
|
||||||
MESH_PACKAGE_BINDING a file the mesh wrote saying where the package registry is
|
|
||||||
MESH_NPM_TOKEN_FILE a file the mesh sealed holding the token for it
|
|
||||||
MESH_GENESIS_PACKAGE_REGISTRY the document the installer sealed at genesis, read while no
|
|
||||||
binding is present (novox/hq ADR 0336)
|
|
||||||
MESH_NPM_REGISTRY a package registry URL, when the mesh has not said (a person, the bootstrap)
|
|
||||||
MESH_NPM_TOKEN the token for it, likewise
|
|
||||||
MESH_NPM_SCOPE the scope it answers for (default: @novox)
|
|
||||||
MESH_WORKSPACE where to clone and build (default: a temporary directory)
|
|
||||||
MESH_BUILD_CGROUP_PARENT the slice every container a build or check starts joins, which carries the
|
|
||||||
machine's limits on builds (novox/hq issue 1798; default: the runtime's own)
|
|
||||||
|
|
||||||
It also builds one module and stops, which is how a mesh is raised — before there is a
|
|
||||||
broker to take work from or a registry to publish into:
|
|
||||||
|
|
||||||
mesh-builder build <repository> [--path P] [--ref COMMIT] [--registry HOST:PORT]
|
|
||||||
|
|
||||||
Without --registry the artifacts stay in this machine's container runtime, named by the
|
|
||||||
digest of their own configuration. The result is printed as JSON.
|
|
||||||
|
|
||||||
mesh-builder ready
|
|
||||||
|
|
||||||
exits 0 once the agent running in this container is bound to its build work and pulls it, and
|
|
||||||
1 before: its container's health check.
|
|
||||||
|
|
||||||
mesh-builder busy
|
|
||||||
|
|
||||||
exits 0 while the agent running in this container has a build in hand, and 1 when it has none;
|
|
||||||
from the first ask the agent takes no new build: its container's busy command, asked before the
|
|
||||||
container is replaced (novox/hq ADR 0335).
|
|
||||||
`
|
|
||||||
|
|
||||||
func run() error {
|
|
||||||
if len(os.Args) > 1 {
|
|
||||||
switch os.Args[1] {
|
|
||||||
case "version":
|
|
||||||
fmt.Println(version)
|
|
||||||
return nil
|
|
||||||
case "build":
|
|
||||||
return buildOnce(context.Background(), os.Args[2:])
|
|
||||||
case "ready":
|
|
||||||
// The container's health check (novox/hq issue 544): exits non-zero until the agent takes work, and
|
|
||||||
// once it has been drained longer than its node-engine would wait to replace it (ADR 0335).
|
|
||||||
if err := ready(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return drainedTooLong(time.Now())
|
|
||||||
case "busy":
|
|
||||||
// The container's busy command (novox/hq ADR 0335): drains the agent, and exits non-zero once
|
|
||||||
// nothing is in hand.
|
|
||||||
said, err := busy()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println(said)
|
|
||||||
return nil
|
|
||||||
case "help", "-h", "--help":
|
|
||||||
fmt.Print(usage)
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
// **An ask this build does not know fails** (novox/hq ADR 0335): an image older than `busy`, asked
|
|
||||||
// it, must read as idle — exiting 0 would hold its container for the node-engine's whole bound.
|
|
||||||
fmt.Fprint(os.Stderr, usage)
|
|
||||||
return fmt.Errorf("%q is not a command this mesh-builder knows", os.Args[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := notReady(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// **The builder's own version is part of every reuse key** (novox/hq ADR 0350 §1): a builder that builds
|
|
||||||
// differently never takes a build another one made.
|
|
||||||
builder.Version = builder.SelfVersion(version)
|
|
||||||
if err := undrained(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
credential, err := brokerFrom()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
registry, err := whereToPublish()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// A build and a check run below the machine's own work (novox/hq issue 1798): the agent itself at the
|
|
||||||
// lowest priority, which its git and go children inherit, and every container in the build slice.
|
|
||||||
_ = syscall.Setpriority(syscall.PRIO_PROCESS, 0, 19)
|
|
||||||
builder.CgroupParent = strings.TrimSpace(os.Getenv("MESH_BUILD_CGROUP_PARENT"))
|
|
||||||
workspace := os.Getenv("MESH_WORKSPACE")
|
|
||||||
if workspace == "" {
|
|
||||||
workspace = os.TempDir() + "/mesh-builder"
|
|
||||||
}
|
|
||||||
// **The mesh's name for this machine, not the container's.** A build is reported to the rest
|
|
||||||
// of the mesh, and a report whose origin reads `104cb10e105b` names something no other module
|
|
||||||
// can look up. The mesh already knows the answer and has a way to say it — `${machine:name}`
|
|
||||||
// in the environment file this module is handed — so the hostname is only what is left when
|
|
||||||
// nobody said.
|
|
||||||
on := os.Getenv("MESH_NODE")
|
|
||||||
if on == "" {
|
|
||||||
hostname, err := os.Hostname()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("this build machine has no name: nothing said MESH_NODE and the host would not say either: %w", err)
|
|
||||||
}
|
|
||||||
on = hostname
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
defer stop()
|
|
||||||
|
|
||||||
js, seat, err := dialFor(credential)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer js.Close()
|
|
||||||
|
|
||||||
// **This machine's holder: paused or not, and the build it is running** (novox/hq ADR 0219). The
|
|
||||||
// paused flag is read from the workspace before anything is taken, so a holder restarted while
|
|
||||||
// paused takes nothing.
|
|
||||||
h := newHolder(on, seat, workspace, func(state link.HolderState) error {
|
|
||||||
body, err := json.Marshal(state)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = js.Context().Publish(link.BuildPausedOf(seat, on), body)
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
if h.Paused() {
|
|
||||||
fmt.Fprintf(os.Stderr, "paused (kept in %s): taking no build until resumed\n", pausedFile(workspace))
|
|
||||||
}
|
|
||||||
// **What an earlier holder here left goes before anything is taken** (novox/hq issue 306): a holder
|
|
||||||
// recreated mid-check left the check's store and bus running, and the ask went to another machine.
|
|
||||||
h.removeLeftBehind()
|
|
||||||
// Paused or drained, it takes nothing new (novox/hq ADR 0219, ADR 0335): a drained agent finishes the build
|
|
||||||
// in hand and then says idle, and its container is replaced between builds.
|
|
||||||
machine := link.MachineOverNATSWith(js, on, seat, link.MachineOptions{
|
|
||||||
Paused: func() bool { return h.Paused() || drained() || markFaulty(time.Now()) || noRoom(workspace) },
|
|
||||||
Taking: func() { taking(seat) },
|
|
||||||
Claim: claim,
|
|
||||||
})
|
|
||||||
defer machine.Close()
|
|
||||||
|
|
||||||
// The seat's verbs, on this machine's subjects. Only the seat that declares them: the retired
|
|
||||||
// one serves none, and a subscription its holder has no grant for would be refused for ever.
|
|
||||||
if seat == link.TheBuildMachine {
|
|
||||||
stopServing, err := link.OverNATS{Conn: js.Conn()}.ServeNodeSeatTools(seat, on, h.handlers(),
|
|
||||||
log.New(os.Stderr, "", 0))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer stopServing()
|
|
||||||
// And says so, for the console to find (novox/hq ADR 0197).
|
|
||||||
stopAnnouncing, err := link.OverNATS{Conn: js.Conn()}.Announce(
|
|
||||||
announcement(seat, moduleOf(credential.User), on), log.New(os.Stderr, "", 0))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer stopAnnouncing()
|
|
||||||
go h.stateWhilePaused(ctx, time.Hour)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "building for the mesh, publishing to %s\n", registry)
|
|
||||||
publisher := builder.Registry{Address: registry, Run: builder.Command}
|
|
||||||
// Check builds are let go of after a week (novox/hq ADR 0350 §5).
|
|
||||||
h.scratch = publisher
|
|
||||||
go h.expireChecks(ctx, time.Hour, time.Now)
|
|
||||||
|
|
||||||
return machine.Take(ctx, func(ctx context.Context, work link.Build) {
|
|
||||||
defer released()
|
|
||||||
holding(work.Request().Repository + " (" + work.Request().ID + ")")
|
|
||||||
answer(ctx, publisher, on, workspace, work, h)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// dialFor opens this machine's link to whichever bus the mesh is on, and says which build seat it
|
|
||||||
// holds.
|
|
||||||
//
|
|
||||||
// **One place chooses**, as everywhere else the bus change went (novox/hq ADR 0116 step 5): a build
|
|
||||||
// machine told about both would take work from one and answer on the other, and every log line would
|
|
||||||
// say it was fine.
|
|
||||||
func dialFor(credential Credential) (*broker.JetStream, string, error) {
|
|
||||||
// **The credential names the bus, and there is one** (novox/hq ADR 0131, design 28 task 5.5).
|
|
||||||
// A credential for the mesh's bus carries user, password and fingerprint beside the address,
|
|
||||||
// and that is enough to dial it, pinned.
|
|
||||||
if !credential.onTheNewBus() {
|
|
||||||
return nil, "", fmt.Errorf("the credential at hand names %q, which is not the mesh's bus", credential.URL)
|
|
||||||
}
|
|
||||||
// Named for what it is, not the controller whose code dials it (novox/hq issue 327).
|
|
||||||
host, _ := os.Hostname()
|
|
||||||
js, err := broker.DialPinned(credential.natsURL(), credential.Fingerprint, nats.Name("build agent on "+host))
|
|
||||||
if err != nil {
|
|
||||||
return nil, "", err
|
|
||||||
}
|
|
||||||
// **The seat this machine serves is the one its credential claims** (novox/hq ADR 0190, the
|
|
||||||
// handover): the mesh issues a build machine's credential naming the seat its module claims,
|
|
||||||
// and one binary serves the old role as `builder` and the new as `build-agent` from that alone.
|
|
||||||
seat := link.BuildSeatClaimed(credential.seatsClaimed())
|
|
||||||
fmt.Fprintf(os.Stderr, "taking build work as a holder of %s\n", seat)
|
|
||||||
return js, seat, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// answer does one build and says what happened, whichever way it went.
|
|
||||||
func answer(ctx context.Context, publisher builder.Publisher, on, workspace string, work link.Build, h *holder) {
|
|
||||||
request := work.Request()
|
|
||||||
|
|
||||||
// **Its own context, so it can be killed alone** (novox/hq ADR 0219): cancelled by `kill`, it ends
|
|
||||||
// this build's commands and nothing else; the machine's own context ending — a SIGTERM — still
|
|
||||||
// reaches it through the parent, and that keeps today's meaning below.
|
|
||||||
building, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
var mine *running
|
|
||||||
if h != nil {
|
|
||||||
mine = h.begin(request, cancel)
|
|
||||||
defer h.end(mine)
|
|
||||||
}
|
|
||||||
|
|
||||||
// **First thing, and to stdout.** A build request that arrives and produces no visible line until
|
|
||||||
// it either finishes or fails is indistinguishable from one that never arrived — which cost a long
|
|
||||||
// diagnosis against a running mesh, chasing "the handler never fired" when the truth was only that
|
|
||||||
// the handler said nothing until the end.
|
|
||||||
fmt.Fprintf(os.Stderr, "a build request arrived for %s (%s)\n", request.Repository, request.ID)
|
|
||||||
|
|
||||||
// **Everything a build says goes two ways**: to stderr, as always, and onto the bus as the
|
|
||||||
// role's own events under the build's id (novox/hq ADR 0157) — so whoever asked, and anybody
|
|
||||||
// watching, reads the same lines this container's log holds, live, and after the fact from the
|
|
||||||
// stream. Said first, before anything runs, so a build that hangs is one that visibly started.
|
|
||||||
// One line at a time: a check's builds for its merge say their lines while its tests say theirs (novox/hq
|
|
||||||
// issue 716).
|
|
||||||
var saying sync.Mutex
|
|
||||||
say := func(step, message string) {
|
|
||||||
saying.Lock()
|
|
||||||
defer saying.Unlock()
|
|
||||||
fmt.Fprintf(os.Stderr, " [%s] %s\n", step, message)
|
|
||||||
work.Say(step, message)
|
|
||||||
if mine != nil && step != "run" && step != "output" {
|
|
||||||
h.stepped(mine, step)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
builder.Said = say
|
|
||||||
defer func() { builder.Said = nil }()
|
|
||||||
if err := work.Began(ctx); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot say a build started: %v\n", err)
|
|
||||||
}
|
|
||||||
// **A running check watches its pull request's key** (novox/hq ADR 0406 §4, issue 974): when the head or the trunk
|
|
||||||
// commit in it moves, the check is for a state nobody wants any more, and ends itself — nothing is asked of the forge
|
|
||||||
// (issue 726). A group's check, a try and a build watch nothing.
|
|
||||||
var superseded atomic.Bool
|
|
||||||
if w, ok := work.(interface {
|
|
||||||
WatchPull(context.Context, func(link.PullKey)) (func(), error)
|
|
||||||
}); ok && request.Check != nil {
|
|
||||||
stopWatching, err := w.WatchPull(building, func(k link.PullKey) {
|
|
||||||
superseded.Store(true)
|
|
||||||
say("superseded", fmt.Sprintf("the pull request's key moved to %.12s on %.12s while this checked %.12s on %.12s: stopped",
|
|
||||||
k.Head, k.Trunk, request.Ref, request.Check.Trunk))
|
|
||||||
cancel()
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "the key of %s/%s#%d could not be watched, so its check runs on if the key moves: %v\n",
|
|
||||||
request.Check.Owner, request.Check.Repo, request.Check.Number, err)
|
|
||||||
} else {
|
|
||||||
defer stopWatching()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result := link.BuildResult{
|
|
||||||
ID: request.ID, Repository: request.Repository, Path: request.Path,
|
|
||||||
Ref: request.Ref, On: on, Source: request.Source, DryRun: request.DryRun,
|
|
||||||
}
|
|
||||||
what := "building " + request.Repository
|
|
||||||
if request.Path != "" {
|
|
||||||
what += " at " + request.Path
|
|
||||||
}
|
|
||||||
if request.Ref != "" {
|
|
||||||
what += " on " + request.Ref
|
|
||||||
}
|
|
||||||
say("build", what)
|
|
||||||
|
|
||||||
npmrc, err := packagesFrom(on)
|
|
||||||
if err == nil {
|
|
||||||
say("packages", saysRegistry(npmrc))
|
|
||||||
}
|
|
||||||
registryHere, isRegistry := publisher.(builder.Registry)
|
|
||||||
var built builder.Result
|
|
||||||
if request.Check != nil {
|
|
||||||
// **A pull request's merge check, not a build** (novox/hq to-be 45 §9): nothing is built,
|
|
||||||
// published or registered; the verdict is the outcome.
|
|
||||||
result.Checked = request.Check
|
|
||||||
registry := ""
|
|
||||||
if r, ok := publisher.(builder.Registry); ok {
|
|
||||||
registry = r.Address
|
|
||||||
}
|
|
||||||
spec := checkSpecOf(request)
|
|
||||||
// The judge cache the build agent declares as its own data, never in the workspace: only judge builds and
|
|
||||||
// mesh-lab's replays mount it, never a check's containers, so no pull request's script can write what a judge
|
|
||||||
// is built with or kept as, or what the replays run (novox/hq ADR 0358 §6, issue 709).
|
|
||||||
spec.JudgeCache = judgeCache()
|
|
||||||
// What the merge would move, built by the check for the walk to take (novox/hq ADR 0350 §1): into the
|
|
||||||
// scratch namespace. **Never with the package registry's credential**: this is code nobody has approved
|
|
||||||
// yet, and a recipe could read the credential it is handed (issue 462). A module that resolves packages
|
|
||||||
// at build time has no reuse key anyway, so nothing is lost by refusing it here.
|
|
||||||
if isRegistry && len(spec.Builds) > 0 {
|
|
||||||
spec.BuildWith = &builder.CheckBuilding{Publish: publisher, Scratch: registryHere, Held: request.Held,
|
|
||||||
Running: request.Check.Running, Seats: request.Seats, Named: namedOf(request.Check.Named)}
|
|
||||||
}
|
|
||||||
var v builder.CheckVerdict
|
|
||||||
v, err = builder.Check(building, builder.Command, spec, workspace, registry, forgeFrom(), say)
|
|
||||||
if err == nil {
|
|
||||||
result.Check = &link.CheckOutcome{Verdict: v.Verdict, Summary: v.Summary, Report: v.Report,
|
|
||||||
Took: v.Took.Round(time.Second).String(), Gate: layerOf(v.Gate), RepoCheck: layerOf(v.Repo),
|
|
||||||
Head: v.Head, Trunk: v.Trunk, Beside: v.Beside, Built: checkBuiltOf(v.Built),
|
|
||||||
Validator: v.Validator, Siblings: siblingsOut(v.Siblings), SiblingsUnknown: v.SiblingsUnknown, JudgeFrom: v.JudgeFrom}
|
|
||||||
// What each of a group's heads was judged as (novox/hq ADR 0323 §6).
|
|
||||||
for _, m := range v.Members {
|
|
||||||
result.Check.Members = append(result.Check.Members, link.CheckedMember{Owner: m.Owner, Repo: m.Repo,
|
|
||||||
Commit: m.Head, Trunk: m.Trunk})
|
|
||||||
}
|
|
||||||
// A branch tried before its pull request reads its outcome from this log, whole (novox/hq issue 686).
|
|
||||||
if request.Check.Try {
|
|
||||||
if line, err := json.Marshal(triedOf(v)); err == nil {
|
|
||||||
say(link.StepTried, string(line))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if err == nil {
|
|
||||||
// The package-registry credential is a build input, so it is resolved before the clone: a
|
|
||||||
// build that could not have resolved its dependencies is refused in front of the reason, not
|
|
||||||
// after a clone that then fails at npm ci.
|
|
||||||
// Every container it starts is labelled with its id, so a kill finds what outlived the
|
|
||||||
// docker client (ADR 0219).
|
|
||||||
if request.Reuse != nil && isRegistry && !request.DryRun {
|
|
||||||
// **A walk's build takes the check's build of the same key** (novox/hq ADR 0350 §2).
|
|
||||||
reuse := builder.Reuse{Scratch: registryHere}
|
|
||||||
for _, p := range request.Reuse.Pulls {
|
|
||||||
reuse.Pulls = append(reuse.Pulls, builder.PullRef{Owner: p.Owner, Repo: p.Repo, Number: p.Number})
|
|
||||||
}
|
|
||||||
// Only what a verdict names is taken (ADR 0350 §2).
|
|
||||||
reuse.Named = namedOf(request.Reuse.Builds)
|
|
||||||
built, err = builder.BuildReusing(building, builder.Labelled(builder.Command, request.ID), publisher, reuse,
|
|
||||||
request.Repository, request.Path, request.Ref, workspace, request.Held, npmrc,
|
|
||||||
forgeFrom(), say, request.Seats)
|
|
||||||
} else {
|
|
||||||
built, err = builder.Build(building, builder.Labelled(builder.Command, request.ID), publisher,
|
|
||||||
request.Repository, request.Path, request.Ref, workspace, request.Held, npmrc,
|
|
||||||
forgeFrom(), say, request.Seats)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// What it copied into the store, whatever became of the build (novox/hq ADR 0257).
|
|
||||||
result.Mirrored = built.Mirrored
|
|
||||||
// Only a build the kill ended: the kill came before its work did. One that finished — built, or
|
|
||||||
// failed on its own — in the moment the kill arrived says what it did, and the kill is refused.
|
|
||||||
killed := false
|
|
||||||
if mine != nil {
|
|
||||||
killed = h.returned(mine) && err != nil
|
|
||||||
}
|
|
||||||
if superseded.Load() && err != nil {
|
|
||||||
// Ended by its own key moving on (ADR 0406 §4): said as the check's failure, as a kill is.
|
|
||||||
result.Failed = link.SupersededByKey
|
|
||||||
say("failed", link.SupersededByKey)
|
|
||||||
} else if killed {
|
|
||||||
// **Killed by hand is the outcome, whatever the build was doing** (novox/hq ADR 0219): the
|
|
||||||
// error it ended with is the kill's consequence, not a fault of the source.
|
|
||||||
result.Failed = link.KilledByHand
|
|
||||||
say("failed", link.KilledByHand)
|
|
||||||
} else if err != nil {
|
|
||||||
// A failure is a result. A build that fails and says nothing is indistinguishable from a
|
|
||||||
// builder that is not running, and those want completely different responses.
|
|
||||||
result.Failed = err.Error()
|
|
||||||
say("failed", err.Error())
|
|
||||||
} else if request.Check != nil {
|
|
||||||
say("checked", result.Check.Verdict+": "+result.Check.Summary)
|
|
||||||
} else {
|
|
||||||
manifest, marshalErr := json.Marshal(built.Manifest)
|
|
||||||
if marshalErr != nil {
|
|
||||||
result.Failed = marshalErr.Error()
|
|
||||||
} else {
|
|
||||||
result.Commit = built.Commit
|
|
||||||
result.Manifest = manifest
|
|
||||||
for _, made := range built.Built {
|
|
||||||
result.Made = append(result.Made, link.MadeArtifact{
|
|
||||||
Name: made.Name, Kind: made.Kind, Reference: made.Reference,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
result.Against = built.Against
|
|
||||||
result.SourceFingerprint = built.Source
|
|
||||||
result.SourceInputs = built.SourceInputs
|
|
||||||
// What the walk did instead of building, or why it built (novox/hq ADR 0350 §3, §4).
|
|
||||||
result.ReuseKey = built.ReuseKey
|
|
||||||
if built.Reused != nil {
|
|
||||||
f := built.Reused.From
|
|
||||||
result.Reused = built.Reused.Words()
|
|
||||||
result.ReusedFrom = &link.ReusedFrom{Pull: link.PullRef{Owner: f.Pull.Owner, Repo: f.Pull.Repo,
|
|
||||||
Number: f.Pull.Number}, Head: f.Head, Trunk: f.Trunk, Check: f.Check, Key: f.Key}
|
|
||||||
} else if request.Reuse != nil {
|
|
||||||
result.BuiltWhy = built.Why
|
|
||||||
if result.BuiltWhy == "" {
|
|
||||||
result.BuiltWhy = "it has no artifact to build"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.Trunk, result.OnTrunk, result.Branches = built.Trunk, built.OnTrunk, built.Branches
|
|
||||||
for _, r := range built.Read {
|
|
||||||
result.Read = append(result.Read, link.ReadRepository{Repository: r.Repository, Ref: r.Ref})
|
|
||||||
}
|
|
||||||
for _, s := range built.Sources {
|
|
||||||
result.Sources = append(result.Sources, link.BuildSource{Repository: s.Repository, Ref: s.Ref, Paths: s.Paths})
|
|
||||||
}
|
|
||||||
// The list of predefined keys the manifest was judged against, for the controller to publish
|
|
||||||
// (novox/hq ADR 0348 §7).
|
|
||||||
if p := built.Predefined; p != nil {
|
|
||||||
result.Predefined = &link.PredefinedList{Keys: p.Keys, Generation: p.Generation}
|
|
||||||
}
|
|
||||||
say("built", built.Manifest.Module+" from "+short(built.Commit))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A killed build is announced on a context of its own: the build's was the one cancelled, and the
|
|
||||||
// outcome must go out and the ask be settled — acknowledged, never redelivered to another machine
|
|
||||||
// to be built again. A machine being stopped is the other case and keeps its meaning: the
|
|
||||||
// parent's context is gone, nothing is announced or settled, and the ask is redelivered.
|
|
||||||
announcing := ctx
|
|
||||||
if killed {
|
|
||||||
fresh, stop := context.WithTimeout(context.Background(), 30*time.Second)
|
|
||||||
defer stop()
|
|
||||||
announcing = fresh
|
|
||||||
}
|
|
||||||
announceErr := work.Announce(announcing, result)
|
|
||||||
if mine != nil {
|
|
||||||
h.said(mine, result.Failed, announceErr == nil)
|
|
||||||
}
|
|
||||||
if err := announceErr; err != nil {
|
|
||||||
// Said, not fatal: the build happened. A build reported as failed because announcing it
|
|
||||||
// failed is a lie about work that was done — and the request stays unsettled below only if
|
|
||||||
// nothing was said at all, so another machine can try.
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot say what came of a build: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Settled only once the outcome is away, so a machine that dies before answering leaves the work
|
|
||||||
// for another rather than losing it.
|
|
||||||
if err := work.Done(); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "the outcome is away and the request could not be settled: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// checkSpecOf is a check request as the builder runs it.
|
|
||||||
func checkSpecOf(request link.BuildRequest) builder.CheckSpec {
|
|
||||||
c := request.Check
|
|
||||||
spec := builder.CheckSpec{ID: request.ID, Repository: request.Repository, Ref: request.Ref,
|
|
||||||
Owner: c.Owner, Repo: c.Repo, Number: c.Number, Paths: c.Paths, Beside: map[string]builder.Beside{},
|
|
||||||
Modules: c.Modules, New: c.New, Manifests: c.Manifests, Judge: c.Judge, Base: c.Base,
|
|
||||||
Toolchain: builder.ToolchainOf(request.Held), Toolchains: builder.ToolchainsOf(request.Held),
|
|
||||||
Tests: c.Tests, StoreOnly: c.StoreOnly, SiblingsUnknown: c.SiblingsUnknown}
|
|
||||||
for _, h := range c.HeldConfig {
|
|
||||||
spec.Held = append(spec.Held, builder.HeldValue{Pull: h.Pull, Head: h.Head, Node: h.Node, Module: h.Module, Key: h.Key,
|
|
||||||
Value: h.Value, Approved: h.Approved, By: h.By, At: h.At})
|
|
||||||
}
|
|
||||||
for _, s := range c.Siblings {
|
|
||||||
spec.Siblings = append(spec.Siblings, builder.Sibling{Repository: s.Repository, Number: s.Number, Head: s.Head,
|
|
||||||
Base: s.Base, Dir: s.Dir})
|
|
||||||
}
|
|
||||||
for dir, b := range c.Beside {
|
|
||||||
spec.Beside[dir] = builder.Beside{Repository: b.Repository, Ref: b.Ref}
|
|
||||||
}
|
|
||||||
for _, b := range c.Builds {
|
|
||||||
spec.Builds = append(spec.Builds, builder.CheckBuildSpec{Module: b.Module, Repository: b.Repository, Path: b.Path,
|
|
||||||
Ref: b.Ref, Merged: b.Merged, Tier: b.Tier, Skip: b.Skip})
|
|
||||||
}
|
|
||||||
for _, m := range c.Members {
|
|
||||||
spec.Group = append(spec.Group, builder.GroupHead{Owner: m.Owner, Repo: m.Repo, Repository: m.Repository,
|
|
||||||
Ref: m.Ref, Paths: m.Paths, Base: m.Base})
|
|
||||||
}
|
|
||||||
return spec
|
|
||||||
}
|
|
||||||
|
|
||||||
// siblingsOut is the siblings a check read as the outcome carries them (novox/hq ADR 0382 §4).
|
|
||||||
func siblingsOut(read []builder.JudgedSibling) []link.CheckSibling {
|
|
||||||
var out []link.CheckSibling
|
|
||||||
for _, s := range read {
|
|
||||||
out = append(out, link.CheckSibling{Repository: s.Repository, Number: s.Number, Head: s.Head, Base: s.Base,
|
|
||||||
BaseCommit: s.BaseCommit})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// judgeCache is the judge cache the build agent's manifest declares (MESH_JUDGE_CACHE, novox/hq ADR 0358 §6): a
|
|
||||||
// directory mounted at the same path on the host, so a judge build's mounts name what the agent sees. Empty when the
|
|
||||||
// agent declares none, and every judge is then built cold under its check's root.
|
|
||||||
func judgeCache() string { return os.Getenv("MESH_JUDGE_CACHE") }
|
|
||||||
|
|
||||||
// triedOf is a try's outcome as its log line says it (novox/hq issue 686): the repository's own check's verdict,
|
|
||||||
// or the check's where that did not run — a head that does not merge — and the lines naming what failed.
|
|
||||||
func triedOf(v builder.CheckVerdict) link.Tried {
|
|
||||||
t := link.Tried{Verdict: v.Verdict, Summary: v.Summary, Took: v.Took.Round(time.Second).String(),
|
|
||||||
Head: v.Head, Trunk: v.Trunk}
|
|
||||||
if v.Repo != nil && (v.Gate == nil || v.Gate.Verdict == "pass") {
|
|
||||||
t.Verdict, t.Summary = v.Repo.Verdict, v.Repo.Summary
|
|
||||||
}
|
|
||||||
if v.Repo != nil && v.Repo.Failed != "" {
|
|
||||||
t.Failed = strings.Split(v.Repo.Failed, "\n")
|
|
||||||
}
|
|
||||||
if t.Verdict == "" {
|
|
||||||
t.Verdict = "error"
|
|
||||||
}
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
// namedOf are the builds the seat's verdicts name, as the builder believes a scratch record by them (novox/hq ADR
|
|
||||||
// 0350 §2, issue 703): each module's key with every artifact's digest.
|
|
||||||
func namedOf(builds []link.CheckedBuild) []builder.NamedBuild {
|
|
||||||
var out []builder.NamedBuild
|
|
||||||
for _, b := range builds {
|
|
||||||
named := builder.NamedBuild{Pull: builder.PullRef{Owner: b.Pull.Owner, Repo: b.Pull.Repo,
|
|
||||||
Number: b.Pull.Number}, Check: b.Check, Module: b.Built.Module, Key: b.Built.Key,
|
|
||||||
Digests: map[string]string{}}
|
|
||||||
for _, m := range b.Built.Made {
|
|
||||||
named.Digests[m.Name] = builder.DigestOf(m.Reference)
|
|
||||||
}
|
|
||||||
out = append(out, named)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// checkBuiltOf is a check's builds for its merge as the outcome carries them (novox/hq ADR 0350 §1).
|
|
||||||
func checkBuiltOf(built []builder.CheckBuilt) []link.CheckBuilt {
|
|
||||||
var out []link.CheckBuilt
|
|
||||||
for _, b := range built {
|
|
||||||
c := link.CheckBuilt{Module: b.Module, Key: b.Key, NotBuilt: b.NotBuilt, Took: b.Took.Round(time.Second).String(),
|
|
||||||
Kept: b.Kept, Same: b.Same, SameAs: b.SameAs}
|
|
||||||
for _, m := range b.Made {
|
|
||||||
c.Made = append(c.Made, link.MadeArtifact{Name: m.Name, Kind: m.Kind, Reference: m.Reference})
|
|
||||||
}
|
|
||||||
out = append(out, c)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// layerOf is one layer of a check as the outcome carries it.
|
|
||||||
func layerOf(l *builder.Layer) *link.CheckLayer {
|
|
||||||
if l == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return &link.CheckLayer{Verdict: l.Verdict, Summary: l.Summary, Modules: l.Modules}
|
|
||||||
}
|
|
||||||
|
|
||||||
// packagesFrom is where a build resolves the mesh's own published packages — the SDK above all
|
|
||||||
// (novox/hq ADR 0076, issue 053) — on this machine.
|
|
||||||
//
|
|
||||||
// **The binding wins, then the genesis document** (novox/hq ADR 0336 §3, §4). The build agent's
|
|
||||||
// npm-package-registry requirement is optional: on genesis's node, before the gitea module is assigned,
|
|
||||||
// nothing provides it, and the installer sealed the forge it raised to this agent as one document
|
|
||||||
// instead. Once the requirement binds, the binding names the endpoint the way the artifact store's
|
|
||||||
// does and a sealed token file the credential, and the genesis document is left unread. A binding
|
|
||||||
// file named and absent is a requirement not bound here, never a fault: the environment names the
|
|
||||||
// path whether or not the mesh wrote it.
|
|
||||||
//
|
|
||||||
// The environment variables remain for a builder run by a person. With none of the three the result
|
|
||||||
// is disabled and says this machine, so a build that needs the registry is refused naming it (§6),
|
|
||||||
// and a build that needs none builds anyway.
|
|
||||||
func packagesFrom(machine string) (builder.Npmrc, error) {
|
|
||||||
scope := strings.TrimSpace(os.Getenv("MESH_NPM_SCOPE"))
|
|
||||||
if scope == "" {
|
|
||||||
scope = "@novox"
|
|
||||||
}
|
|
||||||
none := builder.Npmrc{Machine: machine}
|
|
||||||
|
|
||||||
if path := strings.TrimSpace(os.Getenv("MESH_PACKAGE_BINDING")); path != "" {
|
|
||||||
raw, err := os.ReadFile(path)
|
|
||||||
switch {
|
|
||||||
case errors.Is(err, os.ErrNotExist):
|
|
||||||
// Not bound on this machine: the genesis document, or nothing.
|
|
||||||
case err != nil:
|
|
||||||
return none, fmt.Errorf("cannot read what the mesh said about the package registry: %w", err)
|
|
||||||
default:
|
|
||||||
return fromBinding(path, raw, scope, machine)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if path := strings.TrimSpace(os.Getenv("MESH_GENESIS_PACKAGE_REGISTRY")); path != "" {
|
|
||||||
raw, err := os.ReadFile(path)
|
|
||||||
switch {
|
|
||||||
case errors.Is(err, os.ErrNotExist):
|
|
||||||
// Not this mesh's genesis node, or the installer has not sealed it yet.
|
|
||||||
case err != nil:
|
|
||||||
return none, fmt.Errorf("cannot read the genesis document for the package registry: %w", err)
|
|
||||||
default:
|
|
||||||
return fromGenesis(path, raw, scope, machine)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A builder run by a person.
|
|
||||||
registry := strings.TrimSpace(os.Getenv("MESH_NPM_REGISTRY"))
|
|
||||||
secret := strings.TrimSpace(os.Getenv("MESH_NPM_TOKEN"))
|
|
||||||
username := strings.TrimSpace(os.Getenv("MESH_NPM_USER"))
|
|
||||||
if registry == "" && secret == "" {
|
|
||||||
return none, nil
|
|
||||||
}
|
|
||||||
n := builder.Npmrc{Scope: scope, Registry: registry, From: builder.FromEnvironment, Machine: machine}
|
|
||||||
if username != "" {
|
|
||||||
n.Username, n.Password = username, secret
|
|
||||||
} else {
|
|
||||||
n.Token = secret
|
|
||||||
}
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fromBinding composes the registry from the npm-package-registry binding the mesh wrote and the
|
|
||||||
// credential it sealed beside it. The mesh authenticates the ordinary way — a generated password the
|
|
||||||
// provider only applies (novox/hq ADR 0048) — so with a username the secret is a password (basic
|
|
||||||
// auth); without one it is a bearer token a provider minted.
|
|
||||||
func fromBinding(path string, raw []byte, scope, machine string) (builder.Npmrc, error) {
|
|
||||||
none := builder.Npmrc{Machine: machine}
|
|
||||||
var told struct {
|
|
||||||
From string `json:"from"`
|
|
||||||
At string `json:"at"`
|
|
||||||
As string `json:"as"`
|
|
||||||
Serves map[string]any `json:"serves"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(raw, &told); err != nil {
|
|
||||||
return none, fmt.Errorf("%s is not a binding: %w", path, err)
|
|
||||||
}
|
|
||||||
if told.At == "" {
|
|
||||||
return none, fmt.Errorf(
|
|
||||||
"%s says the package registry is on %q and gives no address for it", path, told.From)
|
|
||||||
}
|
|
||||||
// Composed from what the provider serves, so nothing here knows gitea's URL shape from
|
|
||||||
// another registry's: it states its port, the path its registry answers on, and the scheme.
|
|
||||||
scheme := "https"
|
|
||||||
if s, ok := told.Serves["scheme"]; ok {
|
|
||||||
scheme = fmt.Sprintf("%v", s)
|
|
||||||
}
|
|
||||||
port, ok := told.Serves["port"]
|
|
||||||
if !ok {
|
|
||||||
return none, fmt.Errorf("%s says nothing about which port the package registry answers on", path)
|
|
||||||
}
|
|
||||||
npmPath, ok := told.Serves["npm-path"]
|
|
||||||
if !ok {
|
|
||||||
return none, fmt.Errorf("%s says nothing about the path the package registry answers on", path)
|
|
||||||
}
|
|
||||||
secret := strings.TrimSpace(os.Getenv("MESH_NPM_TOKEN"))
|
|
||||||
if file := strings.TrimSpace(os.Getenv("MESH_NPM_TOKEN_FILE")); file != "" {
|
|
||||||
raw, err := os.ReadFile(file)
|
|
||||||
if err != nil {
|
|
||||||
return none, fmt.Errorf("cannot read this builder's package-registry credential: %w", err)
|
|
||||||
}
|
|
||||||
secret = strings.TrimSpace(string(raw))
|
|
||||||
}
|
|
||||||
n := builder.Npmrc{Scope: scope, Registry: fmt.Sprintf("%s://%s:%v%v", scheme, told.At, port, npmPath),
|
|
||||||
From: builder.FromBinding, Machine: machine}
|
|
||||||
if told.As != "" {
|
|
||||||
n.Username, n.Password = told.As, secret
|
|
||||||
} else {
|
|
||||||
n.Token = secret
|
|
||||||
}
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenesisRegistry is the genesis document (novox/hq ADR 0336 §3): what the installer sealed to the
|
|
||||||
// build agent on its node at genesis's packages step, as the `genesis-package-registry` own-secret —
|
|
||||||
// the forge it raised, the port this node gave the package registry, its npm path, and the account it
|
|
||||||
// made with that account's password. mesh-host writes it; the field names are the two repositories'
|
|
||||||
// agreement.
|
|
||||||
type GenesisRegistry struct {
|
|
||||||
Scheme string `json:"scheme"`
|
|
||||||
At string `json:"at"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
NpmPath string `json:"npm-path"`
|
|
||||||
As string `json:"as"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// fromGenesis composes the registry from the genesis document.
|
|
||||||
func fromGenesis(path string, raw []byte, scope, machine string) (builder.Npmrc, error) {
|
|
||||||
none := builder.Npmrc{Machine: machine}
|
|
||||||
var g GenesisRegistry
|
|
||||||
if err := json.Unmarshal(raw, &g); err != nil {
|
|
||||||
return none, fmt.Errorf("%s is not the genesis document for the package registry: %w", path, err)
|
|
||||||
}
|
|
||||||
var missing []string
|
|
||||||
for name, value := range map[string]string{"at": g.At, "npm-path": g.NpmPath, "as": g.As, "password": g.Password} {
|
|
||||||
if strings.TrimSpace(value) == "" {
|
|
||||||
missing = append(missing, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if g.Port <= 0 {
|
|
||||||
missing = append(missing, "port")
|
|
||||||
}
|
|
||||||
if len(missing) > 0 {
|
|
||||||
sort.Strings(missing)
|
|
||||||
return none, fmt.Errorf("%s, the genesis document for the package registry, says nothing of %s",
|
|
||||||
path, strings.Join(missing, ", "))
|
|
||||||
}
|
|
||||||
scheme := g.Scheme
|
|
||||||
if scheme == "" {
|
|
||||||
scheme = "http"
|
|
||||||
}
|
|
||||||
return builder.Npmrc{Scope: scope, Registry: fmt.Sprintf("%s://%s:%d%s", scheme, g.At, g.Port, g.NpmPath),
|
|
||||||
Username: g.As, Password: g.Password, From: builder.FromGenesis, Machine: machine}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// saysRegistry is the line each build's log carries about the package registry (novox/hq ADR 0336
|
|
||||||
// §4): which of the binding and the genesis document it resolved from, or that it has neither here.
|
|
||||||
func saysRegistry(n builder.Npmrc) string {
|
|
||||||
if n.Enabled() {
|
|
||||||
return "the package registry is resolved from " + n.From
|
|
||||||
}
|
|
||||||
machine := n.Machine
|
|
||||||
if machine == "" {
|
|
||||||
machine = "this machine"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("no package registry: %s is unbound on %s and no genesis document is here; "+
|
|
||||||
"a build needing it is refused", builder.RegistryRequirement, machine)
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgeFrom is the git credential this builder may offer a clone, composed from the same binding
|
|
||||||
// and sealed secret its package-registry half already reads: the forge that answers npm is the
|
|
||||||
// forge that hosts the repositories, and its provisioner applies one password to one user for
|
|
||||||
// both. Anything missing means no credential, and every clone stays anonymous — which is all a
|
|
||||||
// mesh of public repositories ever needs.
|
|
||||||
//
|
|
||||||
// The URL names the binding's own address — the machine the mesh says the forge is on — so a
|
|
||||||
// private repository is registered and built by that address, and a clone of anything else is
|
|
||||||
// never shown this credential (git's credential store matches the whole origin).
|
|
||||||
func forgeFrom() builder.GitCredential {
|
|
||||||
path := strings.TrimSpace(os.Getenv("MESH_PACKAGE_BINDING"))
|
|
||||||
if path == "" {
|
|
||||||
return builder.GitCredential{}
|
|
||||||
}
|
|
||||||
raw, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return builder.GitCredential{}
|
|
||||||
}
|
|
||||||
var told struct {
|
|
||||||
At string `json:"at"`
|
|
||||||
As string `json:"as"`
|
|
||||||
Serves map[string]any `json:"serves"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(raw, &told); err != nil || told.At == "" || told.As == "" {
|
|
||||||
return builder.GitCredential{}
|
|
||||||
}
|
|
||||||
secret := strings.TrimSpace(os.Getenv("MESH_NPM_TOKEN"))
|
|
||||||
if file := strings.TrimSpace(os.Getenv("MESH_NPM_TOKEN_FILE")); file != "" {
|
|
||||||
if raw, err := os.ReadFile(file); err == nil {
|
|
||||||
secret = strings.TrimSpace(string(raw))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if secret == "" {
|
|
||||||
return builder.GitCredential{}
|
|
||||||
}
|
|
||||||
scheme := "https"
|
|
||||||
if s, ok := told.Serves["scheme"]; ok {
|
|
||||||
scheme = fmt.Sprintf("%v", s)
|
|
||||||
}
|
|
||||||
host := told.At
|
|
||||||
if port, ok := told.Serves["port"]; ok {
|
|
||||||
host = fmt.Sprintf("%s:%v", told.At, port)
|
|
||||||
}
|
|
||||||
made := url.URL{Scheme: scheme, User: url.UserPassword(told.As, secret), Host: host}
|
|
||||||
return builder.GitCredential{URL: made.String()}
|
|
||||||
}
|
|
||||||
|
|
||||||
func short(commit string) string {
|
|
||||||
if len(commit) > 8 {
|
|
||||||
return commit[:8]
|
|
||||||
}
|
|
||||||
return commit
|
|
||||||
}
|
|
||||||
|
|
||||||
// whereToPublish is the artifact store this builder uses.
|
|
||||||
//
|
|
||||||
// **Preferably from the mesh.** A builder that is a module requires an artifact store, and the
|
|
||||||
// mesh writes it a file saying which machine answers that and on what port — the same binding any
|
|
||||||
// consumer of any provision gets. Reading it means the address is not a setting somebody keeps in
|
|
||||||
// step by hand, and moving the store is an ordinary reassignment rather than an edit on every
|
|
||||||
// build machine.
|
|
||||||
//
|
|
||||||
// The environment variable remains for a builder run by a person, which is how this started and
|
|
||||||
// how it is still run while being developed.
|
|
||||||
func whereToPublish() (string, error) {
|
|
||||||
binding := strings.TrimSpace(os.Getenv("MESH_BINDING"))
|
|
||||||
if binding == "" {
|
|
||||||
registry := strings.TrimSpace(os.Getenv("MESH_REGISTRY"))
|
|
||||||
if registry == "" {
|
|
||||||
return "", fmt.Errorf("neither MESH_BINDING nor MESH_REGISTRY: a built artifact " +
|
|
||||||
"nobody can fetch is not built")
|
|
||||||
}
|
|
||||||
return registry, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
raw, err := os.ReadFile(binding)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("cannot read what the mesh said about the artifact store: %w", err)
|
|
||||||
}
|
|
||||||
var told struct {
|
|
||||||
From string `json:"from"`
|
|
||||||
At string `json:"at"`
|
|
||||||
Serves map[string]any `json:"serves"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(raw, &told); err != nil {
|
|
||||||
return "", fmt.Errorf("%s is not a binding: %w", binding, err)
|
|
||||||
}
|
|
||||||
if told.At == "" {
|
|
||||||
// The provider is not on the private network, so there is no name to reach it by. Said
|
|
||||||
// rather than falling back to the machine's own name, which would publish to a store on
|
|
||||||
// the wrong machine and be found out much later.
|
|
||||||
return "", fmt.Errorf(
|
|
||||||
"%s says the artifact store is on %q and gives no address for it", binding, told.From)
|
|
||||||
}
|
|
||||||
port, ok := told.Serves["port"]
|
|
||||||
if !ok {
|
|
||||||
return "", fmt.Errorf("%s says nothing about which port the artifact store answers on",
|
|
||||||
binding)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s:%v", told.At, port), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// brokerFrom is where this builder connects, and with what.
|
|
||||||
//
|
|
||||||
// **Preferably from a file the mesh sealed to this machine.** A builder that is a module is given
|
|
||||||
// its credential the way every other module is given one: generated or accepted centrally, sealed
|
|
||||||
// to the machine, written by the host. Putting it in an environment variable instead would mean
|
|
||||||
// the one copy that matters passing through a terminal and a process listing.
|
|
||||||
//
|
|
||||||
// The variable remains for a builder run by a person.
|
|
||||||
func brokerFrom() (Credential, error) {
|
|
||||||
if path := strings.TrimSpace(os.Getenv("MESH_BROKER_FILE")); path != "" {
|
|
||||||
raw, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return Credential{}, fmt.Errorf("cannot read this builder's credential: %w", err)
|
|
||||||
}
|
|
||||||
said := strings.TrimSpace(string(raw))
|
|
||||||
if said == "" {
|
|
||||||
// An empty credential file is a machine that will connect as nobody and be refused,
|
|
||||||
// with the reason three layers away.
|
|
||||||
return Credential{}, fmt.Errorf("%s is empty, so this builder has no credential", path)
|
|
||||||
}
|
|
||||||
var held Credential
|
|
||||||
if err := json.Unmarshal([]byte(said), &held); err == nil && held.URL != "" {
|
|
||||||
return held, nil
|
|
||||||
}
|
|
||||||
// A file holding only a URL, which is what a person writing one by hand produces. The
|
|
||||||
// broker is then verified against whatever this machine already trusts.
|
|
||||||
return Credential{URL: said}, nil
|
|
||||||
}
|
|
||||||
return Credential{}, fmt.Errorf(
|
|
||||||
"no MESH_BROKER_FILE: a build machine with no credential for the bus has nothing to build")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Credential is what a build machine is given so it can reach the broker.
|
|
||||||
//
|
|
||||||
// Two things, because reaching a broker over TLS needs both: who to connect as, and what to check
|
|
||||||
// the certificate against. A mesh's broker presents a certificate of the mesh's own, which is in
|
|
||||||
// no public trust store, so a URL alone can only connect to a broker somebody else vouches for.
|
|
||||||
//
|
|
||||||
// **The same shape a node gets, for the same reason** (novox/hq ADR 0004): the fingerprint travels
|
|
||||||
// out of band — here, sealed with the credential — and the endpoint is verified once at connect.
|
|
||||||
type Credential struct {
|
|
||||||
URL string `json:"url"`
|
|
||||||
Fingerprint string `json:"fingerprint,omitempty"`
|
|
||||||
// User and Password ride beside the address on the bus being built (design 25): a credential
|
|
||||||
// embedded in a URL leaks into every log line that prints a connection, so the mesh seals them
|
|
||||||
// as two fields and this machine joins them once, here, to dial.
|
|
||||||
User string `json:"user,omitempty"`
|
|
||||||
Password string `json:"password,omitempty"`
|
|
||||||
// Claims are the seats the module this credential was issued for claims, as the mesh writes
|
|
||||||
// them beside the credential (novox/hq ADR 0159). The first is the build role this machine
|
|
||||||
// serves; a credential naming none is from before claims travelled in it.
|
|
||||||
Claims []struct {
|
|
||||||
Seat string `json:"seat"`
|
|
||||||
} `json:"claims,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// seatsClaimed is the seats the credential names, in order.
|
|
||||||
func (c Credential) seatsClaimed() []string {
|
|
||||||
out := make([]string, 0, len(c.Claims))
|
|
||||||
for _, claim := range c.Claims {
|
|
||||||
out = append(out, claim.Seat)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// onTheNewBus is whether a credential is for the bus being built: its address says so, and the
|
|
||||||
// mesh only ever seals such a credential with the user and password beside it.
|
|
||||||
func (c Credential) onTheNewBus() bool { return strings.HasPrefix(strings.TrimSpace(c.URL), "nats://") }
|
|
||||||
|
|
||||||
// natsURL is the address with this machine's credential in it, for the one dial that needs it.
|
|
||||||
func (c Credential) natsURL() string {
|
|
||||||
rest := strings.TrimPrefix(strings.TrimSpace(c.URL), "nats://")
|
|
||||||
if c.User == "" {
|
|
||||||
return "nats://" + rest
|
|
||||||
}
|
|
||||||
return "nats://" + c.User + ":" + c.Password + "@" + rest
|
|
||||||
}
|
|
||||||
@@ -1,188 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
)
|
|
||||||
|
|
||||||
// buildOnce is the builder doing one build and stopping, with no broker and no mesh.
|
|
||||||
//
|
|
||||||
// **This is how a mesh is raised** (novox/hq ADR 0073). The installer carries this program and runs
|
|
||||||
// it once, before anything else exists, to produce the control plane from the same repository and
|
|
||||||
// path that every later rebuild of it will use. What raises the mesh is therefore the same thing
|
|
||||||
// that maintains it — not a second mechanism that has to be kept in step with the first and is
|
|
||||||
// exercised once per new mesh, which is how often enough to rot.
|
|
||||||
//
|
|
||||||
// It takes no work from a queue and answers nobody: there is no broker yet, and the only thing
|
|
||||||
// waiting for the answer is the installer that started it. So the result goes to standard output as
|
|
||||||
// JSON, which is what the installer reads.
|
|
||||||
//
|
|
||||||
// With no --registry it publishes nowhere and the image stays in this machine's container runtime,
|
|
||||||
// named by the digest of its own configuration (see builder.Local). That is the genesis case. With
|
|
||||||
// one, it publishes as it always does — the same command is how an operator builds a module by hand
|
|
||||||
// on a mesh that already exists.
|
|
||||||
func buildOnce(ctx context.Context, args []string) error {
|
|
||||||
set := flag.NewFlagSet("build", flag.ContinueOnError)
|
|
||||||
path := set.String("path", "", "the module's directory inside the repository (default: its root)")
|
|
||||||
ref := set.String("ref", "", "the commit to build; a branch is a moving target somebody else controls")
|
|
||||||
registry := set.String("registry", "",
|
|
||||||
"host:port to publish to. Without it the artifacts stay in this machine's container runtime, which is the genesis case")
|
|
||||||
workspace := set.String("workspace", "", "where to clone and build (default: a temporary directory)")
|
|
||||||
on := multiple(set, "on",
|
|
||||||
"a base this build stands on, as <module>/<artifact>=<reference>. Repeatable")
|
|
||||||
positionals, err := parseAround(set, args)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(positionals) != 1 {
|
|
||||||
return errors.New("mesh-builder build <repository> [--path P] [--ref COMMIT] [--registry HOST:PORT]")
|
|
||||||
}
|
|
||||||
repository := positionals[0]
|
|
||||||
|
|
||||||
where := *workspace
|
|
||||||
if where == "" {
|
|
||||||
where = os.TempDir() + "/mesh-builder-once"
|
|
||||||
}
|
|
||||||
|
|
||||||
// What this build may stand on, said on the command line because there is no mesh to ask.
|
|
||||||
//
|
|
||||||
// **This is the genesis case and it is meant to be awkward.** On a running mesh the control
|
|
||||||
// plane answers this, because only it knows what this mesh holds. Here nothing has been built
|
|
||||||
// yet, so whoever runs this says what to use — and for the control plane, which is the one
|
|
||||||
// module raised before anything else exists, the answer is ordinarily nothing at all.
|
|
||||||
bases := map[string]string{}
|
|
||||||
for _, pair := range *on {
|
|
||||||
key, reference, found := strings.Cut(pair, "=")
|
|
||||||
if !found || key == "" || reference == "" {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"--on takes <module>/<artifact>=<reference>, and %q is not that", pair)
|
|
||||||
}
|
|
||||||
bases[key] = reference
|
|
||||||
}
|
|
||||||
|
|
||||||
// Local unless told otherwise, because the moment this exists for has nowhere to publish. A
|
|
||||||
// default pointing at a registry would mean genesis failing at a push to something that is not
|
|
||||||
// there yet, one step away from the thing that could explain it.
|
|
||||||
var publisher builder.Publisher = builder.Local{Run: builder.Command}
|
|
||||||
if *registry != "" {
|
|
||||||
publisher = builder.Registry{Address: *registry, Run: builder.Command}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "building %s", repository)
|
|
||||||
if *path != "" {
|
|
||||||
fmt.Fprintf(os.Stderr, " at %s", *path)
|
|
||||||
}
|
|
||||||
if *ref != "" {
|
|
||||||
fmt.Fprintf(os.Stderr, " at %s", *ref)
|
|
||||||
}
|
|
||||||
fmt.Fprintln(os.Stderr)
|
|
||||||
|
|
||||||
machine := os.Getenv("MESH_NODE")
|
|
||||||
if machine == "" {
|
|
||||||
machine, _ = os.Hostname()
|
|
||||||
}
|
|
||||||
npmrc, err := packagesFrom(machine)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, " [packages] %s\n", saysRegistry(npmrc))
|
|
||||||
built, buildErr := builder.Build(ctx, builder.Command, publisher, repository, *path, *ref, where, bases, npmrc,
|
|
||||||
forgeFrom(),
|
|
||||||
func(step, message string) { fmt.Fprintf(os.Stderr, " [%s] %s\n", step, message) })
|
|
||||||
if buildErr != nil {
|
|
||||||
return buildErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// To standard output, and everything else to standard error, so the caller can read this
|
|
||||||
// without having to separate it from progress.
|
|
||||||
out := onceResult{
|
|
||||||
Module: built.Manifest.Module,
|
|
||||||
Commit: built.Commit,
|
|
||||||
Repository: repository,
|
|
||||||
Path: *path,
|
|
||||||
Ref: *ref,
|
|
||||||
Manifest: built.Manifest,
|
|
||||||
Against: built.Against,
|
|
||||||
Source: built.Source,
|
|
||||||
}
|
|
||||||
for _, r := range built.Read {
|
|
||||||
out.Read = append(out.Read, readRepository{Repository: r.Repository, Ref: r.Ref})
|
|
||||||
}
|
|
||||||
for _, made := range built.Built {
|
|
||||||
out.Made = append(out.Made, madeArtifact{Name: made.Name, Kind: made.Kind, Reference: made.Reference})
|
|
||||||
}
|
|
||||||
body, err := json.MarshalIndent(out, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println(string(body))
|
|
||||||
fmt.Fprintf(os.Stderr, " built %s from %s\n", built.Manifest.Module, short(built.Commit))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// onceResult is what one build reports to whoever started it.
|
|
||||||
//
|
|
||||||
// The same fields the mesh records for a build, so a reader comparing a genesis build against an
|
|
||||||
// ordinary one is comparing the same thing said the same way.
|
|
||||||
type onceResult struct {
|
|
||||||
Module string `json:"module"`
|
|
||||||
Commit string `json:"commit"`
|
|
||||||
Repository string `json:"repository"`
|
|
||||||
Path string `json:"path,omitempty"`
|
|
||||||
Ref string `json:"ref,omitempty"`
|
|
||||||
Manifest any `json:"manifest"`
|
|
||||||
Made []madeArtifact `json:"made"`
|
|
||||||
Against []string `json:"against,omitempty"`
|
|
||||||
Read []readRepository `json:"read,omitempty"`
|
|
||||||
Source string `json:"source-fingerprint,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// readRepository is a repository this build read source from besides the module's own.
|
|
||||||
type readRepository struct {
|
|
||||||
Repository string `json:"repository"`
|
|
||||||
Ref string `json:"ref,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type madeArtifact struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Kind string `json:"kind"`
|
|
||||||
Reference string `json:"reference"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseAround lets flags appear on either side of the repository, because a person writing this by
|
|
||||||
// hand will put them wherever reads best and the standard parser stops at the first thing that is
|
|
||||||
// not a flag. The same helper the control plane's commands use, for the same reason.
|
|
||||||
func parseAround(set *flag.FlagSet, args []string) ([]string, error) {
|
|
||||||
var positionals []string
|
|
||||||
rest := args
|
|
||||||
for {
|
|
||||||
if err := set.Parse(rest); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rest = set.Args()
|
|
||||||
if len(rest) == 0 {
|
|
||||||
return positionals, nil
|
|
||||||
}
|
|
||||||
positionals = append(positionals, rest[0])
|
|
||||||
rest = rest[1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// multiple is a flag that may be given more than once.
|
|
||||||
type repeated []string
|
|
||||||
|
|
||||||
func (r *repeated) String() string { return strings.Join(*r, ", ") }
|
|
||||||
func (r *repeated) Set(v string) error { *r = append(*r, v); return nil }
|
|
||||||
|
|
||||||
func multiple(set *flag.FlagSet, name, usage string) *[]string {
|
|
||||||
var values repeated
|
|
||||||
set.Var(&values, name, usage)
|
|
||||||
return (*[]string)(&values)
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The build agent's package registry (novox/hq ADR 0336, issue 121): its npm-package-registry
|
|
||||||
// requirement is optional, so the binding may be absent, and on genesis's node the installer seals
|
|
||||||
// the forge it raised as one genesis document instead.
|
|
||||||
|
|
||||||
// registryFiles names the three paths the build agent's environment always names, writing only those
|
|
||||||
// given.
|
|
||||||
func registryFiles(t *testing.T, binding, token, genesis string) {
|
|
||||||
t.Helper()
|
|
||||||
dir := t.TempDir()
|
|
||||||
for env, content := range map[string]string{
|
|
||||||
"MESH_PACKAGE_BINDING": binding,
|
|
||||||
"MESH_NPM_TOKEN_FILE": token,
|
|
||||||
"MESH_GENESIS_PACKAGE_REGISTRY": genesis,
|
|
||||||
} {
|
|
||||||
path := filepath.Join(dir, env)
|
|
||||||
t.Setenv(env, path)
|
|
||||||
if content != "" {
|
|
||||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, env := range []string{"MESH_NPM_REGISTRY", "MESH_NPM_TOKEN", "MESH_NPM_USER", "MESH_NPM_SCOPE"} {
|
|
||||||
t.Setenv(env, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
aBinding = `{"from":"gitea","at":"anchor.mesh","as":"anchor-agent",
|
|
||||||
"serves":{"scheme":"http","port":3000,"npm-path":"/api/packages/novox/npm/"}}`
|
|
||||||
aGenesisDocument = `{"scheme":"http","at":"127.0.0.1","port":3100,"npm-path":"/api/packages/novox/npm/",
|
|
||||||
"as":"mesh-builder","password":"genesis-secret"}`
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestUntilTheRequirementBindsTheGenesisDocumentIsRead(t *testing.T) {
|
|
||||||
registryFiles(t, "", "", aGenesisDocument)
|
|
||||||
n, err := packagesFrom("anchor")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("an absent binding beside a genesis document was refused: %v", err)
|
|
||||||
}
|
|
||||||
if n.From != builder.FromGenesis || n.Registry != "http://127.0.0.1:3100/api/packages/novox/npm/" ||
|
|
||||||
n.Username != "mesh-builder" || n.Password != "genesis-secret" || n.Scope != "@novox" {
|
|
||||||
t.Fatalf("the genesis document was not what the registry came from: %+v", n)
|
|
||||||
}
|
|
||||||
file, err := n.File()
|
|
||||||
if err != nil || !strings.Contains(file, "//127.0.0.1:3100/api/packages/novox/npm/:username=mesh-builder") {
|
|
||||||
t.Fatalf("the .npmrc is not written from the genesis document: %v\n%s", err, file)
|
|
||||||
}
|
|
||||||
if got := saysRegistry(n); !strings.Contains(got, "genesis document") {
|
|
||||||
t.Errorf("the log does not say the genesis document: %s", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestARealBindingWinsOverTheGenesisDocument(t *testing.T) {
|
|
||||||
registryFiles(t, aBinding, "bound-secret\n", aGenesisDocument)
|
|
||||||
n, err := packagesFrom("anchor")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if n.From != builder.FromBinding || n.Registry != "http://anchor.mesh:3000/api/packages/novox/npm/" ||
|
|
||||||
n.Username != "anchor-agent" || n.Password != "bound-secret" {
|
|
||||||
t.Fatalf("the binding did not win over the genesis document: %+v", n)
|
|
||||||
}
|
|
||||||
if got := saysRegistry(n); !strings.Contains(got, "binding") {
|
|
||||||
t.Errorf("the log does not say the binding: %s", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithNeitherTheAgentHasNoRegistryAndSaysWhere(t *testing.T) {
|
|
||||||
registryFiles(t, "", "", "")
|
|
||||||
n, err := packagesFrom("workstation")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("an unbound optional requirement was a fault: %v", err)
|
|
||||||
}
|
|
||||||
if n.Enabled() || n.Machine != "workstation" {
|
|
||||||
t.Fatalf("a registry came from nowhere: %+v", n)
|
|
||||||
}
|
|
||||||
got := saysRegistry(n)
|
|
||||||
for _, want := range []string{"npm-package-registry", "unbound", "workstation"} {
|
|
||||||
if !strings.Contains(got, want) {
|
|
||||||
t.Errorf("the log does not say %q: %s", want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAGenesisDocumentMissingAFieldIsRefusedByName(t *testing.T) {
|
|
||||||
registryFiles(t, "", "", `{"at":"127.0.0.1","port":3000,"npm-path":"/api/packages/novox/npm/","as":"mesh-builder"}`)
|
|
||||||
if _, err := packagesFrom("anchor"); err == nil || !strings.Contains(err.Error(), "password") {
|
|
||||||
t.Fatalf("a genesis document with no password was taken: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **The build agent says it is ready** (novox/hq issue 544). It listens on nothing, so no http or tcp
|
|
||||||
// check can see it work; it writes a mark once it is bound to its worker and pulls build work, and its
|
|
||||||
// container's health check runs `mesh-builder ready`, which passes only while the mark is there. Without
|
|
||||||
// it the node-engine could judge the agent only alive, and every walk that moved it waited out the whole
|
|
||||||
// grace on each machine.
|
|
||||||
//
|
|
||||||
// The mark lives in the container's own temporary directory, never in the workspace the machine keeps:
|
|
||||||
// a new container starts without it, and a restarted one removes it before it binds again.
|
|
||||||
func readyMark() string { return filepath.Join(os.TempDir(), "mesh-builder.ready") }
|
|
||||||
|
|
||||||
// notReady removes the mark: a holder starting is not taking work until it says so again.
|
|
||||||
func notReady() error {
|
|
||||||
if err := os.Remove(readyMark()); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
||||||
return fmt.Errorf("cannot remove the mark that this agent takes build work (%s): %w", readyMark(), err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// taking writes the mark. A mark that cannot be written is said, and the agent goes on building: its
|
|
||||||
// check then says it is not ready, which is the truth the gate should hear.
|
|
||||||
func taking(seat string) {
|
|
||||||
if err := os.WriteFile(readyMark(), []byte(seat+"\n"), 0o644); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "cannot mark that this agent takes build work (%s): %v\n", readyMark(), err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "taking build work from %s\n", seat)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ready is the health check: nil when this container's agent is bound and pulling build work.
|
|
||||||
func ready() error {
|
|
||||||
if _, err := os.Stat(readyMark()); err != nil {
|
|
||||||
return fmt.Errorf("not taking build work yet: %s is not there", readyMark())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The health check passes only between the agent saying it takes work and its next start (novox/hq
|
|
||||||
// issue 544).
|
|
||||||
func TestTheAgentIsReadyOnlyOnceItTakesWork(t *testing.T) {
|
|
||||||
t.Setenv("TMPDIR", t.TempDir())
|
|
||||||
if err := ready(); err == nil {
|
|
||||||
t.Fatal("an agent that never took work is ready")
|
|
||||||
}
|
|
||||||
taking("mesh-build-machine")
|
|
||||||
if err := ready(); err != nil {
|
|
||||||
t.Fatalf("an agent taking work is not ready: %v", err)
|
|
||||||
}
|
|
||||||
// Started again, it is not ready until it binds again.
|
|
||||||
if err := notReady(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := ready(); err == nil {
|
|
||||||
t.Fatal("an agent starting again is ready before it takes work")
|
|
||||||
}
|
|
||||||
if err := notReady(); err != nil {
|
|
||||||
t.Fatalf("removing a mark that is not there failed: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := os.Stat(readyMark()); err == nil {
|
|
||||||
t.Fatal("the mark is still there")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The seat a build machine serves comes from its credential (novox/hq ADR 0190 handover).
|
|
||||||
func TestTheCredentialSaysWhichBuildRoleThisMachineServes(t *testing.T) {
|
|
||||||
var held Credential
|
|
||||||
if err := json.Unmarshal([]byte(`{"url":"nats://bus:4222","user":"anchor.builder","password":"x",
|
|
||||||
"claims":[{"seat":"mesh-build-machine","scope":"mesh","serves":[]}]}`), &held); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := link.BuildSeatClaimed(held.seatsClaimed()); got != "mesh-build-machine" {
|
|
||||||
t.Errorf("the old builder's credential serves %q", got)
|
|
||||||
}
|
|
||||||
var bare Credential
|
|
||||||
if err := json.Unmarshal([]byte(`{"url":"nats://bus:4222","user":"anchor.build-agent","password":"x"}`), &bare); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := link.BuildSeatClaimed(bare.seatsClaimed()); got != link.TheBuildMachine {
|
|
||||||
t.Errorf("a credential without claims serves %q, want %s", got, link.TheBuildMachine)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **The genesis path writes its result to stdout and nothing else there.** The installer captures
|
|
||||||
// stdout alone and parses it as JSON; one stray log line makes that fail with "invalid character"
|
|
||||||
// — exactly what a builder fmt.Printf caused, breaking a build that had worked. This keeps
|
|
||||||
// diagnostics on stderr so a future print cannot repeat it silently.
|
|
||||||
func TestBuilderDiagnosticsStayOffStdout(t *testing.T) {
|
|
||||||
allowed := map[string]bool{
|
|
||||||
"string(body)": true, // once.go: the result JSON, which IS stdout
|
|
||||||
"version)": true, // --version
|
|
||||||
`"stopping")`: true, // the loop.s shutdown line
|
|
||||||
"usage)": true, // --help text, for a human
|
|
||||||
"said)": true, // busy: the answer its node-engine reads (novox/hq ADR 0335)
|
|
||||||
}
|
|
||||||
for _, file := range []string{"once.go", "main.go"} {
|
|
||||||
src, err := os.ReadFile(file)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for i, raw := range strings.Split(string(src), "\n") {
|
|
||||||
line := strings.TrimSpace(raw)
|
|
||||||
if !strings.HasPrefix(line, "fmt.Printf(") &&
|
|
||||||
!strings.HasPrefix(line, "fmt.Println(") &&
|
|
||||||
!strings.HasPrefix(line, "fmt.Print(") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ok := false
|
|
||||||
for token := range allowed {
|
|
||||||
if strings.Contains(line, token) {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
t.Errorf("%s:%d writes a diagnostic to stdout, which the installer parses as JSON:\n %s",
|
|
||||||
file, i+1, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A try's outcome, as the build seat says it in the build's log (novox/hq issue 686): the repository's own
|
|
||||||
// check's verdict, how long it took, and each line naming what failed.
|
|
||||||
func TestATrysOutcomeNamesWhatFailed(t *testing.T) {
|
|
||||||
tried := triedOf(builder.CheckVerdict{Verdict: "pass", Summary: "the change touches no module", Took: 303 * time.Second,
|
|
||||||
Head: "c0ffee", Trunk: "ca3f43c", Gate: &builder.Layer{Verdict: "pass"},
|
|
||||||
Repo: &builder.Layer{Verdict: "fail", Summary: "its merge-check.sh failed: TestX",
|
|
||||||
Failed: "--- FAIL: TestX (0.01s)\n x_test.go:12: got 1, want 2"}})
|
|
||||||
if tried.Verdict != "fail" || tried.Took != "5m3s" || len(tried.Failed) != 2 || tried.Trunk != "ca3f43c" ||
|
|
||||||
tried.Summary != "its merge-check.sh failed: TestX" {
|
|
||||||
t.Fatalf("a failed try: %+v", tried)
|
|
||||||
}
|
|
||||||
// A head that does not merge fails both layers: the check's own word.
|
|
||||||
conflict := triedOf(builder.CheckVerdict{Verdict: "fail", Summary: "does not merge", Gate: &builder.Layer{Verdict: "fail"},
|
|
||||||
Repo: &builder.Layer{Verdict: "fail", Summary: "does not merge"}})
|
|
||||||
if conflict.Verdict != "fail" || conflict.Summary != "does not merge" {
|
|
||||||
t.Fatalf("a try that does not merge: %+v", conflict)
|
|
||||||
}
|
|
||||||
// No verdict is an error, never a pass.
|
|
||||||
if none := triedOf(builder.CheckVerdict{}); none.Verdict != "error" {
|
|
||||||
t.Fatalf("a try with no verdict: %+v", none)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,225 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/ed25519"
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
|
||||||
"crypto/x509/pkix"
|
|
||||||
"encoding/hex"
|
|
||||||
"math/big"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Where a builder publishes.
|
|
||||||
//
|
|
||||||
// Preferably from the mesh: a builder that is a module requires an artifact store, and the mesh
|
|
||||||
// writes it a binding saying which machine answers and on what port. Reading it means the address
|
|
||||||
// is not a setting somebody keeps in step by hand.
|
|
||||||
|
|
||||||
func binding(t *testing.T, body string) string {
|
|
||||||
t.Helper()
|
|
||||||
path := filepath.Join(t.TempDir(), "artifact-store.json")
|
|
||||||
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheMeshSaysWhereToPublish(t *testing.T) {
|
|
||||||
t.Setenv("MESH_BINDING", binding(t, `{"binding":1,"provision":"artifact-store",
|
|
||||||
"from":"anchor","at":"anchor.internal","serves":{"port":5000,"scheme":"http"}}`))
|
|
||||||
where, err := whereToPublish()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if where != "anchor.internal:5000" {
|
|
||||||
t.Fatalf("got %q", where)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABindingWithNoAddressIsRefused(t *testing.T) {
|
|
||||||
// The provider is not on the private network, so there is no name to reach it by. Falling
|
|
||||||
// back to anything would publish to a store on the wrong machine and be found out much later.
|
|
||||||
t.Setenv("MESH_BINDING", binding(t, `{"binding":1,"from":"anchor","serves":{"port":5000}}`))
|
|
||||||
_, err := whereToPublish()
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("a binding with nowhere to reach was accepted")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "anchor") {
|
|
||||||
t.Fatalf("the failure does not name the machine: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABindingWithNoPortIsRefused(t *testing.T) {
|
|
||||||
t.Setenv("MESH_BINDING", binding(t, `{"binding":1,"from":"a","at":"a.internal","serves":{}}`))
|
|
||||||
if _, err := whereToPublish(); err == nil {
|
|
||||||
t.Fatal("a binding saying nothing about a port was accepted")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheVariableStillWorksForABuilderRunByAPerson(t *testing.T) {
|
|
||||||
// Which is how this started and how it is still run while being developed.
|
|
||||||
t.Setenv("MESH_BINDING", "")
|
|
||||||
t.Setenv("MESH_REGISTRY", "127.0.0.1:5000")
|
|
||||||
where, err := whereToPublish()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if where != "127.0.0.1:5000" {
|
|
||||||
t.Fatalf("got %q", where)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNeitherIsRefusedRatherThanGuessed(t *testing.T) {
|
|
||||||
t.Setenv("MESH_BINDING", "")
|
|
||||||
t.Setenv("MESH_REGISTRY", "")
|
|
||||||
if _, err := whereToPublish(); err == nil {
|
|
||||||
t.Fatal("a builder with nowhere to publish reported somewhere")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheCredentialComesFromAFileTheMeshSealed(t *testing.T) {
|
|
||||||
// A builder that is a module is given its credential the way every module is: sealed to the
|
|
||||||
// machine and written by the host. An environment variable instead would put the one copy
|
|
||||||
// that matters through a terminal and a process listing.
|
|
||||||
path := filepath.Join(t.TempDir(), "broker")
|
|
||||||
if err := os.WriteFile(path, []byte("amqps://a-builder:secret@broker.internal:5671/\n"), 0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Setenv("MESH_BROKER_FILE", path)
|
|
||||||
t.Setenv("MESH_BROKER_AMQP", "amqp://should-not-be-used@nowhere/")
|
|
||||||
|
|
||||||
got, err := brokerFrom()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got.URL != "amqps://a-builder:secret@broker.internal:5671/" {
|
|
||||||
t.Fatalf("got %q", got.URL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The credential the mesh seals carries what to check the broker's certificate against, because a
|
|
||||||
// mesh's broker presents a certificate of the mesh's own and no public trust store has it. A URL
|
|
||||||
// alone can only reach a broker somebody else vouches for.
|
|
||||||
func TestTheSealedCredentialCarriesWhatVerifiesTheBroker(t *testing.T) {
|
|
||||||
path := filepath.Join(t.TempDir(), "broker")
|
|
||||||
if err := os.WriteFile(path, []byte(
|
|
||||||
`{"url":"amqps://a-builder:secret@broker.internal:5671/","fingerprint":"abc123"}`),
|
|
||||||
0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Setenv("MESH_BROKER_FILE", path)
|
|
||||||
t.Setenv("MESH_BROKER_AMQP", "")
|
|
||||||
|
|
||||||
got, err := brokerFrom()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got.URL != "amqps://a-builder:secret@broker.internal:5671/" {
|
|
||||||
t.Fatalf("the url was lost: %q", got.URL)
|
|
||||||
}
|
|
||||||
if got.Fingerprint != "abc123" {
|
|
||||||
t.Fatal("the builder was given nothing to check the broker against, so it can only " +
|
|
||||||
"connect to a broker some public authority vouches for")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAnEmptyCredentialFileIsRefused(t *testing.T) {
|
|
||||||
// Otherwise the builder connects as nobody and is refused, with the reason three layers away.
|
|
||||||
path := filepath.Join(t.TempDir(), "broker")
|
|
||||||
if err := os.WriteFile(path, []byte("\n"), 0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Setenv("MESH_BROKER_FILE", path)
|
|
||||||
t.Setenv("MESH_BROKER_AMQP", "")
|
|
||||||
if _, err := brokerFrom(); err == nil {
|
|
||||||
t.Fatal("an empty credential was accepted")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABuilderWithNoCredentialAtAllSaysSo(t *testing.T) {
|
|
||||||
t.Setenv("MESH_BROKER_FILE", "")
|
|
||||||
t.Setenv("MESH_BROKER_AMQP", "")
|
|
||||||
if _, err := brokerFrom(); err == nil {
|
|
||||||
t.Fatal("a builder with no broker reported one")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The pin is compared in the spelling the mesh writes it.
|
|
||||||
//
|
|
||||||
// A bare digest against a written fingerprint never matches, and the failure is indistinguishable
|
|
||||||
// from being pointed at the wrong broker — which is the one thing this check exists to report
|
|
||||||
// truthfully. It cost a lab run.
|
|
||||||
func TestThePinIsComparedInTheSpellingTheMeshWritesIt(t *testing.T) {
|
|
||||||
certificate, key := aServerCertificate(t)
|
|
||||||
der := certificate.Certificate[0]
|
|
||||||
sum := sha256.Sum256(der)
|
|
||||||
written := "sha256:" + hex.EncodeToString(sum[:])
|
|
||||||
|
|
||||||
listener, err := tls.Listen("tcp", "127.0.0.1:0", &tls.Config{
|
|
||||||
Certificates: []tls.Certificate{certificate}, MinVersion: tls.VersionTLS12,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
conn, err := listener.Accept()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = conn.(*tls.Conn).Handshake()
|
|
||||||
conn.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
_ = key
|
|
||||||
|
|
||||||
// The pin the mesh wrote must be accepted.
|
|
||||||
if err := handshakeWith(listener.Addr().String(), written); err != nil {
|
|
||||||
t.Fatalf("the broker this builder was told about was refused: %v", err)
|
|
||||||
}
|
|
||||||
// And a different one refused, or the check reports nothing.
|
|
||||||
other := "sha256:" + strings.Repeat("ab", 32)
|
|
||||||
if err := handshakeWith(listener.Addr().String(), other); err == nil {
|
|
||||||
t.Fatal("a broker this builder was not told about was accepted")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handshakeWith runs the pin check the builder dials with against an address.
|
|
||||||
func handshakeWith(address, pin string) error {
|
|
||||||
conn, err := tls.Dial("tcp", address, broker.PinnedToFingerprint(pin))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func aServerCertificate(t *testing.T) (tls.Certificate, ed25519.PrivateKey) {
|
|
||||||
t.Helper()
|
|
||||||
public, private, err := ed25519.GenerateKey(rand.Reader)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
template := &x509.Certificate{
|
|
||||||
SerialNumber: big.NewInt(1),
|
|
||||||
Subject: pkix.Name{CommonName: "a broker"},
|
|
||||||
NotBefore: time.Now().Add(-time.Hour),
|
|
||||||
NotAfter: time.Now().Add(time.Hour),
|
|
||||||
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
|
|
||||||
}
|
|
||||||
der, err := x509.CreateCertificate(rand.Reader, template, template, public, private)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: private}, private
|
|
||||||
}
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A provision whose credential only a person outside the mesh can give (novox/hq ADR 0337, issue 164, issue 165):
|
|
||||||
// given once for its provider, at the controller's terminal, and sealed in one act to every consumer that binds the
|
|
||||||
// provision from that provider now. The value is never read back, printed or kept.
|
|
||||||
|
|
||||||
// outsideOffer is the provider module's own name when `secret accept <node> <module> <name>` names a provision that
|
|
||||||
// module offers with a credential issued outside the mesh, and "" when it names anything else (an own secret, or a
|
|
||||||
// pair with --provider).
|
|
||||||
func outsideOffer(ctx context.Context, module, name, provider string) (string, error) {
|
|
||||||
if provider != "" {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if m, known := shelf[module]; known && m.IssuedOutsideOf(name) {
|
|
||||||
return module, nil
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// offeredOutsideBy is a module that offers the provision with a credential issued outside the mesh, or "".
|
|
||||||
func offeredOutsideBy(ctx context.Context, provision string) (string, error) {
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
module, _ := catalogue.OffersIssuedOutside(shelf, provision)
|
|
||||||
return module, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// consumersFrom is every consumer that binds a provision from one provider now, found by resolving every machine as
|
|
||||||
// its next send would, and the machines that could not be resolved, with why.
|
|
||||||
func consumersFrom(ctx context.Context, open *stores, provider, providerModule, provision string) (
|
|
||||||
[]inventory.PairEnd, []string, error) {
|
|
||||||
// One reading of the mesh for every machine this looks at.
|
|
||||||
ctx = withInputCache(ctx)
|
|
||||||
nodes, err := open.inventory.Nodes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
var out []inventory.PairEnd
|
|
||||||
var unresolved []string
|
|
||||||
for _, n := range nodes {
|
|
||||||
plan, _, err := resolveNode(ctx, open, n.Name)
|
|
||||||
if err != nil {
|
|
||||||
unresolved = append(unresolved, fmt.Sprintf("%s: %s", n.Name, oneLine(err.Error())))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out = append(out, bindsFrom(plan, n.Name, provider, providerModule, provision)...)
|
|
||||||
}
|
|
||||||
return out, unresolved, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// bindsFrom is every consumer on one machine whose resolution answers the provision from that provider. Pure.
|
|
||||||
func bindsFrom(plan catalogue.Resolution, machine, provider, providerModule, provision string) []inventory.PairEnd {
|
|
||||||
var out []inventory.PairEnd
|
|
||||||
for _, need := range plan.Needs {
|
|
||||||
if need.ByRecord || need.Name != provision || need.From != provider {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if need.Module != "" && need.Module != providerModule {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out = append(out, inventory.PairEnd{Consumer: machine, ConsumerModule: need.For, Local: need.Local})
|
|
||||||
}
|
|
||||||
sort.Slice(out, func(i, j int) bool {
|
|
||||||
return out[i].ConsumerModule+"\x00"+out[i].Local < out[j].ConsumerModule+"\x00"+out[j].Local
|
|
||||||
})
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// acceptForProvision gives a provision's credential issued outside the mesh, once for its provider (ADR 0337 §3).
|
|
||||||
func acceptForProvision(ctx context.Context, provider, providerModule, provision, value string) error {
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
consumers, unresolved, err := consumersFrom(ctx, open, provider, providerModule, provision)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
accepted, err := open.inventory.AcceptSecretForProvision(ctx, provision, provider, providerModule, value, consumers)
|
|
||||||
value = ""
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Print(acceptedSaid(provider, providerModule, provision, accepted, unresolved))
|
|
||||||
if len(accepted.Sealed) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := sendTo(ctx, open, accepted.Machines); err != nil {
|
|
||||||
return fmt.Errorf("%s is sealed and could not be sent: %w; `push %s` sends it", provision, err,
|
|
||||||
strings.Join(accepted.Machines, "`, `push "))
|
|
||||||
}
|
|
||||||
fmt.Printf(" sent %s; each consumer starts again on it\n", strings.Join(accepted.Machines, ", "))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// acceptedSaid is what one accept for a provider did, in words that never carry the value. Pure.
|
|
||||||
func acceptedSaid(provider, providerModule, provision string, a inventory.ProvisionAccepted, unresolved []string) string {
|
|
||||||
var b strings.Builder
|
|
||||||
if len(a.Sealed) == 0 {
|
|
||||||
fmt.Fprintf(&b, "%s from %s on %s: no consumer binds it now, so nothing was sealed; a consumer that binds "+
|
|
||||||
"later waits for it, and this line given again seals it to every consumer then\n", provision,
|
|
||||||
providerModule, provider)
|
|
||||||
} else {
|
|
||||||
var names []string
|
|
||||||
for _, c := range a.Sealed {
|
|
||||||
names = append(names, fmt.Sprintf("%s on %s%s", c.ConsumerModule, c.Consumer, asLocal(c.Local)))
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&b, "%s from %s on %s is given, sealed to %s and to %s\n", provision, providerModule, provider,
|
|
||||||
strings.Join(names, ", "), provider)
|
|
||||||
fmt.Fprintf(&b, " the mesh cannot read it back, never makes one of its own for it, and will not rotate it\n")
|
|
||||||
}
|
|
||||||
for _, c := range a.Unsealable {
|
|
||||||
fmt.Fprintf(&b, " not sealed to %s on %s: no sealing key on that machine; give it again once it has one\n",
|
|
||||||
c.ConsumerModule, c.Consumer)
|
|
||||||
}
|
|
||||||
for _, u := range unresolved {
|
|
||||||
fmt.Fprintf(&b, " not known whether a consumer on %s binds it, which did not resolve; give it again once it does\n", u)
|
|
||||||
}
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
@@ -1,378 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
"github.com/nats-io/nats.go/jetstream"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/lease"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Acting under the lease (novox/hq to-be 45 §6, ADR 0227 rule 1).
|
|
||||||
//
|
|
||||||
// **Only the instance holding the lease acts**: sends a declaration, writes a plan, a condition or a
|
|
||||||
// call. Every one of those passes theLease.epoch, which answers the epoch the act carries or why it may
|
|
||||||
// not happen. Three ways a process stands to the lease:
|
|
||||||
//
|
|
||||||
// - **The serving controller** takes it before it does anything else — before it asserts the bus's
|
|
||||||
// objects, which are the controller's to write — waiting while another holds it, and renews it.
|
|
||||||
// Another holder seen, or the key's age passed with no renewal made, is the lease lost: the gate
|
|
||||||
// closes at once and the process exits, so its service manager restarts it as a candidate (serve,
|
|
||||||
// in push.go). A renewal the bus does not answer in time is not a loss (novox/hq issue 477): the
|
|
||||||
// lease is kept, no new act starts once half the key's age passed unrenewed, and the next renewal
|
|
||||||
// made opens the gate again.
|
|
||||||
// - **A command run at a shell** — `push` in the installer, the lab, a person repairing a mesh whose
|
|
||||||
// controller is down (issue 201) — acts **under the holder's epoch** when a controller holds the
|
|
||||||
// lease: it is the same mesh's word, composed and sent under the store's hold of each machine like
|
|
||||||
// the serving controller's, and the epoch it carries is read at the moment it acts, so a handover
|
|
||||||
// between makes it stale and refused like any other. **When nobody holds the lease, the command
|
|
||||||
// takes it** for as long as it runs and gives it back; a controller starting meanwhile waits for
|
|
||||||
// it, as it would for another controller.
|
|
||||||
// - **A process with no bus** — a test, a command that only reads — acts with no epoch and is
|
|
||||||
// refused nothing: there is nothing to order against, and nothing it does reaches a machine.
|
|
||||||
//
|
|
||||||
// **Unleased, said and temporary.** A serving controller whose bus refuses it the lease's key — the bus's
|
|
||||||
// user list is older than this build and does not grant the bucket yet — and that sees no other holder
|
|
||||||
// serves without one, as every controller did before the lease: declarations carry no epoch, which no
|
|
||||||
// node-engine refuses. Said once, kept as a condition (S12), and tried again every renewal interval; the
|
|
||||||
// first push that sends the bus its new user list grants it, and the next try takes it. Refusing to act
|
|
||||||
// instead would be a controller that can never send the user list that lets it act.
|
|
||||||
|
|
||||||
// actor is this process's standing to the lease.
|
|
||||||
type actor struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
// held is the lease this process holds: the serving controller's, or a command's own.
|
|
||||||
held *lease.Lease
|
|
||||||
// unleased is why a serving controller acts without the lease; empty while it holds it or is not
|
|
||||||
// serving.
|
|
||||||
unleased string
|
|
||||||
// serving is a serving controller, which never borrows another's epoch.
|
|
||||||
serving bool
|
|
||||||
// kv is the lease bucket, for a command to read the holder's epoch from.
|
|
||||||
kv jetstream.KeyValue
|
|
||||||
close func()
|
|
||||||
// noBus is a process with no bus configured.
|
|
||||||
noBus bool
|
|
||||||
// reset is when the lease bucket was found raised again from nothing and its revisions moved past
|
|
||||||
// the highest epoch issued, and what was said of it; zero when it was not (S12).
|
|
||||||
reset time.Time
|
|
||||||
resetSaid string
|
|
||||||
// renewEvery and poll, when set, are how often the serving controller renews the lease it holds and
|
|
||||||
// looks again at one another holds, in place of the lease's own: a test's, so two controllers hand the
|
|
||||||
// lease over in a second rather than in a renewal of five (novox/hq issue 591). A renewal is given
|
|
||||||
// the lease's own RenewEvery to answer however often it is made, so a slow bus does not lose it.
|
|
||||||
renewEvery, poll time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// theLease is this process's standing to the lease.
|
|
||||||
var theLease = &actor{}
|
|
||||||
|
|
||||||
// instance names this process among controller instances: its machine, its process and when it
|
|
||||||
// started. The lease's holder and every call this process keeps carry it.
|
|
||||||
var instance = func() string {
|
|
||||||
host, _ := os.Hostname()
|
|
||||||
return fmt.Sprintf("controller@%s pid %d since %s", host, os.Getpid(), time.Now().UTC().Format(time.RFC3339))
|
|
||||||
}()
|
|
||||||
|
|
||||||
// epoch is the gate: the epoch an act carries — zero for none — or why it may not happen.
|
|
||||||
func (a *actor) epoch(ctx context.Context) (uint64, error) {
|
|
||||||
a.mu.Lock()
|
|
||||||
held, serving, unleased, noBus := a.held, a.serving, a.unleased, a.noBus
|
|
||||||
a.mu.Unlock()
|
|
||||||
switch {
|
|
||||||
case held != nil:
|
|
||||||
return held.Epoch()
|
|
||||||
case serving && unleased != "":
|
|
||||||
return 0, nil
|
|
||||||
case serving:
|
|
||||||
return 0, lease.ErrNotHeld
|
|
||||||
case noBus:
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return a.forACommand(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// forACommand is a command's epoch: the holder's, or a lease of its own when nobody holds one.
|
|
||||||
func (a *actor) forACommand(ctx context.Context) (uint64, error) {
|
|
||||||
a.mu.Lock()
|
|
||||||
defer a.mu.Unlock()
|
|
||||||
if a.held != nil {
|
|
||||||
return a.held.Epoch()
|
|
||||||
}
|
|
||||||
if a.kv == nil {
|
|
||||||
address, err := broker.BusAddress()
|
|
||||||
if err != nil {
|
|
||||||
// No bus: this process reaches no machine, and has nothing to order against.
|
|
||||||
a.noBus = true
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
js, err := broker.Dial(address)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("the bus cannot be reached, so whether a controller holds the lease cannot be "+
|
|
||||||
"read and nothing is done: %w", err)
|
|
||||||
}
|
|
||||||
api, err := jetstream.New(js.Conn())
|
|
||||||
if err != nil {
|
|
||||||
js.Close()
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
reading, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
if err := broker.EnsureLeaseBucket(reading, api); err != nil {
|
|
||||||
js.Close()
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
kv, err := api.KeyValue(reading, broker.LeaseBucket)
|
|
||||||
if err != nil {
|
|
||||||
js.Close()
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
a.kv, a.close = kv, js.Close
|
|
||||||
if _, found, err := lease.Current(reading, kv); err == nil && !found {
|
|
||||||
// Nobody: this command takes it for as long as it runs.
|
|
||||||
l, err := lease.Open(reading, api, broker.LeaseBucket, lease.Options{Holder: holderOf(instance),
|
|
||||||
Say: func(format string, args ...any) { fmt.Printf(format+"\n", args...) }})
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
epoch, err := l.TryTake(reading)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("no controller holds the lease and this command could not take it: %w", err)
|
|
||||||
}
|
|
||||||
keeping, stop := context.WithCancel(context.Background())
|
|
||||||
go l.Keep(keeping)
|
|
||||||
a.held = l
|
|
||||||
closeBus := a.close
|
|
||||||
a.close = func() {
|
|
||||||
stop()
|
|
||||||
l.Release(context.Background())
|
|
||||||
closeBus()
|
|
||||||
}
|
|
||||||
return epoch, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reading, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
holder, found, err := lease.Current(reading, a.kv)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("who holds the controller lease cannot be read, so nothing is done: %w", err)
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return 0, errors.New("the controller that held the lease while this command ran let go of it; nothing " +
|
|
||||||
"more is done under an epoch nobody holds — run the command again")
|
|
||||||
}
|
|
||||||
return holder.Epoch, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// release gives back what this process holds, at its end.
|
|
||||||
func (a *actor) release() {
|
|
||||||
a.mu.Lock()
|
|
||||||
closing := a.close
|
|
||||||
a.close = nil
|
|
||||||
a.mu.Unlock()
|
|
||||||
if closing != nil {
|
|
||||||
closing()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// holderOf is this process as the lease's holder.
|
|
||||||
func holderOf(instance string) lease.Holder {
|
|
||||||
host, _ := os.Hostname()
|
|
||||||
build := runningBuild()
|
|
||||||
if build == "" {
|
|
||||||
build = version
|
|
||||||
}
|
|
||||||
return lease.Holder{Instance: instance, Host: host, Build: build}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunningBuildVar is where the declaration tells this process which build it is (module.json, the
|
|
||||||
// controller process's env): the version its bundle is delivered as, `${version}` composed by the
|
|
||||||
// catalogue from the bundle's digest. Empty for a process placed by hand.
|
|
||||||
const RunningBuildVar = "MESH_CONTROLLER_VERSION"
|
|
||||||
|
|
||||||
// runningBuild is the version of the build this process is, or empty when the declaration did not say.
|
|
||||||
func runningBuild() string {
|
|
||||||
return strings.TrimSpace(os.Getenv(RunningBuildVar))
|
|
||||||
}
|
|
||||||
|
|
||||||
// serveUnderTheLease takes the lease for the serving controller, waiting while another holds it, and
|
|
||||||
// keeps it until ctx ends. Lost is closed when it is lost; the caller exits on it.
|
|
||||||
func (a *actor) serveUnderTheLease(ctx context.Context, inv *inventory.Inventory, address string) (lost <-chan struct{}, err error) {
|
|
||||||
a.mu.Lock()
|
|
||||||
a.serving = true
|
|
||||||
a.mu.Unlock()
|
|
||||||
// Its own connection, held as long as the lease, and named so (novox/hq issue 327).
|
|
||||||
js, err := broker.Dial(address, nats.Name(broker.ConnectionName+" lease"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("the mesh is on the bus at %s and this control plane cannot reach it to take the "+
|
|
||||||
"lease: %w", broker.BareAddress(address), err)
|
|
||||||
}
|
|
||||||
api, err := jetstream.New(js.Conn())
|
|
||||||
if err != nil {
|
|
||||||
js.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
asserting, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
err = broker.EnsureLeaseBucket(asserting, api)
|
|
||||||
cancel()
|
|
||||||
if err != nil {
|
|
||||||
js.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
say := func(format string, args ...any) { fmt.Printf(format+"\n", args...) }
|
|
||||||
l, err := lease.Open(ctx, api, broker.LeaseBucket, lease.Options{Holder: holderOf(instance),
|
|
||||||
RenewEvery: a.renewEvery, RenewWithin: lease.RenewEvery, Poll: a.poll, Floor: inv.HighestEpoch, Say: say, Health: controllerHealth, Moved: func(was, floor uint64) {
|
|
||||||
a.mu.Lock()
|
|
||||||
defer a.mu.Unlock()
|
|
||||||
a.reset = time.Now()
|
|
||||||
a.resetSaid = fmt.Sprintf("the lease bucket was at revision %d with epoch %d already issued: it was "+
|
|
||||||
"raised again from nothing (a bus whose data was replaced), and its revisions were moved past %d so "+
|
|
||||||
"no machine refuses the next epoch", was, floor, floor)
|
|
||||||
}})
|
|
||||||
if err != nil {
|
|
||||||
js.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
gone := make(chan struct{})
|
|
||||||
epoch, err := l.Take(ctx)
|
|
||||||
switch {
|
|
||||||
case ctx.Err() != nil:
|
|
||||||
js.Close()
|
|
||||||
return nil, ctx.Err()
|
|
||||||
case err != nil && !errors.Is(err, lease.ErrUnwritable):
|
|
||||||
// Whether another controller acts cannot be told: this one does not act, and exits to try again.
|
|
||||||
js.Close()
|
|
||||||
return nil, err
|
|
||||||
case err != nil:
|
|
||||||
// Nobody holds it and the bus will not let it be written: unleased, said, tried again (see above).
|
|
||||||
a.mu.Lock()
|
|
||||||
a.unleased = err.Error()
|
|
||||||
a.mu.Unlock()
|
|
||||||
say("this controller serves WITHOUT the lease: %v. Its declarations carry no epoch; it tries again "+
|
|
||||||
"every %s, and the first push that sends the bus its user list grants it", err, lease.RenewEvery)
|
|
||||||
go a.takeWhenGranted(ctx, l, inv, gone)
|
|
||||||
default:
|
|
||||||
a.took(ctx, l, inv, epoch, gone)
|
|
||||||
}
|
|
||||||
a.mu.Lock()
|
|
||||||
a.close = func() {
|
|
||||||
if held, err := l.Epoch(); err == nil {
|
|
||||||
ending, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
if err := inv.EndEpoch(ending, held, inventory.EpochReleased); err != nil {
|
|
||||||
say("how epoch %d ended could not be recorded: %v", held, err)
|
|
||||||
}
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
l.Release(context.Background())
|
|
||||||
js.Close()
|
|
||||||
}
|
|
||||||
a.mu.Unlock()
|
|
||||||
return gone, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// took is the lease taken: recorded, earlier epochs nobody gave back ended as expired, kept.
|
|
||||||
func (a *actor) took(ctx context.Context, l *lease.Lease, inv *inventory.Inventory, epoch uint64, gone chan struct{}) {
|
|
||||||
a.mu.Lock()
|
|
||||||
a.held, a.unleased = l, ""
|
|
||||||
a.mu.Unlock()
|
|
||||||
h := holderOf(instance)
|
|
||||||
recording, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
expired, err := inv.TookEpoch(recording, inventory.Epoch{Epoch: epoch, Instance: h.Instance, Host: h.Host,
|
|
||||||
Build: h.Build, Taken: time.Now()})
|
|
||||||
cancel()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("epoch %d could not be recorded as taken, so a stale refusal from it will not name it: %v\n", epoch, err)
|
|
||||||
}
|
|
||||||
for _, e := range expired {
|
|
||||||
fmt.Printf("the controller of epoch %d (%s) stopped renewing the lease without giving it back: it is "+
|
|
||||||
"taken over at epoch %d\n", e.Epoch, e.Instance, epoch)
|
|
||||||
}
|
|
||||||
go l.Keep(ctx)
|
|
||||||
go func() {
|
|
||||||
<-l.Lost()
|
|
||||||
if ctx.Err() == nil {
|
|
||||||
why := l.LostWhy()
|
|
||||||
ending, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
_ = inv.EndEpoch(ending, epoch, inventory.EpochLost)
|
|
||||||
cancel()
|
|
||||||
fmt.Printf("the controller lease was lost (epoch %d): %v — this controller stops and exits, to "+
|
|
||||||
"be started again as a candidate\n", epoch, why)
|
|
||||||
}
|
|
||||||
close(gone)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// takeWhenGranted tries the lease again every renewal interval while serving unleased, and stops this
|
|
||||||
// controller if another took it meanwhile: two serving at once is what the lease is for.
|
|
||||||
func (a *actor) takeWhenGranted(ctx context.Context, l *lease.Lease, inv *inventory.Inventory, gone chan struct{}) {
|
|
||||||
tick := time.NewTicker(lease.RenewEvery)
|
|
||||||
defer tick.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-tick.C:
|
|
||||||
}
|
|
||||||
epoch, err := l.TryTake(ctx)
|
|
||||||
if errors.Is(err, lease.ErrTaken) {
|
|
||||||
fmt.Printf("another controller took the lease while this one served without it: %v — this one "+
|
|
||||||
"stops and exits\n", err)
|
|
||||||
close(gone)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
// Still not written, or not readable this time: unleased, said by S12, tried again.
|
|
||||||
a.mu.Lock()
|
|
||||||
a.unleased = err.Error()
|
|
||||||
a.mu.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
a.took(ctx, l, inv, epoch, gone)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// standing is what `status` and the self-check say of this process and the lease.
|
|
||||||
type standing struct {
|
|
||||||
Epoch uint64
|
|
||||||
Held bool
|
|
||||||
Renewed time.Time
|
|
||||||
Unleased string
|
|
||||||
// Reset is when the lease bucket was found raised again from nothing, and ResetSaid what of it.
|
|
||||||
Reset time.Time
|
|
||||||
ResetSaid string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *actor) standing() standing {
|
|
||||||
a.mu.Lock()
|
|
||||||
held, unleased, reset, resetSaid := a.held, a.unleased, a.reset, a.resetSaid
|
|
||||||
a.mu.Unlock()
|
|
||||||
st := standing{Unleased: unleased, Reset: reset, ResetSaid: resetSaid}
|
|
||||||
if held == nil {
|
|
||||||
return st
|
|
||||||
}
|
|
||||||
epoch, err := held.Epoch()
|
|
||||||
st.Epoch, st.Held, st.Renewed = epoch, err == nil, held.Renewed()
|
|
||||||
return st
|
|
||||||
}
|
|
||||||
|
|
||||||
// The gates, given to what acts: a declaration's send (link) and a plan's write (the inventory).
|
|
||||||
func init() {
|
|
||||||
link.ActingGate = func(ctx context.Context) error {
|
|
||||||
_, err := theLease.epoch(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("this controller may not send: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,460 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"io"
|
|
||||||
"slices"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The things the mesh can be asked to do, separated from how it was asked.
|
|
||||||
//
|
|
||||||
// **A surface is an adapter with no decisions in it** (novox/hq ADR 0035). The command line and
|
|
||||||
// the command API call the same functions here, so an assignment refused at one is refused at the
|
|
||||||
// other for the same reason and in the same words. The moment a surface can accept something
|
|
||||||
// another would reject, the mesh has two answers to one question and people learn which to trust.
|
|
||||||
//
|
|
||||||
// Each returns what happened as text a person can read and a caller can pass on. Neither surface
|
|
||||||
// composes its own explanation, because two explanations of one refusal drift.
|
|
||||||
|
|
||||||
// assign puts a module on a node, and says at once what in the mesh no longer works out.
|
|
||||||
//
|
|
||||||
// The assignment is kept even when the node does not resolve: it is what a person meant, and
|
|
||||||
// assignment is not an ordering. A consumer assigned before its provider does not resolve for as
|
|
||||||
// long as it takes to assign the provider, and refusing the first half of a pair would make the
|
|
||||||
// order somebody types two commands in part of the mesh's rules.
|
|
||||||
//
|
|
||||||
// **What is checked is the mesh, not the one machine** — because that is what the assignment
|
|
||||||
// changes. novox/hq 04-ISSUES/017 names the shape: an action can succeed into a state its own
|
|
||||||
// verify rejects, and it happens when the action's test is not the test the verify uses. Here the
|
|
||||||
// action tested one node and the verify is resolution over all of them, so an assignment could be
|
|
||||||
// reported as fine while it took a provision away from every other machine — a module offering a
|
|
||||||
// mesh-scoped provision stops offering it the moment its own node stops resolving, and every
|
|
||||||
// consumer elsewhere is then told *nothing in this mesh provides it*, with a remedy that names a
|
|
||||||
// module already assigned. That was found on a four-node raise and read as a version bump breaking
|
|
||||||
// provider recognition; it was neither the version nor the provider.
|
|
||||||
//
|
|
||||||
// It costs a resolution per machine. Assignment is a person typing a command, and being told which
|
|
||||||
// machines this just blocked is worth more than the milliseconds.
|
|
||||||
func assign(ctx context.Context, open *stores, node string, modules ...string) (string, error) {
|
|
||||||
return assignWith(ctx, open, node, assignOptions{}, modules...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// assignWith is assign asked with its options: build, for a module known and not built (novox/hq issue 325).
|
|
||||||
func assignWith(ctx context.Context, open *stores, node string, opts assignOptions, modules ...string) (string, error) {
|
|
||||||
if len(modules) == 0 {
|
|
||||||
return "", fmt.Errorf("assign %s names no module", node)
|
|
||||||
}
|
|
||||||
if err := refusedMovingTheController(modules); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// Every line of the answer comes from the one resolution after the write (novox/hq issue 237):
|
|
||||||
// what the reads before it would say about a machine left out is not the answer's.
|
|
||||||
ctx = leftOutSaidBy(ctx, io.Discard)
|
|
||||||
// Held while it is recorded, so it cannot land between a converge's preview and its flip and
|
|
||||||
// be taken without ever having been previewed (novox/hq ADR 0100).
|
|
||||||
ctx, release, err := holdNodes(ctx, open, []string{node})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer release()
|
|
||||||
// **A module the catalogue does not hold is looked for further before it is refused** (novox/hq issue
|
|
||||||
// 325): a build in flight keeps the assignment pending, a module known and not built is said with how to
|
|
||||||
// build it, and only a name the mesh never heard of is refused as unknown. Several modules in one act
|
|
||||||
// are judged together (ADR 0207), so an act naming one not registered is not made in part.
|
|
||||||
if missing, err := notInCatalogue(ctx, open, modules); err != nil {
|
|
||||||
return "", err
|
|
||||||
} else if len(missing) > 0 {
|
|
||||||
if len(modules) == 1 {
|
|
||||||
return notRegistered(ctx, open, node, modules[0], opts)
|
|
||||||
}
|
|
||||||
var lines []string
|
|
||||||
for _, m := range missing {
|
|
||||||
where, err := whereIs(ctx, open.inventory, m, time.Now())
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
lines = append(lines, where.said)
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("%w: nothing was assigned, since modules assigned together are judged together "+
|
|
||||||
"(ADR 0207) and %s not registered:\n %s\n assign them together once each is registered, or each "+
|
|
||||||
"alone to keep it pending on its build", inventory.ErrNoSuchModule, strings.Join(missing, ", "),
|
|
||||||
strings.Join(lines, "\n "))
|
|
||||||
}
|
|
||||||
// **The one assignment refused for what the node lacks** (novox/hq ADR 0207). Everything else
|
|
||||||
// an assignment leaves unresolved is kept, because assignment is not an ordering; a module whose
|
|
||||||
// resources are applied through a seat nothing on the node holds is refused, because that order
|
|
||||||
// — the service manager, the package manager and the runtime before anything that installs,
|
|
||||||
// runs or contains — is the mesh's to keep. Several modules in one act are judged together, so
|
|
||||||
// holders that depend on each other go on in one command.
|
|
||||||
shelf, before, err := seatDependenciesOnAssign(ctx, open, node, modules)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// **Before the new assignment can unsettle a seat somebody holds only by being alone**
|
|
||||||
// (novox/hq 04-ISSUES/170): what the mesh derived so far is written down, and then the
|
|
||||||
// assignment resolves against a record rather than against a coincidence.
|
|
||||||
settled, err := recordDerivedHolders(ctx, open)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
var lines []string
|
|
||||||
var added []string
|
|
||||||
for _, module := range modules {
|
|
||||||
fresh, err := open.inventory.Assign(ctx, node, module)
|
|
||||||
if err != nil {
|
|
||||||
return strings.Join(lines, "\n"), err
|
|
||||||
}
|
|
||||||
if !fresh {
|
|
||||||
// Nothing changed, and saying "is assigned" would read as an action. One node runs one
|
|
||||||
// of each — the module's name is the assignment's identity (novox/hq ADR 0115).
|
|
||||||
lines = append(lines, fmt.Sprintf(
|
|
||||||
"%s already runs %s — one node runs one of each (ADR 0115); nothing changed", node, module))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
added = append(added, module)
|
|
||||||
lines = append(lines, fmt.Sprintf("%s is assigned %s", node, module))
|
|
||||||
}
|
|
||||||
if len(added) == 0 {
|
|
||||||
return strings.Join(lines, "\n"), nil
|
|
||||||
}
|
|
||||||
answer := strings.Join(lines, "\n")
|
|
||||||
for _, line := range settled {
|
|
||||||
answer += "\n " + line
|
|
||||||
}
|
|
||||||
// Its bus credential, in the same act (novox/hq issue 203): an assignment pushed before its
|
|
||||||
// credential exists delivers a process that cannot authenticate and crash-loops until somebody
|
|
||||||
// runs a second verb and a second push. Issued here when the module speaks on the bus and has
|
|
||||||
// no credential yet; kept when it has one, so re-assigning rotates nothing.
|
|
||||||
var issued []string
|
|
||||||
for _, module := range added {
|
|
||||||
if line := issueOnAssign(ctx, open, node, module); line != "" {
|
|
||||||
issued = append(issued, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// **The one resolution the rest of the answer is said from** (novox/hq issue 237), taken after
|
|
||||||
// the write: what this act changed about this node's unmet seat dependencies, and nothing else
|
|
||||||
// (novox/hq ADR 0207) — a dependency of a module just assigned, or one this assignment met; the
|
|
||||||
// rest of the node's list, and every other node's, is `status`'s — and whether the node resolves.
|
|
||||||
after := resolvedAfter(ctx, open, shelf, node)
|
|
||||||
for _, line := range unheldChangeTo(shelf, node, before, after.modules, after.unheld) {
|
|
||||||
answer += "\n " + line
|
|
||||||
}
|
|
||||||
for _, line := range issued {
|
|
||||||
answer += "\n " + line
|
|
||||||
}
|
|
||||||
plan, err := after.plan, after.err
|
|
||||||
if err != nil {
|
|
||||||
// Kept, and still refused. Both halves are the answer, and the rest of the mesh is still
|
|
||||||
// worth reporting: this machine's refusal is rarely the only consequence.
|
|
||||||
return answer + blockedElsewhere(ctx, open, node), err
|
|
||||||
}
|
|
||||||
// Kept, and cannot be hosted here. Said at once rather than discovered at push: a module whose
|
|
||||||
// capability the machine lacks is on the wrong machine, and the assignment records what a person
|
|
||||||
// meant while this line says it will not run until it moves. The rest of the node still pushes.
|
|
||||||
isAdded := map[string]bool{}
|
|
||||||
for _, m := range added {
|
|
||||||
isAdded[m] = true
|
|
||||||
}
|
|
||||||
for _, u := range plan.Unhostable {
|
|
||||||
if !isAdded[u.Module] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, c := range u.Missing {
|
|
||||||
answer += "\n but " + catalogue.WrongMachine(u.Module, c, node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return answer + fmt.Sprintf("\n run `push %s` to send it", node) +
|
|
||||||
blockedElsewhere(ctx, open, node), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// seatDependenciesOnAssign is the refusal ADR 0207 makes at assignment, or nothing, with the
|
|
||||||
// catalogue and the node's assignments it was judged against. Modules already assigned are not new
|
|
||||||
// and are not judged again.
|
|
||||||
func seatDependenciesOnAssign(ctx context.Context, open *stores, node string, modules []string) (
|
|
||||||
map[string]catalogue.Manifest, []string, error) {
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
assigned, err := open.inventory.Assigned(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
already := map[string]bool{}
|
|
||||||
for _, a := range assigned {
|
|
||||||
already[a] = true
|
|
||||||
}
|
|
||||||
var adding []string
|
|
||||||
for _, m := range modules {
|
|
||||||
if !already[m] {
|
|
||||||
adding = append(adding, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// The lines AssignRefusal says beside an assignment it lets through are said by unheldChange
|
|
||||||
// with everything else this act changed, so they are not said twice.
|
|
||||||
if _, err := catalogue.AssignRefusal(shelf, node, assigned, adding); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
// Two modules declaring one path or unit, or one package present and absent, is refused before anything
|
|
||||||
// is recorded (novox/hq ADR 0210, ADR 0303, 04-ISSUES/235): kept, the node would not resolve until one came
|
|
||||||
// off again. Two modules declaring one package present is not a collision (ADR 0303, issue 628).
|
|
||||||
if err := catalogue.CollisionRefusal(shelf, node, assigned, adding); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return shelf, assigned, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// unassign takes modules off a node. What they leave behind is the host's business: a directory
|
|
||||||
// holding anything the mesh did not put there is kept (novox/hq ADR 0030).
|
|
||||||
//
|
|
||||||
// It reports the rest of the mesh for the same reason assign does, and more sharply: taking a
|
|
||||||
// module off one machine is the ordinary way to stop providing something to another, and nothing
|
|
||||||
// about the command's own output would ever have said so.
|
|
||||||
//
|
|
||||||
// **Refused when it takes away the last holder of a seat a module left on the node depends on**
|
|
||||||
// (novox/hq ADR 0207) — the other side of refusing that module's assignment without one. Several
|
|
||||||
// modules in one act are judged together, so a holder and its dependents come off in one command.
|
|
||||||
func unassign(ctx context.Context, open *stores, node string, modules ...string) (string, error) {
|
|
||||||
if err := refusedMovingTheController(modules); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if len(modules) == 0 {
|
|
||||||
return "", fmt.Errorf("unassign %s names no module", node)
|
|
||||||
}
|
|
||||||
// One resolution after the write, as assign's answer (novox/hq issue 237).
|
|
||||||
ctx = leftOutSaidBy(ctx, io.Discard)
|
|
||||||
ctx, release, err := holdNodes(ctx, open, []string{node})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer release()
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
assigned, err := open.inventory.Assigned(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// Every one checked before any is taken off, so a refusal leaves the node as it was.
|
|
||||||
runs := map[string]bool{}
|
|
||||||
for _, a := range assigned {
|
|
||||||
runs[a] = true
|
|
||||||
}
|
|
||||||
// A module only pending on the node (novox/hq issue 325) is withdrawn, when it is the act's one module:
|
|
||||||
// that is the undo of an assignment kept while its build runs.
|
|
||||||
if len(modules) == 1 && !runs[modules[0]] {
|
|
||||||
said, withdrawn, err := withdrawPending(ctx, open.inventory, node, modules[0])
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if withdrawn {
|
|
||||||
return said, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, module := range modules {
|
|
||||||
if !runs[module] {
|
|
||||||
return "", fmt.Errorf("%s is not assigned to %s", module, node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := catalogue.UnassignRefusal(shelf, node, assigned, modules); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, module := range modules {
|
|
||||||
if err := open.inventory.Unassign(ctx, node, module); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
answer := fmt.Sprintf("%s no longer runs %s — run `push %s` to make it so",
|
|
||||||
node, strings.Join(modules, ", "), node)
|
|
||||||
after := resolvedAfter(ctx, open, shelf, node)
|
|
||||||
for _, line := range unheldChangeTo(shelf, node, assigned, after.modules, after.unheld) {
|
|
||||||
answer += "\n " + line
|
|
||||||
}
|
|
||||||
// Whether the node itself still resolves, from the same resolution: before, only the rest of the
|
|
||||||
// mesh's reads said it, on standard error and once per other machine.
|
|
||||||
if after.err != nil {
|
|
||||||
if unresolvable(after.err) {
|
|
||||||
answer += fmt.Sprintf("\n but %s does not resolve now, so nothing will be sent to it:", node)
|
|
||||||
} else {
|
|
||||||
answer += fmt.Sprintf("\n but whether %s still resolves could not be read:", node)
|
|
||||||
}
|
|
||||||
for _, line := range strings.Split(strings.TrimRight(after.err.Error(), "\n"), "\n") {
|
|
||||||
answer += "\n " + strings.TrimSpace(line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// What it leaves behind that is irreplaceable is kept and retired, never removed (novox/hq ADR 0233).
|
|
||||||
for _, line := range keptOnUnassign(ctx, open.inventory, node, modules) {
|
|
||||||
answer += "\n " + line
|
|
||||||
}
|
|
||||||
return answer + blockedElsewhere(ctx, open, node), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// afterAct is the one resolution of a node an act's answer is said from, taken after its write
|
|
||||||
// (novox/hq issue 237): the plan or why there is none, the modules it has on the node, and its unmet
|
|
||||||
// seat dependencies.
|
|
||||||
type afterAct struct {
|
|
||||||
plan catalogue.Resolution
|
|
||||||
err error
|
|
||||||
modules []string
|
|
||||||
unheld []catalogue.Unheld
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolvedAfter resolves the node as `plan` does, once, after the act's write. When the node resolves,
|
|
||||||
// its modules and unmet seat dependencies are the resolution's — the closure, so a holder pulled in by
|
|
||||||
// a requirement counts as `plan` counts it. When it does not, they are judged over the assignments read
|
|
||||||
// after the write, which can only find more unmet than the closure would: a seat said held there is
|
|
||||||
// held in the closure too.
|
|
||||||
func resolvedAfter(ctx context.Context, open *stores, shelf map[string]catalogue.Manifest, node string) afterAct {
|
|
||||||
plan, _, err := resolveNode(ctx, open, node)
|
|
||||||
if err == nil {
|
|
||||||
out := afterAct{plan: plan, unheld: plan.Unheld}
|
|
||||||
for _, m := range plan.Modules {
|
|
||||||
out.modules = append(out.modules, m.Module)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
out := afterAct{err: err}
|
|
||||||
assigned, readErr := open.inventory.Assigned(ctx, node)
|
|
||||||
if readErr != nil {
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
out.modules, out.unheld = assigned, unheldOver(shelf, node, assigned)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// splitModules is a surface's one `module` field as the modules it names: several, comma-separated,
|
|
||||||
// are one act (novox/hq ADR 0207), so the holders that depend on each other go on together from the
|
|
||||||
// command API and the controller seat's verbs as they do from the command line.
|
|
||||||
func splitModules(field string) []string {
|
|
||||||
var out []string
|
|
||||||
for _, m := range strings.Split(field, ",") {
|
|
||||||
if m = strings.TrimSpace(m); m != "" {
|
|
||||||
out = append(out, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// blockedElsewhere is every OTHER machine that cannot be worked out as things now stand.
|
|
||||||
//
|
|
||||||
// **The state, not the cause.** Saying "this assignment broke laptop" would mean resolving the
|
|
||||||
// whole mesh twice and would still be a guess about which of several changes did it; saying
|
|
||||||
// "laptop cannot be worked out, and here is what it says" is true, is what somebody has to fix,
|
|
||||||
// and cannot mislead. The machine that was just changed is left out because its own refusal is
|
|
||||||
// already the answer beside this one.
|
|
||||||
//
|
|
||||||
// Nothing here can fail the act it reports on. A mesh that cannot be read is worth saying and is
|
|
||||||
// not a reason to claim the assignment did not happen — it did.
|
|
||||||
func blockedElsewhere(ctx context.Context, open *stores, except string) string {
|
|
||||||
// One reading of the mesh for every machine this looks at.
|
|
||||||
ctx = withInputCache(ctx)
|
|
||||||
nodes, err := open.inventory.Nodes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "\n\nThe rest of the mesh could not be checked: " + err.Error()
|
|
||||||
}
|
|
||||||
blocked := map[string]string{}
|
|
||||||
for _, n := range nodes {
|
|
||||||
if n.Name == except {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, _, err := resolveNode(ctx, open, n.Name); err != nil {
|
|
||||||
blocked[n.Name] = err.Error()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(blocked) == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
names := make([]string, 0, len(blocked))
|
|
||||||
for name := range blocked {
|
|
||||||
names = append(names, name)
|
|
||||||
}
|
|
||||||
sort.Strings(names)
|
|
||||||
|
|
||||||
var out strings.Builder
|
|
||||||
fmt.Fprintf(&out, "\n\nAND %d other machine(s) cannot be worked out as things stand, so "+
|
|
||||||
"nothing will be sent to them:\n", len(names))
|
|
||||||
for _, name := range names {
|
|
||||||
fmt.Fprintf(&out, " %s\n", name)
|
|
||||||
for _, line := range strings.Split(strings.TrimRight(blocked[name], "\n"), "\n") {
|
|
||||||
fmt.Fprintf(&out, " %s\n", strings.TrimSpace(line))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out.WriteString("\nThis may or may not be what just changed — it is what is true now.")
|
|
||||||
return out.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// issueOnAssign gives a newly assigned module its bus credential, the way `module issue` does, and
|
|
||||||
// says what it did in one line. Nothing for a module that declares no broker secret and runs no
|
|
||||||
// provisioner client; nothing for one whose user is already minted (a credential is rotated on
|
|
||||||
// purpose, never by re-assigning); and when the bus cannot be reached from here, the line names the
|
|
||||||
// verb and the push that would refuse the module until it is run — never a silent placeholder (novox/hq issue 203).
|
|
||||||
func issueOnAssign(ctx context.Context, open *stores, node, module string) string {
|
|
||||||
inv := open.inventory
|
|
||||||
shelf, err := inv.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
m, known := shelf[module]
|
|
||||||
if !known || mayIssue(m) != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
user := broker.Principal{Kind: broker.KindModule, Node: node, Module: module}.Username()
|
|
||||||
_, reads := m.OwnSecrets["broker"]
|
|
||||||
if !reads && !runsTheProvisionerClient(m) {
|
|
||||||
// A module whose bus accounts are its processes' alone has none of its own: only the processes that
|
|
||||||
// are not issued yet are issued (issue 1226).
|
|
||||||
return processAccountsOnAssign(ctx, inv, m, node)
|
|
||||||
}
|
|
||||||
if !reads {
|
|
||||||
// A module running the provisioner client alone is minted the client's account (issue 917).
|
|
||||||
user = broker.Principal{Kind: broker.KindProvisioner, Node: node, Module: module}.Username()
|
|
||||||
}
|
|
||||||
if _, minted, err := inv.BusUserHash(ctx, user); err != nil {
|
|
||||||
return ""
|
|
||||||
} else if minted {
|
|
||||||
// Its own account stands and is kept; a process it gained is not issued yet (issue 1226).
|
|
||||||
return processAccountsOnAssign(ctx, inv, m, node)
|
|
||||||
}
|
|
||||||
busAddress, err := broker.BusAddress()
|
|
||||||
if err == nil {
|
|
||||||
err = issueOnTheNewBus(ctx, inv, m, node, busAddress)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Sprintf("its bus credential is not issued (%v): `module issue %s --node %s` first — "+
|
|
||||||
"`push %s` refuses to send %s until it is", err, module, node, node, module)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("its bus credential is issued and sealed to %s, and arrives with the push", node)
|
|
||||||
}
|
|
||||||
|
|
||||||
// processAccountsOnAssign issues the process accounts of an assigned module not issued yet, and says what it did.
|
|
||||||
func processAccountsOnAssign(ctx context.Context, inv *inventory.Inventory, m catalogue.Manifest, node string) string {
|
|
||||||
line, err := issueMissingProcessAccounts(ctx, inv, m, node)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Sprintf("its process accounts are not issued (%v): `module issue %s --node %s` first — "+
|
|
||||||
"`push %s` refuses to send %s until it is", err, m.Module, node, node, m.Module)
|
|
||||||
}
|
|
||||||
return line
|
|
||||||
}
|
|
||||||
|
|
||||||
// refusedMovingTheController refuses assigning or unassigning the controller's module through a verb (review of
|
|
||||||
// novox/hq ADR 0272): the node it is assigned to is the control-node, and the control-node's operator account is
|
|
||||||
// the controller's terminal, so whoever moves the module chooses the terminal. At the terminal alone, as every
|
|
||||||
// change that says who may change what.
|
|
||||||
func refusedMovingTheController(modules []string) error {
|
|
||||||
verb, through := throughAVerb()
|
|
||||||
if !through || !slices.Contains(modules, controllerModule) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return fmt.Errorf("%s is assigned and unassigned at the controller's terminal only (mesh-cli on the control-node), "+
|
|
||||||
"never through a verb (this came through %q): the node it runs on is the control-node, whose operator is the "+
|
|
||||||
"terminal (novox/hq ADR 0272). Nothing was assigned", controllerModule, verb)
|
|
||||||
}
|
|
||||||
@@ -1,135 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
)
|
|
||||||
|
|
||||||
// What an assignment says about the machines it was not about.
|
|
||||||
|
|
||||||
// **An assignment that blocks another machine says so.**
|
|
||||||
//
|
|
||||||
// The shape found on a four-node raise: a module offering a mesh-scoped provision stops offering it
|
|
||||||
// the moment its own node stops resolving, so an assignment to the provider's machine silently took
|
|
||||||
// a provision away from every consumer elsewhere. Those consumers were then told *nothing in this
|
|
||||||
// mesh provides it*, naming as the remedy a module that was already assigned — which read, on the
|
|
||||||
// way back, as a version bump breaking provider recognition. It was neither the version nor the
|
|
||||||
// provider: it was one machine's set of assignments, and nothing said so.
|
|
||||||
//
|
|
||||||
// novox/hq 04-ISSUES/017 names the general shape — an action succeeding into a state its own verify
|
|
||||||
// rejects, because the action's test is not the test the verify uses. `assign` tested one node; the
|
|
||||||
// verify is resolution over all of them.
|
|
||||||
func TestAnAssignmentThatBlocksAnotherMachineSaysWhichAndWhy(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
// A provider on the hub and a consumer on the other machine, both resolving.
|
|
||||||
register(t, open, catalogue.Manifest{Module: "step-ca", Version: "1",
|
|
||||||
Provides: []catalogue.Offer{{Name: "acme-ca", Scope: catalogue.ScopeMesh}},
|
|
||||||
Serves: map[string]map[string]any{"acme-ca": {"path": "/acme/directory"}}})
|
|
||||||
register(t, open, catalogue.Manifest{Module: "route-proxy", Version: "1",
|
|
||||||
Requires: []string{"acme-ca"}})
|
|
||||||
if _, err := assign(ctx, open, "anchor", "step-ca"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
said, err := assign(ctx, open, "laptop", "route-proxy")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("a mesh that should resolve did not: %v\n%s", err, said)
|
|
||||||
}
|
|
||||||
if strings.Contains(said, "cannot be worked out") {
|
|
||||||
t.Fatalf("a well mesh was reported as blocked:\n%s", said)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now break the hub's own set, with nothing but the command a person has.
|
|
||||||
one, two := rivals()
|
|
||||||
register(t, open, one)
|
|
||||||
register(t, open, two)
|
|
||||||
if _, err := assign(ctx, open, "anchor", "rival-one"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
said, err = assign(ctx, open, "anchor", "rival-two")
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("an assignment that makes its own node incoherent was not reported at all")
|
|
||||||
}
|
|
||||||
|
|
||||||
// The node's own refusal is the error, as it always was. What is new is that the machines this
|
|
||||||
// just took a provision away from are named in the same breath.
|
|
||||||
if !strings.Contains(said, "laptop") {
|
|
||||||
t.Fatalf("the machine this blocked is not named:\n%s\n\n%v", said, err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(said, "acme-ca") {
|
|
||||||
t.Fatalf("what laptop is now missing is not said:\n%s", said)
|
|
||||||
}
|
|
||||||
if !strings.Contains(said, "cannot be worked out as things stand") {
|
|
||||||
t.Fatalf("the report does not say what state the mesh is in:\n%s", said)
|
|
||||||
}
|
|
||||||
// And the assignment is kept: it is what a person meant, and assignment is not an ordering.
|
|
||||||
assigned, err := open.inventory.Assigned(ctx, "anchor")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !contains(assigned, "rival-two") {
|
|
||||||
t.Fatalf("the assignment was not kept: %v", assigned)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A consumer assigned before its provider is refused for itself and kept, because assignment is not
|
|
||||||
// an ordering — refusing the first half of a pair would make the order somebody types two commands
|
|
||||||
// in part of the mesh's rules.
|
|
||||||
func TestAConsumerAssignedBeforeItsProviderIsStillAssigned(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, catalogue.Manifest{Module: "route-proxy", Version: "1",
|
|
||||||
Requires: []string{"acme-ca"}})
|
|
||||||
|
|
||||||
said, err := assign(ctx, open, "laptop", "route-proxy")
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("a consumer with nothing to consume resolved:\n%s", said)
|
|
||||||
}
|
|
||||||
assigned, err := open.inventory.Assigned(ctx, "laptop")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !contains(assigned, "route-proxy") {
|
|
||||||
t.Fatalf("assignment became an ordering: %v", assigned)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unassigning is the ordinary way to stop providing something to another machine, and it reports
|
|
||||||
// the same way for the same reason.
|
|
||||||
func TestUnassigningAProviderNamesWhoIsNowBlocked(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, catalogue.Manifest{Module: "step-ca", Version: "1",
|
|
||||||
Provides: []catalogue.Offer{{Name: "acme-ca", Scope: catalogue.ScopeMesh}},
|
|
||||||
Serves: map[string]map[string]any{"acme-ca": {"path": "/acme/directory"}}})
|
|
||||||
register(t, open, catalogue.Manifest{Module: "route-proxy", Version: "1",
|
|
||||||
Requires: []string{"acme-ca"}})
|
|
||||||
if _, err := assign(ctx, open, "anchor", "step-ca"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := assign(ctx, open, "laptop", "route-proxy"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
said, err := unassign(ctx, open, "anchor", "step-ca")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(said, "laptop") || !strings.Contains(said, "acme-ca") {
|
|
||||||
t.Fatalf("taking the provider away said nothing about who was consuming it:\n%s", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func contains(all []string, one string) bool {
|
|
||||||
for _, s := range all {
|
|
||||||
if s == one {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
@@ -1,228 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/licences"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTheRegistryTrustAndEveryImageFollowThePortTheNodeGaveTheStore(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, aStore())
|
|
||||||
if _, err := assign(ctx, open, "anchor", "distribution"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.SetSettings(ctx, "anchor", "distribution",
|
|
||||||
map[string]any{catalogue.PortsSetting: map[string]any{"5000": 5101}}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// A module the mesh built, recorded by digest and path, running on the other machine.
|
|
||||||
if err := open.inventory.RecordBuild(ctx, inventory.Build{
|
|
||||||
ID: "b1", Repository: "r", Module: "app", Commit: "abc",
|
|
||||||
Made: []inventory.Artifact{{Name: "server", Kind: "image",
|
|
||||||
Reference: catalogue.ArtifactStoreScheme + "app/server@" + aDigest}},
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
register(t, open, catalogue.Manifest{Module: "app", Version: "1",
|
|
||||||
Resources: []map[string]any{{"id": "server", "type": "container", "name": "mesh-app",
|
|
||||||
"image": catalogue.ArtifactStoreScheme + "app/server@" + aDigest}}})
|
|
||||||
if _, err := assign(ctx, open, "laptop", "app"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// The runtime's trust is the runtime's module's to write (novox/hq ADR 0222): a stand-in for it
|
|
||||||
// asks where this machine reaches the store, as the docker module does.
|
|
||||||
register(t, open, catalogue.Manifest{Module: "runtime", Version: "1",
|
|
||||||
Resources: []map[string]any{{"id": "daemon", "type": "file", "path": "/etc/docker/daemon.json",
|
|
||||||
"into": "json", "content": `{"insecure-registries": ["${seat:mesh-artifact-store:reach}"]}` + "\n"}}})
|
|
||||||
if _, err := assign(ctx, open, "laptop", "runtime"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
on := map[string]bool{"anchor": true, "laptop": true}
|
|
||||||
node, port, found, err := artifactStoreOnNetwork(ctx, open.inventory, on)
|
|
||||||
if err != nil || !found || node != "anchor" || port != "5101" {
|
|
||||||
t.Fatalf("the store is found on %q:%q (%v, %v); the node put it on 5101", node, port, found, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var trust string
|
|
||||||
for _, r := range composed(t, open, "laptop").Resources {
|
|
||||||
if r["path"] == "/etc/docker/daemon.json" {
|
|
||||||
trust, _ = r["content"].(string)
|
|
||||||
}
|
|
||||||
if r["id"] == "app.server" && r["image"] != "anchor.internal:5101/app/server@"+aDigest {
|
|
||||||
t.Errorf("the image the mesh built is fetched as %v", r["image"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !strings.Contains(trust, "anchor.internal:5101") || strings.Contains(trust, ":5000") {
|
|
||||||
t.Fatalf("the runtime is told to trust %q; the node put the store on 5101", trust)
|
|
||||||
}
|
|
||||||
|
|
||||||
// And a replay to the catalogue says where the store is now.
|
|
||||||
announced, err := following{open: open}.Announceable(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(announced) != 1 || announced[0].Made[0].Reference != "anchor.internal:5101/app/server@"+aDigest {
|
|
||||||
t.Fatalf("the replay announces %+v", announced)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheControlPlaneIsToldWhereTheNodePutTheStoreAndTheBroker(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
raw, err := os.ReadFile("../../module.json")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
m, err := catalogue.ParseManifest(raw)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
control, err := withSeatPorts(m).Resolve([]catalogue.Built{{Name: "controller", Kind: catalogue.ArtifactBundle,
|
|
||||||
Reference: "https://registry.example/mesh-controller/controller.tar.gz", Digest: aDigest}})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
register(t, open, control)
|
|
||||||
register(t, open, catalogue.Manifest{Module: "postgres", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: "mesh-store", Scope: catalogue.ScopeMesh}},
|
|
||||||
Listens: []catalogue.Listening{{Port: 5432, From: catalogue.FromMesh}},
|
|
||||||
Resources: []map[string]any{{"id": "server", "type": "container", "name": "mesh-store",
|
|
||||||
"ports": []any{"5432:5432"}, "image": "pg@" + aDigest}}})
|
|
||||||
register(t, open, catalogue.Manifest{Module: "lavinmq", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: "mesh-broker", Scope: catalogue.ScopeMesh}},
|
|
||||||
Listens: []catalogue.Listening{{Port: 5671, From: catalogue.FromMesh},
|
|
||||||
{Port: 5672, From: catalogue.FromMesh}},
|
|
||||||
Guards: []int{15672},
|
|
||||||
Resources: []map[string]any{{"id": "server", "type": "container", "name": "mesh-broker",
|
|
||||||
"ports": []any{"5671:5671", "5672:5672", "127.0.0.1:15672:15672"}, "image": "mq@" + aDigest}}})
|
|
||||||
// The control plane's own bus user is the installer's, seeded at genesis before the controller
|
|
||||||
// runs (SeedBusUser); without it a push now refuses the credential nobody issued (issue 203).
|
|
||||||
if err := open.inventory.SeedBusUser(ctx, inventory.BusUser{Username: "anchor.mesh-controller",
|
|
||||||
Kind: inventory.BusController, Node: "anchor", Module: "mesh-controller"}, "bootstrap"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := assign(ctx, open, "anchor", "mesh-controller"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// The store's module is registered and given its port, and NOT assigned: the state genesis
|
|
||||||
// leaves a given-port node in before the foundation is adopted as modules (04-ISSUES/085).
|
|
||||||
if err := open.inventory.SetSettings(ctx, "anchor", "postgres",
|
|
||||||
map[string]any{catalogue.PortsSetting: map[string]any{"5432": 6852}}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := assign(ctx, open, "anchor", "lavinmq"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.SetSettings(ctx, "anchor", "lavinmq",
|
|
||||||
map[string]any{catalogue.PortsSetting: map[string]any{"5672": 5679}}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var env map[string]any
|
|
||||||
for _, r := range composed(t, open, "anchor").Resources {
|
|
||||||
if r["id"] == "mesh-controller.controller" {
|
|
||||||
env, _ = r["env"].(map[string]any)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if env == nil {
|
|
||||||
t.Fatal("the control plane's process is not in its own node's declaration")
|
|
||||||
}
|
|
||||||
for key, want := range map[string]string{
|
|
||||||
"MESH_STORE_INVENTORY_PORT": "6852",
|
|
||||||
"MESH_STORE_IDENTITY_PORT": "6852",
|
|
||||||
"MESH_STORE_LICENCES_PORT": "6852",
|
|
||||||
"MESH_BROKER_AMQP_PORT": "5679",
|
|
||||||
// Neither given nor assigned by the mesh: the manifest's own number is NOT the answer,
|
|
||||||
// because the sealed value beside it carries the port genesis wrote (finding F3).
|
|
||||||
"MESH_BROKER_ADDRESS_PORT": "",
|
|
||||||
"MESH_BROKER_MANAGEMENT_PORT": "",
|
|
||||||
} {
|
|
||||||
if env[key] != want {
|
|
||||||
t.Errorf("the control plane is told %s=%v; the node says %q", key, env[key], want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func aLoneNode(t *testing.T) *stores {
|
|
||||||
t.Helper()
|
|
||||||
inventory.ForTest(t)
|
|
||||||
licences.ForTest(t)
|
|
||||||
open, err := openStores(t.Context())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(open.Close)
|
|
||||||
for _, m := range provided {
|
|
||||||
if err := open.inventory.Provide(t.Context(), m); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
record, err := open.inventory.AddNode(t.Context(), "anchor")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reported, _ := json.Marshal(map[string]any{"capabilities": []map[string]any{
|
|
||||||
{"name": "container-runtime", "present": true}}})
|
|
||||||
var profile map[string]any
|
|
||||||
_ = json.Unmarshal(reported, &profile)
|
|
||||||
if err := open.inventory.RecordProfile(t.Context(), record.ID, profile); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.RecordSealingKey(t.Context(), record.ID, aPublicKey(t)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return open
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOnANodeWithNoNetworkTheStoreIsReachedByLoopback(t *testing.T) {
|
|
||||||
open := aLoneNode(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, aStore())
|
|
||||||
register(t, open, catalogue.Manifest{Module: "builder", Version: "1",
|
|
||||||
Requires: []string{catalogue.ArtifactStoreProvision},
|
|
||||||
Resources: []map[string]any{{"id": "server", "type": "container", "name": "mesh-builder",
|
|
||||||
"image": "registry.example/mesh-builder@" + aDigest}}})
|
|
||||||
if err := open.inventory.RecordBuild(ctx, inventory.Build{
|
|
||||||
ID: "b1", Repository: "r", Module: "postgres", Commit: "abc",
|
|
||||||
Made: []inventory.Artifact{{Name: "runtime", Kind: "image",
|
|
||||||
Reference: catalogue.ArtifactStoreScheme + "postgres/runtime@" + aDigest}},
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
register(t, open, catalogue.Manifest{Module: "postgres", Version: "1",
|
|
||||||
Resources: []map[string]any{{"id": "runtime", "type": "container", "name": "mesh-postgres",
|
|
||||||
"image": catalogue.ArtifactStoreScheme + "postgres/runtime@" + aDigest}}})
|
|
||||||
for _, module := range []string{"distribution", "builder", "postgres"} {
|
|
||||||
if _, err := assign(ctx, open, "anchor", module); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := open.inventory.SetSettings(ctx, "anchor", "distribution",
|
|
||||||
map[string]any{catalogue.PortsSetting: map[string]any{"5000": 5100}}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var image any
|
|
||||||
for _, r := range composed(t, open, "anchor").Resources {
|
|
||||||
if r["id"] == "postgres.runtime" {
|
|
||||||
image = r["image"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if image != "127.0.0.1:5100/postgres/runtime@"+aDigest {
|
|
||||||
t.Fatalf("on the store's own node, off any network, the image is fetched as %v", image)
|
|
||||||
}
|
|
||||||
held := heldBy(ctx)
|
|
||||||
if got := held["postgres/runtime"]; got != "127.0.0.1:5100/postgres/runtime@"+aDigest {
|
|
||||||
t.Fatalf("a builder beside the store is handed the base %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// An address is read from the node's settings where it is used, never recorded with a port
|
|
||||||
// (novox/hq 04-ISSUES/102). Three readers did not follow the setting; each is held to it here.
|
|
||||||
|
|
||||||
var aDigest = "sha256:" + strings.Repeat("e", 64)
|
|
||||||
|
|
||||||
// **A build is recorded by digest and path**, whatever address the builder pushed to — what it
|
|
||||||
// made and what it stood on both; an image the module runs from elsewhere is left where it says.
|
|
||||||
func TestABuildIsRecordedWithoutTheStoresAddress(t *testing.T) {
|
|
||||||
manifest, _ := json.Marshal(map[string]any{
|
|
||||||
"module": "gitea", "version": "1",
|
|
||||||
"resources": []map[string]any{
|
|
||||||
{"id": "server", "type": "container", "name": "mesh-gitea",
|
|
||||||
"image": "anchor.internal:5100/gitea/server@" + aDigest},
|
|
||||||
{"id": "config", "type": "archive", "path": "/etc/gitea", "digest": aDigest,
|
|
||||||
"source": "http://anchor.internal:5100/v2/gitea/config/blobs/" + aDigest},
|
|
||||||
{"id": "cache", "type": "container", "name": "mesh-gitea-cache",
|
|
||||||
"image": "valkey/valkey@" + aDigest},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
kept := buildFrom(link.BuildResult{
|
|
||||||
ID: "b1", Repository: "https://forge.example/gitea.git", Commit: "abc", On: "laptop",
|
|
||||||
Manifest: manifest,
|
|
||||||
Made: []link.MadeArtifact{
|
|
||||||
{Name: "server", Kind: "image", Reference: "anchor.internal:5100/gitea/server@" + aDigest},
|
|
||||||
{Name: "config", Kind: "archive", Reference: "http://anchor.internal:5100/v2/gitea/config/blobs/" + aDigest},
|
|
||||||
},
|
|
||||||
Against: []string{"anchor.internal:5100/mesh-tools/runtime@" + aDigest},
|
|
||||||
})
|
|
||||||
if kept.Module != "gitea" {
|
|
||||||
t.Fatalf("the module was not read from the recorded manifest: %q", kept.Module)
|
|
||||||
}
|
|
||||||
if kept.Made[0].Reference != catalogue.ArtifactStoreScheme+"gitea/server@"+aDigest {
|
|
||||||
t.Errorf("the image is recorded as %q, address and all", kept.Made[0].Reference)
|
|
||||||
}
|
|
||||||
if kept.Made[1].Reference != catalogue.ArtifactStoreScheme+"gitea/config/blobs/"+aDigest {
|
|
||||||
t.Errorf("the archive is recorded as %q, address and all", kept.Made[1].Reference)
|
|
||||||
}
|
|
||||||
recorded, err := catalogue.ParseManifest(kept.Manifest)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := recorded.Resources[0]["image"]; got != catalogue.ArtifactStoreScheme+"gitea/server@"+aDigest {
|
|
||||||
t.Errorf("the recorded manifest's image is %v", got)
|
|
||||||
}
|
|
||||||
if got := recorded.Resources[1]["source"]; got != catalogue.ArtifactStoreScheme+"gitea/config/blobs/"+aDigest {
|
|
||||||
t.Errorf("the recorded manifest's archive is %v", got)
|
|
||||||
}
|
|
||||||
if got := recorded.Resources[2]["image"]; got != "valkey/valkey@"+aDigest {
|
|
||||||
t.Errorf("an image the build did not make was rewritten: %v", got)
|
|
||||||
}
|
|
||||||
if strings.Contains(string(kept.Manifest), "anchor.internal:5100") {
|
|
||||||
t.Errorf("the recorded manifest still carries the store's address:\n%s", kept.Manifest)
|
|
||||||
}
|
|
||||||
// What the build stood on is an edge to another module's artifact, and it is recorded the way
|
|
||||||
// that artifact is: by path in the store, so the edge still names the same thing when the
|
|
||||||
// store answers at another address.
|
|
||||||
if kept.Against[0] != catalogue.ArtifactStoreScheme+"mesh-tools/runtime@"+aDigest {
|
|
||||||
t.Errorf("what the build stood on was recorded by address: %v", kept.Against)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// aStore is a module offering the artifact store on 5000, published the long way as the
|
|
||||||
// distribution module does, so a node may be given another number for it.
|
|
||||||
func aStore() catalogue.Manifest {
|
|
||||||
return catalogue.Manifest{Module: "distribution", Version: "1",
|
|
||||||
Provides: []catalogue.Offer{{Name: catalogue.ArtifactStoreProvision, Scope: catalogue.ScopeMesh}},
|
|
||||||
Serves: map[string]map[string]any{catalogue.ArtifactStoreProvision: {"port": float64(5000)}},
|
|
||||||
Listens: []catalogue.Listening{{Port: 5000, From: catalogue.FromMesh}},
|
|
||||||
Resources: []map[string]any{{"id": "store", "type": "container", "name": "mesh-registry",
|
|
||||||
"ports": []any{"5000:5000"}, "image": "registry@" + aDigest}}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **The trust a machine writes for the store, and the address every built image is fetched
|
|
||||||
// through, say the port the node gave the store** — not the catalogue's number.
|
|
||||||
|
|
||||||
// **The control plane's own connections say the port the node gave the store and the broker.**
|
|
||||||
//
|
|
||||||
// Composed from the control plane's own manifest against a real inventory: the store's module is
|
|
||||||
// given 6852 on this node the way genesis or an operator gives it, and the control plane's
|
|
||||||
// process is told so beside the sealed connection genesis wrote.
|
|
||||||
|
|
||||||
// withSeatPorts is the control plane's manifest with the seat placeholders in its environment —
|
|
||||||
// added here until module.json carries them (the manifest lands one commit after the code that
|
|
||||||
// fills it, so a control plane one build behind never sees a placeholder it cannot fill).
|
|
||||||
func withSeatPorts(m catalogue.Manifest) catalogue.Manifest {
|
|
||||||
seatPorts := map[string]string{
|
|
||||||
"MESH_STORE_INVENTORY_PORT": "${seat:mesh-store:5432}",
|
|
||||||
"MESH_STORE_IDENTITY_PORT": "${seat:mesh-store:5432}",
|
|
||||||
"MESH_STORE_LICENCES_PORT": "${seat:mesh-store:5432}",
|
|
||||||
"MESH_BROKER_AMQP_PORT": "${seat:mesh-broker:5672}",
|
|
||||||
"MESH_BROKER_MANAGEMENT_PORT": "${seat:mesh-broker:15672}",
|
|
||||||
"MESH_BROKER_ADDRESS_PORT": "${seat:mesh-broker:5671}",
|
|
||||||
}
|
|
||||||
out := m
|
|
||||||
out.Resources = nil
|
|
||||||
for _, r := range m.Resources {
|
|
||||||
if r["type"] != "container" && r["type"] != "process" {
|
|
||||||
out.Resources = append(out.Resources, r)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
copied := map[string]any{}
|
|
||||||
for k, v := range r {
|
|
||||||
copied[k] = v
|
|
||||||
}
|
|
||||||
env := map[string]any{}
|
|
||||||
if had, ok := r["env"].(map[string]any); ok {
|
|
||||||
for k, v := range had {
|
|
||||||
env[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for k, v := range seatPorts {
|
|
||||||
if _, said := env[k]; !said {
|
|
||||||
env[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
copied["env"] = env
|
|
||||||
out.Resources = append(out.Resources, copied)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// aLoneNode is one capable machine with nothing placed on any network — the control-node during
|
|
||||||
// genesis, before the "network" step, which is after the store, the broker, the vault and the
|
|
||||||
// catalogue have each been built and pushed (finding F2).
|
|
||||||
|
|
||||||
// **Before the network exists, the store's own node reaches it by loopback** — never refused,
|
|
||||||
// never handed the scheme: a genesis pushes the store, the broker, the vault and the catalogue to
|
|
||||||
// a node on no network, and builds the catalogue on a base it must be able to pull.
|
|
||||||
@@ -1,643 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"net/http"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/overlay"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// novox/hq ADR 0100: taking a module is its cutover; converging a node is one act, previewed, and
|
|
||||||
// refused while a found container is held; returning to adopted keeps what was taken.
|
|
||||||
|
|
||||||
// anAdoptedAnchor is aMesh with the anchor adopted, running a served module the predecessor also
|
|
||||||
// runs and a module with only a file, and a filter module in the catalogue.
|
|
||||||
func anAdoptedAnchor(t *testing.T) (*stores, *[]string) {
|
|
||||||
t.Helper()
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, catalogue.Manifest{Module: "hello-web", Version: "1",
|
|
||||||
Listens: []catalogue.Listening{{Port: 8080, From: catalogue.FromEverywhere}},
|
|
||||||
Resources: []map[string]any{
|
|
||||||
{"id": "page", "type": "file", "path": "/var/lib/hello-web/index.html", "content": "hi"},
|
|
||||||
{"id": "server", "type": "container", "name": "hello-web", "ports": []any{"8080:80"},
|
|
||||||
"image": "registry.example/hello@sha256:" + strings.Repeat("a", 64)},
|
|
||||||
}})
|
|
||||||
register(t, open, catalogue.Manifest{Module: "notes", Version: "1",
|
|
||||||
Resources: []map[string]any{
|
|
||||||
{"id": "conf", "type": "file", "path": "/etc/notes.conf", "content": "x"},
|
|
||||||
}})
|
|
||||||
register(t, open, catalogue.Manifest{Module: "nftables", Version: "1",
|
|
||||||
Filtering: &catalogue.Filtering{Into: "/etc/nftables.conf"},
|
|
||||||
Resources: []map[string]any{{"id": "load", "type": "service", "unit": "mesh-filter.service",
|
|
||||||
"state": "running", "restart-on": []any{"filtering"}}}})
|
|
||||||
if err := open.inventory.SetAdopted(ctx, "anchor", true); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, m := range []string{"hello-web", "notes"} {
|
|
||||||
if _, err := assign(ctx, open, "anchor", m); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sent := &[]string{}
|
|
||||||
saved := sendNodes
|
|
||||||
sendNodes = func(_ context.Context, _ *stores, names []string) error {
|
|
||||||
*sent = append(*sent, names...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { sendNodes = saved })
|
|
||||||
return open, sent
|
|
||||||
}
|
|
||||||
|
|
||||||
// reportsHolding has the anchor report, on what it was last sent, holding what is given.
|
|
||||||
func reportsHolding(t *testing.T, open *stores, held ...link.Held) {
|
|
||||||
t.Helper()
|
|
||||||
reportsReaching(t, open, []link.Reach{
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 22, By: "sshd"},
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 8080, By: "hello-web", Published: true,
|
|
||||||
ContainerPort: 80},
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 5000, By: "predecessor-registry",
|
|
||||||
Published: true, ContainerPort: 5000},
|
|
||||||
{Protocol: "tcp", Address: "127.0.0.1", Port: 15672, By: "mesh-broker",
|
|
||||||
Published: true, ContainerPort: 15672},
|
|
||||||
}, held...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// reportsReaching has the anchor report, on what it was last sent, what is reachable on it and
|
|
||||||
// holding what is given.
|
|
||||||
func reportsReaching(t *testing.T, open *stores, reachable []link.Reach, held ...link.Held) {
|
|
||||||
t.Helper()
|
|
||||||
ctx := t.Context()
|
|
||||||
body, err := composed(t, open, "anchor").Body()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
record, err := open.inventory.NodeByName(ctx, "anchor")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.RecordSent(ctx, record.ID, digestOf(body), nil); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := (link.Enrolment{Inventory: open.inventory}).Heard(ctx, link.Report{
|
|
||||||
Node: "anchor", Applied: []string{"hello-web.x"}, Declared: digestOf(body),
|
|
||||||
Firewall: "ufw", Held: held, Reachable: reachable,
|
|
||||||
// A machine says which of its links face outside on every apply (novox/hq ADR 0140), and a
|
|
||||||
// filter is not sent to one that has not. The anchor reports one, as a real host does; this
|
|
||||||
// fixture lacked it from 2026-09-28 and nothing ran the test (issue 177).
|
|
||||||
Outward: []string{"eth0"},
|
|
||||||
// And what filters it (ADR 0168): its front end, the runtime's own, and a chain a
|
|
||||||
// predecessor left in the runtime's user chain.
|
|
||||||
Filters: anchorFilters,
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// anchorFilters is what the adopted anchor says filters it: ufw's chains, the runtime's, and a
|
|
||||||
// predecessor's chain the mesh did not write.
|
|
||||||
var anchorFilters = []link.Filter{
|
|
||||||
{Where: "table ip filter, chain ufw-reject-input", Owner: "found-firewall", Refuses: "reject"},
|
|
||||||
{Where: "table ip filter, chain DOCKER", Owner: "runtime", Refuses: `iifname != "docker0" oifname "docker0" drop`},
|
|
||||||
{Where: "table ip filter, chain DOCKER-USER", Owner: "other", Refuses: `iifname "eth0" tcp dport 6000 drop`},
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
heldContainer = link.Held{ID: "hello-web.server", Module: "hello-web", Kind: "container",
|
|
||||||
Target: "hello-web", Since: time.Now()}
|
|
||||||
heldFile = link.Held{ID: "notes.conf", Module: "notes", Kind: "file",
|
|
||||||
Target: "/etc/notes.conf", Since: time.Now(), Kept: "/var/lib/mesh-host/kept/abc-notes.conf"}
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTakingAModuleNotOnTheNodeIsRefused(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
if _, err := take(t.Context(), open, "anchor", "nftables", takeOptions{Yes: true}); !errors.Is(err, inventory.ErrNotAssigned) {
|
|
||||||
t.Fatalf("taking an unassigned module gave %v", err)
|
|
||||||
}
|
|
||||||
if _, err := take(t.Context(), open, "laptop", "network", takeOptions{Yes: true}); !errors.Is(err, inventory.ErrNotAdopted) {
|
|
||||||
t.Fatalf("taking on a converged node gave %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConvergingIsRefusedOnAPreviewThatWouldBeStale(t *testing.T) {
|
|
||||||
open, sent := anAdoptedAnchor(t)
|
|
||||||
_, err := converge(t.Context(), open, "anchor", false, "", "")
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "has not reported") {
|
|
||||||
t.Fatalf("a node that never reported was previewed: %v", err)
|
|
||||||
}
|
|
||||||
if len(*sent) != 0 {
|
|
||||||
t.Fatal("a refused converge sent something")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheFlipIsRefusedWhileAFoundContainerIsHeld(t *testing.T) {
|
|
||||||
open, sent := anAdoptedAnchor(t)
|
|
||||||
reportsHolding(t, open, heldContainer, heldFile)
|
|
||||||
_, err := converge(t.Context(), open, "anchor", true, "", "")
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "take anchor hello-web once its data has moved") {
|
|
||||||
t.Fatalf("the flip was not refused while hello-web holds its found container: %v", err)
|
|
||||||
}
|
|
||||||
if n, _ := open.inventory.NodeByName(t.Context(), "anchor"); !n.Adopted || len(*sent) != 0 {
|
|
||||||
t.Fatal("a refused flip changed something")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTakingNamesWhatItReplaces(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
reportsHolding(t, open, heldContainer, heldFile)
|
|
||||||
ctx := t.Context()
|
|
||||||
// The machine holds something for the module, so the take acts on the preview the operator
|
|
||||||
// saw and names its digest (novox/hq ADR 0163).
|
|
||||||
preview, err := take(ctx, open, "anchor", "hello-web", takeOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
saw := takeDigestIn(t, preview)
|
|
||||||
if !strings.Contains(preview, "nothing taken; `take anchor hello-web --yes "+saw+"`") {
|
|
||||||
t.Fatalf("the preview does not say how to act on it:\n%s", preview)
|
|
||||||
}
|
|
||||||
if taken, _ := open.inventory.Taken(ctx, "anchor"); len(taken) != 0 {
|
|
||||||
t.Fatal("the preview took something")
|
|
||||||
}
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err == nil || !strings.Contains(err.Error(), "name its digest") {
|
|
||||||
t.Fatalf("--yes without the digest was not refused: %v", err)
|
|
||||||
}
|
|
||||||
said, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true, Digest: saw})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(said, "container hello-web (hello-web.server)") ||
|
|
||||||
!strings.Contains(said, "push anchor") {
|
|
||||||
t.Fatalf("taking did not say what it replaces and what to run:\n%s", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// takeDigestIn is the digest a take's preview printed.
|
|
||||||
func takeDigestIn(t *testing.T, preview string) string {
|
|
||||||
t.Helper()
|
|
||||||
for _, line := range strings.Split(preview, "\n") {
|
|
||||||
if fields := strings.Fields(line); len(fields) == 2 && fields[0] == "preview" {
|
|
||||||
return fields[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Fatalf("the preview printed no digest:\n%s", preview)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// A take acts on the preview the operator saw, and on an account of the machine that is still the
|
|
||||||
// machine: a changed preview and a stale account refuse (novox/hq ADR 0163, rule 1).
|
|
||||||
func TestATakeIsRefusedOnAChangedPreviewOrAStaleAccount(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
reportsHolding(t, open, heldContainer, heldFile)
|
|
||||||
preview, err := take(ctx, open, "anchor", "hello-web", takeOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
saw := takeDigestIn(t, preview)
|
|
||||||
|
|
||||||
// The machine reports again, and what it holds has changed: the found container now carries
|
|
||||||
// facts the preview never showed.
|
|
||||||
changed := heldContainer
|
|
||||||
changed.Facts = map[string]any{"image": "hello:2", "declared_image": "registry.example/hello"}
|
|
||||||
reportsHolding(t, open, changed, heldFile)
|
|
||||||
_, err = take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true, Digest: saw})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "has changed since preview "+saw) {
|
|
||||||
t.Fatalf("a changed preview was acted on: %v", err)
|
|
||||||
}
|
|
||||||
if taken, _ := open.inventory.Taken(ctx, "anchor"); len(taken) != 0 {
|
|
||||||
t.Fatal("a refused take took something")
|
|
||||||
}
|
|
||||||
|
|
||||||
// And an account older than the flip allows.
|
|
||||||
preview, err = take(ctx, open, "anchor", "hello-web", takeOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
saw = takeDigestIn(t, preview)
|
|
||||||
saved := reportFreshFor
|
|
||||||
reportFreshFor = -time.Second
|
|
||||||
defer func() { reportFreshFor = saved }()
|
|
||||||
_, err = take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true, Digest: saw})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "a take acts only on an account newer than") {
|
|
||||||
t.Fatalf("a stale account was acted on: %v", err)
|
|
||||||
}
|
|
||||||
reportFreshFor = saved
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true, Digest: saw}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// A module the machine holds nothing for has nothing to compare: --yes alone suffices.
|
|
||||||
if _, err := take(ctx, open, "anchor", "notes", takeOptions{Yes: true}); err == nil {
|
|
||||||
// notes holds a file, so this one needs the digest too.
|
|
||||||
t.Fatal("notes holds a found file and was taken without a digest")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConvergingPreviewsThenChangesAndAdoptingKeepsWhatWasTaken(t *testing.T) {
|
|
||||||
open, sent := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reportsHolding(t, open, heldFile)
|
|
||||||
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, want := range []string{
|
|
||||||
"tcp/8080 hello-web (published, container port 80)",
|
|
||||||
"declared by hello-web (from anywhere)",
|
|
||||||
"WILL CLOSE — no module assigned here declares it",
|
|
||||||
// The anchor faces inward and is on the private network: the derived filter admits ssh
|
|
||||||
// from the mesh only.
|
|
||||||
"WILL CLOSE to everything outside the private network — ssh stays open from the mesh",
|
|
||||||
"notes\n replacing the found file /etc/notes.conf (notes.conf), original kept at",
|
|
||||||
"assigns nftables",
|
|
||||||
"the found firewall (ufw) is disabled, never flushed",
|
|
||||||
// What it routes is not a listener: said not to be previewed, and to be dropped.
|
|
||||||
"not previewed: traffic the machine routes that is not a published port",
|
|
||||||
"the derived filter drops it unless a module declares it",
|
|
||||||
} {
|
|
||||||
if !strings.Contains(preview, want) {
|
|
||||||
t.Errorf("the preview does not say %q:\n%s", want, preview)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if strings.Contains(preview, "15672") {
|
|
||||||
t.Errorf("a loopback listener is in the preview:\n%s", preview)
|
|
||||||
}
|
|
||||||
// What filters the machine now, and the fate of each (novox/hq ADR 0168): the predecessor's
|
|
||||||
// chain is named as not the mesh's and left, so the reader knows before the flip.
|
|
||||||
for _, want := range []string{
|
|
||||||
"table ip filter, chain DOCKER-USER",
|
|
||||||
"NOT THE MESH'S; left in force",
|
|
||||||
`iifname "eth0" tcp dport 6000 drop`,
|
|
||||||
"table ip filter, chain ufw-reject-input",
|
|
||||||
"the found firewall's; retired with it",
|
|
||||||
"the container runtime's own; left",
|
|
||||||
} {
|
|
||||||
if !strings.Contains(preview, want) {
|
|
||||||
t.Errorf("the preview does not say %q:\n%s", want, preview)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, line := range strings.Split(preview, "\n") {
|
|
||||||
if strings.Contains(line, "5000") && !strings.Contains(line, "WILL CLOSE") {
|
|
||||||
t.Errorf("an undeclared published port is not said to close: %s", line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if n, _ := open.inventory.NodeByName(ctx, "anchor"); !n.Adopted || len(*sent) != 0 {
|
|
||||||
t.Fatal("the preview changed something")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := converge(ctx, open, "anchor", true, digestIn(t, preview), ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
n, _ := open.inventory.NodeByName(ctx, "anchor")
|
|
||||||
if n.Adopted {
|
|
||||||
t.Fatal("converge --yes left the node adopted")
|
|
||||||
}
|
|
||||||
taken, _ := open.inventory.Taken(ctx, "anchor")
|
|
||||||
if !reflect.DeepEqual(taken, []string{"hello-web", overlay.Name, "nftables", "notes"}) {
|
|
||||||
t.Fatalf("the flip took %v", taken)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(*sent, []string{"anchor"}) {
|
|
||||||
t.Fatalf("the flip sent %v", *sent)
|
|
||||||
}
|
|
||||||
declared := composed(t, open, "anchor")
|
|
||||||
if declared.Adoption != nil {
|
|
||||||
t.Fatal("a converged node is still sent an adoption envelope")
|
|
||||||
}
|
|
||||||
if !hasID(declared.Resources, "nftables.filtering") {
|
|
||||||
t.Fatal("the converged node is not declared the mesh's filter")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := adopt(ctx, open, "anchor"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := adopt(ctx, open, "anchor"); err == nil {
|
|
||||||
t.Fatal("adopting an adopted node was not refused")
|
|
||||||
}
|
|
||||||
again, _ := open.inventory.Taken(ctx, "anchor")
|
|
||||||
if !reflect.DeepEqual(again, taken) {
|
|
||||||
t.Fatalf("returning to adopted lost what was taken: %v", again)
|
|
||||||
}
|
|
||||||
declared = composed(t, open, "anchor")
|
|
||||||
if declared.Adoption == nil || len(declared.Adoption.Untaken) != 0 {
|
|
||||||
t.Fatalf("returned to adopted, the envelope is %+v", declared.Adoption)
|
|
||||||
}
|
|
||||||
if hasID(declared.Resources, "nftables.filtering") || hasID(declared.Resources, "nftables.load") {
|
|
||||||
t.Fatal("returned to adopted, the mesh's filter is still declared")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func hasID(resources []map[string]any, id string) bool {
|
|
||||||
for _, r := range resources {
|
|
||||||
if r["id"] == id {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// The command API refuses a flip exactly as the command line does, in the same words.
|
|
||||||
func TestTheApiRefusesTheFlipInTheCommandLinesWords(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
reportsHolding(t, open, heldContainer)
|
|
||||||
_, direct := converge(t.Context(), open, "anchor", true, "", "")
|
|
||||||
if direct == nil {
|
|
||||||
t.Fatal("the flip was not refused")
|
|
||||||
}
|
|
||||||
got := asking(t, letIn{}, "POST", "/converge", `{"node":"anchor","yes":true}`)
|
|
||||||
if got.Code != http.StatusConflict {
|
|
||||||
t.Fatalf("got %d: %s", got.Code, got.Body.String())
|
|
||||||
}
|
|
||||||
var said map[string]any
|
|
||||||
if err := json.Unmarshal(got.Body.Bytes(), &said); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if said["refused"] != direct.Error() {
|
|
||||||
t.Fatalf("the API said %q and the command line %q", said["refused"], direct.Error())
|
|
||||||
}
|
|
||||||
if got := asking(t, letIn{}, "POST", "/take", `{"node":"anchor"}`); got.Code != http.StatusBadRequest {
|
|
||||||
t.Fatalf("a take naming no module got %d", got.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// novox/hq ADR 0100: the preview says what the derived filter does, rendered as it is rendered. On
|
|
||||||
// a machine that faces inward, ssh is admitted from the private network only, and a port a module
|
|
||||||
// admits from the mesh only closes to everything outside it: both are said to close.
|
|
||||||
func TestThePreviewSaysWhatNarrowsToTheMeshCloses(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, catalogue.Manifest{Module: "store", Version: "1",
|
|
||||||
Listens: []catalogue.Listening{{Port: 5432, From: catalogue.FromMesh}}})
|
|
||||||
if _, err := assign(ctx, open, "anchor", "store"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reportsReaching(t, open, []link.Reach{
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 22, By: "sshd"},
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 5432, By: "postgres"},
|
|
||||||
{Protocol: "tcp", Address: "10.77.0.1", Port: 5432, By: "postgres"},
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 8080, By: "hello-web", Published: true,
|
|
||||||
ContainerPort: 80},
|
|
||||||
})
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
lines := map[string]string{}
|
|
||||||
for _, line := range strings.Split(preview, "\n") {
|
|
||||||
fields := strings.Fields(line)
|
|
||||||
if len(fields) > 1 && strings.HasPrefix(fields[0], "tcp/") {
|
|
||||||
lines[fields[0]+" "+fields[1]] += line + "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if got := lines["tcp/22 sshd"]; !strings.Contains(got, "WILL CLOSE to everything outside "+
|
|
||||||
"the private network") {
|
|
||||||
t.Errorf("ssh on an inward machine is not said to close outside the mesh:\n%s", preview)
|
|
||||||
}
|
|
||||||
store := lines["tcp/5432 postgres"]
|
|
||||||
if strings.Count(store, "WILL CLOSE to everything outside the private network") != 1 ||
|
|
||||||
!strings.Contains(store, "declared by store (from mesh)") {
|
|
||||||
t.Errorf("the store's narrowing is not said to close, or its mesh address is:\n%s", preview)
|
|
||||||
}
|
|
||||||
if got := lines["tcp/8080 hello-web"]; !strings.Contains(got, "declared by hello-web (from anywhere)") {
|
|
||||||
t.Errorf("a port open to everywhere is not said to stay:\n%s", preview)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// digestIn is the digest a converge preview printed.
|
|
||||||
func digestIn(t *testing.T, preview string) string {
|
|
||||||
t.Helper()
|
|
||||||
for _, line := range strings.Split(preview, "\n") {
|
|
||||||
if fields := strings.Fields(line); len(fields) == 2 && fields[0] == "preview" {
|
|
||||||
return fields[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Fatalf("the preview printed no digest:\n%s", preview)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// The flip acts on the preview the operator saw: it names that preview's digest, and it is refused
|
|
||||||
// when the digest is missing, when anything the preview says has changed since, or when the node's
|
|
||||||
// account of itself is too old to be the machine as it is.
|
|
||||||
func TestTheFlipActsOnlyOnThePreviewTheOperatorSaw(t *testing.T) {
|
|
||||||
open, sent := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reportsHolding(t, open, heldFile)
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
saw := digestIn(t, preview)
|
|
||||||
if !strings.Contains(preview, "converge anchor --yes "+saw) {
|
|
||||||
t.Fatalf("the preview does not say how to act on it:\n%s", preview)
|
|
||||||
}
|
|
||||||
unchanged := func() {
|
|
||||||
t.Helper()
|
|
||||||
if n, _ := open.inventory.NodeByName(ctx, "anchor"); !n.Adopted || len(*sent) != 0 {
|
|
||||||
t.Fatal("a refused flip changed something")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := converge(ctx, open, "anchor", true, "", ""); err == nil ||
|
|
||||||
!strings.Contains(err.Error(), "--yes "+saw) {
|
|
||||||
t.Fatalf("a flip naming no preview was not refused: %v", err)
|
|
||||||
}
|
|
||||||
unchanged()
|
|
||||||
|
|
||||||
// Something new is reachable: the preview the operator saw is not what would happen.
|
|
||||||
reportsReaching(t, open, []link.Reach{
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 22, By: "sshd"},
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 8080, By: "hello-web", Published: true,
|
|
||||||
ContainerPort: 80},
|
|
||||||
{Protocol: "tcp", Address: "0.0.0.0", Port: 6000, By: "something-new"},
|
|
||||||
}, heldFile)
|
|
||||||
_, err = converge(ctx, open, "anchor", true, saw, "")
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "has changed since preview "+saw) {
|
|
||||||
t.Fatalf("a flip on a changed preview was not refused: %v", err)
|
|
||||||
}
|
|
||||||
unchanged()
|
|
||||||
|
|
||||||
// An account older than the flip trusts is refused, whatever digest is named.
|
|
||||||
again, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
saved := reportFreshFor
|
|
||||||
reportFreshFor = time.Nanosecond
|
|
||||||
_, err = converge(ctx, open, "anchor", true, digestIn(t, again), "")
|
|
||||||
reportFreshFor = saved
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "wait for its next report") {
|
|
||||||
t.Fatalf("a flip on an old account was not refused: %v", err)
|
|
||||||
}
|
|
||||||
unchanged()
|
|
||||||
|
|
||||||
if _, err := converge(ctx, open, "anchor", true, digestIn(t, again), ""); err != nil {
|
|
||||||
t.Fatalf("the flip on the preview just seen was refused: %v", err)
|
|
||||||
}
|
|
||||||
if n, _ := open.inventory.NodeByName(ctx, "anchor"); n.Adopted {
|
|
||||||
t.Fatal("the flip did not converge the node")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The flip holds the node from its checks to its send, so a push composed meanwhile waits and is
|
|
||||||
// composed after it — never sent after it with the node still adopted. And the send inside the
|
|
||||||
// flip is not made to wait on the flip's own hold.
|
|
||||||
func TestTheFlipHoldsTheNodeWhileItSends(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reportsHolding(t, open, heldFile)
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var heldElsewhere, heldHere error
|
|
||||||
sendNodes = func(inner context.Context, open *stores, names []string) error {
|
|
||||||
// Another caller cannot hold the node while the flip sends it.
|
|
||||||
waiting, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
|
||||||
defer cancel()
|
|
||||||
if release, err := open.inventory.HoldNodes(waiting, names); err == nil {
|
|
||||||
release()
|
|
||||||
heldElsewhere = errors.New("another caller held the node while the flip sent it")
|
|
||||||
}
|
|
||||||
// The flip's own send holds it without waiting on itself.
|
|
||||||
_, release, err := holdNodes(inner, open, names)
|
|
||||||
if err != nil {
|
|
||||||
heldHere = err
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
release()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if _, err := converge(ctx, open, "anchor", true, digestIn(t, preview), ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if heldElsewhere != nil || heldHere != nil {
|
|
||||||
t.Fatalf("%v %v", heldElsewhere, heldHere)
|
|
||||||
}
|
|
||||||
// And given back once it is done.
|
|
||||||
release, err := open.inventory.HoldNodes(ctx, []string{"anchor"})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
release()
|
|
||||||
}
|
|
||||||
|
|
||||||
// novox/hq ADR 0103: the preview names every kind of thing a module the flip takes holds as found,
|
|
||||||
// not only its files, and the digest changes when any of them does.
|
|
||||||
func TestThePreviewNamesEveryHeldKind(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
since := time.Now()
|
|
||||||
held := []link.Held{heldFile,
|
|
||||||
{ID: "notes.data", Module: "notes", Kind: "directory", Target: "/var/lib/notes", Since: since},
|
|
||||||
{ID: "notes.daemon", Module: "notes", Kind: "service", Target: "notes.service", Since: since},
|
|
||||||
{ID: "notes.seed", Module: "notes", Kind: "archive", Target: "/srv/notes", Since: since},
|
|
||||||
{ID: "notes.worker", Module: "notes", Kind: "process", Target: "notes-worker", Since: since},
|
|
||||||
{ID: "notes.account", Module: "notes", Kind: "user", Target: "notes", Since: since},
|
|
||||||
}
|
|
||||||
reportsHolding(t, open, held...)
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, want := range []string{
|
|
||||||
"replacing the found directory /var/lib/notes (notes.data)",
|
|
||||||
"replacing the found service notes.service (notes.daemon)",
|
|
||||||
"replacing the found archive /srv/notes (notes.seed)",
|
|
||||||
"replacing the found process notes-worker (notes.worker)",
|
|
||||||
"replacing the found user notes (notes.account)",
|
|
||||||
} {
|
|
||||||
if !strings.Contains(preview, want) {
|
|
||||||
t.Errorf("the preview does not say %q:\n%s", want, preview)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reportsHolding(t, open, held[:len(held)-1]...)
|
|
||||||
fewer, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if digestIn(t, fewer) == digestIn(t, preview) {
|
|
||||||
t.Fatal("the digest does not change with what is held")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// novox/hq ADR 0100: the flip acts on what the node said is reachable, so an account naming nothing
|
|
||||||
// is refused. Every machine that is up answers on ssh; nothing reported means the host's collectors
|
|
||||||
// did not, and flipping would close ports the preview never named.
|
|
||||||
func TestTheFlipIsRefusedOnAnAccountNamingNothingReachable(t *testing.T) {
|
|
||||||
open, sent := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// Only a loopback listener: nothing off the machine, which is the same silence.
|
|
||||||
reportsReaching(t, open, []link.Reach{
|
|
||||||
{Protocol: "tcp", Address: "127.0.0.1", Port: 15672, By: "mesh-broker"},
|
|
||||||
}, heldFile)
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(preview, "this account looks partial") {
|
|
||||||
t.Errorf("the preview does not mark a partial account:\n%s", preview)
|
|
||||||
}
|
|
||||||
_, err = converge(ctx, open, "anchor", true, digestIn(t, preview), "")
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "says nothing is reachable on it") {
|
|
||||||
t.Fatalf("the flip was not refused on an account naming nothing: %v", err)
|
|
||||||
}
|
|
||||||
if n, _ := open.inventory.NodeByName(ctx, "anchor"); !n.Adopted || len(*sent) != 0 {
|
|
||||||
t.Fatal("a refused flip changed something")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// An assignment cannot land between a preview and the flip that takes every module: assigning
|
|
||||||
// holds the node, so it waits for whatever is converging it.
|
|
||||||
func TestAssigningWaitsForWhateverIsConvergingTheNode(t *testing.T) {
|
|
||||||
open, _ := anAdoptedAnchor(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if _, err := take(ctx, open, "anchor", "hello-web", takeOptions{Yes: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reportsHolding(t, open, heldFile)
|
|
||||||
preview, err := converge(ctx, open, "anchor", false, "", "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
saved, savedPoll := inventory.HoldWaitFor, inventory.HoldPoll
|
|
||||||
inventory.HoldWaitFor, inventory.HoldPoll = time.Second, 50*time.Millisecond
|
|
||||||
defer func() { inventory.HoldWaitFor, inventory.HoldPoll = saved, savedPoll }()
|
|
||||||
|
|
||||||
var whileFlipping error
|
|
||||||
sendNodes = func(context.Context, *stores, []string) error {
|
|
||||||
_, whileFlipping = assign(ctx, open, "anchor", "notes")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if _, err := converge(ctx, open, "anchor", true, digestIn(t, preview), ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !errors.Is(whileFlipping, inventory.ErrNodeBusy) {
|
|
||||||
t.Fatalf("an assignment landed while the node was being converged: %v", whileFlipping)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,180 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **A refusal the bus said once is counted once** (novox/hq issue 402). The bus refused the controller
|
|
||||||
// one publish at 18:52:59 UTC on 2026-10-10 and never again, yet S9 looked at the one remembered
|
|
||||||
// advisory every half minute for the hour it is kept, and the condition said "observed 15 time(s), last
|
|
||||||
// 13s ago" with a refusal in its evidence every 30 seconds: two of us read it as a refusal going on and
|
|
||||||
// went looking for a missing grant. The condition counts what the bus said, with when it last said it,
|
|
||||||
// never how often the controller looked.
|
|
||||||
func TestARefusalSaidOnceIsCountedOnceHoweverOftenItIsLookedAt(t *testing.T) {
|
|
||||||
refused := time.Date(2026, 10, 10, 18, 52, 59, 0, time.UTC)
|
|
||||||
now := refused.Add(10 * time.Second)
|
|
||||||
store := conditions.NewInMemory()
|
|
||||||
k := conditions.NewKeeper(t.Context(), conditions.Options{Store: store, History: store,
|
|
||||||
Teller: &conditions.Told{}, Now: func() time.Time { return now }})
|
|
||||||
t.Cleanup(func() { k.Close(t.Context()) })
|
|
||||||
|
|
||||||
// The words the live condition bus.controller.refused carried on 2026-10-10.
|
|
||||||
said := "the bus refused the controller: nats: permissions violation: Permissions Violation for " +
|
|
||||||
`Publish to "mesh.seat.mesh-delivery.tool.times"`
|
|
||||||
advisory := link.Advisory{Kind: link.AdvisoryRefused, ID: "controller", Said: said,
|
|
||||||
First: refused, Last: refused, Count: 1}
|
|
||||||
look := func() conditions.Condition {
|
|
||||||
t.Helper()
|
|
||||||
f := &signalFacts{now: now, host: "novox", advisories: []link.Advisory{advisory}}
|
|
||||||
if err := k.Reconcile(t.Context(), "S9", watchAdvisories(f)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
c, found, err := conditions.ReadOne(t.Context(), store, "bus.controller.refused")
|
|
||||||
if err != nil || !found {
|
|
||||||
t.Fatalf("bus.controller.refused: found %v, %v", found, err)
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
var c conditions.Condition
|
|
||||||
for range 15 {
|
|
||||||
c = look()
|
|
||||||
now = now.Add(30 * time.Second)
|
|
||||||
}
|
|
||||||
if c.Observations != 1 || len(c.Evidence) != 1 {
|
|
||||||
t.Fatalf("one refusal, looked at 15 times, reads as observed %d time(s) with %d evidence lines: %+v",
|
|
||||||
c.Observations, len(c.Evidence), c.Evidence)
|
|
||||||
}
|
|
||||||
if !c.LastObserved.Equal(refused) || !c.Evidence[0].At.Equal(refused) {
|
|
||||||
t.Fatalf("last observed %s, evidence at %s: the refusal was at %s", c.LastObserved, c.Evidence[0].At, refused)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The bus refuses it three times more between two looks: the condition counts four refusals, says
|
|
||||||
// the newest, and adds one line of evidence for the look that saw them.
|
|
||||||
again := now.Add(-5 * time.Second)
|
|
||||||
advisory.Last, advisory.Count = again, 4
|
|
||||||
c = look()
|
|
||||||
if c.Observations != 4 || len(c.Evidence) != 2 || !c.LastObserved.Equal(again) || !c.Evidence[0].At.Equal(again) {
|
|
||||||
t.Fatalf("four refusals read as observed %d time(s), last %s, evidence %+v", c.Observations, c.LastObserved, c.Evidence)
|
|
||||||
}
|
|
||||||
if !strings.Contains(c.Summary, "(4 times since 18:52 UTC)") {
|
|
||||||
t.Fatalf("summary %q", c.Summary)
|
|
||||||
}
|
|
||||||
now = now.Add(30 * time.Second)
|
|
||||||
if c = look(); c.Observations != 4 || len(c.Evidence) != 2 {
|
|
||||||
t.Fatalf("a look with nothing new counted: observed %d time(s), evidence %+v", c.Observations, c.Evidence)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **A condition that looks at the present still counts its looks** (novox/hq issue 402 changes only
|
|
||||||
// what reads a record): a machine silent for three looks was observed three times, and says so.
|
|
||||||
func TestAConditionOfThePresentCountsItsLooks(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 10, 19, 0, 0, 0, time.UTC)
|
|
||||||
store := conditions.NewInMemory()
|
|
||||||
k := conditions.NewKeeper(t.Context(), conditions.Options{Store: store, History: store,
|
|
||||||
Teller: &conditions.Told{}, Now: func() time.Time { return now }})
|
|
||||||
t.Cleanup(func() { k.Close(t.Context()) })
|
|
||||||
var c conditions.Condition
|
|
||||||
for range 3 {
|
|
||||||
var err error
|
|
||||||
if c, err = k.Observe(t.Context(), conditions.Observation{Scope: conditions.ScopeMachine, ID: "ace",
|
|
||||||
Kind: "silent", Machine: "ace", Severity: conditions.Warning, Summary: "ace has not been heard from",
|
|
||||||
Source: "S1"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
now = now.Add(30 * time.Second)
|
|
||||||
}
|
|
||||||
if c.Observations != 3 || len(c.Evidence) != 3 || !c.LastObserved.Equal(now.Add(-30*time.Second)) {
|
|
||||||
t.Fatalf("observed %d time(s), %d evidence lines, last %s", c.Observations, len(c.Evidence), c.LastObserved)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **One key, one watcher** (novox/hq issue 440). A consumer's max-deliveries condition is said from
|
|
||||||
// DEAD_LETTERS while it holds a message the consumer gave up on (issue 330). A max-deliveries advisory
|
|
||||||
// without a token names the same key; read beside it, each look added an observation and a line of
|
|
||||||
// evidence through the dead-letter half, and the two summaries overwrote each other on every look. The
|
|
||||||
// dead-letter row alone says that key, and counts the messages given up on, never the looks.
|
|
||||||
func TestAHeldDeadLetterIsCountedByWhatWasGivenUpNotByTheLooks(t *testing.T) {
|
|
||||||
gaveUp := time.Date(2026, 10, 10, 21, 4, 0, 0, time.UTC)
|
|
||||||
now := gaveUp.Add(20 * time.Second)
|
|
||||||
store := conditions.NewInMemory()
|
|
||||||
k := conditions.NewKeeper(t.Context(), conditions.Options{Store: store, History: store,
|
|
||||||
Teller: &conditions.Told{}, Now: func() time.Time { return now }})
|
|
||||||
t.Cleanup(func() { k.Close(t.Context()) })
|
|
||||||
|
|
||||||
const key = "bus.EVENTS.media_sonarr.max-deliveries"
|
|
||||||
held := map[string]int{"EVENTS.media_sonarr": 1}
|
|
||||||
newest := map[string]time.Time{"EVENTS.media_sonarr": gaveUp}
|
|
||||||
advisory := link.Advisory{Kind: link.AdvisoryMaxDeliveries, ID: "EVENTS.media_sonarr", Stream: "EVENTS",
|
|
||||||
Consumer: "media_sonarr", Said: "sonarr on media handed message 7 over 5 times and gave up on it",
|
|
||||||
First: gaveUp, Last: gaveUp, Count: 1}
|
|
||||||
look := func() conditions.Condition {
|
|
||||||
t.Helper()
|
|
||||||
f := &signalFacts{now: now, host: "novox", advisories: []link.Advisory{advisory},
|
|
||||||
deadLetters: held, deadLettersNewest: newest}
|
|
||||||
if err := k.Reconcile(t.Context(), "S9", watchAdvisories(f)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
c, found, err := conditions.ReadOne(t.Context(), store, key)
|
|
||||||
if err != nil || !found {
|
|
||||||
t.Fatalf("%s: found %v, %v", key, found, err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(c.Summary, "dead-letters verb") {
|
|
||||||
t.Fatalf("the summary is not the dead-letter row's: %q", c.Summary)
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
var c conditions.Condition
|
|
||||||
for range 5 {
|
|
||||||
c = look()
|
|
||||||
now = now.Add(30 * time.Second)
|
|
||||||
}
|
|
||||||
if c.Observations != 1 || len(c.Evidence) != 1 || !c.LastObserved.Equal(gaveUp) {
|
|
||||||
t.Fatalf("one message given up on, looked at five times, reads as observed %d time(s), last %s, "+
|
|
||||||
"with %d evidence lines: %+v", c.Observations, c.LastObserved, len(c.Evidence), c.Evidence)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The consumer gives up on a second message between two looks: two given up on, one more line.
|
|
||||||
again := now.Add(-10 * time.Second)
|
|
||||||
held["EVENTS.media_sonarr"], newest["EVENTS.media_sonarr"] = 2, again
|
|
||||||
c = look()
|
|
||||||
if c.Observations != 2 || len(c.Evidence) != 2 || !c.LastObserved.Equal(again) {
|
|
||||||
t.Fatalf("two given up on read as observed %d time(s), last %s, evidence %+v", c.Observations,
|
|
||||||
c.LastObserved, c.Evidence)
|
|
||||||
}
|
|
||||||
now = now.Add(30 * time.Second)
|
|
||||||
if c = look(); c.Observations != 2 || len(c.Evidence) != 2 {
|
|
||||||
t.Fatalf("a look with nothing new counted: observed %d time(s), evidence %+v", c.Observations, c.Evidence)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The advisory still says the max-deliveries it alone knows: a message given up on that could not be
|
|
||||||
// kept, under a key of its own (issue 330), which issue 440's change leaves as it was.
|
|
||||||
func TestAMessageThatCouldNotBeKeptIsStillSaidFromTheAdvisory(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 10, 21, 4, 0, 0, time.UTC)
|
|
||||||
f := &signalFacts{now: at, host: "novox", advisories: []link.Advisory{{Kind: link.AdvisoryMaxDeliveries,
|
|
||||||
ID: "EVENTS.media_sonarr", Stream: "EVENTS", Consumer: "media_sonarr", Token: link.AdvisoryNotKept,
|
|
||||||
Said: "sonarr on media gave up on message 7, and it could not be kept", First: at, Last: at, Count: 1}}}
|
|
||||||
said := watchAdvisories(f)
|
|
||||||
if len(said) != 1 || said[0].Key() != "bus.EVENTS.media_sonarr.not-kept" {
|
|
||||||
t.Fatalf("%+v", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A max-deliveries advisory without a token names a consumer's dead-letter key, which only DEAD_LETTERS
|
|
||||||
// says (novox/hq issues 330 and 440): with nothing held, the advisory alone raises nothing.
|
|
||||||
func TestAMaxDeliveriesAdvisoryAloneRaisesNothing(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 10, 21, 4, 0, 0, time.UTC)
|
|
||||||
f := &signalFacts{now: at, host: "novox", advisories: []link.Advisory{{Kind: link.AdvisoryMaxDeliveries,
|
|
||||||
ID: "EVENTS.media_sonarr", Stream: "EVENTS", Consumer: "media_sonarr",
|
|
||||||
Said: "sonarr on media handed message 7 over 5 times and gave up on it", First: at, Last: at, Count: 1}}}
|
|
||||||
if said := watchAdvisories(f); len(said) != 0 {
|
|
||||||
t.Fatalf("said %+v", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,253 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// The account agents run as (novox/hq ADR 0266).
|
|
||||||
//
|
|
||||||
// On the control node every agent session ran as the operator's account, which may become root without a
|
|
||||||
// password — so any agent there could become root without a person, and ADR 0259 §8 (an answer from the
|
|
||||||
// operator's phone authorises an act) rests on that being false where the router and its channels run. The
|
|
||||||
// decision: a node may name an account its agents run as, of their own and without sudo; the operator's
|
|
||||||
// account keeps its sudo.
|
|
||||||
//
|
|
||||||
// - **Named at the controller's terminal only** (`node agent-account`): not a verb, not a setting, so no
|
|
||||||
// agent can name itself another account. Empty is a real state: agents run as the operator there.
|
|
||||||
// - **Composed** as `${machine:agent-account}`, `${machine:agent-home}` and `${machine:agent-root}` for the
|
|
||||||
// agent's module, which declares the account with `root: never`, and as MESH_AGENT_ACCOUNT and
|
|
||||||
// MESH_AGENT_HOME for its tools (catalogue/machine_into_files.go, runtime.go).
|
|
||||||
// - **Judged by the machine itself.** The node-engine reads, on every look, whether an account declared
|
|
||||||
// `root: never` can become root without a person — uid 0, a group that grants root, a sudo rule, a
|
|
||||||
// secret of the mesh it may read — and says it as the declaring module's account verdict, marked
|
|
||||||
// Root "never". agentConfined reads that verdict; the self-check (probe DA) raises
|
|
||||||
// `agent-can-become-root` while it does not hold, and `node show` says it.
|
|
||||||
//
|
|
||||||
// A verdict not given is never a pass: an engine older than the judging, an account not yet declared, a
|
|
||||||
// statement that says nothing of it — each is "not judged", and fails.
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// kindAgentCanBecomeRoot is the condition raised while a machine's agent account can become root without
|
|
||||||
// a person, or is not judged (ADR 0266).
|
|
||||||
const kindAgentCanBecomeRoot = "agent-can-become-root"
|
|
||||||
|
|
||||||
// agentAccountProbe is the self-check's probe of it.
|
|
||||||
const agentAccountProbe = "DA"
|
|
||||||
|
|
||||||
// agentConfined says whether the agents of a machine that names an agent account are confined: the
|
|
||||||
// machine's newest statement holds a healthy account verdict, judged for root, on that account. named is
|
|
||||||
// false for a machine that names none — agents run as the operator account there, which this does not
|
|
||||||
// judge. why is said either way, in the mesh's words; err is a store that could not be read.
|
|
||||||
//
|
|
||||||
// The one judgement: `node show`, the self-check, and ADR 0259's router honouring a verified sender read
|
|
||||||
// it here.
|
|
||||||
// now is the judging clock, threaded so a caller judging several things at one instant judges them all at it.
|
|
||||||
func agentConfined(ctx context.Context, inv *inventory.Inventory, node string, now time.Time) (named, confined bool, why string, err error) {
|
|
||||||
n, err := inv.NodeByName(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
return false, false, "", err
|
|
||||||
}
|
|
||||||
if n.AgentAccount == "" {
|
|
||||||
return false, false, fmt.Sprintf("%s names no agent account: agents run as the operator account (%s)",
|
|
||||||
node, orNoneKnown(n.Account)), nil
|
|
||||||
}
|
|
||||||
h, had, err := inv.HealthOf(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
return true, false, "", err
|
|
||||||
}
|
|
||||||
confined, why = judgedConfined(n.AgentAccount, h, had, now)
|
|
||||||
return true, confined, why, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// verdictFreshFor is how old the statement holding the verdict may be, by this controller's clock. A
|
|
||||||
// node-engine states its health on every change and at least every five minutes (mesh-host's sayAnyway), so
|
|
||||||
// three statements missed is a node-engine stopped, or a machine away. **A stale verdict is not a pass**: an
|
|
||||||
// agent that stopped the node-engine must not leave "cannot become root" standing from before.
|
|
||||||
const verdictFreshFor = 15 * time.Minute
|
|
||||||
|
|
||||||
// judgedConfined is the judgement over one statement, without the store, at now.
|
|
||||||
func judgedConfined(agent string, h inventory.NodeHealth, had bool, now time.Time) (bool, string) {
|
|
||||||
if !had {
|
|
||||||
return false, fmt.Sprintf("the agent account %s is not judged: the machine's node-engine has stated "+
|
|
||||||
"nothing of what it runs", agent)
|
|
||||||
}
|
|
||||||
if age := now.Sub(h.HeardAt); age > verdictFreshFor {
|
|
||||||
return false, fmt.Sprintf("the agent account %s is not judged: the machine's newest statement was heard at "+
|
|
||||||
"%s, more than %d minutes ago, and a verdict that old is not a verdict on now", agent,
|
|
||||||
h.HeardAt.Local().Format("2006-01-02 15:04"), int(verdictFreshFor.Minutes()))
|
|
||||||
}
|
|
||||||
if h.Contract < link.RootContract {
|
|
||||||
return false, fmt.Sprintf("the agent account %s is not judged: the machine's node-engine is older than "+
|
|
||||||
"the judging of an account's root (its statement's contract is %d, the judging is %d)",
|
|
||||||
agent, h.Contract, link.RootContract)
|
|
||||||
}
|
|
||||||
var verdicts []inventory.ResourceHealth
|
|
||||||
for _, r := range h.Resources {
|
|
||||||
if r.Kind == link.KindAccount && r.Target == agent && r.Root == link.RootNever {
|
|
||||||
verdicts = append(verdicts, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(verdicts) == 0 {
|
|
||||||
return false, fmt.Sprintf("the agent account %s is not judged: the machine's newest statement holds no "+
|
|
||||||
"verdict on it — no module there declares it never to become root, or the declaration naming it "+
|
|
||||||
"has not been applied", agent)
|
|
||||||
}
|
|
||||||
sort.Slice(verdicts, func(i, j int) bool {
|
|
||||||
return verdicts[i].Module+verdicts[i].Resource < verdicts[j].Module+verdicts[j].Resource
|
|
||||||
})
|
|
||||||
for _, v := range verdicts {
|
|
||||||
switch v.State {
|
|
||||||
case link.StateHealthy:
|
|
||||||
case link.StateUnhealthy:
|
|
||||||
// The engine's own words, which start with link.ReasonRoot when it found a way to root.
|
|
||||||
return false, fmt.Sprintf("the agent account %s %s (said by %s's %s)", agent,
|
|
||||||
orNoneKnown(v.Reason), v.Module, v.Resource)
|
|
||||||
default:
|
|
||||||
return false, fmt.Sprintf("the agent account %s is not judged: %s (%s's %s, %s)", agent,
|
|
||||||
orNoneKnown(v.Reason), v.Module, v.Resource, v.State)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true, fmt.Sprintf("the agent account %s cannot become root without a person (judged %s)", agent,
|
|
||||||
h.SaidAt.Local().Format("2006-01-02 15:04"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// searchQuietFor is how long the controller lets an agent account's verdict wait for the node-engine's setuid
|
|
||||||
// search before that is itself the urgent condition: the longest a search is expected to take (link.RootSearchQuiet,
|
|
||||||
// the engine's own value; novox/hq issue 361 — the search runs to completion, with no bound of its own), counted
|
|
||||||
// from when this controller first saw it waiting, never from the engine's start. Quiet raises nothing; it never
|
|
||||||
// makes the agent account confined, which only a healthy verdict from a complete, fresh search does.
|
|
||||||
const searchQuietFor = link.RootSearchQuiet
|
|
||||||
|
|
||||||
// The kinds of an agent account's verdict, for the quiet a search earns.
|
|
||||||
const (
|
|
||||||
verdictOther = iota // anything else: a stale statement, an older engine, no verdict, another unknown
|
|
||||||
verdictPending // waiting for the search, and otherwise healthy
|
|
||||||
verdictComplete // judged: healthy, or a way to root found
|
|
||||||
)
|
|
||||||
|
|
||||||
// rootVerdictKind reads a statement for the agent account (novox/hq ADR 0266): pending when every verdict on it
|
|
||||||
// is healthy or not judged yet because the engine's setuid search runs, at least one of them that; complete when
|
|
||||||
// every verdict is healthy or one found a way to root. The engine's own "since" is not read: it starts again at
|
|
||||||
// every restart of the engine.
|
|
||||||
func rootVerdictKind(agent string, h inventory.NodeHealth, had bool, now time.Time) int {
|
|
||||||
if !had || now.Sub(h.HeardAt) > verdictFreshFor || h.Contract < link.RootContract {
|
|
||||||
return verdictOther
|
|
||||||
}
|
|
||||||
pending, any := false, false
|
|
||||||
for _, r := range h.Resources {
|
|
||||||
if r.Kind != link.KindAccount || r.Target != agent || r.Root != link.RootNever {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
any = true
|
|
||||||
switch {
|
|
||||||
case r.State == link.StateHealthy:
|
|
||||||
case r.State == link.StateUnhealthy:
|
|
||||||
return verdictComplete
|
|
||||||
case r.State == link.StateUnknown && strings.HasPrefix(r.Reason, link.ReasonRootPending):
|
|
||||||
pending = true
|
|
||||||
default:
|
|
||||||
return verdictOther
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case !any:
|
|
||||||
return verdictOther
|
|
||||||
case pending:
|
|
||||||
return verdictPending
|
|
||||||
}
|
|
||||||
return verdictComplete
|
|
||||||
}
|
|
||||||
|
|
||||||
// searchStillRunning says the one thing keeping an agent account from being judged is the node-engine's setuid
|
|
||||||
// search, and that this controller first saw it waiting less than searchQuietFor ago — kept in the store, so a
|
|
||||||
// node-engine restarted in a loop does not keep it quiet. A complete verdict forgets when it began.
|
|
||||||
func searchStillRunning(ctx context.Context, inv *inventory.Inventory, node, agent string, h inventory.NodeHealth,
|
|
||||||
had bool, now time.Time) (bool, error) {
|
|
||||||
switch rootVerdictKind(agent, h, had, now) {
|
|
||||||
case verdictComplete:
|
|
||||||
return false, inv.RootSearchJudged(ctx, node)
|
|
||||||
case verdictPending:
|
|
||||||
since, err := inv.RootSearchPending(ctx, node, now)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return now.Sub(since) <= searchQuietFor, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// probeAgentAccounts is DA: every machine that names an agent account has it judged, on its node-engine's
|
|
||||||
// newest statement, unable to become root without a person (ADR 0266).
|
|
||||||
func probeAgentAccounts(ctx context.Context, d *doctor) ([]conditions.Observation, error) {
|
|
||||||
inv := d.open.inventory
|
|
||||||
nodes, err := inv.Nodes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, n := range nodes {
|
|
||||||
if n.AgentAccount == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
h, had, err := inv.HealthOf(ctx, n.Name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
now := time.Now()
|
|
||||||
quiet, err := searchStillRunning(ctx, inv, n.Name, n.AgentAccount, h, had, now)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
confined, why := judgedConfined(n.AgentAccount, h, had, now)
|
|
||||||
if confined {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Not judged yet only because the node-engine's search runs and no complete, fresh one judges: not the
|
|
||||||
// urgent condition after every restart. The agent is still not confined — ADR 0259's router reads
|
|
||||||
// agentConfined, not this — and `node show` still says not judged. Loud again once the search fails,
|
|
||||||
// waits past searchQuietFor, or the statement goes stale.
|
|
||||||
if quiet {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out = append(out, conditions.Observation{Scope: conditions.ScopeMachine, ID: n.Name, Token: "agent-root",
|
|
||||||
Machine: n.Name, Severity: conditions.Urgent,
|
|
||||||
Summary: fmt.Sprintf("on %s, %s (ADR 0266): an agent there may become root without a person, and "+
|
|
||||||
"no answer from a channel authorises an act there (ADR 0259 §8)", n.Name, why),
|
|
||||||
Said: why})
|
|
||||||
}
|
|
||||||
return sortedFound(out), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// agentAccountLines is what `node show` says of the account agents run as.
|
|
||||||
func agentAccountLines(ctx context.Context, inv *inventory.Inventory, n inventory.Node) []string {
|
|
||||||
if n.AgentAccount == "" {
|
|
||||||
return []string{fmt.Sprintf(" agents run as the operator account (%s); no agent account is named",
|
|
||||||
orNoneKnown(n.Account))}
|
|
||||||
}
|
|
||||||
_, confined, why, err := agentConfined(ctx, inv, n.Name, time.Now())
|
|
||||||
if err != nil {
|
|
||||||
return []string{fmt.Sprintf(" agents run as %s (home %s); whether it can become root could NOT be read: %v",
|
|
||||||
n.AgentAccount, n.AgentHome(), err)}
|
|
||||||
}
|
|
||||||
verdict := "CAN become root, or is not judged: " + why
|
|
||||||
if confined {
|
|
||||||
verdict = why
|
|
||||||
}
|
|
||||||
return []string{fmt.Sprintf(" agents run as %s (home %s)", n.AgentAccount, n.AgentHome()),
|
|
||||||
" " + verdict}
|
|
||||||
}
|
|
||||||
|
|
||||||
// orNoneKnown is a value, or that none is known.
|
|
||||||
func orNoneKnown(s string) string {
|
|
||||||
if strings.TrimSpace(s) == "" {
|
|
||||||
return "none known"
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
@@ -1,161 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
snapshot "git.novox.be/novox/mesh-sdk/go/facts"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
"github.com/novox/mesh-host/rootsearch"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTheSelfCheckSaysAnAgentAccountThatCanBecomeRoot(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
for _, n := range []string{"anchor", "laptop"} {
|
|
||||||
if _, err := inv.NodeByName(ctx, n); err != nil {
|
|
||||||
if _, err := inv.AddNode(ctx, n); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := inv.SetAccount(ctx, n, "ops", ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := inv.SetAgentAccount(ctx, "anchor", "agent", ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
d := &doctor{open: open}
|
|
||||||
found, err := probeAgentAccounts(ctx, d)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
found = onlyMachine(found, "anchor")
|
|
||||||
if len(found) != 1 || found[0].Machine != "anchor" || found[0].Severity != conditions.Urgent ||
|
|
||||||
!strings.Contains(found[0].Said, "not judged") {
|
|
||||||
t.Fatalf("a named agent account with no verdict: %+v", found)
|
|
||||||
}
|
|
||||||
w := plainWordings[kindAgentCanBecomeRoot](conditions.Observation{Kind: kindAgentCanBecomeRoot, Machine: "anchor"})
|
|
||||||
if w.Headline == "" || w.Needs == "" || w.Resolved == "" {
|
|
||||||
t.Errorf("the condition has no plain words: %+v", w)
|
|
||||||
}
|
|
||||||
|
|
||||||
healthy := inventory.ResourceHealth{Module: "claude-code", Resource: "claude-code.agent-account",
|
|
||||||
Kind: link.KindAccount, Target: "agent", State: link.StateHealthy, Root: link.RootNever, Account: "agent"}
|
|
||||||
if _, err := inv.RecordHealth(ctx, inventory.NodeHealth{Node: "anchor", Contract: link.RootContract,
|
|
||||||
SaidAt: time.Now(), HeardAt: time.Now(), Resources: []inventory.ResourceHealth{healthy}}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if found, err = probeAgentAccounts(ctx, d); err != nil || len(onlyMachine(found, "anchor")) != 0 {
|
|
||||||
t.Fatalf("a judged agent account still fails: %+v %v", found, err)
|
|
||||||
}
|
|
||||||
if named, confined, why, err := agentConfined(ctx, inv, "anchor", time.Now()); err != nil || !named || !confined {
|
|
||||||
t.Fatalf("agentConfined on anchor: %v %v %q %v", named, confined, why, err)
|
|
||||||
}
|
|
||||||
if named, _, why, err := agentConfined(ctx, inv, "laptop", time.Now()); err != nil || named ||
|
|
||||||
!strings.Contains(why, "operator account") {
|
|
||||||
t.Fatalf("agentConfined on a machine naming none: %v %q %v", named, why, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheFactsCarryTheAgentAccountAsAPseudonym(t *testing.T) {
|
|
||||||
open, _ := aMeshWithSecrets(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if err := open.inventory.SetAccount(ctx, "anchor", "keeper", ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.SetAgentAccount(ctx, "anchor", "warden", ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
f, err := gatherFacts(ctx, open, "2.11.17")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
body, _ := f.Encode()
|
|
||||||
if strings.Contains(string(body), "warden") {
|
|
||||||
t.Error("the agent account's name is in the snapshot")
|
|
||||||
}
|
|
||||||
m, ok := f.Machine(snapshot.Pseudonym("machine", "anchor"))
|
|
||||||
if !ok || m.AgentAccount != snapshot.Pseudonym("account", "warden") || m.Account == m.AgentAccount {
|
|
||||||
t.Fatalf("the anchor's agent account reads as %q (operator %q)", m.AgentAccount, m.Account)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestASearchStillRunningAfterARestartIsNotUrgent(t *testing.T) {
|
|
||||||
if searchQuietFor != rootsearch.Quiet {
|
|
||||||
t.Fatalf("the quiet is %s and the node-engine's %s: they are one value", searchQuietFor, rootsearch.Quiet)
|
|
||||||
}
|
|
||||||
// A daily search that finishes within the quiet never leaves the account unjudged between two of them.
|
|
||||||
if rootsearch.FreshFor < rootsearch.Every+rootsearch.Quiet {
|
|
||||||
t.Fatalf("a complete search judges for %s, less than a day's search (%s) and the quiet (%s)",
|
|
||||||
rootsearch.FreshFor, rootsearch.Every, rootsearch.Quiet)
|
|
||||||
}
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
if _, err := inv.NodeByName(ctx, "anchor"); err != nil {
|
|
||||||
if _, err := inv.AddNode(ctx, "anchor"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := inv.SetAgentAccount(ctx, "anchor", "agent", ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
d := &doctor{open: open}
|
|
||||||
say := func(state, reason string) []conditions.Observation {
|
|
||||||
t.Helper()
|
|
||||||
// The engine's since is always now: it was just restarted.
|
|
||||||
v := inventory.ResourceHealth{Module: "claude-code", Resource: "claude-code.agent-account",
|
|
||||||
Kind: link.KindAccount, Target: "agent", State: state, Reason: reason, Root: link.RootNever,
|
|
||||||
Account: "agent", Since: time.Now()}
|
|
||||||
if _, err := inv.RecordHealth(ctx, inventory.NodeHealth{Node: "anchor", Contract: link.RootContract,
|
|
||||||
SaidAt: time.Now(), HeardAt: time.Now(), Resources: []inventory.ResourceHealth{v}}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
found, err := probeAgentAccounts(ctx, d)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return onlyMachine(found, "anchor")
|
|
||||||
}
|
|
||||||
running := link.ReasonRootPending + ": the search for setuid programs, started at 19:21, has not finished yet"
|
|
||||||
healthy := func() {
|
|
||||||
t.Helper()
|
|
||||||
if found := say(link.StateHealthy, ""); len(found) != 0 {
|
|
||||||
t.Fatalf("a healthy verdict raised: %+v", found)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if found := say(link.StateUnknown, running); len(found) != 0 {
|
|
||||||
t.Fatalf("a search first seen now was raised: %+v", found)
|
|
||||||
}
|
|
||||||
if _, confined, why, _ := agentConfined(ctx, inv, "anchor", time.Now()); confined || !strings.Contains(why, "not judged") {
|
|
||||||
t.Fatalf("an account whose search runs was read as confined: %q", why)
|
|
||||||
}
|
|
||||||
// The engine restarted again and again, each statement's own since fresh: the controller's clock runs on.
|
|
||||||
if _, err := inv.RootSearchPending(ctx, "anchor", time.Now().Add(-searchQuietFor-time.Minute)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
healthy() // a complete verdict forgets when the waiting began …
|
|
||||||
if _, err := inv.RootSearchPending(ctx, "anchor", time.Now().Add(-searchQuietFor-time.Minute)); err != nil {
|
|
||||||
t.Fatal(err) // … and this restart loop began past the bound
|
|
||||||
}
|
|
||||||
if found := say(link.StateUnknown, running); len(found) != 1 || found[0].Severity != conditions.Urgent {
|
|
||||||
t.Fatalf("a search pending past the bound, by the controller's clock, was not urgent: %+v", found)
|
|
||||||
}
|
|
||||||
healthy()
|
|
||||||
incomplete := rootsearch.ReasonIncomplete + ": the search for setuid programs did not finish (last tried at " +
|
|
||||||
"19:23: timeout); it is tried again later"
|
|
||||||
if found := say(link.StateUnknown, incomplete); len(found) != 1 || found[0].Severity != conditions.Urgent {
|
|
||||||
t.Fatalf("a search that did not finish was not urgent: %+v", found)
|
|
||||||
}
|
|
||||||
if found := say(link.StateUnhealthy, link.ReasonRoot+": in the group docker; "+running); len(found) != 1 ||
|
|
||||||
found[0].Severity != conditions.Urgent {
|
|
||||||
t.Fatalf("a way to root found while the search runs was not urgent: %+v", found)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The agent account's judgement (novox/hq ADR 0266): confined only on a healthy account verdict judged for
|
|
||||||
// root, on the very account; every verdict not given — no statement, an older engine, no verdict on it, a
|
|
||||||
// verdict of unknown — is "not judged" and fails, never a pass.
|
|
||||||
func TestAnAgentAccountIsConfinedOnlyOnAHealthyVerdictJudgedForRoot(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 8, 19, 21, 0, 0, time.UTC)
|
|
||||||
verdict := func(target, root, state, reason string) inventory.ResourceHealth {
|
|
||||||
return inventory.ResourceHealth{Module: "claude-code", Resource: "claude-code.agent-account",
|
|
||||||
Kind: link.KindAccount, Target: target, State: state, Reason: reason, Root: root, Account: target}
|
|
||||||
}
|
|
||||||
statement := func(contract int, rs ...inventory.ResourceHealth) inventory.NodeHealth {
|
|
||||||
return inventory.NodeHealth{Node: "anchor", Contract: contract, SaidAt: at, HeardAt: at, Resources: rs}
|
|
||||||
}
|
|
||||||
for _, c := range []struct {
|
|
||||||
name string
|
|
||||||
h inventory.NodeHealth
|
|
||||||
had bool
|
|
||||||
confined bool
|
|
||||||
says string
|
|
||||||
}{
|
|
||||||
{"judged and unable", statement(link.RootContract, verdict("agent", link.RootNever, link.StateHealthy, "")),
|
|
||||||
true, true, "cannot become root without a person"},
|
|
||||||
{"judged and able", statement(link.RootContract, verdict("agent", link.RootNever, link.StateUnhealthy,
|
|
||||||
link.ReasonRoot+": in the group docker, which grants root")), true, false, "in the group docker"},
|
|
||||||
{"a verdict of unknown", statement(link.RootContract, verdict("agent", link.RootNever, link.StateUnknown,
|
|
||||||
"sudo could not be read")), true, false, "not judged"},
|
|
||||||
{"no statement", inventory.NodeHealth{}, false, false, "not judged"},
|
|
||||||
{"an older engine", statement(link.ReadinessContract, verdict("agent", "", link.StateHealthy, "")),
|
|
||||||
true, false, "older than the judging"},
|
|
||||||
{"a verdict on groups only", statement(link.RootContract, verdict("agent", "", link.StateHealthy, "")),
|
|
||||||
true, false, "no verdict on it"},
|
|
||||||
{"a verdict on another account", statement(link.RootContract, verdict("ops", link.RootNever, link.StateHealthy, "")),
|
|
||||||
true, false, "no verdict on it"},
|
|
||||||
} {
|
|
||||||
confined, why := judgedConfined("agent", c.h, c.had, at.Add(time.Minute))
|
|
||||||
if confined != c.confined || !strings.Contains(why, c.says) {
|
|
||||||
t.Errorf("%s: confined %v, %q; want %v saying %q", c.name, confined, why, c.confined, c.says)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// A verdict heard longer ago than the bound is no verdict: an agent that stopped the node-engine must not
|
|
||||||
// leave "healthy" standing.
|
|
||||||
fresh := statement(link.RootContract, verdict("agent", link.RootNever, link.StateHealthy, ""))
|
|
||||||
if ok, _ := judgedConfined("agent", fresh, true, at.Add(verdictFreshFor)); !ok {
|
|
||||||
t.Error("a verdict exactly at the bound is still one")
|
|
||||||
}
|
|
||||||
if ok, why := judgedConfined("agent", fresh, true, at.Add(verdictFreshFor+time.Second)); ok ||
|
|
||||||
!strings.Contains(why, "not judged") {
|
|
||||||
t.Errorf("a stale healthy verdict passed: %q", why)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DA raises an urgent condition, with plain words, on a machine whose agent account is not judged unable to
|
|
||||||
// become root; a machine that names none is not its to judge.
|
|
||||||
|
|
||||||
func TestTheAgentRootWordsArePlain(t *testing.T) {
|
|
||||||
w := plainWordings[kindAgentCanBecomeRoot](conditions.Observation{Kind: kindAgentCanBecomeRoot, Machine: "anchor"})
|
|
||||||
if why, ok := conditions.PlainWords(w, "anchor"); !ok {
|
|
||||||
t.Fatalf("not plain: %s: %+v", why, w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func onlyMachine(obs []conditions.Observation, machine string) []conditions.Observation {
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, o := range obs {
|
|
||||||
if o.Machine == machine {
|
|
||||||
out = append(out, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// The snapshot a merge check composes from carries the agent account as a pseudonym (novox/hq ADR 0266),
|
|
||||||
// so a change is judged against machines that name one, and the name never leaves.
|
|
||||||
|
|
||||||
// No verb runs a `node` command that sets something: through the generic `command` verb, `node account`,
|
|
||||||
// `node agent-account` and every other `node` subcommand but list and show are refused, naming the terminal.
|
|
||||||
func TestNoVerbSetsANodesAccounts(t *testing.T) {
|
|
||||||
for _, line := range []string{
|
|
||||||
"node agent-account novox --clear",
|
|
||||||
"node agent-account novox ops",
|
|
||||||
"node account novox agent",
|
|
||||||
"node account novox",
|
|
||||||
"node add intruder",
|
|
||||||
"node public-domain novox --clear",
|
|
||||||
"node",
|
|
||||||
"node frobnicate",
|
|
||||||
} {
|
|
||||||
argv, err := argvFor("command", map[string]any{"command": line})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "controller's terminal") ||
|
|
||||||
!strings.Contains(err.Error(), "ADR 0266") {
|
|
||||||
t.Errorf("%q ran as %v (%v); want a refusal naming the terminal", line, argv, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, line := range []string{"node show novox", "node list --json", "status --json"} {
|
|
||||||
if _, err := argvFor("command", map[string]any{"command": line}); err != nil {
|
|
||||||
t.Errorf("%q, a read, was refused: %v", line, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := terminalOnly([]string{"node", "account", "a", "b"}); err == nil {
|
|
||||||
t.Error("the refusal is not only the command verb's")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Naming the agent account is the controller's terminal's alone: the `node` verb only shows.
|
|
||||||
func TestTheNodeVerbOnlyShows(t *testing.T) {
|
|
||||||
argv, err := argvFor("node", map[string]any{"node": "anchor"})
|
|
||||||
if err != nil || strings.Join(argv, " ") != "node show anchor" {
|
|
||||||
t.Fatalf("the node verb runs %v (%v)", argv, err)
|
|
||||||
}
|
|
||||||
for _, v := range catalogue.ControllerVerbs {
|
|
||||||
if strings.Contains(v.Name, "agent") {
|
|
||||||
t.Errorf("a verb %q may name the agent account", v.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Until a complete search for setuid programs judges — none since the node-engine's state was kept, or the last
|
|
||||||
// older than the engine lets one judge — the agent account is not judged, and the search runs to its end with no
|
|
||||||
// bound (novox/hq issue 361). DA does not raise that as urgent within searchQuietFor, counted from when this
|
|
||||||
// controller first saw it waiting — never from the engine's own "since", which a restart resets, so an engine
|
|
||||||
// restarted in a loop does not keep it quiet. The account is still not confined, and `node show` still says not
|
|
||||||
// judged; a search that failed, or a way to root found, is urgent at once.
|
|
||||||
@@ -1,186 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The command API: what the mesh can be asked to do, over a network.
|
|
||||||
//
|
|
||||||
// **An adapter and nothing else** (novox/hq ADR 0035). Every route here calls the same function
|
|
||||||
// the command line calls, so a refusal is the same refusal in the same words. Nothing is decided
|
|
||||||
// in this file — the moment it validates something the command line does not, the mesh has two
|
|
||||||
// answers to one question.
|
|
||||||
//
|
|
||||||
// **It refuses everything unless it was told how to know who is asking.** The board is published
|
|
||||||
// on a public name, and this is what stands behind it: an unauthenticated command surface reachable
|
|
||||||
// from the internet is authority over the mesh handed to whoever finds it. So there is no
|
|
||||||
// permissive default and no flag that removes the check — a mesh that has not been told how to
|
|
||||||
// authenticate serves nothing, loudly.
|
|
||||||
//
|
|
||||||
// The intended authenticator is an OAuth2 provider (ADR 0035), which is an ordinary module. Until
|
|
||||||
// one is configured this refuses, which is the correct behaviour rather than a placeholder: a
|
|
||||||
// surface that worked without authentication would be one somebody left running.
|
|
||||||
func apiCommand(ctx context.Context, args []string) error {
|
|
||||||
set := flag.NewFlagSet("api", flag.ContinueOnError)
|
|
||||||
listen := set.String("listen", "127.0.0.1:8081", "where to serve it")
|
|
||||||
issuer := set.String("issuer", "",
|
|
||||||
"the OAuth2 issuer whose tokens this accepts; without it, nothing is served")
|
|
||||||
if _, err := parseAround(set, args); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(*issuer) == "" {
|
|
||||||
// Refused at start rather than per request, so it is discovered by whoever ran it rather
|
|
||||||
// than by whoever finds it.
|
|
||||||
return errors.New(
|
|
||||||
"--issuer is not set, and this serves commands rather than pages: it will not run " +
|
|
||||||
"without being told whose tokens to believe. An OAuth2 provider is an ordinary " +
|
|
||||||
"module (novox/hq ADR 0035)")
|
|
||||||
}
|
|
||||||
|
|
||||||
server := &http.Server{
|
|
||||||
Addr: *listen,
|
|
||||||
ReadHeaderTimeout: 10 * time.Second,
|
|
||||||
Handler: commands(mustAuthenticate(*issuer)),
|
|
||||||
}
|
|
||||||
fmt.Printf("the command API is on http://%s\n", *listen)
|
|
||||||
fmt.Printf(" it accepts tokens from %s and refuses everything else\n", *issuer)
|
|
||||||
fmt.Printf(" every route calls what the command line calls\n")
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
closing, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
_ = server.Shutdown(closing)
|
|
||||||
}()
|
|
||||||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Authenticator says whether a request may act, and as whom.
|
|
||||||
//
|
|
||||||
// An interface so the check can be driven by a test without an identity provider, and so the one
|
|
||||||
// real implementation is the only thing that has to be right.
|
|
||||||
type Authenticator interface {
|
|
||||||
// Who returns the subject a request is acting as, or an error naming why it may not.
|
|
||||||
Who(r *http.Request) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// mustAuthenticate is the real one: a bearer token from the configured issuer.
|
|
||||||
//
|
|
||||||
// **Not yet verifying the signature**, and it says so rather than pretending. Verification needs
|
|
||||||
// the issuer's keys, which needs an identity provider to exist — so this refuses every request
|
|
||||||
// until that is built, which is the same answer as having no API at all and is honest about why.
|
|
||||||
func mustAuthenticate(issuer string) Authenticator { return notYet{issuer: issuer} }
|
|
||||||
|
|
||||||
type notYet struct{ issuer string }
|
|
||||||
|
|
||||||
func (n notYet) Who(*http.Request) (string, error) {
|
|
||||||
return "", fmt.Errorf(
|
|
||||||
"this mesh has no way to verify a token from %s yet: the identity provider is a module "+
|
|
||||||
"and none is running. Use the command line, which authenticates through nothing "+
|
|
||||||
"because it is already behind the machine's own login", n.issuer)
|
|
||||||
}
|
|
||||||
|
|
||||||
// commands is the routing, separate so a test can drive it without a listener.
|
|
||||||
func commands(who Authenticator) http.Handler {
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
|
|
||||||
mux.HandleFunc("POST /assign", acting(who, true, func(ctx context.Context, open *stores, in request) (string, error) {
|
|
||||||
return assign(ctx, open, in.Node, splitModules(in.Module)...)
|
|
||||||
}))
|
|
||||||
mux.HandleFunc("POST /unassign", acting(who, true, func(ctx context.Context, open *stores, in request) (string, error) {
|
|
||||||
return unassign(ctx, open, in.Node, splitModules(in.Module)...)
|
|
||||||
}))
|
|
||||||
// Adoption (novox/hq ADR 0100): the same acts as `take`, `converge` and `adopt`.
|
|
||||||
mux.HandleFunc("POST /take", acting(who, true, func(ctx context.Context, open *stores, in request) (string, error) {
|
|
||||||
return take(ctx, open, in.Node, in.Module, takeOptions{Yes: in.Yes, Digest: in.Digest})
|
|
||||||
}))
|
|
||||||
mux.HandleFunc("POST /converge", acting(who, false, func(ctx context.Context, open *stores, in request) (string, error) {
|
|
||||||
return converge(ctx, open, in.Node, in.Yes, in.Digest, in.Filter)
|
|
||||||
}))
|
|
||||||
mux.HandleFunc("POST /adopt", acting(who, false, func(ctx context.Context, open *stores, in request) (string, error) {
|
|
||||||
return adopt(ctx, open, in.Node)
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Anything else is said plainly, because a command surface answering 404 to a verb somebody
|
|
||||||
// expected is indistinguishable from one that is down.
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
refuse(w, http.StatusNotFound, fmt.Errorf(
|
|
||||||
"%s %s is not something this mesh can be asked; it accepts POST /assign, "+
|
|
||||||
"POST /unassign, POST /take, POST /converge and POST /adopt", r.Method, r.URL.Path))
|
|
||||||
})
|
|
||||||
return mux
|
|
||||||
}
|
|
||||||
|
|
||||||
type request struct {
|
|
||||||
Node string `json:"node"`
|
|
||||||
Module string `json:"module"`
|
|
||||||
// Yes, Digest and Filter are converge's and take's: do it rather than preview it, the digest
|
|
||||||
// of the preview it acts on, and (converge) which module loads the mesh's filter.
|
|
||||||
Yes bool `json:"yes,omitempty"`
|
|
||||||
Digest string `json:"digest,omitempty"`
|
|
||||||
Filter string `json:"filter,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// acting is the shape every route shares: authenticate, read, act, answer.
|
|
||||||
func acting(
|
|
||||||
who Authenticator,
|
|
||||||
needsModule bool,
|
|
||||||
do func(context.Context, *stores, request) (string, error),
|
|
||||||
) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if _, err := who.Who(r); err != nil {
|
|
||||||
refuse(w, http.StatusUnauthorized, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var in request
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
||||||
refuse(w, http.StatusBadRequest, fmt.Errorf("this is not a request this understands: %w", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if in.Node == "" || (needsModule && in.Module == "") {
|
|
||||||
if needsModule {
|
|
||||||
refuse(w, http.StatusBadRequest, errors.New(`both "node" and "module" are needed`))
|
|
||||||
} else {
|
|
||||||
refuse(w, http.StatusBadRequest, errors.New(`"node" is needed`))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
open, err := openStores(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
refuse(w, http.StatusServiceUnavailable, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
|
|
||||||
said, err := do(r.Context(), open, in)
|
|
||||||
if err != nil {
|
|
||||||
// **The refusal the command line would have given, unchanged.** Carrying `said` with
|
|
||||||
// it matters: an assignment that was kept and still does not resolve is two facts,
|
|
||||||
// and dropping either makes the answer wrong.
|
|
||||||
answer(w, http.StatusConflict, map[string]any{"said": said, "refused": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
answer(w, http.StatusOK, map[string]any{"said": said})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func answer(w http.ResponseWriter, status int, body map[string]any) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(status)
|
|
||||||
_ = json.NewEncoder(w).Encode(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func refuse(w http.ResponseWriter, status int, err error) {
|
|
||||||
answer(w, status, map[string]any{"refused": err.Error()})
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type letIn struct{}
|
|
||||||
|
|
||||||
func (letIn) Who(*http.Request) (string, error) { return "somebody", nil }
|
|
||||||
|
|
||||||
func asking(t *testing.T, who Authenticator, method, path, body string) *httptest.ResponseRecorder {
|
|
||||||
t.Helper()
|
|
||||||
recorded := httptest.NewRecorder()
|
|
||||||
commands(who).ServeHTTP(recorded, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
||||||
return recorded
|
|
||||||
}
|
|
||||||
|
|
||||||
// **Nothing is served to somebody the mesh cannot identify**, and that is the default rather than
|
|
||||||
// a setting. The board this stands behind is published on a public name (novox/hq 11-a-board), so
|
|
||||||
// an unauthenticated command surface is authority over the mesh handed to whoever finds it.
|
|
||||||
func TestAnUnauthenticatedRequestIsRefused(t *testing.T) {
|
|
||||||
got := asking(t, mustAuthenticate("https://identity.example/realms/mesh"),
|
|
||||||
"POST", "/assign", `{"node":"anchor","module":"umami"}`)
|
|
||||||
if got.Code != http.StatusUnauthorized {
|
|
||||||
t.Fatalf("an unidentified caller got %d", got.Code)
|
|
||||||
}
|
|
||||||
// And it says what to do instead, because the answer is not "give up".
|
|
||||||
if !strings.Contains(got.Body.String(), "command line") {
|
|
||||||
t.Errorf("the refusal does not say what still works: %s", got.Body.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The API refuses to start at all without being told whose tokens to believe.
|
|
||||||
//
|
|
||||||
// At start rather than per request, so it is found by whoever ran it rather than by whoever
|
|
||||||
// finds it.
|
|
||||||
func TestTheApiWillNotRunWithoutAnIssuer(t *testing.T) {
|
|
||||||
err := apiCommand(context.Background(), []string{})
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("it served commands without being told who may give them")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "issuer") {
|
|
||||||
t.Errorf("the refusal does not name what is missing: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A request missing what it acts on is refused before anything is opened.
|
|
||||||
func TestARequestThatNamesNothingIsRefused(t *testing.T) {
|
|
||||||
// **The exact refusal, not merely "not accepted".** Asserting non-200 passes even when the
|
|
||||||
// request got as far as opening a store and failing there, which proves nothing about whether
|
|
||||||
// anything was checked — that is what the first version of this test did.
|
|
||||||
for _, body := range []string{`{}`, `{"node":"anchor"}`, `{"module":"umami"}`, `not json`} {
|
|
||||||
got := asking(t, letIn{}, "POST", "/assign", body)
|
|
||||||
if got.Code != http.StatusBadRequest {
|
|
||||||
t.Errorf("%s got %d, and a request naming nothing is a bad request", body, got.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A verb nobody implemented is said plainly. A command surface answering 404 to something
|
|
||||||
// somebody expected is indistinguishable from one that is down.
|
|
||||||
func TestAnUnknownRouteSaysWhatIsAccepted(t *testing.T) {
|
|
||||||
got := asking(t, letIn{}, "POST", "/rotate", `{}`)
|
|
||||||
if got.Code != http.StatusNotFound {
|
|
||||||
t.Fatalf("got %d", got.Code)
|
|
||||||
}
|
|
||||||
var said map[string]any
|
|
||||||
_ = json.Unmarshal(got.Body.Bytes(), &said)
|
|
||||||
if !strings.Contains(said["refused"].(string), "/assign") {
|
|
||||||
t.Errorf("it does not say what it does accept: %v", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// kindApplyFailing is S21's kind: a machine whose declarations have been refused, or applied only in part,
|
|
||||||
// for longer than one apply interval (novox/hq issue 233).
|
|
||||||
const kindApplyFailing = "apply-failing"
|
|
||||||
|
|
||||||
// applyEvery is how often a node-engine applies what it was last told when nothing new arrives: its
|
|
||||||
// ReconcileEvery. A failure still there after one more of them is not one the next apply clears.
|
|
||||||
const applyEvery = 5 * time.Minute
|
|
||||||
|
|
||||||
// packageManagerKey is how a node-engine names a package manager that does not answer among what failed
|
|
||||||
// (mesh-host's probe.go): "the package manager (pacman)".
|
|
||||||
const packageManagerKey = "the package manager ("
|
|
||||||
|
|
||||||
// watchApplyFailing is S21: **a machine that does not apply what it was sent is said by the mesh, not only
|
|
||||||
// to someone who asks for `status`** (novox/hq issue 233, issue 187's family). A workstation refused every
|
|
||||||
// declaration for an hour and a half because its package manager did not answer, and the mesh's only word
|
|
||||||
// on it was a line in `status`. Raised once the machine's first report of this failure is older than one apply
|
|
||||||
// interval **while the machine is heard** — judged by time, not by how many reports said it: a node-engine
|
|
||||||
// says a re-apply only when how it went changed, so the same failure is one report however long it lasts.
|
|
||||||
// Names the refusal's or the failure's words; cleared by the clean apply the node-engine then says. Not while
|
|
||||||
// the machine is silent, asleep or not known: S1 says silent, and asleep is not lost.
|
|
||||||
func watchApplyFailing(f *signalFacts) []conditions.Observation {
|
|
||||||
heard := map[string]bool{}
|
|
||||||
for _, m := range f.machines {
|
|
||||||
if !m.asleep() && !m.lastHeard.IsZero() && !m.silent(f.now) {
|
|
||||||
heard[m.name] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var out []conditions.Observation
|
|
||||||
said := map[string]bool{}
|
|
||||||
for _, d := range f.doing {
|
|
||||||
if !d.Wrong() || d.Since == nil || f.now.Sub(*d.Since) <= applyEvery || !heard[d.Node] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out = append(out, applyFailing(d, f.now))
|
|
||||||
said[d.Node] = true
|
|
||||||
}
|
|
||||||
// **A module's files the provisioner failed to place are a failed apply too** (novox/hq ADR 0362 §3): its client's
|
|
||||||
// newest word, judged by the same bound — the client says a failure once, and fills again on every change —
|
|
||||||
// on a machine whose node-engine applied what it was sent.
|
|
||||||
failed := map[string][]inventory.ProvReport{}
|
|
||||||
for _, r := range f.provFailed {
|
|
||||||
if r.Status != inventory.ProvFailed || f.now.Sub(r.At) <= applyEvery || !heard[r.Node] || said[r.Node] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
failed[r.Node] = append(failed[r.Node], r)
|
|
||||||
}
|
|
||||||
for _, node := range sortedNames(failed) {
|
|
||||||
out = append(out, provisioningFailing(node, failed[node], f.now))
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// provisioningFailing is one machine's S21 condition for the modules whose files its provisioner client failed to
|
|
||||||
// place.
|
|
||||||
func provisioningFailing(m string, failed []inventory.ProvReport, now time.Time) conditions.Observation {
|
|
||||||
sort.Slice(failed, func(a, b int) bool { return failed[a].Module < failed[b].Module })
|
|
||||||
var names, words []string
|
|
||||||
since := failed[0].At
|
|
||||||
for _, r := range failed {
|
|
||||||
names = append(names, r.Module)
|
|
||||||
words = append(words, r.Module+": "+oneLine(orNoReason(r.Reason)))
|
|
||||||
if r.At.Before(since) {
|
|
||||||
since = r.At
|
|
||||||
}
|
|
||||||
}
|
|
||||||
o := conditions.Observation{Scope: conditions.ScopeMachine, ID: m, Kind: kindApplyFailing, Machine: m,
|
|
||||||
Severity: conditions.Warning, Said: strings.Join(words, "; "),
|
|
||||||
Resolved: m + " applies what it is sent again"}
|
|
||||||
o.Summary = fmt.Sprintf("%s's provisioner has failed to place the files of %s since %s (bound %s)", m,
|
|
||||||
strings.Join(names, ", "), since.UTC().Format("2006-01-02 15:04 MST"), ago(applyEvery))
|
|
||||||
o.Headline = m + " is not applying its update"
|
|
||||||
o.Explanation = fmt.Sprintf("The provisioner on %s has not been able to write the files of %s for %s. The "+
|
|
||||||
"details say why.", m, strings.Join(names, ", "), humanDuration(now.Sub(since)))
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
func orNoReason(s string) string {
|
|
||||||
if s == "" {
|
|
||||||
return "no reason given"
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// applyFailing is one machine's S21 condition.
|
|
||||||
func applyFailing(d inventory.Doing, now time.Time) conditions.Observation {
|
|
||||||
m := d.Node
|
|
||||||
since := d.Since.UTC().Format("2006-01-02 15:04 MST")
|
|
||||||
o := conditions.Observation{Scope: conditions.ScopeMachine, ID: m, Kind: kindApplyFailing, Machine: m,
|
|
||||||
Severity: conditions.Warning, Said: doingWords(d),
|
|
||||||
Resolved: m + " applies what it is sent again"}
|
|
||||||
how := "refused its declaration whole"
|
|
||||||
if d.Outcome != inventory.OutcomeRefused {
|
|
||||||
how = "applied its declaration only in part"
|
|
||||||
}
|
|
||||||
o.Summary = fmt.Sprintf("%s has %s since %s (bound %s): %s", m, how, since, ago(applyEvery), failedNames(d))
|
|
||||||
o.Headline = m + " is not applying its update"
|
|
||||||
o.Explanation = fmt.Sprintf("%s has %s for %s, and trying again has not helped. The details say what fails.",
|
|
||||||
m, plainHow(d), humanDuration(now.Sub(*d.Since)))
|
|
||||||
if tool := unansweringTool(d); tool != "" {
|
|
||||||
o.Summary = fmt.Sprintf("%s: %s does not answer, so no package reaches it; everything else it was sent "+
|
|
||||||
"is applied, since %s (bound %s)", m, tool, since, ago(applyEvery))
|
|
||||||
o.Headline = fmt.Sprintf("%s cannot install packages", m)
|
|
||||||
o.Explanation = fmt.Sprintf("The package manager on %s (%s) has not answered for %s, so no package reaches "+
|
|
||||||
"it. Everything else is still applied, so an update that repairs it can land; if none does, it needs a "+
|
|
||||||
"look on the machine.", m, tool, humanDuration(now.Sub(*d.Since)))
|
|
||||||
}
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// plainHow is how a machine failed, as the operator reads it.
|
|
||||||
func plainHow(d inventory.Doing) string {
|
|
||||||
if d.Outcome == inventory.OutcomeRefused {
|
|
||||||
return "refused every update"
|
|
||||||
}
|
|
||||||
return "applied its updates only in part"
|
|
||||||
}
|
|
||||||
|
|
||||||
// unansweringTool is the package manager a machine said does not answer, or "".
|
|
||||||
func unansweringTool(d inventory.Doing) string {
|
|
||||||
for _, r := range d.Failed {
|
|
||||||
if rest, ok := strings.CutPrefix(r.ID, packageManagerKey); ok {
|
|
||||||
return strings.TrimSuffix(rest, ")")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// failedNames is what failed, by name: the refusal, or the failed resources' ids.
|
|
||||||
func failedNames(d inventory.Doing) string {
|
|
||||||
if d.Outcome == inventory.OutcomeRefused {
|
|
||||||
return "refused"
|
|
||||||
}
|
|
||||||
ids := make([]string, 0, len(d.Failed))
|
|
||||||
for _, r := range d.Failed {
|
|
||||||
ids = append(ids, r.ID)
|
|
||||||
}
|
|
||||||
sort.Strings(ids)
|
|
||||||
return "failed " + strings.Join(ids, ", ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// doingWords is the machine's own words on what failed: the evidence, kept inside the mesh.
|
|
||||||
func doingWords(d inventory.Doing) string {
|
|
||||||
if d.Outcome == inventory.OutcomeRefused {
|
|
||||||
return "refused: " + oneLine(d.Refused)
|
|
||||||
}
|
|
||||||
var parts []string
|
|
||||||
for _, r := range d.Failed {
|
|
||||||
parts = append(parts, r.ID+": "+oneLine(r.Error))
|
|
||||||
}
|
|
||||||
sort.Strings(parts)
|
|
||||||
return strings.Join(parts, "; ")
|
|
||||||
}
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// unansweringPackageManager is anchor's last report as the workstation's read on 2026-10-04, once its
|
|
||||||
// node-engine applies the rest (novox/hq issue 233): the package failed and the package manager did not answer,
|
|
||||||
// the same failure on times reports since for before now.
|
|
||||||
func unansweringPackageManager(now time.Time, forHowLong time.Duration, times int) inventory.Doing {
|
|
||||||
since := now.Add(-forHowLong)
|
|
||||||
return inventory.Doing{Node: "anchor", Outcome: inventory.OutcomeFailed, At: now.Add(-time.Second),
|
|
||||||
Since: &since, Times: times, Applied: 41, Failed: []inventory.FailedResource{
|
|
||||||
{ID: "apply", Error: `applying "pacman.base": nothing can be said about "base": this is the arch host and ` +
|
|
||||||
"pacman does not answer here. Either this machine is not Arch, or its package database is broken: " +
|
|
||||||
"pacman exited 1: error: config file /etc/pacman.conf could not be read: No such file or directory"},
|
|
||||||
{ID: "the package manager (pacman)", Error: "pacman does not answer here, so every package in this " +
|
|
||||||
"declaration fails and the rest of it is applied (novox/hq issue 233): pacman exited 1: error: config " +
|
|
||||||
"file /etc/pacman.conf could not be read: No such file or directory"}}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **A machine whose package manager does not answer is raised by the mesh itself, naming the tool and the
|
|
||||||
// probe's words** (novox/hq issue 233): the words in the evidence, the path kept out of the summary.
|
|
||||||
func TestAPackageManagerThatDoesNotAnswerIsRaisedNamingItsWords(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 4, 14, 0, 0, 0, time.UTC)
|
|
||||||
f := calm(now)
|
|
||||||
f.doing = []inventory.Doing{unansweringPackageManager(now, 90*time.Minute, 18)}
|
|
||||||
got := watchApplyFailing(f)
|
|
||||||
if len(got) != 1 {
|
|
||||||
t.Fatalf("raised %d conditions, want one: %+v", len(got), got)
|
|
||||||
}
|
|
||||||
o := got[0]
|
|
||||||
if o.Key() != "machine.anchor.apply-failing" || o.Machine != "anchor" {
|
|
||||||
t.Errorf("the condition is not the machine's: %s", o.Key())
|
|
||||||
}
|
|
||||||
if !strings.Contains(o.Summary, "pacman does not answer") || !strings.Contains(o.Summary, "since 2026-10-04 12:30") {
|
|
||||||
t.Errorf("the summary does not name the tool and since when: %s", o.Summary)
|
|
||||||
}
|
|
||||||
if strings.Contains(o.Summary, "/etc/") {
|
|
||||||
t.Errorf("the summary carries a path, which belongs in the evidence: %s", o.Summary)
|
|
||||||
}
|
|
||||||
if !strings.Contains(o.Said, "could not be read") {
|
|
||||||
t.Errorf("the evidence does not carry the probe's words: %s", o.Said)
|
|
||||||
}
|
|
||||||
if !strings.Contains(o.Headline, "cannot install packages") || o.Explanation == "" || o.Resolved == "" {
|
|
||||||
t.Errorf("the plain words do not say it: %+v", o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **A whole refusal is raised too**, with the node-engine's words: what the workstation's report said before the
|
|
||||||
// node-engine applied the rest.
|
|
||||||
func TestARefusalThatGoesOnIsRaised(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 4, 14, 0, 0, 0, time.UTC)
|
|
||||||
since := now.Add(-time.Hour)
|
|
||||||
f := calm(now)
|
|
||||||
f.doing = []inventory.Doing{{Node: "anchor", Outcome: inventory.OutcomeRefused, Since: &since, Times: 12,
|
|
||||||
Refused: "this is the arch host and pacman does not answer here", At: now.Add(-time.Minute)}}
|
|
||||||
got := watchApplyFailing(f)
|
|
||||||
if len(got) != 1 || !strings.Contains(got[0].Summary, "refused its declaration whole") ||
|
|
||||||
!strings.Contains(got[0].Said, "pacman does not answer") {
|
|
||||||
t.Fatalf("a refusal that went on for an hour was not raised with its words: %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **Judged by time while the machine is heard, not by how many reports said it** (review of novox/hq issue 233):
|
|
||||||
// a node-engine says the same failure once, so one report an apply interval old is a fault that goes on; one
|
|
||||||
// younger is not yet; and a silent or sleeping machine is S1's.
|
|
||||||
func TestOneReportOlderThanAnApplyIsRaisedAndAQuietMachineIsNot(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 4, 14, 0, 0, 0, time.UTC)
|
|
||||||
f := calm(now)
|
|
||||||
f.doing = []inventory.Doing{unansweringPackageManager(now, 6*time.Minute, 1)}
|
|
||||||
if got := watchApplyFailing(f); len(got) != 1 {
|
|
||||||
t.Errorf("one report of a failure six minutes old was not raised: %+v", got)
|
|
||||||
}
|
|
||||||
f.doing = []inventory.Doing{unansweringPackageManager(now, 4*time.Minute, 1)}
|
|
||||||
if got := watchApplyFailing(f); len(got) != 0 {
|
|
||||||
t.Errorf("a failure four minutes old was raised before one apply could clear it: %+v", got)
|
|
||||||
}
|
|
||||||
f.doing = []inventory.Doing{unansweringPackageManager(now, time.Hour, 1)}
|
|
||||||
f.machines[0].lastHeard = now.Add(-time.Hour)
|
|
||||||
if got := watchApplyFailing(f); len(got) != 0 {
|
|
||||||
t.Errorf("a silent machine was raised for its last report: %+v", got)
|
|
||||||
}
|
|
||||||
f = calm(now)
|
|
||||||
f.doing = []inventory.Doing{unansweringPackageManager(now, time.Hour, 1)}
|
|
||||||
f.doing[0].Node = "a-machine-the-mesh-does-not-know"
|
|
||||||
if got := watchApplyFailing(f); len(got) != 0 {
|
|
||||||
t.Errorf("a machine not heard of was raised: %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **A failed re-apply followed by a clean one clears S21** (review of novox/hq issue 233): the node-engine says
|
|
||||||
// the clean re-apply, the store keeps it as applied with no failure since, and the condition the failure raised
|
|
||||||
// is cleared through the keeper on the next tick.
|
|
||||||
func TestACleanReapplyAfterAFailedOneClearsTheCondition(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 4, 14, 0, 0, 0, time.UTC)
|
|
||||||
store := conditions.NewInMemory()
|
|
||||||
k := conditions.NewKeeper(t.Context(), conditions.Options{Store: store, History: store,
|
|
||||||
Teller: &conditions.Told{}, Now: func() time.Time { return now }})
|
|
||||||
defer k.Close(context.Background())
|
|
||||||
w := &watchdogs{keeper: k, started: now.Add(-time.Hour)}
|
|
||||||
|
|
||||||
failing := calm(now)
|
|
||||||
failing.doing = []inventory.Doing{unansweringPackageManager(now, 20*time.Minute, 1)}
|
|
||||||
w.see(t.Context(), failing)
|
|
||||||
open, err := k.Open(t.Context())
|
|
||||||
if err != nil || len(open) != 1 || open[0].Kind != kindApplyFailing {
|
|
||||||
t.Fatalf("the failed re-apply raised %+v (%v), want apply-failing", open, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
clean := calm(now.Add(5 * time.Minute))
|
|
||||||
clean.doing = nil // the clean re-apply recorded: NotDoingWhatTheyWereTold no longer lists the machine
|
|
||||||
w.see(t.Context(), clean)
|
|
||||||
if open, _ := k.Open(t.Context()); len(open) != 0 {
|
|
||||||
t.Fatalf("the condition stayed open after the clean re-apply: %+v", open)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ask calls one of a module's tools, through the control plane's own broker connection.
|
|
||||||
//
|
|
||||||
// A module serves tools under an account scoped to exactly that (novox/hq ADR 0047), and nothing
|
|
||||||
// else in the mesh held an account that could ask one — not an operator at a terminal, not an agent
|
|
||||||
// acting for one (novox/hq 04-ISSUES/049). The control plane does, so it is the way in: one
|
|
||||||
// process, one connection, one place a question can be seen to have been asked (ADR 0095).
|
|
||||||
func askCommand(ctx context.Context, args []string) error {
|
|
||||||
positionals, flags := split(args)
|
|
||||||
set := flag.NewFlagSet("ask", flag.ContinueOnError)
|
|
||||||
wait := set.Duration("wait", 60*time.Second, "how long to wait for the module's answer")
|
|
||||||
if err := set.Parse(flags); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(positionals) < 2 || len(positionals) > 3 {
|
|
||||||
return errors.New("ask <module> <tool> [json arguments] [--wait 60s]")
|
|
||||||
}
|
|
||||||
module, tool := positionals[0], positionals[1]
|
|
||||||
var arguments json.RawMessage
|
|
||||||
if len(positionals) == 3 {
|
|
||||||
if !json.Valid([]byte(positionals[2])) {
|
|
||||||
return fmt.Errorf("the arguments are not JSON: %s", positionals[2])
|
|
||||||
}
|
|
||||||
arguments = json.RawMessage(positionals[2])
|
|
||||||
}
|
|
||||||
|
|
||||||
server, err := connectLink(ctx, nil, nil, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
answer, err := link.Ask(ctx, server.Bus(), module, tool, arguments, *wait)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// The answer as the module gave it, to standard output, for a person or a program. A tool
|
|
||||||
// that answered with an error has still answered: printed the same way, and the exit status
|
|
||||||
// says which.
|
|
||||||
body, err := json.Marshal(answer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Fprintln(os.Stdout, string(body))
|
|
||||||
if answer.Error != "" {
|
|
||||||
return fmt.Errorf("%s.%s answered with an error: %s", module, tool, answer.Error)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Issue 1334, the look: when the bus did not answer an ask of any module of a tier, no build is in flight and no event follows,
|
|
||||||
// so the look itself comes back (it fails, and the looker retries it), asks the modules again, and past the bound fails the plan.
|
|
||||||
func TestALookComesBackForATierTheBusRefusedWhole(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
for _, name := range []string{"a", "b"} {
|
|
||||||
if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: name, Version: "1"},
|
|
||||||
inventory.Source{Repository: "novox/" + name, Seat: "git", Ref: "main", BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p := inventory.Plan{ID: "plan-bus", Repository: "novox/a", Branch: "main", Commit: "c1", Created: time.Now().UTC(),
|
|
||||||
State: inventory.PlanBuilding, Tiers: [][]string{{"a", "b"}}, Modules: map[string]*inventory.PlanModule{"a": {}, "b": {}}}
|
|
||||||
if err := inv.SavePlan(ctx, &p); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
bus := false
|
|
||||||
was := askABuild
|
|
||||||
askABuild = func(_ context.Context, source buildSource, _, _ string) (string, error) {
|
|
||||||
if !bus {
|
|
||||||
return "", fmt.Errorf("%w: nats: no responders available for request", inventory.ErrNoChain)
|
|
||||||
}
|
|
||||||
return "build-" + source.Repository, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { askABuild = was })
|
|
||||||
look := func() bool {
|
|
||||||
t.Helper()
|
|
||||||
release, err := inv.HoldPlans(ctx, true)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer release()
|
|
||||||
return advanceHeld(ctx, open)
|
|
||||||
}
|
|
||||||
read := func() inventory.Plan {
|
|
||||||
t.Helper()
|
|
||||||
got, err := inv.PlanByID(ctx, "plan-bus")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return got
|
|
||||||
}
|
|
||||||
|
|
||||||
// Every module refused: the look says it failed, which has the looker come back; the plan is building and says why.
|
|
||||||
if !look() {
|
|
||||||
t.Fatal("a look that left the whole tier unasked for the bus did not fail, so nothing would come back to it")
|
|
||||||
}
|
|
||||||
if got := read(); got.State != inventory.PlanBuilding || !strings.Contains(got.Note, "bus did not answer") ||
|
|
||||||
got.Modules["a"].State != "" || got.Modules["b"].State != "" {
|
|
||||||
t.Fatalf("the plan after a refused tier: %s %q %+v %+v", got.State, got.Note, got.Modules["a"], got.Modules["b"])
|
|
||||||
}
|
|
||||||
// Again, still refused: the same fact is not saved again, and the look still comes back.
|
|
||||||
before := read().Revision
|
|
||||||
if !look() || read().Revision != before {
|
|
||||||
t.Fatalf("a second refused look failed to come back or saved the plan again (revision %d, was %d)", read().Revision, before)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The bus answers at the next look: both are asked, and nothing waits any more.
|
|
||||||
bus = true
|
|
||||||
if look() {
|
|
||||||
t.Fatal("a look that asked the whole tier failed")
|
|
||||||
}
|
|
||||||
if got := read(); got.Modules["a"].State != "asked" || got.Modules["b"].State != "asked" || got.Modules["a"].BusWaitFrom != nil {
|
|
||||||
t.Fatalf("the tier was not asked once the bus answered: %+v %+v", got.Modules["a"], got.Modules["b"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestALookFailsTheTierTheBusRefusedPastTheBound(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: "a", Version: "1"},
|
|
||||||
inventory.Source{Repository: "novox/a", Seat: "git", Ref: "main", BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
long := time.Now().UTC().Add(-busWaitBound - time.Minute)
|
|
||||||
p := inventory.Plan{ID: "plan-bus", Repository: "novox/a", Branch: "main", Commit: "c1", Created: time.Now().UTC(),
|
|
||||||
State: inventory.PlanBuilding, Tiers: [][]string{{"a"}}, Modules: map[string]*inventory.PlanModule{"a": {BusWaitFrom: &long}}}
|
|
||||||
if err := inv.SavePlan(ctx, &p); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
was := askABuild
|
|
||||||
askABuild = func(context.Context, buildSource, string, string) (string, error) {
|
|
||||||
return "", fmt.Errorf("%w: nats: no responders available for request", inventory.ErrNoChain)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { askABuild = was })
|
|
||||||
release, err := inv.HoldPlans(ctx, true)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
advanceHeld(ctx, open)
|
|
||||||
release()
|
|
||||||
got, err := inv.PlanByID(ctx, "plan-bus")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got.State != inventory.PlanFailed || !strings.Contains(got.Note, "the bus did not answer an ask for 10m0s (since ") {
|
|
||||||
t.Fatalf("a bus silent past its bound did not fail the plan in words: %s %q", got.State, got.Note)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rebuilt by hand while the bus still reloads: said as waiting, not as a failure and not with an empty reason.
|
|
||||||
joined, said, err := joinAPlan(ctx, open, "a")
|
|
||||||
if err != nil || !joined || !strings.Contains(said, "not asked yet: the bus did not answer") {
|
|
||||||
t.Fatalf("a rebuild left for the bus is said as %v %q %v", joined, said, err)
|
|
||||||
}
|
|
||||||
// And retried by hand: the module is named as waiting, not as asked again.
|
|
||||||
got, _ = inv.PlanByID(ctx, "plan-bus")
|
|
||||||
if got.State == inventory.PlanFailed {
|
|
||||||
got.Modules["a"].State, got.Modules["a"].Why = "failed", "x"
|
|
||||||
if err := inv.SavePlan(ctx, &got); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
said, err := retryPlan(ctx, open, "plan-bus")
|
|
||||||
if err != nil || !strings.Contains(said, "not asked yet: the bus did not answer") {
|
|
||||||
t.Fatalf("a retry left for the bus is said as %q %v", said, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Issue 1334: a bus that does not answer for a moment (it reloads its user list, and JetStream's API has no responder for a few
|
|
||||||
// seconds) is not a failed build. The module is left unasked and the plan building, and the next look asks it again; only a
|
|
||||||
// module's own fault, or a bus silent for ten minutes, fails the plan.
|
|
||||||
|
|
||||||
func aTierOfThree() (*inventory.Plan, map[string]inventory.Entry) {
|
|
||||||
p := &inventory.Plan{ID: "plan-1", State: inventory.PlanBuilding, Tiers: [][]string{{"a", "b", "c"}},
|
|
||||||
Modules: map[string]*inventory.PlanModule{}}
|
|
||||||
byName := map[string]inventory.Entry{}
|
|
||||||
for _, m := range []string{"a", "b", "c"} {
|
|
||||||
byName[m] = inventory.Entry{Source: inventory.Source{Repository: "novox/" + m, Seat: "git", Ref: "main"}}
|
|
||||||
}
|
|
||||||
return p, byName
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABusThatDoesNotAnswerForAMomentDoesNotFailTheTier(t *testing.T) {
|
|
||||||
p, byName := aTierOfThree()
|
|
||||||
bus := false
|
|
||||||
was := askABuild
|
|
||||||
askABuild = func(_ context.Context, source buildSource, _, _ string) (string, error) {
|
|
||||||
if source.Repository == "novox/b" && !bus {
|
|
||||||
return "", fmt.Errorf("%w: the bucket mesh-controller_builds is not on the bus: nats: no responders available for request",
|
|
||||||
inventory.ErrNoChain)
|
|
||||||
}
|
|
||||||
return "build-" + source.Repository, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { askABuild = was })
|
|
||||||
|
|
||||||
for _, m := range []string{"a", "b", "c"} {
|
|
||||||
askModule(t.Context(), p, m, byName)
|
|
||||||
}
|
|
||||||
if p.State != inventory.PlanBuilding {
|
|
||||||
t.Fatalf("a bus that did not answer for a moment failed the plan: %s, %q", p.State, p.Note)
|
|
||||||
}
|
|
||||||
if p.Modules["a"].State != "asked" || p.Modules["c"].State != "asked" {
|
|
||||||
t.Fatalf("the modules the bus did answer for were not asked: %+v %+v", p.Modules["a"], p.Modules["c"])
|
|
||||||
}
|
|
||||||
if s := p.Modules["b"]; s.State != "" {
|
|
||||||
t.Fatalf("the module the bus did not answer for is %q, not left unasked", s.State)
|
|
||||||
}
|
|
||||||
if got := unaskedOf(*p); len(got) != 1 || got[0] != "b" {
|
|
||||||
t.Fatalf("the next look would ask %v, not b", got)
|
|
||||||
}
|
|
||||||
if !strings.Contains(p.Note, "bus did not answer") {
|
|
||||||
t.Fatalf("the plan says nothing of the wait: %q", p.Note)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The next look, the bus answering: b is asked and the plan goes on.
|
|
||||||
bus = true
|
|
||||||
for _, m := range unaskedOf(*p) {
|
|
||||||
askModule(t.Context(), p, m, byName)
|
|
||||||
}
|
|
||||||
if p.State != inventory.PlanBuilding || p.Modules["b"].State != "asked" || p.Modules["b"].Build == "" {
|
|
||||||
t.Fatalf("b was not asked once the bus answered: %s %+v", p.State, p.Modules["b"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABusSilentForTenMinutesFailsTheTierAsBefore(t *testing.T) {
|
|
||||||
p, byName := aTierOfThree()
|
|
||||||
was := askABuild
|
|
||||||
askABuild = func(context.Context, buildSource, string, string) (string, error) {
|
|
||||||
return "", fmt.Errorf("%w: nats: no responders available for request", inventory.ErrNoChain)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { askABuild = was })
|
|
||||||
askModule(t.Context(), p, "a", byName)
|
|
||||||
if p.State != inventory.PlanBuilding {
|
|
||||||
t.Fatalf("the first refusal failed the plan: %q", p.Note)
|
|
||||||
}
|
|
||||||
long := time.Now().Add(-busWaitBound - time.Minute)
|
|
||||||
p.Modules["a"].BusWaitFrom = &long
|
|
||||||
askModule(t.Context(), p, "a", byName)
|
|
||||||
if p.State != inventory.PlanFailed || p.Modules["a"].State != "failed" {
|
|
||||||
t.Fatalf("a bus silent past its bound did not fail the plan: %s %+v", p.State, p.Modules["a"])
|
|
||||||
}
|
|
||||||
// Said in plain words: the bus, the wait, and since when.
|
|
||||||
want := "a could not be asked for: the bus did not answer an ask for 10m0s (since " + long.UTC().Format(time.RFC3339) +
|
|
||||||
"): this kind of record lives on the bus"
|
|
||||||
if !strings.HasPrefix(p.Note, want) || p.Modules["a"].Why != strings.TrimPrefix(want, "a could not be asked for: ")+p.Note[len(want):] {
|
|
||||||
t.Fatalf("the plan says %q, not words starting %q", p.Note, want)
|
|
||||||
}
|
|
||||||
// The wait has ended: a Retry made while the bus still reloads waits again, and does not fail at its first refusal.
|
|
||||||
if p.Modules["a"].BusWaitFrom != nil {
|
|
||||||
t.Fatalf("a failed module still holds the start of its wait: %v", p.Modules["a"].BusWaitFrom)
|
|
||||||
}
|
|
||||||
p.State = inventory.PlanBuilding
|
|
||||||
askModule(t.Context(), p, "a", byName)
|
|
||||||
if p.State != inventory.PlanBuilding || p.Modules["a"].State != "" || p.Modules["a"].BusWaitFrom == nil {
|
|
||||||
t.Fatalf("a retry made while the bus is silent did not wait afresh: %s %+v", p.State, p.Modules["a"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAPlanWithAModuleWaitingForTheBusIsOneTheLookComesBackTo(t *testing.T) {
|
|
||||||
p, _ := aTierOfThree()
|
|
||||||
if busWaiting(*p) {
|
|
||||||
t.Fatal("a plan whose modules are not asked yet waits for the bus")
|
|
||||||
}
|
|
||||||
now := time.Now()
|
|
||||||
p.Modules["b"] = &inventory.PlanModule{BusWaitFrom: &now}
|
|
||||||
p.Modules["a"] = &inventory.PlanModule{State: "asked"}
|
|
||||||
if got := busWaitingIn(*p); len(got) != 1 || got[0] != "b" {
|
|
||||||
t.Fatalf("the modules waiting for the bus are %v, not b", got)
|
|
||||||
}
|
|
||||||
p.State = inventory.PlanFailed
|
|
||||||
if busWaiting(*p) {
|
|
||||||
t.Fatal("a plan that is not open is waited for")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAModulesOwnFaultStillFailsThePlan(t *testing.T) {
|
|
||||||
p, byName := aTierOfThree()
|
|
||||||
delete(byName, "a")
|
|
||||||
askModule(t.Context(), p, "a", byName)
|
|
||||||
if p.State != inventory.PlanFailed || !strings.Contains(p.Note, "no longer in the catalogue") {
|
|
||||||
t.Fatalf("a module gone from the catalogue did not fail the plan: %s %q", p.State, p.Note)
|
|
||||||
}
|
|
||||||
|
|
||||||
p, byName = aTierOfThree()
|
|
||||||
was := askABuild
|
|
||||||
askABuild = func(context.Context, buildSource, string, string) (string, error) {
|
|
||||||
return "", errors.New("git.example/novox/a is no repository the catalogue builds a module from")
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { askABuild = was })
|
|
||||||
askModule(t.Context(), p, "a", byName)
|
|
||||||
if p.State != inventory.PlanFailed || p.Modules["a"].State != "failed" {
|
|
||||||
t.Fatalf("a module's own refusal did not fail the plan: %s %+v", p.State, p.Modules["a"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"slices"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
"github.com/nats-io/nats.go/jetstream"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// busWaitBound is how long a module whose ask the bus did not answer is asked again at every look before the plan fails
|
|
||||||
// (issue 1334).
|
|
||||||
const busWaitBound = 10 * time.Minute
|
|
||||||
|
|
||||||
// busDidNotAnswer says an ask failed because the bus's JetStream had no responder at that moment, as it has for a few seconds
|
|
||||||
// when the bus reloads its user list (novox/hq issue 353), and not because of the module: the same ask is made again.
|
|
||||||
func busDidNotAnswer(err error) bool {
|
|
||||||
if err == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if errors.Is(err, nats.ErrNoResponders) || errors.Is(err, nats.ErrJetStreamNotEnabled) ||
|
|
||||||
errors.Is(err, jetstream.ErrJetStreamNotEnabled) || errors.Is(err, inventory.ErrNoChain) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// An error that crossed a boundary as text only.
|
|
||||||
text := err.Error()
|
|
||||||
return strings.Contains(text, "no responders available") || strings.Contains(text, "jetstream not enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
// waitForTheBus leaves a module whose ask the bus did not answer unasked, the plan building, and says so in the plan's note: the
|
|
||||||
// next look asks the tier's unasked modules again. It says false, so the caller fails the plan as for any refusal, once the bus
|
|
||||||
// has been silent for busWaitBound.
|
|
||||||
func waitForTheBus(p *inventory.Plan, state *inventory.PlanModule, name string, err error, now time.Time) bool {
|
|
||||||
if state.BusWaitFrom == nil {
|
|
||||||
state.BusWaitFrom = &now
|
|
||||||
}
|
|
||||||
if now.Sub(*state.BusWaitFrom) > busWaitBound {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
state.State, state.Why = "", ""
|
|
||||||
p.Note = fmt.Sprintf("tier %d: %s not asked: the bus did not answer (%v); asked again at the next look, failing after %s",
|
|
||||||
p.Tier, name, firstLine(err.Error()), busWaitBound)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// busWaiting says an open plan has a module of its current tier that the bus did not answer an ask of: the look must come back
|
|
||||||
// for it, as nothing else will when no build of the tier is in flight (issue 1334).
|
|
||||||
func busWaiting(p inventory.Plan) bool {
|
|
||||||
return len(busWaitingIn(p)) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// busWaitingIn are the modules of the plan's current tier left unasked because the bus did not answer, sorted.
|
|
||||||
func busWaitingIn(p inventory.Plan) []string {
|
|
||||||
if !p.Open() || p.Tier >= len(p.Tiers) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var out []string
|
|
||||||
for _, m := range p.Tiers[p.Tier] {
|
|
||||||
if s := p.Modules[m]; s != nil && s.State == "" && s.BusWaitFrom != nil {
|
|
||||||
out = append(out, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(out)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// busWaitWords is how a verb says modules are waiting for the bus: not asked yet, and asked again by the plan at its next look.
|
|
||||||
func busWaitWords(p inventory.Plan, modules []string) string {
|
|
||||||
return fmt.Sprintf("%s not asked yet: the bus did not answer; %s asks again at its next look", strings.Join(modules, ", "), p.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// slicesWithout is names without those in drop, in order.
|
|
||||||
func slicesWithout(names, drop []string) []string {
|
|
||||||
var out []string
|
|
||||||
for _, n := range names {
|
|
||||||
if !slices.Contains(drop, n) {
|
|
||||||
out = append(out, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,153 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
"github.com/nats-io/nats.go/jetstream"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
"github.com/novox/mesh-controller/internal/testbus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// busAsker is an asker on a real bus's `asked` bucket, counting what it performs: two of them are two
|
|
||||||
// controllers sharing one record.
|
|
||||||
type busAskerRig struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
called int
|
|
||||||
acts int
|
|
||||||
open []conditions.Condition
|
|
||||||
sent [][]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rig *busAskerRig) asker(t *testing.T, conn *nats.Conn, now time.Time) *asker {
|
|
||||||
return &asker{
|
|
||||||
open: func(context.Context) ([]conditions.Condition, error) {
|
|
||||||
rig.mu.Lock()
|
|
||||||
defer rig.mu.Unlock()
|
|
||||||
return rig.open, nil
|
|
||||||
},
|
|
||||||
silence: func(context.Context, string, time.Duration, string, string) error { return nil },
|
|
||||||
store: busAsked{conn: conn},
|
|
||||||
publish: func(_ context.Context, subject string, body []byte, _ string) error {
|
|
||||||
rig.mu.Lock()
|
|
||||||
defer rig.mu.Unlock()
|
|
||||||
if subject == asks.AskSubject(askerName) {
|
|
||||||
rig.sent = append(rig.sent, body)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
call: func(context.Context, conditions.Action, map[string]string) error {
|
|
||||||
time.Sleep(20 * time.Millisecond) // long enough for the other delivery to arrive meanwhile
|
|
||||||
rig.mu.Lock()
|
|
||||||
defer rig.mu.Unlock()
|
|
||||||
rig.called++
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
record: func(context.Context, link.HandAct) error {
|
|
||||||
rig.mu.Lock()
|
|
||||||
defer rig.mu.Unlock()
|
|
||||||
rig.acts++
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
now: func() time.Time { return now },
|
|
||||||
logf: t.Logf,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func askedBus(t *testing.T) *nats.Conn {
|
|
||||||
t.Helper()
|
|
||||||
conn, err := nats.Connect(testbus.URL(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(conn.Close)
|
|
||||||
js, err := jetstream.New(conn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := js.CreateKeyValue(context.Background(), jetstream.KeyValueConfig{Bucket: broker.AskedBucket}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return conn
|
|
||||||
}
|
|
||||||
|
|
||||||
// The review of 2026-10-09 (L7): two deliveries of one warrant, to two controllers at once, perform its act
|
|
||||||
// exactly once and record it once — the record's compare-and-set decides, never the warrant's message id.
|
|
||||||
func TestTwoAnswersAtOnceActOnce(t *testing.T) {
|
|
||||||
conn := askedBus(t)
|
|
||||||
now := time.Date(2026, 10, 9, 14, 0, 0, 0, time.UTC)
|
|
||||||
rig := &busAskerRig{open: []conditions.Condition{heldCondition()}}
|
|
||||||
first, second := rig.asker(t, conn, now), rig.asker(t, conn, now)
|
|
||||||
if err := first.reconcile(context.Background()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(rig.sent) != 1 {
|
|
||||||
t.Fatalf("asked %d times", len(rig.sent))
|
|
||||||
}
|
|
||||||
var q asks.Ask
|
|
||||||
_ = json.Unmarshal(rig.sent[0], &q)
|
|
||||||
release, _ := q.Option("release")
|
|
||||||
w := asks.Warrant{Ask: q.ID, Asker: askerName, About: q.About, Outcome: asks.OutcomeChosen, Option: release.ID,
|
|
||||||
Label: release.Label, Level: release.Level, Channel: "telegram", Proofs: []string{"P1"}, At: now,
|
|
||||||
AskDigest: q.Digest(), By: &asks.Person{Who: asks.Operator, Kind: "telegram", Identity: "42", Verified: "user id verified"}}
|
|
||||||
body, _ := json.Marshal(w)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for _, a := range []*asker{first, second, first, second} {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(a *asker) {
|
|
||||||
defer wg.Done()
|
|
||||||
if err := a.Decided(context.Background(), body); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}(a)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
if rig.called != 1 || rig.acts != 1 {
|
|
||||||
t.Fatalf("performed %d time(s), recorded %d time(s)", rig.called, rig.acts)
|
|
||||||
}
|
|
||||||
got, err := busAsked{conn: conn}.Get(context.Background(), q.ID)
|
|
||||||
if err != nil || got == nil || got.Acted != "done" {
|
|
||||||
t.Fatalf("kept as %+v (%v)", got, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The review of 2026-10-09 (L2): a write decided on a record read earlier never lands over one made since. A
|
|
||||||
// cancel read before the answer was acted on leaves the act's record as it is.
|
|
||||||
func TestAStaleCancelDoesNotWriteOverAnAct(t *testing.T) {
|
|
||||||
conn := askedBus(t)
|
|
||||||
now := time.Date(2026, 10, 9, 14, 0, 0, 0, time.UTC)
|
|
||||||
rig := &busAskerRig{open: []conditions.Condition{heldCondition()}}
|
|
||||||
a := rig.asker(t, conn, now)
|
|
||||||
if err := a.reconcile(context.Background()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var q asks.Ask
|
|
||||||
_ = json.Unmarshal(rig.sent[0], &q)
|
|
||||||
stale, _ := busAsked{conn: conn}.Get(context.Background(), q.ID)
|
|
||||||
release, _ := q.Option("release")
|
|
||||||
w := asks.Warrant{Ask: q.ID, Asker: askerName, About: q.About, Outcome: asks.OutcomeChosen, Option: release.ID,
|
|
||||||
Label: release.Label, Level: release.Level, Channel: "telegram", At: now, AskDigest: q.Digest(),
|
|
||||||
By: &asks.Person{Who: asks.Operator, Kind: "telegram", Identity: "42", Verified: "user id verified"}}
|
|
||||||
body, _ := json.Marshal(w)
|
|
||||||
if err := a.Decided(context.Background(), body); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := a.cancel(context.Background(), *stale, "the condition ended"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
got, _ := busAsked{conn: conn}.Get(context.Background(), q.ID)
|
|
||||||
if got.State != string(asks.OutcomeChosen) || got.Acted != "done" {
|
|
||||||
t.Errorf("a stale cancel wrote over the act: %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
"github.com/novox/mesh-controller/internal/testbus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 1315/AC3: the operator's Retry on a failed plan, acted on through the real callAction and the controller's own
|
|
||||||
// `plans` verb, sets the plan building again at its tier and ends its plan-failed condition (novox/hq ADR 0449 §1).
|
|
||||||
func TestAWarrantForRetryRetriesAFailedPlanInTheStoreAndEndsItsCondition(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
asked := asksRecorded(t)
|
|
||||||
planMovedHeard(t) // the bus the verb says its moves on, and the hand-act log
|
|
||||||
twoTiers(t, open)
|
|
||||||
before := time.Now().UTC().Add(-time.Hour)
|
|
||||||
ended := before.Add(time.Minute)
|
|
||||||
failed := inventory.Plan{ID: "plan-1315-retry", Repository: "novox/a", Branch: "main", Commit: "c0ffee",
|
|
||||||
Created: before, State: inventory.PlanFailed, Tier: 0, Tiers: [][]string{{"a"}, {"b"}},
|
|
||||||
Note: "a failed to build in tier 0",
|
|
||||||
Delivery: &inventory.PlanDelivery{},
|
|
||||||
Modules: map[string]*inventory.PlanModule{"a": {State: "failed", AskedAt: &before, Build: "build-1",
|
|
||||||
Why: link.KilledByHand}},
|
|
||||||
Times: &inventory.PlanTimes{Cut: &before, Started: &before, Ended: &ended, Class: inventory.ClassLeaf}}
|
|
||||||
if err := open.inventory.SavePlan(ctx, &failed); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := nats.Connect(testbus.URL(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
// The controller's seat answers `plans` as it does: by running the verb.
|
|
||||||
sub, err := conn.Subscribe(link.SeatToolSubject("mesh-controller", "plans"), func(m *nats.Msg) {
|
|
||||||
var args map[string]string
|
|
||||||
_ = json.Unmarshal(m.Data, &args)
|
|
||||||
answer := link.Answer{Result: json.RawMessage(`"ok"`)}
|
|
||||||
if len(args) != 1 || args["retry"] == "" {
|
|
||||||
answer = link.Answer{Error: "the retry was asked with more than the plan"}
|
|
||||||
} else if err := plansCommand(context.Background(), []string{"retry", args["retry"]}); err != nil {
|
|
||||||
answer = link.Answer{Error: err.Error()}
|
|
||||||
}
|
|
||||||
body, _ := json.Marshal(answer)
|
|
||||||
_ = m.Respond(body)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func() { _ = sub.Unsubscribe() }()
|
|
||||||
|
|
||||||
// What S23 raises for it, read from the store as the watchdog reads it.
|
|
||||||
raised := func(context.Context) ([]conditions.Condition, error) {
|
|
||||||
facts, err := gatherFailedPlans(ctx, open.inventory, time.Now())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var out []conditions.Condition
|
|
||||||
for _, o := range watchPlansFailed(&signalFacts{now: time.Now(), failed: facts}) {
|
|
||||||
out = append(out, conditions.Condition{Key: o.Key(), Kind: o.Kind, Severity: o.Severity, Headline: o.Headline,
|
|
||||||
Explanation: conditions.Verdict(o.Needs, o.Explanation), Needs: o.Needs, Actions: o.Actions})
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
now := time.Now()
|
|
||||||
store := memAskedStore{}
|
|
||||||
var sent []published
|
|
||||||
var acts []link.HandAct
|
|
||||||
a := &asker{open: raised, store: store,
|
|
||||||
silence: func(context.Context, string, time.Duration, string, string) error { return nil },
|
|
||||||
publish: func(_ context.Context, subject string, body []byte, id string) error {
|
|
||||||
sent = append(sent, published{subject, id, body})
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
call: callAction(conn),
|
|
||||||
record: func(_ context.Context, act link.HandAct) error { acts = append(acts, act); return nil },
|
|
||||||
now: func() time.Time { return now },
|
|
||||||
logf: t.Logf}
|
|
||||||
cs, _ := raised(ctx)
|
|
||||||
if len(cs) != 1 || len(cs[0].Actions) != 1 {
|
|
||||||
t.Fatalf("the failed plan raised %+v", cs)
|
|
||||||
}
|
|
||||||
if err := a.reconcile(ctx); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var w asks.Warrant
|
|
||||||
for _, r := range store {
|
|
||||||
for _, o := range r.Ask.Options {
|
|
||||||
if o.Label == "Retry" {
|
|
||||||
w = asks.Warrant{Ask: r.ID, Asker: "mesh-controller", About: r.Condition, Outcome: asks.OutcomeChosen,
|
|
||||||
Option: o.ID, Label: o.Label, Level: o.Level, Channel: "telegram", Proofs: []string{"P1"}, At: now,
|
|
||||||
AskDigest: r.Ask.Digest(),
|
|
||||||
By: &asks.Person{Who: asks.Operator, Kind: "telegram", Identity: "42", Verified: "user id verified"}}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if w.Ask == "" {
|
|
||||||
t.Fatalf("no ask offers Retry: %+v", store)
|
|
||||||
}
|
|
||||||
body, _ := json.Marshal(w)
|
|
||||||
if err := a.Decided(ctx, body); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := a.Decided(ctx, body); err != nil { // heard again: nothing more
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := open.inventory.PlanByID(ctx, failed.ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
m := p.Modules["a"]
|
|
||||||
if p.State != inventory.PlanBuilding || p.Tier != 0 || m.State != "asked" || m.Build == "" || m.Build == "build-1" {
|
|
||||||
t.Fatalf("after the warrant the plan is %s at tier %d and a is %+v", p.State, p.Tier, m)
|
|
||||||
}
|
|
||||||
if len(*asked) != 1 {
|
|
||||||
t.Fatalf("the build was asked %d times: %v", len(*asked), *asked)
|
|
||||||
}
|
|
||||||
if got := store[w.Ask]; got.Acted != "done" {
|
|
||||||
t.Fatalf("the ask ended %q", got.Acted)
|
|
||||||
}
|
|
||||||
if len(acts) != 1 || acts[0].Verb != handActWarrant || !strings.Contains(strings.Join(acts[0].Args, " "), "retry="+failed.ID) {
|
|
||||||
t.Fatalf("the warrant was recorded as %+v", acts)
|
|
||||||
}
|
|
||||||
if cs, _ = raised(ctx); len(cs) != 0 {
|
|
||||||
t.Fatalf("the plan-failed condition did not end: %+v", cs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,612 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// The asker on the bus: its asks in the controller's bucket `asked`, its asks and cancels published on the
|
|
||||||
// seat under the controller's name, the verbs a warrant chooses called with the controller's grant, and the
|
|
||||||
// router's record of its asks read under its name (novox/hq ADR 0259).
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
"github.com/nats-io/nats.go/jetstream"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// askerFrom is the serving controller's asker; nil in any other process.
|
|
||||||
var askerFrom *asker
|
|
||||||
|
|
||||||
// askWithin is how long a verb a warrant chose is given to answer.
|
|
||||||
const askWithin = time.Minute
|
|
||||||
|
|
||||||
type busAsked struct{ conn *nats.Conn }
|
|
||||||
|
|
||||||
func (b busAsked) kv(ctx context.Context) (jetstream.KeyValue, error) {
|
|
||||||
js, err := jetstream.New(b.conn)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return js.KeyValue(ctx, broker.AskedBucket)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b busAsked) Get(ctx context.Context, id string) (*asked, error) {
|
|
||||||
kv, err := b.kv(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
e, err := kv.Get(ctx, id)
|
|
||||||
if errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var r asked
|
|
||||||
return &r, json.Unmarshal(e.Value(), &r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create keeps a new ask under its id, and only where none is kept: never over another.
|
|
||||||
func (b busAsked) Create(ctx context.Context, r asked) error {
|
|
||||||
kv, err := b.kv(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
body, err := json.Marshal(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = kv.Create(ctx, r.ID, body)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change applies change to the ask kept under id by compare-and-set on its key's revision (the review of
|
|
||||||
// 2026-10-09, L2): read, changed, and written only over the revision read; when another write came between,
|
|
||||||
// read again and asked again, at most askChangeTries times. change says whether to write at all.
|
|
||||||
func (b busAsked) Change(ctx context.Context, id string, change func(*asked) bool) (bool, error) {
|
|
||||||
kv, err := b.kv(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
for try := 0; try < askChangeTries; try++ {
|
|
||||||
e, err := kv.Get(ctx, id)
|
|
||||||
if errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
var r asked
|
|
||||||
if err := json.Unmarshal(e.Value(), &r); err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if !change(&r) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
body, err := json.Marshal(r)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if _, err := kv.Update(ctx, id, body, e.Revision()); err != nil {
|
|
||||||
var api *jetstream.APIError
|
|
||||||
if errors.Is(err, jetstream.ErrKeyExists) || (errors.As(err, &api) && api.ErrorCode == jetstream.JSErrCodeStreamWrongLastSequence) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, fmt.Errorf("the ask %s changed under every one of %d tries", id, askChangeTries)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b busAsked) All(ctx context.Context) ([]asked, error) {
|
|
||||||
kv, err := b.kv(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
lister, err := kv.ListKeys(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer func() { _ = lister.Stop() }()
|
|
||||||
var out []asked
|
|
||||||
for k := range lister.Keys() {
|
|
||||||
e, err := kv.Get(ctx, k)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var r asked
|
|
||||||
if json.Unmarshal(e.Value(), &r) == nil {
|
|
||||||
out = append(out, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// verbRefused is a verb's own refusal of an act: the seat answered, and said no. Any other error of callAction (a bus
|
|
||||||
// that did not answer, a grant that is refused) is not one: the verb may have run.
|
|
||||||
type verbRefused struct{ verb, words string }
|
|
||||||
|
|
||||||
func (e verbRefused) Error() string { return e.verb + " refused: " + e.words }
|
|
||||||
|
|
||||||
// callAction performs an action's verb as the controller, through the grant that names it.
|
|
||||||
func callAction(conn *nats.Conn) func(ctx context.Context, a conditions.Action, args map[string]string) error {
|
|
||||||
return func(ctx context.Context, a conditions.Action, args map[string]string) error {
|
|
||||||
seat, verb, ok := strings.Cut(a.Verb, ".")
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("%q names no seat and verb", a.Verb)
|
|
||||||
}
|
|
||||||
body := map[string]any{}
|
|
||||||
for k, v := range args {
|
|
||||||
body[k] = v
|
|
||||||
}
|
|
||||||
if seat == catalogue.DeliverySeat {
|
|
||||||
_, err := askDeliveryOwner(ctx, conn, verb, body)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
granted := false
|
|
||||||
for _, v := range broker.VerbsTheControllerActsOnAWarrant {
|
|
||||||
granted = granted || (v.Seat == seat && v.Verb == verb)
|
|
||||||
}
|
|
||||||
if !granted {
|
|
||||||
return fmt.Errorf("%s: %w", a.Verb, errNotGranted)
|
|
||||||
}
|
|
||||||
var answer link.Answer
|
|
||||||
var err error
|
|
||||||
if a.Machine != "" {
|
|
||||||
answer, err = link.AskSeatTool(ctx, conn, seat, verb, a.Machine, body, askWithin)
|
|
||||||
} else {
|
|
||||||
answer, err = link.AskMeshSeatTool(ctx, conn, seat, verb, body, askWithin)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if answer.Error != "" {
|
|
||||||
return verbRefused{verb: a.Verb, words: answer.Error}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// tellThrough tells the operator one plain notice through the operator channel's `tell`, over the controller's own
|
|
||||||
// connection and grant (novox/hq ADR 0380); a holder's refusal is an error naming it.
|
|
||||||
func tellThrough(conn *nats.Conn) func(ctx context.Context, headline string, lines []string) error {
|
|
||||||
return func(ctx context.Context, headline string, lines []string) error {
|
|
||||||
answer, err := link.AskMeshSeatTool(ctx, conn, "operator-channel", "tell",
|
|
||||||
map[string]any{"headline": headline, "lines": lines}, tellWithin)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if answer.Error != "" {
|
|
||||||
return fmt.Errorf("operator-channel.tell refused: %s", answer.Error)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// routerRecordOf reads the router's record of one of the controller's asks, under its name, and answers
|
|
||||||
// how it ended when it did: the bucket is the one the asks seat's declarer names as its records.
|
|
||||||
func routerRecordOf(conn *nats.Conn, inv *inventory.Inventory) func(ctx context.Context, id string) (*asks.Warrant, error) {
|
|
||||||
return func(ctx context.Context, id string) (*asks.Warrant, error) {
|
|
||||||
bucket, err := asksRecords(ctx, inv)
|
|
||||||
if err != nil || bucket == "" {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
reply, err := conn.RequestWithContext(ctx, "$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+askerName+"."+id, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if reply.Header.Get("Status") != "" {
|
|
||||||
return nil, nil // none, or not readable: the event says it
|
|
||||||
}
|
|
||||||
var rec struct {
|
|
||||||
State string `json:"state"`
|
|
||||||
Warrant *asks.Warrant `json:"warrant"`
|
|
||||||
}
|
|
||||||
if json.Unmarshal(reply.Data, &rec) != nil || rec.State == "open" || rec.Warrant == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return rec.Warrant, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// asksRecords is the bucket the asks seat's declarer keeps its record of asks in.
|
|
||||||
func asksRecords(ctx context.Context, inv catalogueReader) (string, error) {
|
|
||||||
declared, err := inv.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, m := range declared {
|
|
||||||
for _, s := range m.DefinesSeats {
|
|
||||||
if s.Name == broker.AsksSeat && len(s.Records) > 0 {
|
|
||||||
return broker.BucketName(m.Module, s.Records[0]), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// routerHereIn says whether a module declaring the asks seat, with its ask named by its caller, is assigned:
|
|
||||||
// without it nothing takes an ask, and asking would only fill a queue nobody reads.
|
|
||||||
func routerHereIn(inv *inventory.Inventory) func(ctx context.Context) (bool, error) {
|
|
||||||
return func(ctx context.Context) (bool, error) {
|
|
||||||
entries, err := inv.Catalogued(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
for _, e := range entries {
|
|
||||||
for _, s := range e.Manifest.DefinesSeats {
|
|
||||||
if s.Name == broker.AsksSeat && s.NamedByCaller("ask") && len(e.On) > 0 {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// grantHeldIn says whether the bus holds the controller's grant to ask (novox/hq issue 353), judged on the two
|
|
||||||
// facts that make it so, and failing closed on each:
|
|
||||||
//
|
|
||||||
// 1. **composed**: the user list the mesh composes now grants the controller itself to publish its ask
|
|
||||||
// (controllerMayAsk) — the controller's own grant, not whether any user of the list changed;
|
|
||||||
// 2. **in force**: the bus's own module, on the machine holding the bus, says the user list there allows it
|
|
||||||
// and the server reloaded after that list was written (the nats module's `nats_user_can`, `in_force`) — a
|
|
||||||
// list sent is not a list the bus enforces until it reloads.
|
|
||||||
//
|
|
||||||
// Anything that cannot be worked out is not held, with why, so the conditions that need the operator are said
|
|
||||||
// as undelivered rather than asked into a refusal.
|
|
||||||
func grantHeldIn(open *stores, conn *nats.Conn) func(ctx context.Context) (bool, string, error) {
|
|
||||||
return grantHeldBy(composedGrantIn(open), busGrantIn(open, conn), time.Now)
|
|
||||||
}
|
|
||||||
|
|
||||||
// grantJudge is one of the two facts: whether it holds, and why not.
|
|
||||||
type grantJudge func(ctx context.Context) (bool, string, error)
|
|
||||||
|
|
||||||
// grantHeldBy is the judgement over the two facts, remembered for grantLookEvery: composing the list resolves
|
|
||||||
// the bus's machine whole, and asking the bus's module is a call.
|
|
||||||
func grantHeldBy(composed, inForce grantJudge, now func() time.Time) func(ctx context.Context) (bool, string, error) {
|
|
||||||
var mu sync.Mutex
|
|
||||||
var at time.Time
|
|
||||||
var held bool
|
|
||||||
var why string
|
|
||||||
return func(ctx context.Context) (bool, string, error) {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
if !at.IsZero() && now().Sub(at) < grantLookEvery {
|
|
||||||
return held, why, nil
|
|
||||||
}
|
|
||||||
held, why = judgeGrant(ctx, composed, inForce)
|
|
||||||
at = now()
|
|
||||||
return held, why, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// judgeGrant is held only when both facts hold; an error is a fact not known, never a yes. A fact that holds
|
|
||||||
// with a caveat (a reload not confirmed) gives that caveat as the why of a yes.
|
|
||||||
func judgeGrant(ctx context.Context, composed, inForce grantJudge) (bool, string) {
|
|
||||||
var caveat []string
|
|
||||||
for _, fact := range []struct {
|
|
||||||
judge grantJudge
|
|
||||||
what string
|
|
||||||
}{{composed, "whether the mesh composes the controller a grant to ask"}, {inForce, "whether the bus enforces it"}} {
|
|
||||||
if fact.judge == nil {
|
|
||||||
return false, fact.what + " is not judged here"
|
|
||||||
}
|
|
||||||
ok, why, err := fact.judge(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return false, fact.what + " could not be worked out: " + err.Error()
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
return false, why
|
|
||||||
}
|
|
||||||
if why != "" {
|
|
||||||
caveat = append(caveat, why)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true, strings.Join(caveat, "; ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// controllerMayAsk says whether the controller's own grant, as composed from these records, lets it publish its
|
|
||||||
// ask (ADR 0259 §3: composed while a router that takes asks under its asker's name is assigned).
|
|
||||||
func controllerMayAsk(records broker.Records) (bool, error) {
|
|
||||||
users, err := broker.Users(records)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
for _, u := range users {
|
|
||||||
if u.Kind != broker.KindController {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
perms, err := broker.PermissionsFor(u)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return broker.MayPublish(perms, asks.AskSubject(askerName)), nil
|
|
||||||
}
|
|
||||||
return false, errors.New("the composition holds no controller")
|
|
||||||
}
|
|
||||||
|
|
||||||
// composedGrantIn is the first fact, from the mesh's records.
|
|
||||||
func composedGrantIn(open *stores) grantJudge {
|
|
||||||
return func(ctx context.Context) (bool, string, error) {
|
|
||||||
if open == nil || open.inventory == nil {
|
|
||||||
return false, "", errors.New("the mesh's records are not open")
|
|
||||||
}
|
|
||||||
records, err := open.inventory.BusRecords(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return false, "", err
|
|
||||||
}
|
|
||||||
may, err := controllerMayAsk(records)
|
|
||||||
if err != nil || may {
|
|
||||||
return may, "", err
|
|
||||||
}
|
|
||||||
return false, "the mesh composes the controller no grant to ask: no router that takes asks under its " +
|
|
||||||
"asker's name is assigned", nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// busGrantIn is the second fact, asked of the bus's own module on the machine holding the bus, with the check
|
|
||||||
// main made before it (novox/hq issue 353, PR 186: is the user list sent to the bus's machine the one composed
|
|
||||||
// now) kept for the cases the module's word does not settle.
|
|
||||||
func busGrantIn(open *stores, conn *nats.Conn) grantJudge {
|
|
||||||
ask := func(ctx context.Context) (string, json.RawMessage, error) {
|
|
||||||
if open == nil || open.inventory == nil || conn == nil {
|
|
||||||
return "", nil, errors.New("the mesh's records or the bus are not open")
|
|
||||||
}
|
|
||||||
holders, err := seatHolders(ctx, open.inventory)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
h, held := holders[theBrokerSeat]
|
|
||||||
if !held || h.Node == "" || h.Module == "" {
|
|
||||||
return "", nil, nil
|
|
||||||
}
|
|
||||||
a, err := link.AskModuleToolOn(ctx, conn, h.Module, busUserCanTool, h.Node, map[string]any{
|
|
||||||
"user": broker.ControllerName, "action": "publish", "subject": asks.AskSubject(askerName)}, 15*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
return h.Node, nil, err
|
|
||||||
}
|
|
||||||
if a.Error != "" {
|
|
||||||
return h.Node, nil, errors.New(a.Error)
|
|
||||||
}
|
|
||||||
return h.Node, a.Result, nil
|
|
||||||
}
|
|
||||||
list := func(ctx context.Context) (busListState, error) {
|
|
||||||
if open == nil || open.inventory == nil {
|
|
||||||
return busListState{}, errors.New("the mesh's records are not open")
|
|
||||||
}
|
|
||||||
s, err := brokerListState(ctx, open, nil)
|
|
||||||
if err != nil || s.Machine == "" || !s.Behind {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
step, err := busHeld(ctx, open.inventory, []string{s.Machine})
|
|
||||||
if err != nil {
|
|
||||||
return busListState{}, err
|
|
||||||
}
|
|
||||||
s.BusStep = step[s.Machine]
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
return busGrantBy(ask, list)
|
|
||||||
}
|
|
||||||
|
|
||||||
// busListState is the user list on the bus's machine as the controller's record knows it: whether the list
|
|
||||||
// sent there is behind the one composed now, why it could not be composed, and the bus step a send there waits
|
|
||||||
// on (that machine cannot be pushed until a person's `bus upgrade`).
|
|
||||||
type busListState struct {
|
|
||||||
Machine string
|
|
||||||
Behind bool
|
|
||||||
Uncomposed error
|
|
||||||
BusStep string
|
|
||||||
}
|
|
||||||
|
|
||||||
// remedy is the one thing that sends the bus's machine the list composed now: the plan's own reason when it
|
|
||||||
// cannot be composed, the bus upgrade while sends there wait on it, and otherwise a push of that machine.
|
|
||||||
func (s busListState) remedy(node string) string {
|
|
||||||
if s.Machine != "" {
|
|
||||||
node = s.Machine
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case s.Uncomposed != nil:
|
|
||||||
return fmt.Sprintf("the user list for %s cannot be composed (%v); `plan %s` says what to fix first",
|
|
||||||
node, s.Uncomposed, node)
|
|
||||||
case s.BusStep != "":
|
|
||||||
return fmt.Sprintf("sends to %s wait on the bus's planned step, so a push will not carry it: %s", node, s.BusStep)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("`push %s` carries it", node)
|
|
||||||
}
|
|
||||||
|
|
||||||
// busGrantBy judges the bus's word, and names for each way it is not held the one thing that makes it held:
|
|
||||||
//
|
|
||||||
// - **in force**: held;
|
|
||||||
// - **allowed, from a module older than `in_force`**: held, with the caveat that the reload is not confirmed.
|
|
||||||
// The file allows the publish, and the bus reloads within about 5 s of a change to it, so the worst case is
|
|
||||||
// one ask refused in those seconds, which is asked again (issue 369). The list check is not consulted:
|
|
||||||
// while a bus step is pending the bus's machine cannot be pushed, so any change to the composed list would
|
|
||||||
// read as behind for good and silence every ask until a person's bus upgrade (the review of PR 188);
|
|
||||||
// - **allowed, written, not reloaded** (the module gives both times): wait about 5 s;
|
|
||||||
// - **allowed, in force not known** (the module could not read a time): what is not known, said;
|
|
||||||
// - **not allowed, or the module cannot answer**: the list check decides — the remedy that sends the list
|
|
||||||
// when it is behind (busListState.remedy), and, for a module that cannot answer, held while the list sent
|
|
||||||
// is current, as main judged it.
|
|
||||||
//
|
|
||||||
// ask answers the machine holding the bus and its module's answer ("" when nothing holds the bus); list is the
|
|
||||||
// record's word on the list sent to that machine.
|
|
||||||
func busGrantBy(ask func(ctx context.Context) (string, json.RawMessage, error),
|
|
||||||
list func(ctx context.Context) (busListState, error)) grantJudge {
|
|
||||||
return func(ctx context.Context) (bool, string, error) {
|
|
||||||
node, raw, askErr := ask(ctx)
|
|
||||||
if askErr == nil && node == "" {
|
|
||||||
return false, "no module holds the bus, so nothing says what it enforces", nil
|
|
||||||
}
|
|
||||||
var answer busAnswer
|
|
||||||
if askErr == nil {
|
|
||||||
if answer, askErr = readBusAnswer(raw); askErr != nil {
|
|
||||||
askErr = fmt.Errorf("its answer could not be read: %w", askErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if askErr != nil {
|
|
||||||
if node == "" {
|
|
||||||
node = "the bus's machine"
|
|
||||||
}
|
|
||||||
s, err := list(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return false, "", fmt.Errorf("the bus module on %s could not answer (%v), and whether the list sent "+
|
|
||||||
"there is the one composed now could not be worked out: %w", node, askErr, err)
|
|
||||||
}
|
|
||||||
if s.Behind {
|
|
||||||
return false, fmt.Sprintf("the bus module on %s could not answer (%v), and the user list sent there is "+
|
|
||||||
"behind what the mesh composes; %s", node, askErr, s.remedy(node)), nil
|
|
||||||
}
|
|
||||||
return true, fmt.Sprintf("the bus module on %s could not answer (%v); judged by the user list sent there, "+
|
|
||||||
"which is the one composed now", node, askErr), nil
|
|
||||||
}
|
|
||||||
notAllowed := func(older string) (bool, string, error) {
|
|
||||||
s, err := list(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return false, "", fmt.Errorf("the user list on %s does not allow the controller to ask%s, and whether "+
|
|
||||||
"the list sent there is the one composed now could not be worked out: %w", node, older, err)
|
|
||||||
}
|
|
||||||
if s.Behind {
|
|
||||||
return false, fmt.Sprintf("the user list on %s does not allow the controller to ask%s: it is behind "+
|
|
||||||
"what the mesh composes; %s", node, older, s.remedy(node)), nil
|
|
||||||
}
|
|
||||||
return false, fmt.Sprintf("the user list on %s does not allow the controller to ask%s, though the list "+
|
|
||||||
"sent there is the one composed now; %s again", node, older, s.remedy(node)), nil
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case answer.InForce == nil && answer.Allowed:
|
|
||||||
return true, fmt.Sprintf("the user list on %s allows the controller to ask, but its reload is not "+
|
|
||||||
"confirmed: the bus module there is older than the answer whether a grant is in force, and the bus "+
|
|
||||||
"upgrade (`bus upgrade`) carries one that confirms it", node), nil
|
|
||||||
case answer.InForce == nil:
|
|
||||||
return notAllowed(" (the bus module there is older than the answer whether a grant is in force)")
|
|
||||||
case !answer.Allowed:
|
|
||||||
return notAllowed("")
|
|
||||||
case *answer.InForce:
|
|
||||||
return true, "", nil
|
|
||||||
case answer.FileWritten != "" && answer.ServerLoaded != "":
|
|
||||||
return false, fmt.Sprintf("the bus on %s has the controller's grant to ask written (%s) but has not "+
|
|
||||||
"reloaded since (last load %s); it reloads in place within about 5 s, so nothing is to be done but wait",
|
|
||||||
node, answer.FileWritten, answer.ServerLoaded), nil
|
|
||||||
}
|
|
||||||
why := answer.Why
|
|
||||||
if why == "" {
|
|
||||||
why = "it gives no reason"
|
|
||||||
}
|
|
||||||
return false, fmt.Sprintf("the user list on %s allows the controller to ask, but whether the bus enforces it "+
|
|
||||||
"is not known: %s", node, why), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// busUserCanTool is the bus module's answer to whether a user may do something, and whether it is in force.
|
|
||||||
const busUserCanTool = "nats_user_can"
|
|
||||||
|
|
||||||
// busAnswer is the bus module's answer to busUserCanTool; InForce is nil from a module older than that field,
|
|
||||||
// and FileWritten and ServerLoaded are given only when it could read both times (mesh-catalog's nats InForce).
|
|
||||||
type busAnswer struct {
|
|
||||||
Allowed bool `json:"allowed"`
|
|
||||||
InForce *bool `json:"in_force"`
|
|
||||||
Why string `json:"why"`
|
|
||||||
FileWritten string `json:"file_written"`
|
|
||||||
ServerLoaded string `json:"server_loaded"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func readBusAnswer(raw json.RawMessage) (busAnswer, error) {
|
|
||||||
var answer busAnswer
|
|
||||||
err := json.Unmarshal(raw, &answer)
|
|
||||||
return answer, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// grantLookEvery is how often the bus's user list is judged against the one its machine was last sent.
|
|
||||||
const grantLookEvery = 30 * time.Second
|
|
||||||
|
|
||||||
// channelsIn is what the channels are now, as a fingerprint: each module claiming a kind of the channel
|
|
||||||
// bench, where, promising what, and whether of its own account. An ask the router refused is asked again
|
|
||||||
// once this changes.
|
|
||||||
func channelsIn(inv *inventory.Inventory) func(ctx context.Context) string {
|
|
||||||
return func(ctx context.Context) string {
|
|
||||||
entries, err := inv.Catalogued(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
var parts []string
|
|
||||||
for _, e := range entries {
|
|
||||||
for _, c := range e.Manifest.Claims {
|
|
||||||
if c.Kind == "" || !catalogue.KindedBenches[c.Name] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
on := append([]string(nil), e.On...)
|
|
||||||
sort.Strings(on)
|
|
||||||
caps := append([]string(nil), c.Capabilities...)
|
|
||||||
sort.Strings(caps)
|
|
||||||
parts = append(parts, fmt.Sprintf("%s/%s=%s@%s[%s]own:%t", c.Name, c.Kind, e.Manifest.Module,
|
|
||||||
strings.Join(on, ","), strings.Join(caps, ","), e.Manifest.RunsAs != ""))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(parts)
|
|
||||||
return strings.Join(parts, ";")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// startAsking makes the serving controller's asker and hands it the router's words.
|
|
||||||
func startAsking(ctx context.Context, open *stores, server *link.Server, conn *nats.Conn, keeper *conditions.Keeper) {
|
|
||||||
js, err := jetstream.New(conn)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("the operator cannot be asked: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
a := servingAsker(open, conn, js, keeper)
|
|
||||||
if err := server.Decides(a); err != nil {
|
|
||||||
fmt.Printf("the operator's answers cannot be heard, so nothing is asked: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
askerFrom = a
|
|
||||||
go a.keep(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// servingAsker is the serving controller's asker, wired to the mesh: what it reads, publishes, performs and
|
|
||||||
// records, and the judgements it asks by — the bus's grant to ask among them (novox/hq issue 353).
|
|
||||||
func servingAsker(open *stores, conn *nats.Conn, js jetstream.JetStream, keeper *conditions.Keeper) *asker {
|
|
||||||
return &asker{
|
|
||||||
open: keeper.Open,
|
|
||||||
silence: func(ctx context.Context, key string, d time.Duration, by, why string) error {
|
|
||||||
_, err := keeper.Silence(ctx, key, d, by, why)
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
store: busAsked{conn: conn},
|
|
||||||
publish: func(ctx context.Context, subject string, body []byte, id string) error {
|
|
||||||
_, err := js.Publish(ctx, subject, body, jetstream.WithMsgID(id))
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
call: callAction(conn),
|
|
||||||
// A proposed settings layer is set by this controller itself on the warrant (novox/hq ADR 0277).
|
|
||||||
setLayer: setLayerIn(open),
|
|
||||||
record: func(ctx context.Context, act link.HandAct) error {
|
|
||||||
_, err := link.RecordHandAct(ctx, conn, act)
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
tell: tellThrough(conn),
|
|
||||||
routerRecord: routerRecordOf(conn, open.inventory),
|
|
||||||
routerHere: routerHereIn(open.inventory),
|
|
||||||
grantHeld: grantHeldIn(open, conn),
|
|
||||||
channels: channelsIn(open.inventory),
|
|
||||||
raise: func(ctx context.Context, obs []conditions.Observation) error {
|
|
||||||
return keeper.Reconcile(ctx, sourceAsker, obs)
|
|
||||||
},
|
|
||||||
now: time.Now,
|
|
||||||
logf: func(format string, args ...any) { fmt.Printf(format+"\n", args...) },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,186 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/compose"
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/overlay"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestAModuleAssignedAndNotPushedIsAwaitingAPushNotUncomposable(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, aKeeper())
|
|
||||||
pushedBy(t, open, "laptop") // the machine was pushed before; then the module is assigned
|
|
||||||
if _, err := assign(ctx, open, "laptop", "keeper"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The read the rest of the mesh asks still refuses it, with the composer's typed error: nothing
|
|
||||||
// can be compared about a secret that does not exist (status), and nothing here string-matches.
|
|
||||||
_, _, err := composeQuestion(ctx, open, "laptop", compose.Reading, false)
|
|
||||||
var notMade *catalogue.NotMadeError
|
|
||||||
if !errors.As(err, ¬Made) || notMade.Module != "keeper" || notMade.Name != "repository" {
|
|
||||||
t.Fatalf("a read composition did not say which secret is not made, typed: %v", err)
|
|
||||||
}
|
|
||||||
// Foreseen, it composes, names what the push makes, and made nothing.
|
|
||||||
foreseen, _, err := composeQuestion(ctx, open, "laptop", compose.Foreseeing, false)
|
|
||||||
if err != nil || len(foreseen.foreseen) != 1 || foreseen.foreseen[0] != "keeper/repository" {
|
|
||||||
t.Fatalf("foreseen: %v %v", foreseen.foreseen, err)
|
|
||||||
}
|
|
||||||
if _, held, err := open.inventory.ModuleSecretIfIssued(ctx, "laptop", "keeper", "repository"); err != nil || held {
|
|
||||||
t.Fatalf("asking ahead of the push made the secret (held %v, %v)", held, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Within the bound: nothing at all — no urgent, no warning, nobody notified.
|
|
||||||
if got := d1(t, open); len(got) != 0 {
|
|
||||||
t.Fatalf("a machine waiting for a push raised %+v", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Past the bound: a warning, not urgent, saying what the push will make.
|
|
||||||
before := awaitingPushBound
|
|
||||||
awaitingPushBound = -time.Minute
|
|
||||||
t.Cleanup(func() { awaitingPushBound = before })
|
|
||||||
got := d1(t, open)
|
|
||||||
if len(got) != 1 || got[0].Key() != "machine.laptop.awaiting-push" || got[0].Severity != conditions.Warning ||
|
|
||||||
!strings.Contains(got[0].Summary, "keeper/repository") || !strings.Contains(got[0].Summary, "push laptop") {
|
|
||||||
t.Fatalf("a machine left un-pushed past the bound: %+v", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pushed: the secret is made and D1 says nothing.
|
|
||||||
pushedBy(t, open, "laptop")
|
|
||||||
if got := d1(t, open); len(got) != 0 {
|
|
||||||
t.Fatalf("a pushed machine still raised %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestASecretThePushCannotMakeIsStillUncomposable(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, aTalker())
|
|
||||||
if _, err := assign(ctx, open, "laptop", "talker"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
got := d1(t, open)
|
|
||||||
if len(got) != 1 || got[0].Key() != "machine.laptop.uncomposable" || got[0].Severity != conditions.Urgent ||
|
|
||||||
!strings.Contains(got[0].Summary, "module issue talker --node laptop") {
|
|
||||||
t.Fatalf("a push that will be refused was not said urgently: %+v", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// And a machine whose composition fails for anything else, with a secret waiting beside it, is
|
|
||||||
// uncomposable for that — the stand-in hides nothing.
|
|
||||||
register(t, open, aKeeper())
|
|
||||||
if _, err := assign(ctx, open, "anchor", "keeper"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
one, two := rivals()
|
|
||||||
register(t, open, one)
|
|
||||||
register(t, open, two)
|
|
||||||
_, _ = assign(ctx, open, "anchor", "rival-one")
|
|
||||||
_, _ = assign(ctx, open, "anchor", "rival-two")
|
|
||||||
keys := map[string]conditions.Severity{}
|
|
||||||
for _, o := range d1(t, open) {
|
|
||||||
keys[o.Key()] = o.Severity
|
|
||||||
}
|
|
||||||
if keys["machine.anchor.uncomposable"] != conditions.Urgent {
|
|
||||||
t.Fatalf("a real failure beside a waiting secret: %v", keys)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAGivenSecretUnderAnOldKeyIsNotForeseen(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, aKeeper())
|
|
||||||
if _, err := assign(ctx, open, "laptop", "keeper"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.AcceptSecretForModule(ctx, "laptop", "keeper", "repository", "given"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
record, err := open.inventory.NodeByName(ctx, "laptop")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.RecordSealingKey(ctx, record.ID, aPublicKey(t)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
got := d1(t, open)
|
|
||||||
if len(got) != 1 || got[0].Key() != "machine.laptop.uncomposable" || !strings.Contains(got[0].Summary, "issue it again") {
|
|
||||||
t.Fatalf("a given secret under an old key: %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAMachineAwaitsAPushSinceItsOldestAssignmentSinceTheLastSend(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
register(t, open, aKeeper())
|
|
||||||
pushedBy(t, open, "laptop")
|
|
||||||
sent := time.Now()
|
|
||||||
since, err := open.inventory.AwaitingSince(ctx, "laptop")
|
|
||||||
if err != nil || since.After(sent) {
|
|
||||||
t.Fatalf("nothing assigned since the send: waiting since %v (%v), the send was before %v", since, err, sent)
|
|
||||||
}
|
|
||||||
if _, err := assign(ctx, open, "laptop", "keeper"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
since, err = open.inventory.AwaitingSince(ctx, "laptop")
|
|
||||||
if err != nil || since.Before(sent.Add(-time.Second)) {
|
|
||||||
t.Fatalf("assigned after the send: waiting since %v (%v), not from the assignment", since, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestALastSendIsReadFromTheSendAndTheReport(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
// What a machine was sent is read from the commits its assignments name (issue 1109): a module is carried by a send only
|
|
||||||
// when it was built from a commit, and a hand send on a machine with nothing assigned leaves no key to mark unknown.
|
|
||||||
if err := open.inventory.RegisterModule(ctx, aKeeper(), inventory.Source{BuiltFrom: "c-keeper"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
record, err := open.inventory.NodeByName(ctx, "laptop")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.RecordSent(ctx, record.ID, "carried", map[string]string{overlay.Name: "c-overlay"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := assign(ctx, open, "laptop", "keeper"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
sends, err := readDeliveries(ctx, open.inventory)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if sends["laptop"].settled("keeper", time.Now()) {
|
|
||||||
t.Fatal("a holder assigned and not pushed is expected to answer")
|
|
||||||
}
|
|
||||||
if !sends["laptop"].settled(overlay.Name, time.Now().Add(time.Hour)) {
|
|
||||||
t.Fatal("a module the machine was sent long ago is not expected to answer")
|
|
||||||
}
|
|
||||||
pushedBy(t, open, "laptop")
|
|
||||||
sends, err = readDeliveries(ctx, open.inventory)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if sends["laptop"].settled("keeper", time.Now()) {
|
|
||||||
t.Fatal("pushed a moment ago and not reported, a holder is already expected")
|
|
||||||
}
|
|
||||||
if !sends["laptop"].settled("keeper", time.Now().Add(reportGrace+time.Minute)) {
|
|
||||||
t.Fatal("pushed past the grace, a holder is not expected")
|
|
||||||
}
|
|
||||||
if _, ok := sends["laptop"].carried["keeper"]; !ok {
|
|
||||||
t.Fatalf("the send's builds do not carry keeper: %v", sends["laptop"].carried)
|
|
||||||
}
|
|
||||||
pushedAndApplied(t, open, "laptop")
|
|
||||||
if sends, err = readDeliveries(ctx, open.inventory); err != nil || !sends["laptop"].settled("keeper", time.Now()) {
|
|
||||||
t.Fatalf("pushed and reported applied, a holder is not expected to answer (%v)", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Between `assign` and `push` a module's own secrets are not made yet: the push makes them. D1 composed
|
|
||||||
// the machine's declaration without making anything, failed on the missing secret, and raised an urgent
|
|
||||||
// "nothing can be sent to <machine>" — seen live on 2026-10-06, a desktop notification for a machine
|
|
||||||
// the next push sent to without a word (novox/hq issue 275). D1 now composes as the push would, with a
|
|
||||||
// stand-in for what the push makes: pending, not broken, and said only past a bound, as a warning.
|
|
||||||
|
|
||||||
// aKeeper is a module with an own secret the mesh makes — a backup repository's password.
|
|
||||||
func aKeeper() catalogue.Manifest {
|
|
||||||
return catalogue.Manifest{Module: "keeper", Version: "1",
|
|
||||||
OwnSecrets: catalogue.OwnSecrets{"repository": {Path: "/var/lib/mesh/keeper/repository"}},
|
|
||||||
Resources: []map[string]any{
|
|
||||||
{"id": "state", "type": "directory", "path": "/var/lib/mesh/keeper", "mode": "0700"},
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushedBy records a send to a machine as a push does — composed on the send path, so its own secrets
|
|
||||||
// are made — without a bus to carry it.
|
|
||||||
func pushedBy(t *testing.T, open *stores, node string) string {
|
|
||||||
t.Helper()
|
|
||||||
ctx := t.Context()
|
|
||||||
plan, settings, err := resolveNode(ctx, open, node)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
declared, err := declarationFor(ctx, open, node, plan, settings)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
body, err := declared.Body()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
record, err := open.inventory.NodeByName(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
digest := digestOf(body)
|
|
||||||
// A push records the build of each module it carries (plan.go, Allocating); a module is carried by a send only when it was
|
|
||||||
// built from a commit, for what a machine was sent is read from the commits its assignments name (issue 1109).
|
|
||||||
builds := declared.Builds
|
|
||||||
if builds == nil {
|
|
||||||
current, err := open.inventory.CurrentBuilds(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
builds = map[string]string{}
|
|
||||||
for _, m := range plan.Modules {
|
|
||||||
builds[m.Module] = current[m.Module].Commit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := open.inventory.RecordSent(ctx, record.ID, digest, builds); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return digest
|
|
||||||
}
|
|
||||||
|
|
||||||
// pushedAndApplied is pushedBy, and the machine reporting it applied that declaration.
|
|
||||||
func pushedAndApplied(t *testing.T, open *stores, node string) {
|
|
||||||
t.Helper()
|
|
||||||
digest := pushedBy(t, open, node)
|
|
||||||
record, err := open.inventory.NodeByName(t.Context(), node)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := open.inventory.RecordDoing(t.Context(), record.ID, inventory.Doing{
|
|
||||||
Outcome: inventory.OutcomeApplied, Declared: digest}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// d1 runs D1 once.
|
|
||||||
func d1(t *testing.T, open *stores) []conditions.Observation {
|
|
||||||
t.Helper()
|
|
||||||
got, err := probeDeclarations(t.Context(), &doctor{open: open})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return got
|
|
||||||
}
|
|
||||||
|
|
||||||
// **THE WINDOW, REPRODUCED**: assigned and not pushed, a module whose own secret the push makes is
|
|
||||||
// waiting, not uncomposable; past the bound it is a warning naming what the push makes; pushed, nothing.
|
|
||||||
|
|
||||||
// **A REAL FAILURE IS STILL URGENT**: a secret the push would be refused on is not one it will make.
|
|
||||||
// A bus credential nobody issued (issue 203) fails the push, so D1 says it — urgent, in the push's words.
|
|
||||||
|
|
||||||
// A given secret sealed to a key the machine no longer has is not the mesh's to make again: the push is
|
|
||||||
// refused on it, so D1 is too.
|
|
||||||
|
|
||||||
// The bound is read from when the machine began waiting: the oldest assignment since its last send.
|
|
||||||
|
|
||||||
// **D3 AND D13 WAIT FOR THE SEND**: a holder is expected to answer on a machine once the machine was sent
|
|
||||||
// it and had time to report — never between assign and push.
|
|
||||||
func TestAHolderIsExpectedOnlyOnceSentAndReported(t *testing.T) {
|
|
||||||
now := time.Now()
|
|
||||||
sent := now.Add(-time.Minute)
|
|
||||||
long := now.Add(-time.Hour)
|
|
||||||
cases := []struct {
|
|
||||||
name string
|
|
||||||
send lastSend
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{"never sent", lastSend{}, false},
|
|
||||||
{"sent without it", lastSend{sent: &long, current: true, carried: map[string]string{"other": "c"}}, false},
|
|
||||||
{"sent with it, not reported yet", lastSend{sent: &sent, carried: map[string]string{"keeper": "c"}}, false},
|
|
||||||
{"sent with it and reported", lastSend{sent: &sent, current: true, carried: map[string]string{"keeper": "c"}}, true},
|
|
||||||
{"sent with it long ago, never reported", lastSend{sent: &long, carried: map[string]string{"keeper": "c"}}, true},
|
|
||||||
{"sent before builds were kept", lastSend{sent: &long, current: true}, true},
|
|
||||||
}
|
|
||||||
for _, c := range cases {
|
|
||||||
if got := c.send.settled("keeper", now); got != c.want {
|
|
||||||
t.Errorf("%s: settled %v, want %v", c.name, got, c.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// And through the stores: assigned, not in the last send; pushed and reported, carried and settled.
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"slices"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A machine out of touch when a tier is sent is not sent to and not waited for (novox/hq ADR 0435 §1, issue 1161): the
|
|
||||||
// delivery goes on with the machines heard, says the others were away, and the away ones get the builds by the send
|
|
||||||
// they are given when heard again (ADR 0353, ADR 0360 §5). Away is the silent check's one verdict, machineFacts.silent
|
|
||||||
// over the watchman's presence (ADR 0433 §3), never a second rule; a sleeping machine is away once presence says so.
|
|
||||||
|
|
||||||
// awayOf is the machines out of touch at a moment: those the silent check calls silent (a sleeping machine
|
|
||||||
// is away once presence says so). A machine
|
|
||||||
// never heard from has not joined and is not away.
|
|
||||||
func awayOf(machines []machineFacts, now time.Time) map[string]bool {
|
|
||||||
out := map[string]bool{}
|
|
||||||
for _, m := range machines {
|
|
||||||
if m.silent(now) {
|
|
||||||
out[m.name] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// judgeAway is the walk's reading of which machines are away as a tier is sent: every node with its last-heard time and
|
|
||||||
// the watchman's verdict on its presence key. A bus that cannot be asked leaves none away, which is what a walk did
|
|
||||||
// before: a machine is not passed on a reading that was not made.
|
|
||||||
var judgeAway = func(ctx context.Context, open *stores, now time.Time) (map[string]bool, error) {
|
|
||||||
nodes, err := open.inventory.Nodes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
address, err := broker.BusAddress()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
js, err := broker.Dial(address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer js.Close()
|
|
||||||
machines := make([]machineFacts, len(nodes))
|
|
||||||
for i, n := range nodes {
|
|
||||||
machines[i] = machineFacts{name: n.Name, lastHeard: n.LastSeen}
|
|
||||||
}
|
|
||||||
if err := withPresence(ctx, js.Conn(), machines, now); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return awayOf(machines, now), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// rememberAway keeps on the plan the machines of running that are away: sorted, each once.
|
|
||||||
func rememberAway(p *inventory.Plan, running []string, away map[string]bool) {
|
|
||||||
for _, n := range running {
|
|
||||||
if away[n] && !slices.Contains(p.Away, n) {
|
|
||||||
p.Away = append(p.Away, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(p.Away)
|
|
||||||
}
|
|
||||||
|
|
||||||
// runningHeard is the machines of running that the walk waits on for a module: those a send of the walk reached, and
|
|
||||||
// those it did not pass as away.
|
|
||||||
func runningHeard(p inventory.Plan, running []string, s *inventory.PlanModule) []string {
|
|
||||||
return slices.DeleteFunc(slices.Clone(running), func(n string) bool {
|
|
||||||
if !slices.Contains(p.Away, n) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
_, sent := s.Rest[n]
|
|
||||||
return !sent
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// awaySaying is what a plan says of the machines it passed as away, empty when it passed none.
|
|
||||||
func awaySaying(p inventory.Plan) string {
|
|
||||||
var said []string
|
|
||||||
for _, n := range p.Away {
|
|
||||||
said = append(said, n+" was away: sent when it is heard again")
|
|
||||||
}
|
|
||||||
return strings.Join(said, "; ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// judgedAway reads which machines are away for a tier about to be sent, and says when it cannot.
|
|
||||||
func judgedAway(ctx context.Context, open *stores, now time.Time) map[string]bool {
|
|
||||||
away, err := judgeAway(ctx, open, now)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "which machines are away cannot be read, so none is passed over: %v\n", err)
|
|
||||||
return nil // empty-on-error: none is passed over on a reading that was not made; the error is said just above
|
|
||||||
}
|
|
||||||
return away
|
|
||||||
}
|
|
||||||
@@ -1,228 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
"github.com/novox/mesh-controller/internal/testbus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 1161/AC5, with the store and a real bus and no ticker: a plan of two tiers whose first tier runs on a machine away when it
|
|
||||||
// is sent finishes that tier without waiting for it and without its bound, sends the second tier to the machine heard
|
|
||||||
// alone, and says the machine was away (issue 1161). The machine away keeps its want: it was sent nothing of the plan's
|
|
||||||
// builds, so the send it gets when it is heard again carries them (AC3).
|
|
||||||
func TestAPlanGoesOnWithoutAMachineAwayWhenItsTierIsSent(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
|
|
||||||
// laptop is away as every tier is sent; anchor is heard.
|
|
||||||
wasAway := judgeAway
|
|
||||||
judgeAway = func(context.Context, *stores, time.Time) (map[string]bool, error) {
|
|
||||||
return map[string]bool{"laptop": true}, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { judgeAway = wasAway })
|
|
||||||
|
|
||||||
js, err := broker.Dial(testbus.URL(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(js.Close)
|
|
||||||
if err := broker.AssertMeshStreams(js); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := js.EnsureControllerBuckets(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
legacy, err := js.Conn().JetStream()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
checkEvents = link.OverNATS{Conn: js.Conn(), JS: legacy}
|
|
||||||
t.Cleanup(func() { checkEvents = nil })
|
|
||||||
sentHeard := make(chan *nats.Msg, 32)
|
|
||||||
sub, err := js.Conn().ChanSubscribe(link.SeatEventSubject(link.MeshControllerSeat, link.KeySent), sentHeard)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
|
||||||
|
|
||||||
wasSaved := inventory.PlanSaved
|
|
||||||
inventory.PlanSaved = servingPlanSaved(ctx, checkEvents, open)
|
|
||||||
t.Cleanup(func() { inventory.PlanSaved = wasSaved })
|
|
||||||
|
|
||||||
// The build machine and the machines, as far as the plan meets them: an ask is counted, a send is recorded and kept.
|
|
||||||
var mu sync.Mutex
|
|
||||||
var asks []string
|
|
||||||
var sends [][]string
|
|
||||||
wasAsk := askABuild
|
|
||||||
askABuild = func(_ context.Context, source buildSource, _, _ string) (string, error) {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
id := link.NewBuildID(time.Now().Add(time.Duration(len(asks)) * time.Millisecond))
|
|
||||||
asks = append(asks, source.Repository+"#"+id)
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { askABuild = wasAsk })
|
|
||||||
digests := 0
|
|
||||||
wasSend := sendRollout
|
|
||||||
sendRollout = func(ctx context.Context, open *stores, names []string) ([]string, error) {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
sends = append(sends, append([]string(nil), names...))
|
|
||||||
current, err := open.inventory.CurrentBuilds(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
builds := map[string]string{}
|
|
||||||
for _, m := range []string{"a", "b"} {
|
|
||||||
builds[m] = current[m].Commit
|
|
||||||
}
|
|
||||||
for _, node := range names {
|
|
||||||
digests++
|
|
||||||
if err := open.inventory.RecordSent(ctx, nodeID(t, open, node), fmt.Sprintf("d-%s-%d", node, digests), builds); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return names, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { sendRollout = wasSend })
|
|
||||||
|
|
||||||
register := func(m, commit string, asked time.Time) {
|
|
||||||
t.Helper()
|
|
||||||
if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: m, Version: commit},
|
|
||||||
inventory.Source{Repository: "novox/" + m, Seat: "git", Path: "modules/" + m, BuiltFrom: commit, Head: commit,
|
|
||||||
Asked: asked}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
record := func(id, m, commit string, at time.Time) {
|
|
||||||
t.Helper()
|
|
||||||
b := inventory.Build{ID: id, Module: m, Commit: commit, Repository: "novox/" + m, Path: "modules/" + m, Asked: at, At: at}
|
|
||||||
b.Manifest, _ = json.Marshal(catalogue.Manifest{Module: m, Version: commit})
|
|
||||||
if err := inv.RecordBuild(ctx, b); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
old := time.Now().Add(-2 * time.Hour)
|
|
||||||
for _, m := range []string{"a", "b"} {
|
|
||||||
record("build-"+m+"-1", m, "c1", old)
|
|
||||||
register(m, "c1", old)
|
|
||||||
for _, n := range []string{"anchor", "laptop"} {
|
|
||||||
if _, err := inv.Assign(ctx, n, m); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, n := range []string{"anchor", "laptop"} {
|
|
||||||
if err := inv.RecordSent(ctx, nodeID(t, open, n), "d-"+n+"-c1", map[string]string{"a": "c1", "b": "c1"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
record("build-a-2", "a", "c2", time.Now().Add(-time.Minute))
|
|
||||||
register("a", "c2", time.Now().Add(-time.Minute))
|
|
||||||
|
|
||||||
// A machine's report on what it was sent, heard as the controller hears it.
|
|
||||||
listener := nudgingListener{Enrolment: link.Enrolment{Inventory: inv}}
|
|
||||||
seq := int64(0)
|
|
||||||
reports := func(node string) {
|
|
||||||
t.Helper()
|
|
||||||
last, found, err := inv.SentTo(ctx, node)
|
|
||||||
if err != nil || !found {
|
|
||||||
t.Fatalf("%s was sent nothing: %v", node, err)
|
|
||||||
}
|
|
||||||
seq++
|
|
||||||
body, _ := json.Marshal(map[string]any{"node": node, "applied": []string{"x.server"}, "declared": last.Digest,
|
|
||||||
"report_sequence": seq})
|
|
||||||
var report link.Report
|
|
||||||
if err := json.Unmarshal(body, &report); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := listener.Heard(ctx, report); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
plan := func() inventory.Plan {
|
|
||||||
t.Helper()
|
|
||||||
p, err := inv.PlanByID(ctx, "plan-1160")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
built := time.Now().UTC()
|
|
||||||
p := inventory.Plan{ID: "plan-1160", Repository: "novox/mesh-catalog", Branch: "main", Commit: "c2", Created: time.Now().UTC(),
|
|
||||||
State: inventory.PlanBuilding, Tiers: [][]string{{"a"}, {"b"}},
|
|
||||||
Delivery: &inventory.PlanDelivery{Order: []inventory.Edge{{From: "b", To: "a", Kind: edgeChangeOrder}}},
|
|
||||||
Modules: map[string]*inventory.PlanModule{
|
|
||||||
"a": {State: "built", BuiltAt: &built, Commit: "c2", Build: "build-a-2"},
|
|
||||||
"b": {},
|
|
||||||
}}
|
|
||||||
first, stopFirst := context.WithCancel(ctx)
|
|
||||||
defer stopFirst()
|
|
||||||
waitFirst := startRolloutEvents(first, open, js)
|
|
||||||
// Ends with the test before the cleanups restore what the look reads, which run after it (issue 1338).
|
|
||||||
t.Cleanup(func() { stopFirst(); waitFirst() })
|
|
||||||
if err := inv.SavePlan(ctx, &p); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
eventually(t, "tier 0 sent to anchor alone", func() bool {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
return len(sends) == 1 && strings.Join(sends[0], ",") == "anchor"
|
|
||||||
})
|
|
||||||
reports("anchor")
|
|
||||||
// The note of the move to tier 1 is read in the step that sees it: later steps of the tier say their own.
|
|
||||||
var moved string
|
|
||||||
eventually(t, "tier 1 reached on anchor's report alone", func() bool {
|
|
||||||
if q := plan(); q.Tier == 1 {
|
|
||||||
moved = q.Note
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
if !strings.Contains(moved, "laptop was away: sent when it is heard again") || strings.Contains(moved, "anchor") {
|
|
||||||
t.Errorf("1161/AC2: the plan says %q; want laptop named away and not anchor", moved)
|
|
||||||
}
|
|
||||||
// The ask for tier 1's build is the next step, and records its build id on the module.
|
|
||||||
eventually(t, "tier 1 asked", func() bool { m := plan().Modules["b"]; return m != nil && m.Build != "" })
|
|
||||||
b := plan().Modules["b"]
|
|
||||||
askedAt, _ := link.BuildAskedAt(b.Build)
|
|
||||||
record(b.Build, "b", "c2", askedAt)
|
|
||||||
register("b", "c2", askedAt)
|
|
||||||
planBuilt(ctx, open, "b", "c2", "", askedAt, b.Build)
|
|
||||||
eventually(t, "tier 1 sent to anchor alone", func() bool {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
return len(sends) == 2 && strings.Join(sends[1], ",") == "anchor"
|
|
||||||
})
|
|
||||||
// 1161/AC3: laptop was sent nothing of the plan's builds, so it still wants them for the send it gets when heard again.
|
|
||||||
was, known, err := inv.SentBuilds(ctx, "laptop")
|
|
||||||
if err != nil || !known {
|
|
||||||
t.Fatalf("1161/AC3: laptop's last send: %v, known %v", err, known)
|
|
||||||
}
|
|
||||||
current, err := inv.CurrentBuilds(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if was["a"] == current["a"].Commit || was["a"] != "c1" {
|
|
||||||
t.Errorf("1161/AC3: laptop was last sent a at %q and a is now %q; want it still at c1, owed c2", was["a"], current["a"].Commit)
|
|
||||||
}
|
|
||||||
if got := plan().Away; strings.Join(got, ",") != "laptop" {
|
|
||||||
t.Errorf("1161/AC2: the plan keeps %v away; want laptop", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A machine out of touch when a tier is sent is not sent to and not waited for (novox/hq ADR 0435 §1, issue 1161): the
|
|
||||||
// walk judges it with the silent check's one verdict, machineFacts.silent, and a sleeping machine is away once presence says so.
|
|
||||||
|
|
||||||
// 1161/AC1: away is the silent check's verdict; AC4: a machine heard is not away.
|
|
||||||
func TestAMachineIsAwayAsTheSilentCheckJudgesIt(t *testing.T) {
|
|
||||||
now := time.Now().UTC()
|
|
||||||
machines := []machineFacts{
|
|
||||||
{name: "heard", lastHeard: now.Add(-time.Minute)},
|
|
||||||
{name: "quiet", lastHeard: now.Add(-silentBound - time.Minute)},
|
|
||||||
{name: "stuck", lastHeard: now, presence: presenceSilent},
|
|
||||||
{name: "back", lastHeard: now.Add(-time.Hour), presence: presenceHeard},
|
|
||||||
{name: "new"},
|
|
||||||
}
|
|
||||||
got := awayOf(machines, now)
|
|
||||||
for node, want := range map[string]bool{"heard": false, "quiet": true, "stuck": true, "back": false,
|
|
||||||
"new": false} {
|
|
||||||
if got[node] != want {
|
|
||||||
t.Errorf("1161/AC1: %s away is %v; want %v (away: %v)", node, got[node], want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1161/AC1, AC2: the plan keeps the machines of the tier it passed as away, sorted and once.
|
|
||||||
func TestAPlanKeepsTheMachinesItPassedAsAway(t *testing.T) {
|
|
||||||
p := &inventory.Plan{}
|
|
||||||
away := map[string]bool{"g14": true, "ace": true}
|
|
||||||
rememberAway(p, []string{"anchor", "g14"}, away)
|
|
||||||
rememberAway(p, []string{"g14", "ace"}, away)
|
|
||||||
if !slices.Equal(p.Away, []string{"ace", "g14"}) {
|
|
||||||
t.Fatalf("1161/AC2: the plan keeps %v away; want ace, g14", p.Away)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1161/AC2: the plan says it, where its other passed machines are said.
|
|
||||||
func TestAPlanSaysTheMachinesItPassedAsAway(t *testing.T) {
|
|
||||||
say := awaySaying(inventory.Plan{Away: []string{"g14"}})
|
|
||||||
if !strings.Contains(say, "g14 was away: sent when it is heard again") {
|
|
||||||
t.Fatalf("1161/AC2: the plan says %q", say)
|
|
||||||
}
|
|
||||||
if got := awaySaying(inventory.Plan{}); got != "" {
|
|
||||||
t.Fatalf("1161/AC2: a plan that passed no machine says %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1161/AC1: the gate does not wait for a machine passed as away, unless a send reached it; AC4: a machine heard is waited for.
|
|
||||||
func TestAGateDoesNotWaitForAMachinePassedAsAway(t *testing.T) {
|
|
||||||
p := inventory.Plan{Away: []string{"g14"}}
|
|
||||||
none := &inventory.PlanModule{}
|
|
||||||
if got := runningHeard(p, []string{"anchor", "g14"}, none); !slices.Equal(got, []string{"anchor"}) {
|
|
||||||
t.Fatalf("1161/AC1: the gate waits on %v; want anchor alone", got)
|
|
||||||
}
|
|
||||||
sent := &inventory.PlanModule{Rest: map[string]inventory.SentDeclaration{"g14": {Digest: "d"}}}
|
|
||||||
if got := runningHeard(p, []string{"anchor", "g14"}, sent); !slices.Equal(got, []string{"anchor", "g14"}) {
|
|
||||||
t.Fatalf("1161/AC4: a machine a send reached is waited for: %v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,308 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
"github.com/novox/mesh-controller/internal/testbus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// aBackupSeatOnTheBus is one machine's node-backup holder as a test makes it: it counts the `now` asks it hears and
|
|
||||||
// answers `backed-up` with the last night it is told (novox/hq ADR 0404).
|
|
||||||
type aBackupSeatOnTheBus struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
nows []string
|
|
||||||
night map[string]any // module → lastNight
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *aBackupSeatOnTheBus) serve(t *testing.T, conn *nats.Conn, node string) {
|
|
||||||
t.Helper()
|
|
||||||
reply := func(m *nats.Msg, result any) {
|
|
||||||
body, _ := json.Marshal(map[string]any{"result": result, "error": "", "node": node})
|
|
||||||
_ = m.Respond(body)
|
|
||||||
}
|
|
||||||
now, err := conn.Subscribe(link.NodeSeatToolSubject(catalogue.BackupSeat, "now", node), func(m *nats.Msg) {
|
|
||||||
var a struct {
|
|
||||||
Module string `json:"module"`
|
|
||||||
}
|
|
||||||
_ = json.Unmarshal(m.Data, &a)
|
|
||||||
f.mu.Lock()
|
|
||||||
f.nows = append(f.nows, a.Module)
|
|
||||||
f.mu.Unlock()
|
|
||||||
reply(m, map[string]any{"started": a.Module})
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
read, err := conn.Subscribe(link.NodeSeatToolSubject(catalogue.BackupSeat, "backed-up", node), func(m *nats.Msg) {
|
|
||||||
f.mu.Lock()
|
|
||||||
defer f.mu.Unlock()
|
|
||||||
var out []map[string]any
|
|
||||||
for module, n := range f.night {
|
|
||||||
out = append(out, map[string]any{"module": module, "lastNight": n})
|
|
||||||
}
|
|
||||||
reply(m, out)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { _ = now.Unsubscribe(); _ = read.Unsubscribe() })
|
|
||||||
if err := conn.Flush(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *aBackupSeatOnTheBus) answers(module string, ok bool, words string) {
|
|
||||||
f.mu.Lock()
|
|
||||||
defer f.mu.Unlock()
|
|
||||||
f.night = map[string]any{module: map[string]any{"ok": ok, "at": time.Now().UTC().Add(time.Second), "error": words}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *aBackupSeatOnTheBus) asks() int {
|
|
||||||
f.mu.Lock()
|
|
||||||
defer f.mu.Unlock()
|
|
||||||
return len(f.nows)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Acceptance test 3 of design 47, on the controller's side: a delivery plan that moves a module keeping data waits
|
|
||||||
// for its backups on both machines before it asks a build; a backup that fails holds it, with the machine, the
|
|
||||||
// module and the backup's words, and a condition says the same; the verb the delivery's release calls asks the
|
|
||||||
// backups again, and the plan goes on. One bus and one store for the scenarios.
|
|
||||||
func TestADeliveryThatMovesABackedUpModuleWaitsForItsBackupAndAFailedOneHoldsItAndIsSaid(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
asked := asksRecorded(t)
|
|
||||||
js, err := broker.Dial(testbus.URL(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(js.Close)
|
|
||||||
before := handActConn
|
|
||||||
handActConn = js.Conn()
|
|
||||||
t.Cleanup(func() { handActConn = before })
|
|
||||||
|
|
||||||
if _, err := inv.SeedSeats(ctx, catalogue.DefaultSeats()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
register(t, open, catalogue.Manifest{Module: "keeper", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: catalogue.BackupSeat, Scope: catalogue.ScopeNode, Serves: []string{"backed-up", "now", "restore"}}}})
|
|
||||||
notes, err := catalogue.ParseManifest([]byte(`{"module":"notes","version":"1","data":{"own":[` +
|
|
||||||
`{"id":"files","path":"${dir:files}","class":"valuable","why":"people's notes"}]},` +
|
|
||||||
`"resources":[{"id":"files","type":"directory"}]}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := inv.RegisterModule(ctx, notes, inventory.Source{Repository: "novox/mesh-catalog", Seat: "git",
|
|
||||||
Path: "modules/notes", Ref: "main", BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, node := range []string{"anchor", "laptop"} {
|
|
||||||
if _, err := assign(ctx, open, node, "keeper"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := assign(ctx, open, node, "notes"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
seats := map[string]*aBackupSeatOnTheBus{"anchor": {}, "laptop": {}}
|
|
||||||
for node, s := range seats {
|
|
||||||
s.serve(t, js.Conn(), node)
|
|
||||||
}
|
|
||||||
// Short waits: the bound and the look are variables, so nothing here waits for the minutes of a real backup.
|
|
||||||
wasWithin, wasEvery := backupsWithin, backupsPollEvery
|
|
||||||
backupsWithin, backupsPollEvery = time.Minute, 10*time.Millisecond
|
|
||||||
t.Cleanup(func() { backupsWithin, backupsPollEvery = wasWithin, wasEvery })
|
|
||||||
|
|
||||||
newPlan := func(id string) {
|
|
||||||
t.Helper()
|
|
||||||
p := inventory.Plan{ID: id, Repository: "novox/mesh-catalog", Branch: "main", Commit: "c1", Created: time.Now().UTC(),
|
|
||||||
State: inventory.PlanBuilding, Tiers: [][]string{{"notes"}}, Modules: map[string]*inventory.PlanModule{"notes": {}}}
|
|
||||||
if err := inv.SavePlan(ctx, &p); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
read := func(id string) inventory.Plan {
|
|
||||||
t.Helper()
|
|
||||||
p, err := inv.PlanByID(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. Both machines are asked; while one has not answered, no build is asked.
|
|
||||||
newPlan("plan-one")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if seats["anchor"].asks() != 1 || seats["laptop"].asks() != 1 || len(*asked) != 0 {
|
|
||||||
t.Fatalf("asks anchor=%d laptop=%d builds=%v", seats["anchor"].asks(), seats["laptop"].asks(), *asked)
|
|
||||||
}
|
|
||||||
seats["anchor"].answers("notes", true, "")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if len(*asked) != 0 || read("plan-one").Backups.State != inventory.BackupPending {
|
|
||||||
t.Fatalf("a tier was asked while a backup was pending: %v %+v", *asked, read("plan-one").Backups)
|
|
||||||
}
|
|
||||||
seats["laptop"].answers("notes", true, "")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if len(*asked) != 1 || read("plan-one").Backups.State != inventory.BackupDone {
|
|
||||||
t.Fatalf("the tier was not asked once every backup was done: %v %+v", *asked, read("plan-one").Backups)
|
|
||||||
}
|
|
||||||
finish := read("plan-one")
|
|
||||||
finish.State = inventory.PlanDone
|
|
||||||
if err := inv.SavePlan(ctx, &finish); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. A backup that fails holds the plan: not failed, nothing asked, the words kept, a condition raised.
|
|
||||||
seats["anchor"].night, seats["laptop"].night = nil, nil
|
|
||||||
newPlan("plan-two")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
seats["anchor"].answers("notes", true, "")
|
|
||||||
seats["laptop"].answers("notes", false, "no space left on device")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
held := read("plan-two")
|
|
||||||
if held.State != inventory.PlanBuilding || held.Backups.State != inventory.BackupFailed || len(*asked) != 1 {
|
|
||||||
t.Fatalf("the plan was not held: %s %+v builds=%v", held.State, held.Backups, *asked)
|
|
||||||
}
|
|
||||||
var failed *inventory.PlanBackup
|
|
||||||
for i := range held.Backups.Asked {
|
|
||||||
if held.Backups.Asked[i].State == inventory.BackupFailed {
|
|
||||||
failed = &held.Backups.Asked[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if failed == nil || failed.Machine != "laptop" || failed.Module != "notes" || failed.Words != "no space left on device" ||
|
|
||||||
strings.Contains(held.Note, "tried again") {
|
|
||||||
t.Fatalf("the failed backup: %+v note=%q", failed, held.Note)
|
|
||||||
}
|
|
||||||
advancePlans(ctx, open) // not retried on the next look
|
|
||||||
if again := read("plan-two"); again.Backups.State != inventory.BackupFailed || len(*asked) != 1 ||
|
|
||||||
seats["laptop"].asks() != 2 {
|
|
||||||
t.Fatalf("a held plan was asked again by itself: %+v builds=%v asks=%d", again.Backups, *asked, seats["laptop"].asks())
|
|
||||||
}
|
|
||||||
said, err := probeBackupHeld(ctx, &doctor{open: open})
|
|
||||||
if err != nil || len(said) != 1 || said[0].Kind != kindBackupHeld || !strings.Contains(said[0].Said, "no space left on device") {
|
|
||||||
t.Fatalf("the condition: %+v %v", said, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. The verb the release calls: the failed backup is asked again, and only it, and the plan goes on.
|
|
||||||
seats["laptop"].answers("notes", false, "no space left on device")
|
|
||||||
if err := deliveryCommand(ctx, []string{"backups-again", "plan-two", "--why", "a person released it", "--by",
|
|
||||||
"mesh-delivery for jochen"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := deliveryCommand(ctx, []string{"backups-again", "plan-two", "--why", "again"}); err == nil {
|
|
||||||
t.Fatal("a plan with no failed backup was asked again")
|
|
||||||
}
|
|
||||||
seats["laptop"].night = nil
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if seats["laptop"].asks() != 3 || seats["anchor"].asks() != 2 {
|
|
||||||
t.Fatalf("asked again: laptop=%d anchor=%d", seats["laptop"].asks(), seats["anchor"].asks())
|
|
||||||
}
|
|
||||||
seats["laptop"].answers("notes", true, "")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if got := read("plan-two"); got.Backups.State != inventory.BackupDone || len(*asked) != 2 {
|
|
||||||
t.Fatalf("the plan did not go on: %+v builds=%v", got.Backups, *asked)
|
|
||||||
}
|
|
||||||
if said, _ := probeBackupHeld(ctx, &doctor{open: open}); len(said) != 0 {
|
|
||||||
t.Fatalf("the condition stays: %+v", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Issue 1086: a plan cut from a merge asked its first tier at the cut, so a module keeping data was sent without the
|
|
||||||
// backup the plan promised. The cut now takes the backups first (askFirstTierAtCut, batches.go), and the walk's own
|
|
||||||
// step asks the tier once they are done.
|
|
||||||
func TestAPlanCutFromAMergeOfABackedUpModuleAsksItsBackupAndNotItsTierUntilItIsDone(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
settings := t.TempDir() + "/merge-window.json"
|
|
||||||
if err := os.WriteFile(settings, []byte(`{"merge-window": "90s", "merge-window-at-most": "10m"}`), 0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Setenv(mergeWindowFileVar, settings)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asked := asksRecorded(t)
|
|
||||||
js, err := broker.Dial(testbus.URL(t))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(js.Close)
|
|
||||||
before := handActConn
|
|
||||||
handActConn = js.Conn()
|
|
||||||
t.Cleanup(func() { handActConn = before })
|
|
||||||
|
|
||||||
if _, err := inv.SeedSeats(ctx, catalogue.DefaultSeats()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
register(t, open, catalogue.Manifest{Module: "keeper", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: catalogue.BackupSeat, Scope: catalogue.ScopeNode, Serves: []string{"backed-up", "now", "restore"}}}})
|
|
||||||
if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: "mesh-controller", Version: "1"},
|
|
||||||
inventory.Source{Repository: "novox/mesh-controller", Seat: "git", Ref: "main", BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
notes, err := catalogue.ParseManifest([]byte(`{"module":"notes","version":"1","data":{"own":[` +
|
|
||||||
`{"id":"files","path":"${dir:files}","class":"valuable","why":"people's notes"}]},` +
|
|
||||||
`"resources":[{"id":"files","type":"directory"}]}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := inv.RegisterModule(ctx, notes, inventory.Source{Repository: "novox/notes", Seat: "git", Ref: "main",
|
|
||||||
BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, m := range []string{"keeper", "notes"} {
|
|
||||||
if _, err := assign(ctx, open, "anchor", m); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
seat := &aBackupSeatOnTheBus{}
|
|
||||||
seat.serve(t, js.Conn(), "anchor")
|
|
||||||
wasWithin, wasEvery := backupsWithin, backupsPollEvery
|
|
||||||
backupsWithin, backupsPollEvery = time.Minute, 10*time.Millisecond
|
|
||||||
t.Cleanup(func() { backupsWithin, backupsPollEvery = wasWithin, wasEvery })
|
|
||||||
|
|
||||||
// 1. A merge of notes is heard and its window closes: the batch is cut through cutBatch.
|
|
||||||
hear(t, open, repoMerge("notes", "c-notes", t0), t0.Add(time.Second))
|
|
||||||
cutAt(t, open, t0.Add(time.Second+90*time.Second))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 {
|
|
||||||
t.Fatalf("%d walk(s)", len(ws))
|
|
||||||
}
|
|
||||||
// 2. Right after the cut no tier is asked, the record has anchor/notes pending, the seat heard one `now`.
|
|
||||||
cut, err := inv.PlanByID(ctx, ws[0].ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(*asked) != 0 || askedInTier(cut) {
|
|
||||||
t.Fatalf("the first tier was asked at the cut: %v %+v", *asked, cut.Modules)
|
|
||||||
}
|
|
||||||
if cut.Backups == nil || cut.Backups.State != inventory.BackupPending || len(cut.Backups.Asked) != 1 ||
|
|
||||||
cut.Backups.Asked[0].Machine != "anchor" || cut.Backups.Asked[0].Module != "notes" {
|
|
||||||
t.Fatalf("the backups of the cut: %+v", cut.Backups)
|
|
||||||
}
|
|
||||||
if seat.asks() != 1 {
|
|
||||||
t.Fatalf("the seat heard %d `now`", seat.asks())
|
|
||||||
}
|
|
||||||
// 3. The backup answers done: the walk's own step asks the first tier, once, and asks no second backup.
|
|
||||||
seat.answers("notes", true, "")
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
after, err := inv.PlanByID(ctx, ws[0].ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(*asked) != 1 || after.Backups.State != inventory.BackupDone || seat.asks() != 1 {
|
|
||||||
t.Fatalf("builds=%v backups=%+v asks=%d", *asked, after.Backups, seat.asks())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,698 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
func windowed(t *testing.T) *stores {
|
|
||||||
t.Helper()
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
settings := t.TempDir() + "/merge-window.json"
|
|
||||||
if err := os.WriteFile(settings, []byte(`{"merge-window": "90s", "merge-window-at-most": "10m"}`), 0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Setenv(mergeWindowFileVar, settings)
|
|
||||||
noGroups(t)
|
|
||||||
if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: "mesh-controller", Version: "1"},
|
|
||||||
inventory.Source{Repository: "novox/mesh-controller", Seat: "git", Ref: "main", BuiltFrom: "c0",
|
|
||||||
Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, m := range []struct{ module, repository, path string }{
|
|
||||||
{"app", "novox/mesh-catalog", "modules/app"},
|
|
||||||
{"notes", "novox/mesh-catalog", "modules/notes"},
|
|
||||||
{"one", "novox/one", ""},
|
|
||||||
{"two", "novox/two", ""},
|
|
||||||
{"three", "novox/three", ""},
|
|
||||||
} {
|
|
||||||
if err := inv.RegisterModule(ctx, catalogue.Manifest{Module: m.module, Version: "1"},
|
|
||||||
inventory.Source{Repository: m.repository, Seat: "git", Path: m.path, Ref: "main", BuiltFrom: "c0",
|
|
||||||
Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return open
|
|
||||||
}
|
|
||||||
|
|
||||||
func hear(t *testing.T, open *stores, m link.SourceMoved, now time.Time) {
|
|
||||||
t.Helper()
|
|
||||||
if err := (following{open: open}).hearMerge(t.Context(), m, now); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cutAt(t *testing.T, open *stores, now time.Time) {
|
|
||||||
t.Helper()
|
|
||||||
if err := cutBatchesHeld(t.Context(), open, now); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTwoMergesSecondsApartAreOneWalkAtTheLaterCommit(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asked := asksWithPaths(t)
|
|
||||||
claude := catalogueMerge("553b7191claude", "app", t0)
|
|
||||||
dunst := catalogueMerge("48bda475dunst", "notes", t0.Add(17*time.Second))
|
|
||||||
hear(t, open, claude, t0.Add(time.Second))
|
|
||||||
hear(t, open, dunst, t0.Add(18*time.Second))
|
|
||||||
if ws, bs := walks(t, open); len(ws) != 0 || len(bs) != 1 {
|
|
||||||
t.Fatalf("within the window: %d walk(s), %d batch(es)", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(18*time.Second+89*time.Second))
|
|
||||||
if ws, _ := walks(t, open); len(ws) != 0 {
|
|
||||||
t.Fatalf("cut before the window closed: %+v", ws)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(18*time.Second+90*time.Second))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 1 || len(bs) != 0 {
|
|
||||||
t.Fatalf("after the window: %d walk(s), %d batch(es)", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
w := ws[0]
|
|
||||||
if w.Commit != dunst.Commit || len(w.Commits) != 1 || w.Commits[0].Commit != dunst.Commit {
|
|
||||||
t.Fatalf("the walk is at %s %+v, not the later commit", w.Commit, w.Commits)
|
|
||||||
}
|
|
||||||
for _, m := range []string{"app", "notes"} {
|
|
||||||
if _, in := w.Modules[m]; !in {
|
|
||||||
t.Fatalf("the walk does not build %s, which one of its merges moved: %v", m, w.Modules)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
want := []inventory.PlanMerge{{Repository: "novox/mesh-catalog", Commit: claude.Commit, Carried: dunst.Commit},
|
|
||||||
{Repository: "novox/mesh-catalog", Commit: dunst.Commit}}
|
|
||||||
if w.Delivery == nil || !slices.EqualFunc(w.Delivery.Merges, want, sameMerge) {
|
|
||||||
t.Fatalf("the walk answers %+v, want %+v", w.Delivery, want)
|
|
||||||
}
|
|
||||||
if len(*asked) != 2 || (*asked)[0][2] != "main" || (*asked)[1][2] != "main" {
|
|
||||||
t.Fatalf("the walk did not ask its modules at the branch, which holds the commit it carries: %v", *asked)
|
|
||||||
}
|
|
||||||
// Heard again, as the bus may hand it over twice: never a second merge, nor a second walk.
|
|
||||||
hear(t, open, claude, t0.Add(5*time.Minute))
|
|
||||||
if ws, bs := walks(t, open); len(ws) != 1 || len(bs) != 0 {
|
|
||||||
t.Fatalf("a merge heard twice made %d walk(s) and %d batch(es)", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestThreeRepositoriesInOneWindowAreOneWalk(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
groupsWaiting(t, "feat/three")
|
|
||||||
asksRecorded(t)
|
|
||||||
for i, r := range []string{"one", "two", "three"} {
|
|
||||||
hear(t, open, fromHead(repoMerge(r, "c-"+r, t0.Add(time.Duration(i)*20*time.Second)), "feat/three"),
|
|
||||||
t0.Add(time.Duration(i)*20*time.Second+time.Second))
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(41*time.Second+90*time.Second))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 1 || len(bs) != 0 {
|
|
||||||
t.Fatalf("%d walk(s), %d batch(es)", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
w := ws[0]
|
|
||||||
if len(w.Commits) != 3 || len(w.Delivery.Merges) != 3 {
|
|
||||||
t.Fatalf("the walk carries %+v and answers %+v", w.Commits, w.Delivery.Merges)
|
|
||||||
}
|
|
||||||
for _, r := range []string{"one", "two", "three"} {
|
|
||||||
if w.CommitOf("novox/"+r) != "c-"+r {
|
|
||||||
t.Fatalf("the walk carries %s at %q", r, w.CommitOf("novox/"+r))
|
|
||||||
}
|
|
||||||
if _, in := w.Modules[r]; !in {
|
|
||||||
t.Fatalf("the walk does not build %s: %v", r, w.Modules)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Each module's ask is recorded at its own repository's commit.
|
|
||||||
for _, r := range []string{"one", "two", "three"} {
|
|
||||||
q, found, err := open.inventory.BuildRequestByID(t.Context(), w.Modules[r].Build)
|
|
||||||
if err != nil || !found || q.Commit != "c-"+r {
|
|
||||||
t.Fatalf("%s's build was not recorded at its own commit: %+v %v %v", r, q, found, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAMergeRestartsTheWindowAndTheMaximumClosesIt(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
groupsWaiting(t, "feat/both", "feat/busy")
|
|
||||||
asksWithPaths(t)
|
|
||||||
hear(t, open, fromHead(repoMerge("one", "c1", t0), "feat/both"), t0)
|
|
||||||
hear(t, open, fromHead(repoMerge("two", "c2", t0.Add(80*time.Second)), "feat/both"), t0.Add(80*time.Second))
|
|
||||||
cutAt(t, open, t0.Add(100*time.Second))
|
|
||||||
if ws, _ := walks(t, open); len(ws) != 0 {
|
|
||||||
t.Fatal("the window closed 90 seconds after its first merge, not after its last")
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(170*time.Second))
|
|
||||||
if ws, _ := walks(t, open); len(ws) != 1 || len(ws[0].Commits) != 2 {
|
|
||||||
t.Fatalf("the window did not close 90 seconds after its last merge: %+v", ws)
|
|
||||||
}
|
|
||||||
|
|
||||||
busy := windowed(t)
|
|
||||||
groupsWaiting(t, "feat/both", "feat/busy")
|
|
||||||
asksWithPaths(t)
|
|
||||||
var last time.Time
|
|
||||||
for i := 0; i < 12; i++ {
|
|
||||||
last = t0.Add(time.Duration(i) * time.Minute)
|
|
||||||
repo := []string{"one", "two", "three"}[i%3]
|
|
||||||
hear(t, busy, fromHead(repoMerge(repo, "c"+string(rune('a'+i)), last), "feat/busy"), last)
|
|
||||||
if ws, _ := walks(t, busy); len(ws) > 0 {
|
|
||||||
if i != 10 {
|
|
||||||
t.Fatalf("a busy window closed at merge %d (minute %d), not at its maximum", i, i)
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ws, _ := walks(t, busy)
|
|
||||||
if len(ws) != 1 {
|
|
||||||
t.Fatalf("ten minutes of merges a minute apart were never cut: %d walk(s)", len(ws))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheAssemblingBatchIsShown(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
groupsWaiting(t, "pr/553b7191claude")
|
|
||||||
asksWithPaths(t)
|
|
||||||
var said []inventory.Plan
|
|
||||||
was := inventory.PlanSaved
|
|
||||||
inventory.PlanSaved = func(_ *inventory.Inventory, p inventory.Plan) { said = append(said, p) }
|
|
||||||
t.Cleanup(func() { inventory.PlanSaved = was })
|
|
||||||
now := time.Now().UTC()
|
|
||||||
claude := catalogueMerge("553b7191claude", "app", now.Add(-20*time.Second))
|
|
||||||
claude.Number, claude.Title = 174, "claude-code: an agent proposes a section"
|
|
||||||
dunst := catalogueMerge("48bda475dunst", "notes", now.Add(-2*time.Second))
|
|
||||||
dunst.Number, dunst.Title = 175, "dunst: the font the operator chose"
|
|
||||||
hear(t, open, claude, now.Add(-19*time.Second))
|
|
||||||
hear(t, open, dunst, now.Add(-time.Second))
|
|
||||||
hear(t, open, fromHead(repoMerge("one", "a6bc0931one", now), dunst.Head), now)
|
|
||||||
out := captured(t, func() error { return plansCommand(t.Context(), nil) })
|
|
||||||
lines := strings.Split(out, "\n")
|
|
||||||
first := lines[0]
|
|
||||||
for _, want := range []string{"assembling: ", " s left (at the latest ", "grouped: novox/mesh-catalog@48bda475 " +
|
|
||||||
"(answers 553b7191), novox/one@a6bc0931; plan not yet calculated"} {
|
|
||||||
if !strings.Contains(first, want) {
|
|
||||||
t.Fatalf("plans' first line %q does not say %q", first, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Under it, one line per repository: the pull request, what it answers, what it moves.
|
|
||||||
if len(lines) < 3 || strings.TrimSpace(lines[1]) != "mesh-catalog #175 dunst: the font the operator chose (answers "+
|
|
||||||
"#174 claude-code: an agent proposes a section) · app, notes" ||
|
|
||||||
strings.TrimSpace(lines[2]) != "one a6bc0931 · one" {
|
|
||||||
t.Fatalf("the grouped list reads %q", lines[1:4])
|
|
||||||
}
|
|
||||||
if len(said) == 0 || said[len(said)-1].State != inventory.PlanAssembling || said[len(said)-1].Delivery.Batch == nil ||
|
|
||||||
len(said[len(said)-1].Delivery.Merges) != 3 {
|
|
||||||
t.Fatalf("the batch was not said as assembling with its merges: %+v", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func captured(t *testing.T, run func() error) string {
|
|
||||||
t.Helper()
|
|
||||||
r, w, err := os.Pipe()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
was := os.Stdout
|
|
||||||
os.Stdout = w
|
|
||||||
runErr := run()
|
|
||||||
os.Stdout = was
|
|
||||||
_ = w.Close()
|
|
||||||
var b bytes.Buffer
|
|
||||||
_, _ = io.Copy(&b, r)
|
|
||||||
if runErr != nil {
|
|
||||||
t.Fatal(runErr)
|
|
||||||
}
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAMergeDuringAWalkJoinsTheNextBatch(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0), t0)
|
|
||||||
cutAt(t, open, t0.Add(90*time.Second))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 || !ws[0].Open() {
|
|
||||||
t.Fatalf("the first batch was not cut: %+v", ws)
|
|
||||||
}
|
|
||||||
first := ws[0]
|
|
||||||
hear(t, open, repoMerge("one", "c2", t0.Add(2*time.Minute)), t0.Add(2*time.Minute))
|
|
||||||
cutAt(t, open, t0.Add(5*time.Minute))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 1 || len(bs) != 1 || bs[0].State != inventory.PlanQueued || bs[0].Delivery.Batch.Behind != first.ID {
|
|
||||||
t.Fatalf("the merge during the walk: %d walk(s), batches %+v", len(ws), bs)
|
|
||||||
}
|
|
||||||
if got, _ := open.inventory.PlanByID(t.Context(), first.ID); !got.Open() {
|
|
||||||
t.Fatalf("the started walk was %s by the next merge", got.State)
|
|
||||||
}
|
|
||||||
if !strings.HasPrefix(batchWords(bs[0], t0.Add(5*time.Minute)), "queued behind "+first.ID) {
|
|
||||||
t.Fatalf("a queued batch reads %q", batchWords(bs[0], t0.Add(5*time.Minute)))
|
|
||||||
}
|
|
||||||
first.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(t.Context(), &first); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(6*time.Minute))
|
|
||||||
ws, bs = walks(t, open)
|
|
||||||
if len(ws) != 2 || len(bs) != 0 || ws[0].Commit != "c2" {
|
|
||||||
t.Fatalf("the queued batch was not cut when the walk ended: %+v %+v", ws, bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAWaitingWalkIsFoldedIntoTheNextBatch(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if err := open.inventory.RegisterModule(ctx, catalogue.Manifest{Module: "mesh-delivery", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: catalogue.DeliverySeat, Scope: catalogue.ScopeMesh}}},
|
|
||||||
inventory.Source{Repository: "novox/mesh-catalog", Seat: "git", Path: "modules/mesh-delivery", Ref: "main",
|
|
||||||
BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := open.inventory.Assign(ctx, "anchor", "mesh-delivery"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0), t0)
|
|
||||||
cutAt(t, open, t0.Add(90*time.Second))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 || !ws[0].Waiting() {
|
|
||||||
t.Fatalf("the walk does not wait for its word: %+v", ws)
|
|
||||||
}
|
|
||||||
waiting := ws[0]
|
|
||||||
hear(t, open, repoMerge("one", "c2", t0.Add(2*time.Minute)), t0.Add(2*time.Minute))
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute+90*time.Second))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(bs) != 0 || len(ws) != 2 {
|
|
||||||
t.Fatalf("%d walk(s), %d batch(es)", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
folded, err := open.inventory.PlanByID(ctx, waiting.ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
newer := ws[0]
|
|
||||||
if folded.State != inventory.PlanSuperseded || folded.Delivery.TakenOverBy != newer.ID {
|
|
||||||
t.Fatalf("the waiting walk is %s, taken over by %q", folded.State, folded.Delivery.TakenOverBy)
|
|
||||||
}
|
|
||||||
if len(newer.Delivery.Merges) != 2 || newer.CommitOf("novox/one") != "c2" {
|
|
||||||
t.Fatalf("the newer walk answers %+v", newer.Delivery.Merges)
|
|
||||||
}
|
|
||||||
if _, in := newer.Modules["one"]; !in {
|
|
||||||
t.Fatalf("what the folded walk was to build is not built: %v", newer.Modules)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestALateMergeIsAnsweredByTheWalkOfTheLaterOne(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
later := catalogueMerge("48bda475dunst", "notes", t0.Add(17*time.Second))
|
|
||||||
hear(t, open, later, t0.Add(18*time.Second))
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
w := ws[0]
|
|
||||||
w.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &w); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// Moves only notes, which the done walk built from the branch after it: answered there, at once.
|
|
||||||
hear(t, open, catalogueMerge("553b7191early", "notes", t0), t0.Add(15*time.Minute))
|
|
||||||
got, _ := open.inventory.PlanByID(ctx, w.ID)
|
|
||||||
if got.State != inventory.PlanDone || len(got.Delivery.Merges) != 2 ||
|
|
||||||
!sameMerge(got.Delivery.Merges[0], inventory.PlanMerge{Repository: "novox/mesh-catalog", Commit: "553b7191early",
|
|
||||||
Carried: later.Commit}) {
|
|
||||||
t.Fatalf("the late merge is not named by the walk that carried it: %+v", got.Delivery.Merges)
|
|
||||||
}
|
|
||||||
// Moves app, which that walk never built: the next batch walks it.
|
|
||||||
hear(t, open, catalogueMerge("1111aaaaapp", "app", t0.Add(time.Second)), t0.Add(16*time.Minute))
|
|
||||||
_, bs := walks(t, open)
|
|
||||||
if len(bs) != 1 || bs[0].Delivery.Merges[0].Commit != "1111aaaaapp" {
|
|
||||||
t.Fatalf("a late merge moving what the walk never built did not join the next batch: %+v", bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAFailedWalkWalksItsEarlierMergesAlone(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asked := asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
// Made before the cut marks their modules seen, as merges are: walked again, they are news all the same.
|
|
||||||
oldest := catalogueMerge("aaaa0001", "app", t0.Add(-3*time.Minute))
|
|
||||||
middle := catalogueMerge("bbbb0002", "app", t0.Add(-3*time.Minute+10*time.Second))
|
|
||||||
newest := catalogueMerge("cccc0003", "notes", t0.Add(-3*time.Minute+20*time.Second))
|
|
||||||
for i, m := range []link.SourceMoved{oldest, middle, newest} {
|
|
||||||
hear(t, open, m, t0.Add(time.Duration(i)*10*time.Second+time.Second))
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
failed := ws[0]
|
|
||||||
failed.State, failed.Note = inventory.PlanFailed, "notes failed to build in tier 0"
|
|
||||||
if err := open.inventory.SavePlan(ctx, &failed); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(3*time.Minute))
|
|
||||||
got, _ := open.inventory.PlanByID(ctx, failed.ID)
|
|
||||||
if got.State != inventory.PlanFailed || len(got.Delivery.Merges) != 3 {
|
|
||||||
t.Fatalf("the failed walk's record changed: %s %+v", got.State, got.Delivery.Merges)
|
|
||||||
}
|
|
||||||
ws, _ = walks(t, open)
|
|
||||||
alone := ws[0]
|
|
||||||
if alone.ID == failed.ID || alone.Commit != middle.Commit || len(alone.Delivery.Merges) != 1 ||
|
|
||||||
!sameMerge(alone.Delivery.Merges[0], inventory.PlanMerge{Repository: "novox/mesh-catalog", Commit: middle.Commit}) {
|
|
||||||
t.Fatalf("the newest earlier merge was not walked alone on its own commit: %+v", alone)
|
|
||||||
}
|
|
||||||
if _, in := alone.Modules["app"]; !in || (*asked)[len(*asked)-1] != [3]string{"novox/mesh-catalog", "modules/app",
|
|
||||||
middle.Commit} {
|
|
||||||
t.Fatalf("the merge walked alone does not build app at its own commit: %v, asked %v", alone.Modules, *asked)
|
|
||||||
}
|
|
||||||
// Retried, the failed walk would name merges the search answers now.
|
|
||||||
if _, err := retryPlan(ctx, open, failed.ID); err == nil || !strings.Contains(err.Error(), "walked alone") {
|
|
||||||
t.Fatalf("a failed walk whose merges are searched was retried: %v", err)
|
|
||||||
}
|
|
||||||
alone.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &alone); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(4*time.Minute))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if ws[0].ID != alone.ID || len(bs) != 0 {
|
|
||||||
t.Fatalf("the search went on after a merge was delivered: %+v", ws[0])
|
|
||||||
}
|
|
||||||
done, _ := open.inventory.PlanByID(ctx, alone.ID)
|
|
||||||
if len(done.Delivery.Merges) != 2 || !sameMerge(done.Delivery.Merges[0], inventory.PlanMerge{Repository: "novox/mesh-catalog",
|
|
||||||
Commit: oldest.Commit, Carried: middle.Commit}) {
|
|
||||||
t.Fatalf("the oldest merge is not answered by the walk that delivered the one after it: %+v", done.Delivery.Merges)
|
|
||||||
}
|
|
||||||
// A stopped walk starts no search: a person ended it.
|
|
||||||
hear(t, open, catalogueMerge("dddd0004", "app", t0.Add(5*time.Minute)), t0.Add(5*time.Minute))
|
|
||||||
hear(t, open, catalogueMerge("eeee0005", "notes", t0.Add(5*time.Minute+time.Second)), t0.Add(5*time.Minute+time.Second))
|
|
||||||
cutAt(t, open, t0.Add(8*time.Minute))
|
|
||||||
ws, _ = walks(t, open)
|
|
||||||
if _, err := stopWalk(ctx, open.inventory, ws[0].ID, "mesh-delivery for jochen", "not now"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(9*time.Minute))
|
|
||||||
if again, _ := walks(t, open); again[0].ID != ws[0].ID {
|
|
||||||
t.Fatalf("a stopped walk's earlier merge was walked alone: %+v", again[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAGroupsOrderBecomesTiersInsideTheWalk(t *testing.T) {
|
|
||||||
for _, grouped := range []bool{true, false} {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
for _, m := range []string{"mesh-host"} {
|
|
||||||
if err := open.inventory.RegisterModule(ctx, catalogue.Manifest{Module: m, Version: "1"},
|
|
||||||
inventory.Source{Repository: "novox/" + m, Seat: "git", Ref: "main", BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host := repoMerge("mesh-host", "h1", t0)
|
|
||||||
ctl := repoMerge("mesh-controller", "k1", t0.Add(time.Second))
|
|
||||||
host.Head, ctl.Head = "feat/together", "feat/together"
|
|
||||||
if !grouped {
|
|
||||||
ctl.Head = "feat/alone"
|
|
||||||
}
|
|
||||||
hear(t, open, ctl, t0.Add(2*time.Second))
|
|
||||||
hear(t, open, host, t0.Add(3*time.Second))
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 {
|
|
||||||
t.Fatalf("%d walks", len(ws))
|
|
||||||
}
|
|
||||||
tiers := ws[0].Tiers
|
|
||||||
if grouped {
|
|
||||||
if len(tiers) != 2 || !slices.Equal(tiers[0], []string{"mesh-host"}) ||
|
|
||||||
!slices.Equal(tiers[1], []string{"mesh-controller"}) {
|
|
||||||
t.Fatalf("the group's order is not the walk's tiers: %v", tiers)
|
|
||||||
}
|
|
||||||
} else if len(tiers) != 1 {
|
|
||||||
t.Fatalf("two merges of no group were ordered: %v", tiers)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABatchSurvivesARestart(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
groupsWaiting(t, "feat/both")
|
|
||||||
asksWithPaths(t)
|
|
||||||
hear(t, open, fromHead(repoMerge("one", "c1", t0), "feat/both"), t0)
|
|
||||||
hear(t, open, fromHead(repoMerge("two", "c2", t0.Add(30*time.Second)), "feat/both"), t0.Add(30*time.Second))
|
|
||||||
|
|
||||||
// A new controller: nothing of the first one's but the store.
|
|
||||||
again, err := openStores(t.Context())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(again.Close)
|
|
||||||
hear(t, again, fromHead(repoMerge("one", "c1", t0), "feat/both"), t0.Add(60*time.Second)) // the bus hands it over again
|
|
||||||
cutAt(t, again, t0.Add(119*time.Second))
|
|
||||||
if ws, bs := walks(t, again); len(ws) != 0 || len(bs) != 1 || len(bs[0].Delivery.Merges) != 2 {
|
|
||||||
t.Fatalf("the restarted controller's batch: %d walk(s), %+v", len(ws), bs)
|
|
||||||
}
|
|
||||||
cutAt(t, again, t0.Add(120*time.Second))
|
|
||||||
ws, bs := walks(t, again)
|
|
||||||
if len(ws) != 1 || len(bs) != 0 || len(ws[0].Delivery.Merges) != 2 {
|
|
||||||
t.Fatalf("the restarted controller did not cut the batch at its window: %+v %+v", ws, bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestALateMergeOfAOneModuleRepositoryIsNamed(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
c2 := repoMerge("one", "c2", t0.Add(-50*time.Second))
|
|
||||||
hear(t, open, c2, t0)
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
w := ws[0]
|
|
||||||
w.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &w); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0.Add(-60*time.Second)), t0.Add(15*time.Minute))
|
|
||||||
got, _ := open.inventory.PlanByID(ctx, w.ID)
|
|
||||||
if len(got.Delivery.Merges) != 2 || !sameMerge(got.Delivery.Merges[0], inventory.PlanMerge{Repository: "novox/one",
|
|
||||||
Commit: "c1", Carried: "c2"}) {
|
|
||||||
t.Fatalf("the late merge was not named by the walk that carried it: %+v", got.Delivery.Merges)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A walk of two that built only two: a late merge of one is not its to name.
|
|
||||||
hear(t, open, repoMerge("two", "d2", t0.Add(16*time.Minute)), t0.Add(16*time.Minute))
|
|
||||||
cutAt(t, open, t0.Add(18*time.Minute))
|
|
||||||
ws, _ = walks(t, open)
|
|
||||||
w = ws[0]
|
|
||||||
delete(w.Modules, "two")
|
|
||||||
w.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &w); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hear(t, open, repoMerge("two", "d1", t0.Add(15*time.Minute)), t0.Add(20*time.Minute))
|
|
||||||
if got, _ := open.inventory.PlanByID(ctx, w.ID); len(got.Delivery.Merges) != 1 {
|
|
||||||
t.Fatalf("a walk that never built what the late merge moves named it: %+v", got.Delivery.Merges)
|
|
||||||
}
|
|
||||||
if _, bs := walks(t, open); len(bs) != 1 || bs[0].Delivery.Merges[0].Commit != "d1" {
|
|
||||||
t.Fatalf("the late merge did not join the next batch: %+v", bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAWalkLetGoBesideAStartedOneStartsOnceItEnded(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
asked := asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0), t0)
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
started, _ := walks(t, open)
|
|
||||||
waiting := inventory.Plan{ID: "plan-waiting", Repository: "novox/one", Branch: "main", Commit: "d1", Created: t0,
|
|
||||||
State: inventory.PlanBuilding, Tiers: [][]string{{"one"}}, Modules: map[string]*inventory.PlanModule{"one": {}},
|
|
||||||
Delivery: &inventory.PlanDelivery{Awaits: catalogue.DeliverySeat}}
|
|
||||||
if err := open.inventory.SavePlan(ctx, &waiting); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := letGo(ctx, open.inventory, waiting.ID, catalogue.DeliverySeat, "its turn"); err != nil {
|
|
||||||
t.Fatalf("the word was refused beside %s: %v", started[0].ID, err)
|
|
||||||
}
|
|
||||||
before := len(*asked)
|
|
||||||
advanceHeld(ctx, open)
|
|
||||||
got, _ := open.inventory.PlanByID(ctx, waiting.ID)
|
|
||||||
if len(*asked) != before || !strings.Contains(got.Note, "starts once "+started[0].ID) {
|
|
||||||
t.Fatalf("a walk let go beside a started one asked (%d → %d) or does not say it waits: %q", before, len(*asked), got.Note)
|
|
||||||
}
|
|
||||||
// Read as a wait, an hour on: its note on the line, never LATE, and no tier of it late for S3.
|
|
||||||
later := time.Now().Add(time.Hour)
|
|
||||||
if line := planLine(got, later); !strings.Contains(line, "starts once "+started[0].ID) || strings.Contains(line, "LATE") {
|
|
||||||
t.Fatalf("a deferred walk reads %q", line)
|
|
||||||
}
|
|
||||||
facts, _, err := gatherPlans(ctx, open.inventory, later, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, f := range facts {
|
|
||||||
if f.id == got.ID {
|
|
||||||
t.Fatalf("a deferred walk is watched as a tier running late: %+v", f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
done := started[0]
|
|
||||||
done.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &done); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
advanceHeld(ctx, open)
|
|
||||||
if len(*asked) != before+1 {
|
|
||||||
t.Fatalf("the walk did not start once the started one ended: asked %v", *asked)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAMergeOnTheControllersPathNeverSharesABatch(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asked := asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if err := open.inventory.RegisterModule(ctx, catalogue.Manifest{Module: "mesh-delivery", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: catalogue.DeliverySeat, Scope: catalogue.ScopeMesh}}},
|
|
||||||
inventory.Source{Repository: "novox/mesh-catalog", Seat: "git", Path: "modules/mesh-delivery", Ref: "main",
|
|
||||||
BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := open.inventory.Assign(ctx, "anchor", "mesh-delivery"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hear(t, open, catalogueMerge("aaaa0001", "app", t0), t0)
|
|
||||||
hear(t, open, repoMerge("mesh-controller", "k1", t0.Add(10*time.Second)), t0.Add(10*time.Second))
|
|
||||||
hear(t, open, catalogueMerge("bbbb0002", "notes", t0.Add(20*time.Second)), t0.Add(20*time.Second))
|
|
||||||
_, bs := walks(t, open)
|
|
||||||
var ownBatch, catalogueBatch inventory.Plan
|
|
||||||
for _, b := range bs {
|
|
||||||
if b.OwnPath() {
|
|
||||||
ownBatch = b
|
|
||||||
} else {
|
|
||||||
catalogueBatch = b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(bs) != 2 || ownBatch.ID == "" || catalogueBatch.ID == "" || len(catalogueBatch.Delivery.Merges) != 2 ||
|
|
||||||
len(ownBatch.Delivery.Merges) != 1 {
|
|
||||||
t.Fatalf("a controller merge and two catalogue merges in one window: %+v", bs)
|
|
||||||
}
|
|
||||||
if !strings.Contains(batchWords(ownBatch, t0.Add(30*time.Second)), "own path") {
|
|
||||||
t.Fatalf("the own-path batch does not say so: %q", batchWords(ownBatch, t0.Add(30*time.Second)))
|
|
||||||
}
|
|
||||||
// Both windows closed, the controller's batch is cut first and starts; the catalogue batch queues behind it,
|
|
||||||
// is cut when the controller's walk ended, and waits for its word with both merges.
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 1 || ws[0].Waiting() || ws[0].CommitOf("novox/mesh-controller") != "k1" {
|
|
||||||
t.Fatalf("the controller's batch was not cut first into a started walk: %+v", ws)
|
|
||||||
}
|
|
||||||
ownWalk := ws[0]
|
|
||||||
for _, m := range []string{"app", "notes"} {
|
|
||||||
if _, in := ownWalk.Modules[m]; in {
|
|
||||||
t.Fatalf("the controller's walk builds %s, a catalogue module: it skipped its turn", m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(bs) != 1 || bs[0].State != inventory.PlanQueued || bs[0].Delivery.Batch.Behind != ownWalk.ID || bs[0].OwnPath() {
|
|
||||||
t.Fatalf("the catalogue batch does not queue behind the controller's walk: %+v", bs)
|
|
||||||
}
|
|
||||||
ownWalk.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &ownWalk); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(3*time.Minute))
|
|
||||||
ws, bs = walks(t, open)
|
|
||||||
if len(bs) != 0 || ws[0].ID != catalogueBatch.ID || !ws[0].Waiting() || len(ws[0].Delivery.Merges) != 2 {
|
|
||||||
t.Fatalf("the catalogue batch was not cut into a waiting walk once the controller's ended: %+v %+v", ws, bs)
|
|
||||||
}
|
|
||||||
for _, m := range []string{"app", "notes"} {
|
|
||||||
if _, in := ws[0].Modules[m]; !in {
|
|
||||||
t.Fatalf("the catalogue walk does not build %s: %v", m, ws[0].Modules)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, a := range *asked {
|
|
||||||
if a[1] == "modules/app" || a[1] == "modules/notes" {
|
|
||||||
t.Fatalf("a catalogue module was asked without the word: %v", *asked)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A catalogue walk waiting for its word when an own-path batch is cut keeps waiting beside the own-path
|
|
||||||
// walk, is not folded into it, and its word is taken meanwhile: it starts once the own-path walk ended.
|
|
||||||
catalogueWalk := ws[0]
|
|
||||||
hear(t, open, repoMerge("mesh-controller", "k2", t0.Add(4*time.Minute)), t0.Add(4*time.Minute))
|
|
||||||
cutAt(t, open, t0.Add(6*time.Minute))
|
|
||||||
ws, _ = walks(t, open)
|
|
||||||
kept, _ := open.inventory.PlanByID(ctx, catalogueWalk.ID)
|
|
||||||
if !kept.Waiting() || ws[0].CommitOf("novox/mesh-controller") != "k2" || ws[0].Waiting() {
|
|
||||||
t.Fatalf("the waiting catalogue walk was not kept waiting beside the controller's walk: %s %+v", kept.State, ws)
|
|
||||||
}
|
|
||||||
if _, in := ws[0].Modules["app"]; in {
|
|
||||||
t.Fatalf("the controller's walk folded the waiting catalogue walk in: %v", ws[0].Modules)
|
|
||||||
}
|
|
||||||
if _, err := letGo(ctx, open.inventory, catalogueWalk.ID, catalogue.DeliverySeat, "its turn"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
before := len(*asked)
|
|
||||||
advanceHeld(ctx, open)
|
|
||||||
if len(*asked) != before {
|
|
||||||
t.Fatalf("the catalogue walk started beside the controller's: asked %v", (*asked)[before:])
|
|
||||||
}
|
|
||||||
own2 := ws[0]
|
|
||||||
own2.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &own2); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
advanceHeld(ctx, open)
|
|
||||||
if len(*asked) < before+1 || (*asked)[before][1] != "modules/app" {
|
|
||||||
t.Fatalf("the catalogue walk did not start once the controller's ended: %v", (*asked)[before:])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestALateCatalogueMergeIsNotAnsweredByAnOwnPathWalk(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
for _, m := range []struct {
|
|
||||||
module string
|
|
||||||
claims []catalogue.Claim
|
|
||||||
}{
|
|
||||||
{"nats", nil},
|
|
||||||
{"mesh-delivery", []catalogue.Claim{{Name: catalogue.DeliverySeat, Scope: catalogue.ScopeMesh}}},
|
|
||||||
} {
|
|
||||||
if err := open.inventory.RegisterModule(ctx, catalogue.Manifest{Module: m.module, Version: "1", Claims: m.claims},
|
|
||||||
inventory.Source{Repository: "novox/mesh-catalog", Seat: "git", Path: "modules/" + m.module, Ref: "main",
|
|
||||||
BuiltFrom: "c0", Head: "c0"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err := open.inventory.Assign(ctx, "anchor", "mesh-delivery"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
mixed := link.SourceMoved{Owner: "novox", Repo: "mesh-catalog", Base: "main", Commit: "m1xed", MergedAt: t0.Format(time.RFC3339Nano),
|
|
||||||
Paths: []string{"modules/nats/module.json", "modules/app/module.json"}, ModuleDirs: []string{"modules/nats", "modules/app"},
|
|
||||||
ModuleDirsSaid: true}
|
|
||||||
hear(t, open, mixed, t0.Add(time.Second))
|
|
||||||
cutAt(t, open, t0.Add(2*time.Minute))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 || ws[0].Waiting() {
|
|
||||||
t.Fatalf("the mixed merge's walk waited, or was not cut: %+v", ws)
|
|
||||||
}
|
|
||||||
done := ws[0]
|
|
||||||
done.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &done); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hear(t, open, catalogueMerge("ear1ier", "app", t0.Add(-30*time.Second)), t0.Add(3*time.Minute))
|
|
||||||
got, _ := open.inventory.PlanByID(ctx, done.ID)
|
|
||||||
_, bs := walks(t, open)
|
|
||||||
if len(got.Delivery.Merges) != 1 || len(bs) != 1 || bs[0].OwnPath() || bs[0].Delivery.Merges[0].Commit != "ear1ier" {
|
|
||||||
t.Fatalf("the late catalogue merge was answered by the own-path walk (%+v) rather than the next catalogue batch (%+v)",
|
|
||||||
got.Delivery.Merges, bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,293 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The suite's tests of a merge, but for the merge window's own (this file's), plan the merge as it is heard: a
|
|
||||||
// window of nothing closes at once, so a merge with no walk open is cut into its walk at once, as every merge
|
|
||||||
// was planned before novox/hq ADR 0276. The window's tests set it through the controller's settings.
|
|
||||||
func init() {
|
|
||||||
mergeWindowDefault = 0
|
|
||||||
// No forge in a test: no manifest is found at any commit, so a merge draws no edge of the manifests' change
|
|
||||||
// (delivery_edges.go). A test that wants some replaces these (forgeHolding).
|
|
||||||
manifestAt = func(context.Context, string, string, string, string) (catalogue.Manifest, bool, error) {
|
|
||||||
return catalogue.Manifest{}, false, nil
|
|
||||||
}
|
|
||||||
parentOf = func(context.Context, string, string, string) (string, error) { return "", nil }
|
|
||||||
}
|
|
||||||
|
|
||||||
// windowed is a mesh whose controller's settings give a merge window of 90 seconds and at most 10 minutes, as its
|
|
||||||
// settings file says them, and
|
|
||||||
// modules built from the catalogue (app, notes) and from three repositories of their own.
|
|
||||||
|
|
||||||
// t0 is the moment a test's clock starts: merges are dated by it, and heard after it.
|
|
||||||
var t0 = time.Now().UTC().Truncate(time.Second)
|
|
||||||
|
|
||||||
// catalogueMerge is a merge of the catalogue changing one module's directory, made at a moment, from a pull
|
|
||||||
// request's head branch of its own, as the forge announces every merge. Whether its head names a delivery group
|
|
||||||
// with a member still to merge is mesh-delivery's word, which windowed stubs as no group at all (noGroups); a test
|
|
||||||
// whose merges wait for the window as a group's says so (groupsWaiting).
|
|
||||||
func catalogueMerge(commit, module string, made time.Time) link.SourceMoved {
|
|
||||||
return link.SourceMoved{Owner: "novox", Repo: "mesh-catalog", Base: "main", Head: "pr/" + commit, Commit: commit,
|
|
||||||
MergedAt: made.Format(time.RFC3339Nano), Paths: []string{"modules/" + module + "/module.json"},
|
|
||||||
ModuleDirs: []string{"modules/" + module}, ModuleDirsSaid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
// repoMerge is a merge of a repository of one module's own, from a head branch of its own (catalogueMerge).
|
|
||||||
func repoMerge(repo, commit string, made time.Time) link.SourceMoved {
|
|
||||||
return link.SourceMoved{Owner: "novox", Repo: repo, Base: "main", Head: "pr/" + commit, Commit: commit,
|
|
||||||
MergedAt: made.Format(time.RFC3339Nano), Paths: []string{"main.go"}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// noGroups stubs mesh-delivery's word on groups for a test: no head names a group.
|
|
||||||
func noGroups(t *testing.T) {
|
|
||||||
t.Helper()
|
|
||||||
was := deliveryGroupOf
|
|
||||||
deliveryGroupOf = func(context.Context, bool, string) ([]groupMember, bool, error) { return nil, false, nil }
|
|
||||||
t.Cleanup(func() { deliveryGroupOf = was })
|
|
||||||
}
|
|
||||||
|
|
||||||
// groupsWaiting stubs mesh-delivery's word on groups for a test of the merge window's own timing: each head named
|
|
||||||
// is one whose group mesh-delivery cannot say, so a merge from it waits for the window (ADR 0306 decision 2) and
|
|
||||||
// nothing holds the batch open; every other head names no group. A group with a member still to merge, which holds
|
|
||||||
// its batch open however long it takes (ADR 0418 clause 3), is groupStates'.
|
|
||||||
func groupsWaiting(t *testing.T, heads ...string) {
|
|
||||||
t.Helper()
|
|
||||||
was := deliveryGroupOf
|
|
||||||
deliveryGroupOf = func(_ context.Context, _ bool, head string) ([]groupMember, bool, error) {
|
|
||||||
if slices.Contains(heads, head) {
|
|
||||||
return nil, false, errors.New("the test's mesh-delivery does not say")
|
|
||||||
}
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { deliveryGroupOf = was })
|
|
||||||
}
|
|
||||||
|
|
||||||
// everyHeadWaits stubs mesh-delivery's word on groups for a test whose merges each wait for the window as they did
|
|
||||||
// before novox/hq ADR 0304: mesh-delivery cannot say any head's group.
|
|
||||||
func everyHeadWaits(t *testing.T) {
|
|
||||||
t.Helper()
|
|
||||||
was := deliveryGroupOf
|
|
||||||
deliveryGroupOf = func(context.Context, bool, string) ([]groupMember, bool, error) {
|
|
||||||
return nil, false, errors.New("the test's mesh-delivery does not say")
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { deliveryGroupOf = was })
|
|
||||||
}
|
|
||||||
|
|
||||||
// groupStates stubs mesh-delivery's word on one group: its members and their states, which a test changes as the
|
|
||||||
// members merge.
|
|
||||||
func groupStates(t *testing.T, head string, states map[string]string) {
|
|
||||||
t.Helper()
|
|
||||||
groupStatesFrom(t, head, states, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
// groupStatesFrom is groupStates for a mesh-delivery that does (composed) or does not yet say the group's gate is the
|
|
||||||
// composed check's: only the first holds the group's batch open for its last member.
|
|
||||||
func groupStatesFrom(t *testing.T, head string, states map[string]string, composed bool) {
|
|
||||||
t.Helper()
|
|
||||||
was := deliveryGroupOf
|
|
||||||
deliveryGroupOf = func(_ context.Context, _ bool, asked string) ([]groupMember, bool, error) {
|
|
||||||
if asked != head {
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
var members []groupMember
|
|
||||||
for repository, state := range states {
|
|
||||||
members = append(members, groupMember{Repository: repository, State: state, Composed: composed})
|
|
||||||
}
|
|
||||||
return members, true, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { deliveryGroupOf = was })
|
|
||||||
}
|
|
||||||
|
|
||||||
// fromHead is a merge from a head branch a test names: merges from one head are members of one delivery group,
|
|
||||||
// related by it (novox/hq ADR 0304 decision 1), so they share a batch whatever else they share.
|
|
||||||
func fromHead(m link.SourceMoved, head string) link.SourceMoved {
|
|
||||||
m.Head = head
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
// lone is a merge that names no head: known to be no delivery group's, it waits for no window unless it is
|
|
||||||
// related to a batch assembling (ADR 0304 decision 1).
|
|
||||||
func lone(m link.SourceMoved) link.SourceMoved {
|
|
||||||
m.Head = ""
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
// hear is a merge heard at a moment.
|
|
||||||
|
|
||||||
// cutAt is the cutter looking at a moment.
|
|
||||||
|
|
||||||
// walks is every plan record that is a walk, newest first, and the batches not yet cut.
|
|
||||||
func walks(t *testing.T, open *stores) (walks, batches []inventory.Plan) {
|
|
||||||
t.Helper()
|
|
||||||
recent, err := open.inventory.RecentPlans(t.Context(), 50)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, p := range recent {
|
|
||||||
if p.Batch() {
|
|
||||||
batches = append(batches, p)
|
|
||||||
} else {
|
|
||||||
walks = append(walks, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return walks, batches
|
|
||||||
}
|
|
||||||
|
|
||||||
// The replay of issue 362: two catalogue merges 18 seconds apart are one batch, cut once into one walk at the
|
|
||||||
// later commit, which names both merges — the earlier carried by the later.
|
|
||||||
|
|
||||||
// Merges of three repositories in one window are one walk, one commit for each.
|
|
||||||
|
|
||||||
// Each merge restarts the window: a merge at second 80 keeps the batch open until second 170; the window
|
|
||||||
// closes at most ten minutes after its first merge however busy.
|
|
||||||
|
|
||||||
// `plans` lists the batch being assembled first, in the operator's words: how long is left, when at the latest,
|
|
||||||
// what is grouped, and that the plan is not yet calculated. It says so on the bus as plan-moved, too.
|
|
||||||
|
|
||||||
// captured is what a command printed.
|
|
||||||
|
|
||||||
// A merge heard during a walk it is related to (the same repository, issue 973) joins the next batch, which is
|
|
||||||
// cut when the walk ends; the walk that started is never superseded (ADR 0276 decision 4, ADR 0407 decision 4).
|
|
||||||
|
|
||||||
// A walk waiting for its delivery's word is folded into the next batch's walk, which answers its merges; it
|
|
||||||
// names the walk that took it over.
|
|
||||||
|
|
||||||
// A merge heard after a later merge of its branch was walked is answered by that walk when it builds all it
|
|
||||||
// moves — named at once on a walk done — and joins the next batch when it does not.
|
|
||||||
|
|
||||||
// A failed walk marks nothing delivered: its earlier merges are walked alone, on their own commit, the newest
|
|
||||||
// first; the first delivered answers the older ones, and the search stops.
|
|
||||||
|
|
||||||
// A delivery group's order holds inside the walk (ADR 0249 unchanged): merges of two repositories from one head
|
|
||||||
// branch name, the node-engine's before the controller's, are tiered so; without the group, one tier.
|
|
||||||
|
|
||||||
// A restarted controller resumes the window where it stood: the batch, its merges and their times are in the
|
|
||||||
// store, a merge handed over again is not doubled, and the batch is cut when its window closes.
|
|
||||||
|
|
||||||
// The window's settings reach the controller in its settings file, read at every look; one that says no
|
|
||||||
// duration, or no file, is the default.
|
|
||||||
func TestTheWindowIsTheControllersSetting(t *testing.T) {
|
|
||||||
file := t.TempDir() + "/merge-window.json"
|
|
||||||
t.Setenv(mergeWindowFileVar, file)
|
|
||||||
for _, c := range []struct {
|
|
||||||
content string
|
|
||||||
window, atMost time.Duration
|
|
||||||
}{
|
|
||||||
{`{"merge-window": "60s", "merge-window-at-most": "5m"}`, time.Minute, 5 * time.Minute},
|
|
||||||
{`{"merge-window": 30, "merge-window-at-most": ""}`, 30 * time.Second, mergeWindowAtMostDefault},
|
|
||||||
{`{"merge-window": "soon"}`, mergeWindowDefault, mergeWindowAtMostDefault},
|
|
||||||
} {
|
|
||||||
if err := os.WriteFile(file, []byte(c.content), 0o600); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if w, m := mergeWindowOf(); w != c.window || m != c.atMost {
|
|
||||||
t.Errorf("%s reads as %s and %s, want %s and %s", c.content, w, m, c.window, c.atMost)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Setenv(mergeWindowFileVar, file+".gone")
|
|
||||||
if w, m := mergeWindowOf(); w != mergeWindowDefault || m != mergeWindowAtMostDefault {
|
|
||||||
t.Errorf("no file reads as %s and %s", w, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// S16 names every merge a waiting walk answers, never one commit a supersession may have ended (issue 362).
|
|
||||||
func TestAWaitingWalkNamesTheMergesItAnswers(t *testing.T) {
|
|
||||||
f := calm(t0)
|
|
||||||
f.waits = []waitFacts{{id: "plan-2", repository: "novox/mesh-catalog", commit: "48bda475dunst", awaits: "mesh-delivery",
|
|
||||||
since: t0.Add(-31 * time.Minute), merges: []inventory.PlanMerge{
|
|
||||||
{Repository: "novox/mesh-catalog", Commit: "553b7191claude", Carried: "48bda475dunst"},
|
|
||||||
{Repository: "novox/mesh-catalog", Commit: "48bda475dunst"}}}}
|
|
||||||
got := watchWaits(f)
|
|
||||||
if len(got) != 1 || !strings.Contains(got[0].Summary, "novox/mesh-catalog@553b7191") ||
|
|
||||||
!strings.Contains(got[0].Summary, "novox/mesh-catalog@48bda475") {
|
|
||||||
t.Fatalf("the waiting walk does not name both merges: %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A merge heard after a later merge of its branch was cut is named by that walk however its modules read since
|
|
||||||
// the cut, in a repository of one module (review of this change); a walk that never built what it moves names
|
|
||||||
// nothing, and the merge joins the next batch.
|
|
||||||
|
|
||||||
// A related walk waits for the delivery's word too: a waiting walk let go beside a started one it is related to
|
|
||||||
// (the same repository, issue 973) takes the word and starts once that one ended, asking nothing before.
|
|
||||||
|
|
||||||
// A file a merge of the batch removed and a later one brought back is not removed; one removed last is.
|
|
||||||
func TestARemovedFileIsTheLastMergesWord(t *testing.T) {
|
|
||||||
ev := func(commit string, paths, removed []string) inventory.BatchedMerge {
|
|
||||||
m := link.SourceMoved{Owner: "novox", Repo: "one", Base: "main", Commit: commit, Paths: paths, Removed: removed}
|
|
||||||
b, _ := json.Marshal(m)
|
|
||||||
return inventory.BatchedMerge{Repository: "novox/one", Branch: "main", Commit: commit, Event: b,
|
|
||||||
Merged: t0.Add(time.Duration(len(commit)) * time.Second)}
|
|
||||||
}
|
|
||||||
got := combinedMerges([]inventory.BatchedMerge{
|
|
||||||
ev("a", []string{"x/module.json", "y/module.json"}, []string{"x/module.json", "y/module.json"}),
|
|
||||||
ev("bb", []string{"x/module.json"}, nil),
|
|
||||||
ev("ccc", []string{"z.go"}, nil)})
|
|
||||||
if len(got) != 1 || got[0].Commit != "ccc" || !slices.Equal(got[0].Removed, []string{"y/module.json"}) ||
|
|
||||||
len(got[0].Paths) != 3 {
|
|
||||||
t.Fatalf("combined as %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A merge on the controller's own path never shares a batch with one that waits for mesh-delivery's word
|
|
||||||
// (decided during the build, 2026-10-10): the two are batches of their own, cut one after the other; a walk on
|
|
||||||
// the own path folds no waiting catalogue walk but batches its merges again behind it; the catalogue's walk
|
|
||||||
// still waits for the word, so no catalogue delivery skips its turn.
|
|
||||||
|
|
||||||
// `plans` names a walk by what it moves (asked by the operator, 2026-10-10): the repository's pull request and
|
|
||||||
// title, the modules it moves and the machines running them, then the state words; a record naming no pull
|
|
||||||
// request is named by its commit, and a title is cut at fifty runes.
|
|
||||||
func TestAPlanLineNamesWhatItMoves(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC)
|
|
||||||
p := inventory.Plan{ID: "plan-1", Repository: "novox/mesh-catalog", Branch: "main", Commit: "c1c1c1c1c1",
|
|
||||||
State: inventory.PlanBuilding, Tiers: [][]string{{"messenger", "telegram"}}, TierEntered: now.Add(-2 * time.Minute),
|
|
||||||
Modules: map[string]*inventory.PlanModule{"messenger": {State: "asked"}, "telegram": {State: "asked"}},
|
|
||||||
Commits: []inventory.PlanCommit{{Repository: "novox/mesh-catalog", Branch: "main", Commit: "c1c1c1c1c1"}},
|
|
||||||
Delivery: &inventory.PlanDelivery{Merges: []inventory.PlanMerge{{Repository: "novox/mesh-catalog",
|
|
||||||
Commit: "c1c1c1c1c1", Number: 175, Title: "a tap shows its outcome", Moves: []string{"telegram", "messenger"}}}}}
|
|
||||||
running := func(module string) []string {
|
|
||||||
if module == "messenger" || module == "telegram" {
|
|
||||||
return []string{"novox"}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if got, want := planLineOn(p, now, pauseView{}, tierAtLeast, running),
|
|
||||||
"mesh-catalog #175 a tap shows its outcome · messenger, telegram → novox · tier 1 of 1, building for 2m0s"; got != want {
|
|
||||||
t.Fatalf("the line reads %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
if got := planLine(p, now); !strings.HasPrefix(got, "mesh-catalog #175 a tap shows its outcome · messenger, telegram · tier") {
|
|
||||||
t.Fatalf("without the machines the line reads %q", got)
|
|
||||||
}
|
|
||||||
old := p
|
|
||||||
old.Commits, old.Delivery = nil, nil
|
|
||||||
if got := planLine(old, now); !strings.HasPrefix(got, "mesh-catalog c1c1c1c1 · tier 1 of 1") {
|
|
||||||
t.Fatalf("a record naming no pull request reads %q", got)
|
|
||||||
}
|
|
||||||
long := p
|
|
||||||
long.Delivery.Merges[0].Title = strings.Repeat("abcdefghij", 6)
|
|
||||||
if got := planHeadline(long, nil); !strings.Contains(got, "#175 "+strings.Repeat("abcdefghij", 4)+"abcdefghi…") {
|
|
||||||
t.Fatalf("a long title is not cut at fifty runes: %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sameMerge compares what a walk names of a merge: its repository, commit and the commit carrying it.
|
|
||||||
func sameMerge(a, b inventory.PlanMerge) bool {
|
|
||||||
return a.Repository == b.Repository && a.Commit == b.Commit && a.Carried == b.Carried
|
|
||||||
}
|
|
||||||
|
|
||||||
// A catalogue merge heard after a later merge of its branch was walked without the word (a merge that touched
|
|
||||||
// the bus too, on the controller's own path) is not answered by that walk while the delivery seat has a holder:
|
|
||||||
// its delivery would skip its turn. It joins the next catalogue batch.
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **Every wait of a batch has a bound and a condition** (novox/hq ADR 0276 decision 9): a batch still
|
|
||||||
// assembling a minute past its maximum while no walk is open is a cut the controller failed to make (S18),
|
|
||||||
// and a batch waiting behind an open walk longer than that walk's bound waits on a walk gone wrong (S19).
|
|
||||||
|
|
||||||
// The kinds S18 and S19 raise.
|
|
||||||
const (
|
|
||||||
kindBatchNotCut = "batch-not-cut"
|
|
||||||
kindBatchBehindWalk = "batch-behind-walk"
|
|
||||||
// kindGroupNotMerged is raised for a group whose last member has not merged by the merge window's maximum after
|
|
||||||
// its first (novox/hq ADR 0418 clause 3).
|
|
||||||
kindGroupNotMerged = "group-not-merged"
|
|
||||||
)
|
|
||||||
|
|
||||||
// batchFacts is one batch not yet cut, as the watchdogs read it.
|
|
||||||
type batchFacts struct {
|
|
||||||
id, state, grouped string
|
|
||||||
// atMost is when its window closes at the latest; closed when it closed.
|
|
||||||
atMost, closed time.Time
|
|
||||||
// behind is the open walk it waits behind, and walkBound that walk's bound: a tier's bound for each of its
|
|
||||||
// tiers.
|
|
||||||
behind string
|
|
||||||
walkBound time.Duration
|
|
||||||
// holds are the groups the batch is held for, by head, with why; created is when the batch was made, the first
|
|
||||||
// merge of the group heard; atMostWindow is the maximum a window may take (the controller's setting).
|
|
||||||
holds map[string]string
|
|
||||||
created time.Time
|
|
||||||
atMostWindow time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// gatherBatches is every batch not yet cut, with the bound of the walk it waits behind.
|
|
||||||
func gatherBatches(ctx context.Context, inv *inventory.Inventory, now time.Time) ([]batchFacts, error) {
|
|
||||||
batches, err := inv.Batches(ctx)
|
|
||||||
if err != nil || len(batches) == 0 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
bounds, err := measuredTierBounds(ctx, inv, now)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var out []batchFacts
|
|
||||||
for _, b := range batches {
|
|
||||||
_, atMostWindow := mergeWindowOf()
|
|
||||||
f := batchFacts{id: b.ID, state: b.State, grouped: groupedWords(b), created: b.Created, atMostWindow: atMostWindow}
|
|
||||||
if w := b.Delivery; w != nil && w.Batch != nil {
|
|
||||||
f.holds = w.Batch.Holds
|
|
||||||
f.atMost, f.closed, f.behind = w.Batch.AtMost, w.Batch.ClosesAt, w.Batch.Behind
|
|
||||||
if f.atMost.Before(f.closed) {
|
|
||||||
f.closed = f.atMost
|
|
||||||
}
|
|
||||||
if w.Batch.CutAtOnce && !b.Created.IsZero() {
|
|
||||||
// Its window closed when it was made, which is its latest too (ADR 0304 decision 1).
|
|
||||||
f.atMost, f.closed = b.Created, b.Created
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if f.behind != "" {
|
|
||||||
if walk, err := inv.PlanByID(ctx, f.behind); err == nil {
|
|
||||||
f.walkBound = bounds.of(walk.Repository) * time.Duration(max(1, len(walk.Tiers)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out = append(out, f)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// watchBatchesNotCut is S18: a batch still assembling a minute past its maximum, while no walk is open.
|
|
||||||
func watchBatchesNotCut(f *signalFacts) []conditions.Observation {
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, b := range f.batches {
|
|
||||||
if b.state != inventory.PlanAssembling || b.atMost.IsZero() || f.now.Sub(b.atMost) <= batchLateAfter ||
|
|
||||||
len(b.holds) > 0 { // a batch held for a group waits on a member, which has its own condition
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
late := f.now.Sub(b.atMost)
|
|
||||||
out = append(out, conditions.Observation{Scope: conditions.ScopePlan, ID: b.id, Kind: kindBatchNotCut,
|
|
||||||
Severity: conditions.Warning,
|
|
||||||
Summary: fmt.Sprintf("the batch %s is still assembling %s past its latest close (%s), with no walk open: "+
|
|
||||||
"the controller did not cut it; grouped: %s", b.id, ago(late), b.atMost.UTC().Format(time.RFC3339), b.grouped),
|
|
||||||
Said: fmt.Sprintf("assembling since %s past its maximum", ago(late)),
|
|
||||||
Headline: "Merged changes are not being delivered",
|
|
||||||
Explanation: fmt.Sprintf("Merges collected for one delivery should have been planned %s ago and were not. "+
|
|
||||||
"Nothing is lost; the mesh keeps them until it plans them.", humanDuration(late)),
|
|
||||||
Resolved: "Merged changes are being delivered again"})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// watchBatchesBehind is S19: a batch waiting behind an open walk longer than that walk's bound, naming it.
|
|
||||||
func watchBatchesBehind(f *signalFacts) []conditions.Observation {
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, b := range f.batches {
|
|
||||||
if b.state != inventory.PlanQueued || b.behind == "" || b.walkBound <= 0 || f.now.Sub(b.closed) <= b.walkBound {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
in := f.now.Sub(b.closed)
|
|
||||||
out = append(out, conditions.Observation{Scope: conditions.ScopePlan, ID: b.id, Kind: kindBatchBehindWalk,
|
|
||||||
Severity: conditions.Warning,
|
|
||||||
Summary: fmt.Sprintf("the batch %s has waited %s behind the walk %s, longer than that walk's bound (%s); "+
|
|
||||||
"grouped: %s; `plans %s` says where that walk stands", b.id, ago(in), b.behind, ago(b.walkBound),
|
|
||||||
b.grouped, b.behind),
|
|
||||||
Said: fmt.Sprintf("queued behind %s for %s", b.behind, ago(in)),
|
|
||||||
Headline: "Merged changes wait behind a slow delivery",
|
|
||||||
Explanation: fmt.Sprintf("Merges collected for the next delivery have waited %s for the delivery before "+
|
|
||||||
"them, which is taking longer than it should. Nothing is lost.", humanDuration(in)),
|
|
||||||
Resolved: "Merged changes no longer wait behind a slow delivery"})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// watchGroupsNotMerged is the condition of a batch held for a group whose last member has not merged by the merge
|
|
||||||
// window's maximum after the first member merged, naming the member and why (novox/hq ADR 0418 clause 3): nothing of
|
|
||||||
// the group is delivered while it waits.
|
|
||||||
func watchGroupsNotMerged(f *signalFacts) []conditions.Observation {
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, b := range f.batches {
|
|
||||||
if len(b.holds) == 0 || b.created.IsZero() || f.now.Sub(b.created) <= b.atMostWindow {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
heads := make([]string, 0, len(b.holds))
|
|
||||||
for h := range b.holds {
|
|
||||||
heads = append(heads, h)
|
|
||||||
}
|
|
||||||
sort.Strings(heads)
|
|
||||||
var why []string
|
|
||||||
for _, h := range heads {
|
|
||||||
why = append(why, b.holds[h])
|
|
||||||
}
|
|
||||||
waited := f.now.Sub(b.created)
|
|
||||||
out = append(out, conditions.Observation{Scope: conditions.ScopePlan, ID: b.id,
|
|
||||||
Kind: kindGroupNotMerged, Severity: conditions.Warning,
|
|
||||||
Summary: fmt.Sprintf("%s; its first member merged %s ago, past the merge window's maximum (%s), and nothing "+
|
|
||||||
"of the group is delivered while it waits (batch %s)", strings.Join(why, "; "), ago(waited), ago(b.atMostWindow), b.id),
|
|
||||||
Said: "a group waits for a member that has not merged",
|
|
||||||
Headline: "A change waits for the rest of its group to merge",
|
|
||||||
Explanation: fmt.Sprintf("The changes of a group are delivered together, once the last one is merged. "+
|
|
||||||
"One has not merged in %s. Nothing is lost; the merged ones are kept and delivered with it.",
|
|
||||||
humanDuration(waited)),
|
|
||||||
Resolved: "The group's last change merged"})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// An unrelated closed batch is cut into a walk beside an open one, and that walk asks its first tier at once
|
|
||||||
// (issue 973, novox/hq ADR 0407 decision 4): a hotfix is never held by an unrelated delivery.
|
|
||||||
func TestAnUnrelatedBatchWalksBesideAnOpenWalk(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0), t0)
|
|
||||||
cutAt(t, open, t0.Add(90*time.Second))
|
|
||||||
if ws, _ := walks(t, open); len(ws) != 1 || !ws[0].Open() {
|
|
||||||
t.Fatalf("the first walk is not open: %+v", ws)
|
|
||||||
}
|
|
||||||
heard := t0.Add(100 * time.Second)
|
|
||||||
hear(t, open, lone(repoMerge("two", "c2", heard)), heard)
|
|
||||||
advanceHeld(ctx, open)
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 2 || len(bs) != 0 {
|
|
||||||
t.Fatalf("%d walk(s) and %d batch(es); want the unrelated batch cut beside the open walk", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
for _, w := range ws {
|
|
||||||
if !w.Open() || w.Waiting() || deferred(w) || !askedAny(w) {
|
|
||||||
t.Errorf("%s (%s) is not walking: open %v, deferred %v, asked %v, note %q", w.ID, w.Named(), w.Open(),
|
|
||||||
deferred(w), askedAny(w), w.Note)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A closed batch related to an open walk is queued behind it with the reason, while one that shares nothing is
|
|
||||||
// cut beside it; the walk that started is never superseded (ADR 0276 decision 4, issue 973).
|
|
||||||
func TestARelatedBatchWaitsBehindTheOpenWalk(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0), t0)
|
|
||||||
cutAt(t, open, t0.Add(90*time.Second))
|
|
||||||
first := func() inventory.Plan { ws, _ := walks(t, open); return ws[len(ws)-1] }()
|
|
||||||
heard := t0.Add(100 * time.Second)
|
|
||||||
hear(t, open, lone(repoMerge("one", "c2", heard)), heard)
|
|
||||||
hear(t, open, lone(repoMerge("three", "c3", heard.Add(time.Second))), heard.Add(time.Second))
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 2 || len(bs) != 1 {
|
|
||||||
t.Fatalf("%d walk(s) and %d batch(es); want the unrelated merge walking and the related one queued", len(ws), len(bs))
|
|
||||||
}
|
|
||||||
b := bs[0]
|
|
||||||
words := batchWords(b, heard)
|
|
||||||
if b.State != inventory.PlanQueued || b.Delivery.Batch.Behind != first.ID || b.CommitOf("novox/one") != "c2" ||
|
|
||||||
!strings.Contains(words, "queued behind "+first.ID) || !strings.Contains(words, "both change novox/one") {
|
|
||||||
t.Fatalf("the related batch is %s, behind %q, reading %q", b.State, b.Delivery.Batch.Behind, words)
|
|
||||||
}
|
|
||||||
if got, _ := open.inventory.PlanByID(ctx, first.ID); !got.Open() {
|
|
||||||
t.Fatalf("the started walk was %s by the related merge", got.State)
|
|
||||||
}
|
|
||||||
first.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &first); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, heard.Add(2*time.Second))
|
|
||||||
if _, bs := walks(t, open); len(bs) != 0 {
|
|
||||||
t.Fatalf("the queued batch was not cut once the walk it waited for ended: %+v", bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A batch on the controller's own path never walks beside another walk, and nothing walks beside it, whatever
|
|
||||||
// the two share (issue 973): it replaces the controller, so it goes alone.
|
|
||||||
func TestAnOwnPathBatchNeverWalksBeside(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
asksWithPaths(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
hear(t, open, repoMerge("one", "c1", t0), t0)
|
|
||||||
cutAt(t, open, t0.Add(90*time.Second))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
first := ws[0]
|
|
||||||
heard := t0.Add(100 * time.Second)
|
|
||||||
hear(t, open, lone(repoMerge("mesh-controller", "k1", heard)), heard)
|
|
||||||
ws, bs := walks(t, open)
|
|
||||||
if len(ws) != 1 || len(bs) != 1 || !bs[0].OwnPath() || bs[0].State != inventory.PlanQueued ||
|
|
||||||
bs[0].Delivery.Batch.Behind != first.ID || !strings.Contains(batchWords(bs[0], heard), "own path") {
|
|
||||||
t.Fatalf("an own-path batch beside an open walk: %d walk(s), batches %+v", len(ws), bs)
|
|
||||||
}
|
|
||||||
first.State = inventory.PlanDone
|
|
||||||
if err := open.inventory.SavePlan(ctx, &first); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
cutAt(t, open, heard.Add(time.Second))
|
|
||||||
ws, _ = walks(t, open)
|
|
||||||
if len(ws) != 2 || !ws[0].Open() || ws[0].CommitOf("novox/mesh-controller") != "k1" {
|
|
||||||
t.Fatalf("the own-path batch was not cut once the walk ended: %+v", ws)
|
|
||||||
}
|
|
||||||
own := ws[0]
|
|
||||||
// Unrelated to the own-path walk by every footprint, a merge still waits for it.
|
|
||||||
later := heard.Add(time.Minute)
|
|
||||||
hear(t, open, lone(repoMerge("two", "c2", later)), later)
|
|
||||||
ws, bs = walks(t, open)
|
|
||||||
if len(ws) != 2 || len(bs) != 1 || bs[0].State != inventory.PlanQueued || bs[0].Delivery.Batch.Behind != own.ID {
|
|
||||||
t.Fatalf("a merge beside the own-path walk: %d walk(s), batches %+v", len(ws), bs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,142 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A binding to data moves only by a person (novox/hq ADR 0232, issue 273).
|
|
||||||
//
|
|
||||||
// The resolver keeps each consumer of a provision that keeps its data at the provider it was last
|
|
||||||
// sent (catalogue/bound.go) and says what it would have moved. This is where that is said: on the
|
|
||||||
// push that composed it, and by the self-check's D12 every run, as an urgent condition naming the
|
|
||||||
// consumer, both providers and the pin that confirms the move.
|
|
||||||
|
|
||||||
// The condition kinds of D12.
|
|
||||||
const (
|
|
||||||
// kindBindingKept is a move the resolver refused: the consumer is still where its data is.
|
|
||||||
kindBindingKept = "binding-kept"
|
|
||||||
// kindBindingMoved is a consumer about to be sent another provider than the one on record with
|
|
||||||
// no pin naming it — what the resolver exists to make impossible, said if it ever is not.
|
|
||||||
kindBindingMoved = "binding-moved"
|
|
||||||
// kindBindingMoving is a move a pin asked for, not yet sent: a person's act, said so that the
|
|
||||||
// data is moved before the push that carries it.
|
|
||||||
kindBindingMoving = "binding-moving"
|
|
||||||
)
|
|
||||||
|
|
||||||
// probeBindingsID is the self-check's id for this probe, and the source of what it raises.
|
|
||||||
const probeBindingsID = "D12"
|
|
||||||
|
|
||||||
// keptObservation is the urgent condition for one refused move.
|
|
||||||
func keptObservation(k catalogue.KeptBinding) conditions.Observation {
|
|
||||||
return conditions.Observation{Scope: conditions.ScopeMachine, ID: bindingID(k.Machine, k.Consumer, k.Provision),
|
|
||||||
Token: kindBindingKept, Kind: kindBindingKept, Machine: k.Machine, Also: otherMachines(k.Machine, k.Bound.Node, k.Would.Node),
|
|
||||||
Severity: conditions.Urgent, Resolver: conditions.ResolverOperator,
|
|
||||||
Summary: fmt.Sprintf("on %s, the mesh %s", k.Machine, k.String())}
|
|
||||||
}
|
|
||||||
|
|
||||||
func bindingID(machine, consumer, provision string) string {
|
|
||||||
return machine + "." + consumer + "." + provision
|
|
||||||
}
|
|
||||||
|
|
||||||
func otherMachines(machine string, nodes ...string) []string {
|
|
||||||
var out []string
|
|
||||||
seen := map[string]bool{machine: true}
|
|
||||||
for _, n := range nodes {
|
|
||||||
if n != "" && !seen[n] {
|
|
||||||
seen[n] = true
|
|
||||||
out = append(out, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// reportKept says every move a machine's resolution refused, on the push composing it, and raises its
|
|
||||||
// condition at once where this process keeps the conditions: a push is when a person is looking.
|
|
||||||
func reportKept(ctx context.Context, plan catalogue.Resolution) {
|
|
||||||
for _, k := range plan.Kept {
|
|
||||||
fmt.Printf("%s: the mesh %s\n", plan.Node, k)
|
|
||||||
if conditionsFrom != nil {
|
|
||||||
o := keptObservation(k)
|
|
||||||
o.Source = probeBindingsID
|
|
||||||
if _, err := conditionsFrom.Observe(ctx, o); err != nil {
|
|
||||||
fmt.Printf("%s: and the condition for it could not be raised: %v\n", plan.Node, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// probeBindings is D12: every consumer of a provision that keeps its data is bound where it was last
|
|
||||||
// sent, on every machine — the resolver kept it there (said, urgent, until a person pins), or a pin
|
|
||||||
// moves it (said, so the data goes first), and never anything else.
|
|
||||||
func probeBindings(ctx context.Context, d *doctor) ([]conditions.Observation, error) {
|
|
||||||
// One reading of the mesh for every machine this looks at.
|
|
||||||
ctx = withInputCache(ctx)
|
|
||||||
inv := d.open.inventory
|
|
||||||
nodes, err := inv.Nodes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, n := range nodes {
|
|
||||||
plan, _, err := resolveNode(ctx, d.open, n.Name)
|
|
||||||
if err != nil {
|
|
||||||
if unresolvable(err) {
|
|
||||||
// D1 says it, with the binding that refused it when that is why.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("%s cannot be worked out: %w", n.Name, err)
|
|
||||||
}
|
|
||||||
bound, err := inv.BindingsFor(ctx, n.Name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
pins, err := inv.PinsFor(ctx, n.Name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
out = append(out, bindingFindings(plan, bound, pins)...)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// bindingFindings is what one machine's resolution says against its record.
|
|
||||||
func bindingFindings(plan catalogue.Resolution, bound map[string]map[string]catalogue.Chosen,
|
|
||||||
pins map[string]catalogue.Chosen) []conditions.Observation {
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, k := range plan.Kept {
|
|
||||||
out = append(out, keptObservation(k))
|
|
||||||
}
|
|
||||||
said := map[string]bool{}
|
|
||||||
for _, need := range plan.Needs {
|
|
||||||
if !need.KeepsData || need.ByRecord {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
was, recorded := bound[need.For][need.Name]
|
|
||||||
now := catalogue.Chosen{Node: need.From, Module: need.Module}
|
|
||||||
if !recorded || was == now || (was.Module == "" && was.Node == now.Node) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
id := bindingID(plan.Node, need.For, need.Name)
|
|
||||||
if said[id] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
said[id] = true
|
|
||||||
o := conditions.Observation{Scope: conditions.ScopeMachine, ID: id, Machine: plan.Node,
|
|
||||||
Also: otherMachines(plan.Node, was.Node, now.Node), Resolver: conditions.ResolverOperator}
|
|
||||||
if pin, pinned := pins[need.Name]; pinned && pin.Node == now.Node && (pin.Module == "" || pin.Module == now.Module) {
|
|
||||||
o.Token, o.Kind, o.Severity = kindBindingMoving, kindBindingMoving, conditions.Warning
|
|
||||||
o.Summary = fmt.Sprintf("on %s, %s's %s moves from %s to %s at the next push, by the pin — its data "+
|
|
||||||
"is on %s: move it first", plan.Node, need.For, need.Name, was, now, was)
|
|
||||||
} else {
|
|
||||||
o.Token, o.Kind, o.Severity = kindBindingMoved, kindBindingMoved, conditions.Urgent
|
|
||||||
o.Summary = fmt.Sprintf("on %s, %s's %s would be sent %s, and it is bound to %s, where its data is, "+
|
|
||||||
"with no pin naming %s — a move nothing asked for", plan.Node, need.For, need.Name, now, was, now)
|
|
||||||
}
|
|
||||||
out = append(out, o)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTheIncidentAConsumerStaysBesideItsDataUntilAPersonPinsIt(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
if _, err := inv.SeedSeats(ctx, catalogue.DefaultSeats()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, m := range storeManifests() {
|
|
||||||
register(t, open, m)
|
|
||||||
}
|
|
||||||
assignAll := func(pairs ...[2]string) {
|
|
||||||
for _, a := range pairs {
|
|
||||||
if _, err := assign(ctx, open, a[0], a[1]); err != nil {
|
|
||||||
t.Fatalf("assign %s %s: %v", a[0], a[1], err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// The anchor's store and resolver hold the mesh's seats, on record; the laptop runs its own of each.
|
|
||||||
assignAll([2]string{"anchor", "store"}, [2]string{"anchor", "resolver"})
|
|
||||||
for _, seat := range [][2]string{{"mesh-store", "store"}, {"mesh-dns-resolver", "resolver"}} {
|
|
||||||
if err := inv.HoldSeat(ctx, seat[0], catalogue.ScopeMesh, "anchor", seat[1]); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assignAll([2]string{"laptop", "store"}, [2]string{"laptop", "resolver"}, [2]string{"laptop", "network"},
|
|
||||||
[2]string{"laptop", "board"})
|
|
||||||
|
|
||||||
plan, _, err := resolveNode(ctx, open, "laptop")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if n := need(t, plan, "board", "postgres-database"); n.From != "laptop" || n.Module != "store" || !n.KeepsData {
|
|
||||||
t.Fatalf("the consumer was bound to %s/%s (keeps data: %v); its data is beside it", n.From, n.Module, n.KeepsData)
|
|
||||||
}
|
|
||||||
if n := need(t, plan, "network", "wildcard-resolution"); n.From != "anchor" || n.KeepsData {
|
|
||||||
t.Fatalf("the resolver was bound to %s (keeps data: %v); its seat is held on anchor (issue 258)", n.From, n.KeepsData)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sent, and recorded: only the binding to data.
|
|
||||||
bindings := boundToData(plan, nil)
|
|
||||||
if len(bindings) != 1 || bindings[0].Provider != (catalogue.Chosen{Node: "laptop", Module: "store"}) {
|
|
||||||
t.Fatalf("recorded %+v", bindings)
|
|
||||||
}
|
|
||||||
if err := inv.RecordBindings(ctx, "laptop", bindings); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if found, err := probeBindings(ctx, &doctor{open: open}); err != nil || len(found) != 0 {
|
|
||||||
t.Fatalf("a mesh bound where it was sent: %+v, %v", found, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The laptop's store taken away: refused, not moved to the anchor's empty one.
|
|
||||||
if err := inv.Unassign(ctx, "laptop", "store"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, _, err := resolveNode(ctx, open, "laptop"); err == nil || !unresolvable(err) ||
|
|
||||||
!strings.Contains(err.Error(), "board on laptop is bound to laptop/store") ||
|
|
||||||
!strings.Contains(err.Error(), "pin laptop postgres-database anchor store") {
|
|
||||||
t.Fatalf("the consumer's store went and it was answered elsewhere: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A person pins the anchor's: it moves, said before it is sent, and the record keeps where it was.
|
|
||||||
if err := inv.PinProvision(ctx, "laptop", "postgres-database", "anchor", "store"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
plan, _, err = resolveNode(ctx, open, "laptop")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if n := need(t, plan, "board", "postgres-database"); n.From != "anchor" {
|
|
||||||
t.Fatalf("pinned to the anchor and bound to %s", n.From)
|
|
||||||
}
|
|
||||||
found, err := probeBindings(ctx, &doctor{open: open})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(found) != 1 || found[0].Kind != kindBindingMoving || found[0].Severity != conditions.Warning ||
|
|
||||||
!strings.Contains(found[0].Summary, "board's postgres-database moves from laptop/store to anchor/store") {
|
|
||||||
t.Fatalf("a pinned move is not said before it is sent: %+v", found)
|
|
||||||
}
|
|
||||||
if err := inv.RecordBindings(ctx, "laptop", boundToData(plan, nil)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
all, err := inv.Bindings(ctx)
|
|
||||||
if err != nil || len(all) != 1 || all[0].MovedFrom != "laptop/store" {
|
|
||||||
t.Fatalf("%+v, %v", all, err)
|
|
||||||
}
|
|
||||||
if found, err := probeBindings(ctx, &doctor{open: open}); err != nil || len(found) != 0 {
|
|
||||||
t.Fatalf("a move sent is still said: %+v, %v", found, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
)
|
|
||||||
|
|
||||||
// novox/hq issue 273, ADR 0232: a consumer of a provision that keeps its data moves only by a pin.
|
|
||||||
|
|
||||||
func storeManifests() []catalogue.Manifest {
|
|
||||||
return []catalogue.Manifest{
|
|
||||||
{Module: "store", Version: "1",
|
|
||||||
Provides: []catalogue.Offer{{Name: "postgres-database", Scope: catalogue.ScopeMesh}},
|
|
||||||
Claims: []catalogue.Claim{{Name: "mesh-store", Scope: catalogue.ScopeMesh}},
|
|
||||||
Serves: map[string]map[string]any{"postgres-database": {"port": 5432}},
|
|
||||||
Grants: map[string]string{"postgres-database": "/var/lib/mesh/store/grants"}},
|
|
||||||
{Module: "resolver", Version: "1",
|
|
||||||
Provides: []catalogue.Offer{{Name: "wildcard-resolution", Scope: catalogue.ScopeMesh}},
|
|
||||||
Claims: []catalogue.Claim{{Name: "mesh-dns-resolver", Scope: catalogue.ScopeMesh}}},
|
|
||||||
{Module: "network", Version: "1", Requires: []string{"wildcard-resolution"}},
|
|
||||||
{Module: "board", Version: "1", Requires: []string{"postgres-database"}},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func need(t *testing.T, plan catalogue.Resolution, consumer, provision string) catalogue.Needed {
|
|
||||||
t.Helper()
|
|
||||||
for _, n := range plan.Needs {
|
|
||||||
if n.For == consumer && n.Name == provision {
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Fatalf("no %s for %s: %+v", provision, consumer, plan.Needs)
|
|
||||||
return catalogue.Needed{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The incident through the stores: the laptop runs its own store and a consumer of it, the anchor's
|
|
||||||
// store holds the mesh's seat. The consumer stays beside its data, the resolver follows its seat, the
|
|
||||||
// binding is recorded as sent, and a pin — only a pin — moves it, said before the push that carries it.
|
|
||||||
|
|
||||||
// What one machine's resolution says against its record.
|
|
||||||
func TestBindingFindingsSayAKeptMoveAndAMoveNothingAskedFor(t *testing.T) {
|
|
||||||
home, anchor := catalogue.Chosen{Node: "home", Module: "store"}, catalogue.Chosen{Node: "anchor", Module: "store"}
|
|
||||||
plan := catalogue.Resolution{Node: "laptop",
|
|
||||||
Kept: []catalogue.KeptBinding{{Machine: "laptop", Consumer: "board", Provision: "postgres-database",
|
|
||||||
Bound: home, Would: anchor}},
|
|
||||||
Needs: []catalogue.Needed{
|
|
||||||
{Name: "postgres-database", For: "board", From: "home", Module: "store", KeepsData: true},
|
|
||||||
{Name: "postgres-database", For: "game", From: "anchor", Module: "store", KeepsData: true},
|
|
||||||
{Name: "wildcard-resolution", For: "network", From: "anchor", Module: "resolver"},
|
|
||||||
}}
|
|
||||||
bound := map[string]map[string]catalogue.Chosen{
|
|
||||||
"board": {"postgres-database": home},
|
|
||||||
"game": {"postgres-database": home},
|
|
||||||
"network": {"wildcard-resolution": {Node: "home", Module: "resolver"}},
|
|
||||||
}
|
|
||||||
found := linted(bindingFindings(plan, bound, nil))
|
|
||||||
if len(found) != 2 {
|
|
||||||
t.Fatalf("found %+v", found)
|
|
||||||
}
|
|
||||||
kept, moved := found[0], found[1]
|
|
||||||
if kept.Kind != kindBindingKept || kept.Severity != conditions.Urgent || kept.Machine != "laptop" ||
|
|
||||||
!strings.Contains(kept.Summary, "would move board's postgres-database from home/store to anchor/store") ||
|
|
||||||
!strings.Contains(kept.Summary, "its data is on home/store") ||
|
|
||||||
!strings.Contains(kept.Summary, "`pin laptop postgres-database anchor store` to confirm a move (and move the data first)") {
|
|
||||||
t.Errorf("kept: %+v", kept)
|
|
||||||
}
|
|
||||||
if moved.Kind != kindBindingMoved || moved.Severity != conditions.Urgent ||
|
|
||||||
!strings.Contains(moved.Summary, "game's postgres-database would be sent anchor/store") {
|
|
||||||
t.Errorf("moved: %+v", moved)
|
|
||||||
}
|
|
||||||
if kept.Key() == moved.Key() {
|
|
||||||
t.Error("two consumers, one condition")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A push says the move it refused, and raises its condition at once.
|
|
||||||
func TestAPushSaysAKeptMoveAndRaisesItsCondition(t *testing.T) {
|
|
||||||
k, _ := withConditionsInMemory(t)
|
|
||||||
reportKept(t.Context(), catalogue.Resolution{Node: "laptop", Kept: []catalogue.KeptBinding{{Machine: "laptop",
|
|
||||||
Consumer: "board", Provision: "postgres-database", Bound: catalogue.Chosen{Node: "home", Module: "store"},
|
|
||||||
Would: catalogue.Chosen{Node: "anchor", Module: "store"}}}})
|
|
||||||
open, err := k.Open(t.Context())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(open) != 1 || open[0].Kind != kindBindingKept || open[0].Severity != conditions.Urgent ||
|
|
||||||
open[0].Source != probeBindingsID {
|
|
||||||
t.Fatalf("raised %+v", open)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,324 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"net/http"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// boardCommand serves the three questions as a page.
|
|
||||||
//
|
|
||||||
// **It reads through the same functions everything else does and holds nothing**
|
|
||||||
// (novox/hq 03-DESIGN/01-to-be/11-a-board.md). The board being replaced is one service that reads
|
|
||||||
// every context's database directly — [ADR 0008](novox/hq) violated by the one component with a
|
|
||||||
// reason to violate it, and the cost is that a boundary nothing may cross can move, while one
|
|
||||||
// thing crossing it is enough to freeze it. A board that reads the provisioning tables is a board
|
|
||||||
// that breaks when provisioning changes its tables, and the change then gets weighed against the
|
|
||||||
// board.
|
|
||||||
//
|
|
||||||
// **It stores nothing of its own.** No cache that can disagree, no table of what the mesh looked
|
|
||||||
// like last time. Every request reads the mesh now; if that is slow, the answer belongs in the
|
|
||||||
// context that owns it, where everything else asking gets it too.
|
|
||||||
//
|
|
||||||
// **Reading is the whole of it.** Every action a board could offer already exists as a command,
|
|
||||||
// and a button that does something no command does is a second implementation of a decision.
|
|
||||||
func boardCommand(ctx context.Context, args []string) error {
|
|
||||||
set := flag.NewFlagSet("board", flag.ContinueOnError)
|
|
||||||
// The private network, not everything. A board says which machines are broken and what they
|
|
||||||
// are running, which is exactly the map somebody attacking this would like — and there is no
|
|
||||||
// reason for it to be reachable from further away than the mesh.
|
|
||||||
listen := set.String("listen", "127.0.0.1:8080", "where to serve it")
|
|
||||||
if _, err := parseAround(set, args); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
server := &http.Server{
|
|
||||||
Addr: *listen,
|
|
||||||
ReadHeaderTimeout: 10 * time.Second,
|
|
||||||
Handler: board(),
|
|
||||||
}
|
|
||||||
fmt.Printf("the board is on http://%s\n", *listen)
|
|
||||||
fmt.Printf(" it reads the mesh on every request and keeps nothing\n")
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
closing, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
_ = server.Shutdown(closing)
|
|
||||||
}()
|
|
||||||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// board is the handler, separate so a test can drive it without a listener.
|
|
||||||
func board() http.Handler {
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path != "/" {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
asked, err := ask(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
// **Said, not blank.** A board that cannot reach the mesh and renders an empty page
|
|
||||||
// says "nothing is wrong" in the one situation where nobody can know that.
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
|
||||||
_ = page.Execute(w, view{Unreachable: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
if err := page.Execute(w, asked); err != nil {
|
|
||||||
// The page is half-written by now; there is nothing useful left to say to the
|
|
||||||
// browser, and saying it here is what stops the failure being silent.
|
|
||||||
fmt.Printf("the board could not render: %v\n", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// The same answers for something that is not a person, from the same read. A board and a
|
|
||||||
// script disagreeing about which machine is broken would be worse than either alone.
|
|
||||||
mux.HandleFunc("/mesh.json", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
open, err := openStores(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
asked, err := theThreeQuestions(r.Context(), open)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
body, err := statusAsJSON(asked)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
_, _ = w.Write(append(body, '\n'))
|
|
||||||
})
|
|
||||||
return mux
|
|
||||||
}
|
|
||||||
|
|
||||||
// ask reads the mesh for one request.
|
|
||||||
func ask(ctx context.Context) (view, error) {
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return view{}, err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
|
|
||||||
asked, err := theThreeQuestions(ctx, open)
|
|
||||||
if err != nil {
|
|
||||||
return view{}, err
|
|
||||||
}
|
|
||||||
return viewOf(asked), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// view is what the page is given. Nothing is derived here that the reader could not derive.
|
|
||||||
type view struct {
|
|
||||||
Unreachable string
|
|
||||||
Machines int
|
|
||||||
Broken []brokenMachine
|
|
||||||
Quiet []quietMachine
|
|
||||||
Behind []staleModule
|
|
||||||
Waiting []waitingMachine
|
|
||||||
// Unresolved is every machine that cannot be worked out at all. Shown above everything else,
|
|
||||||
// because a machine here is in none of the other lists: nothing was computed for it, so it is
|
|
||||||
// not broken, not quiet and not behind — and a page without this said "all well" about a mesh
|
|
||||||
// where nothing could be sent anywhere.
|
|
||||||
Unresolved []blockedMachine
|
|
||||||
// Network is why the private network could not be computed, when it could not.
|
|
||||||
Network string
|
|
||||||
// Adopted is every node still adopted (novox/hq ADR 0100). Not broken: nothing forces the
|
|
||||||
// flip, so a node left adopted is shown rather than read as converged.
|
|
||||||
Adopted []string
|
|
||||||
At string
|
|
||||||
}
|
|
||||||
|
|
||||||
type blockedMachine struct {
|
|
||||||
Node string
|
|
||||||
// Said is the mesh's own words, a line at a time. Every unmet requirement, not the first:
|
|
||||||
// a machine is usually blocked by more than one and fixing one of them changes nothing.
|
|
||||||
Said []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type waitingMachine struct {
|
|
||||||
Node string
|
|
||||||
// Never told is not out of date: nobody has ever asked this machine to be anything. Same
|
|
||||||
// remedy, different situation, and the page says which.
|
|
||||||
Never bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokenMachine struct {
|
|
||||||
Node string
|
|
||||||
// Outcome is refused or failed, and stays distinct all the way to the page. **Refused means
|
|
||||||
// the machine is exactly as it was and what is wrong is in what was sent; failed means it is
|
|
||||||
// in a state nobody declared and what is wrong is on the machine.** They are fixed in
|
|
||||||
// different places, so one word for both would send half the readers to the wrong one.
|
|
||||||
Outcome string
|
|
||||||
Said []string
|
|
||||||
When string
|
|
||||||
}
|
|
||||||
|
|
||||||
type quietMachine struct {
|
|
||||||
Node string
|
|
||||||
// Heard is "never" or how long ago. Never heard from is not the same as quiet for a while:
|
|
||||||
// one may be a machine that was never sent anything.
|
|
||||||
Heard string
|
|
||||||
}
|
|
||||||
|
|
||||||
type staleModule struct {
|
|
||||||
Module string
|
|
||||||
Holds string
|
|
||||||
Source string
|
|
||||||
Running []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func viewOf(asked answers) view {
|
|
||||||
out := view{Machines: len(asked.nodes), At: time.Now().Format("15:04:05"),
|
|
||||||
Network: asked.network, Adopted: adoptedNodes(asked.nodes)}
|
|
||||||
var blocked []string
|
|
||||||
for name := range asked.refused {
|
|
||||||
blocked = append(blocked, name)
|
|
||||||
}
|
|
||||||
sort.Strings(blocked)
|
|
||||||
for _, name := range blocked {
|
|
||||||
one := blockedMachine{Node: name}
|
|
||||||
for _, line := range strings.Split(strings.TrimRight(asked.refused[name], "\n"), "\n") {
|
|
||||||
one.Said = append(one.Said, strings.TrimSpace(line))
|
|
||||||
}
|
|
||||||
out.Unresolved = append(out.Unresolved, one)
|
|
||||||
}
|
|
||||||
for _, d := range asked.wrong {
|
|
||||||
one := brokenMachine{Node: d.Node, Outcome: d.Outcome,
|
|
||||||
When: d.At.Local().Format("2006-01-02 15:04")}
|
|
||||||
if d.Refused != "" {
|
|
||||||
// The host's own words. It says exactly what it could not accept, and nothing
|
|
||||||
// written here would say it better.
|
|
||||||
one.Said = append(one.Said, firstLine(d.Refused))
|
|
||||||
}
|
|
||||||
for _, f := range d.Failed {
|
|
||||||
one.Said = append(one.Said, f.ID+": "+firstLine(f.Error))
|
|
||||||
}
|
|
||||||
out.Broken = append(out.Broken, one)
|
|
||||||
}
|
|
||||||
for _, n := range asked.quiet {
|
|
||||||
out.Quiet = append(out.Quiet, quietMachine{Node: n.Name, Heard: heardFrom(n)})
|
|
||||||
}
|
|
||||||
for _, m := range asked.waiting {
|
|
||||||
out.Waiting = append(out.Waiting, waitingMachine{Node: m.Node, Never: m.Never})
|
|
||||||
}
|
|
||||||
for module, on := range asked.behind {
|
|
||||||
from := asked.sources[module]
|
|
||||||
out.Behind = append(out.Behind, staleModule{
|
|
||||||
Module: module, Holds: short(from.BuiltFrom), Source: short(from.Head), Running: on,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// The page. Deliberately one file with no assets: a board that cannot render without fetching
|
|
||||||
// something is a board that is blank exactly when the mesh is unwell.
|
|
||||||
var page = template.Must(template.New("board").Parse(`<!doctype html>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<title>the mesh</title>
|
|
||||||
<style>
|
|
||||||
:root { color-scheme: light dark; }
|
|
||||||
body { font: 15px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; margin: 2rem auto;
|
|
||||||
max-width: 52rem; padding: 0 1rem; }
|
|
||||||
h1 { font-size: 1.1rem; font-weight: 600; margin: 0 0 1.5rem; }
|
|
||||||
h2 { font-size: 1rem; font-weight: 600; margin: 2rem 0 .5rem; }
|
|
||||||
.quiet { opacity: .65; }
|
|
||||||
.said { opacity: .8; padding-left: 1.5rem; }
|
|
||||||
.outcome { display: inline-block; min-width: 4.5rem; }
|
|
||||||
.refused { color: #b26b00; }
|
|
||||||
.failed { color: #c0392b; }
|
|
||||||
ul { list-style: none; padding: 0; margin: 0; }
|
|
||||||
li { padding: .15rem 0; }
|
|
||||||
footer { margin-top: 3rem; opacity: .6; font-size: .85rem; }
|
|
||||||
</style>
|
|
||||||
{{if .Unreachable}}
|
|
||||||
<h1>the mesh cannot be read</h1>
|
|
||||||
<p class="failed">{{.Unreachable}}</p>
|
|
||||||
<p class="quiet">This says nothing about whether the mesh is well — only that this page could not
|
|
||||||
find out.</p>
|
|
||||||
{{else}}
|
|
||||||
<h1>{{.Machines}} machine{{if ne .Machines 1}}s{{end}}</h1>
|
|
||||||
|
|
||||||
{{if .Unresolved}}
|
|
||||||
<h2>Can everything be worked out?</h2>
|
|
||||||
<ul>
|
|
||||||
{{range .Unresolved}}
|
|
||||||
<li><span class="outcome failed">blocked</span> <strong>{{.Node}}</strong>
|
|
||||||
{{range .Said}}<div class="said">{{.}}</div>{{end}}
|
|
||||||
</li>
|
|
||||||
{{end}}
|
|
||||||
</ul>
|
|
||||||
<p class="quiet">Nothing can be sent to a machine here, and it appears in none of the lists below:
|
|
||||||
nothing was computed for it, so there is nothing it can be behind.</p>
|
|
||||||
{{end}}
|
|
||||||
{{if .Network}}
|
|
||||||
<p class="failed">The private network could not be computed: {{.Network}}</p>
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
<h2>Is anything broken?</h2>
|
|
||||||
{{if .Broken}}
|
|
||||||
<ul>
|
|
||||||
{{range .Broken}}
|
|
||||||
<li>
|
|
||||||
<span class="outcome {{.Outcome}}">{{.Outcome}}</span>
|
|
||||||
<strong>{{.Node}}</strong> <span class="quiet">{{.When}}</span>
|
|
||||||
{{range .Said}}<div class="said">{{.}}</div>{{end}}
|
|
||||||
</li>
|
|
||||||
{{end}}
|
|
||||||
</ul>
|
|
||||||
{{else}}<p class="quiet">No. Every machine is doing what it was told.</p>{{end}}
|
|
||||||
|
|
||||||
<h2>Is anything not answering?</h2>
|
|
||||||
{{if .Quiet}}
|
|
||||||
<ul>{{range .Quiet}}<li><strong>{{.Node}}</strong> <span class="quiet">{{.Heard}}</span></li>{{end}}</ul>
|
|
||||||
<p class="quiet">Not heard from is not the same as tried and could not — a machine here may be
|
|
||||||
new, switched off, or unreachable.</p>
|
|
||||||
{{else}}<p class="quiet">No. Every machine has been heard from.</p>{{end}}
|
|
||||||
|
|
||||||
<h2>Is anything out of date?</h2>
|
|
||||||
{{if .Behind}}
|
|
||||||
<ul>
|
|
||||||
{{range .Behind}}
|
|
||||||
<li><strong>{{.Module}}</strong> holds {{.Holds}}, source has {{.Source}}
|
|
||||||
{{if .Running}}<div class="said">running on {{range $i, $n := .Running}}{{if $i}}, {{end}}{{$n}}{{end}}</div>
|
|
||||||
{{else}}<div class="said quiet">assigned to nothing</div>{{end}}
|
|
||||||
</li>
|
|
||||||
{{end}}
|
|
||||||
</ul>
|
|
||||||
{{else}}<p class="quiet">No. Every module is what its source last had.</p>{{end}}
|
|
||||||
|
|
||||||
{{if .Waiting}}
|
|
||||||
<ul>
|
|
||||||
{{range .Waiting}}
|
|
||||||
<li><strong>{{.Node}}</strong>
|
|
||||||
{{if .Never}}<span class="quiet">has never been sent anything</span>
|
|
||||||
{{else}}<span class="quiet">is not running what the mesh would send it</span>{{end}}
|
|
||||||
</li>
|
|
||||||
{{end}}
|
|
||||||
</ul>
|
|
||||||
<p class="quiet">Never told is not out of date: nobody has asked that machine to be anything yet.
|
|
||||||
Both are sent by <code>push --behind</code>.</p>
|
|
||||||
{{else}}<p class="quiet">Every machine is running what the mesh would send it.</p>{{end}}
|
|
||||||
{{if .Adopted}}
|
|
||||||
<h2>Which machines are adopted?</h2>
|
|
||||||
<ul>{{range .Adopted}}<li><strong>{{.}}</strong> <span class="quiet">adopted — what was found on it is kept until each module is taken</span></li>{{end}}</ul>
|
|
||||||
{{end}}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
<footer>Read at {{.At}}. This page holds nothing and changes nothing.</footer>
|
|
||||||
`))
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Refused and failed stay distinct all the way to the page.
|
|
||||||
//
|
|
||||||
// **Refused means the machine is exactly as it was and what is wrong is in what was sent; failed
|
|
||||||
// means it is in a state nobody declared and what is wrong is on the machine.** They are fixed in
|
|
||||||
// different places, so a page saying "error" for both sends half its readers to the wrong one.
|
|
||||||
func TestRefusedAndFailedReachThePageAsDifferentThings(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{
|
|
||||||
nodes: []inventory.Node{{Name: "anchor"}, {Name: "laptop"}},
|
|
||||||
wrong: []inventory.Doing{
|
|
||||||
{Node: "anchor", Outcome: "refused", Refused: "not a declaration this host speaks",
|
|
||||||
At: time.Now()},
|
|
||||||
{Node: "laptop", Outcome: "failed", At: time.Now(),
|
|
||||||
Failed: []inventory.FailedResource{{ID: "web.container", Error: "no such image"}}},
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
// The rendered outcome, not the word anywhere on the page: both words appear in the
|
|
||||||
// stylesheet, so a plain Contains passes whatever the machine actually said. It did.
|
|
||||||
for _, want := range []string{`<span class="outcome refused">refused</span>`,
|
|
||||||
`<span class="outcome failed">failed</span>`} {
|
|
||||||
if !strings.Contains(rendered, want) {
|
|
||||||
t.Fatalf("the page does not distinguish the two — missing %s:\n%s", want, rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// And the host's own words, which say exactly what it could not accept.
|
|
||||||
for _, want := range []string{"not a declaration this host speaks", "web.container", "no such image"} {
|
|
||||||
if !strings.Contains(rendered, want) {
|
|
||||||
t.Fatalf("the page does not say %q, so a reader must go and ask:\n%s", want, rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nothing wrong is said, not left blank. An empty page and a well mesh must not look alike.
|
|
||||||
func TestAWellMeshSaysSoRatherThanShowingNothing(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{nodes: []inventory.Node{{Name: "anchor"}}}))
|
|
||||||
for _, want := range []string{
|
|
||||||
"Every machine is doing what it was told",
|
|
||||||
"Every machine has been heard from",
|
|
||||||
"Every module is what its source last had",
|
|
||||||
} {
|
|
||||||
if !strings.Contains(rendered, want) {
|
|
||||||
t.Fatalf("a well mesh does not say %q:\n%s", want, rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A board that cannot reach the mesh must not render an empty page: that says "nothing is wrong"
|
|
||||||
// in the one situation where nobody can know it.
|
|
||||||
func TestABoardThatCannotReadTheMeshSaysSo(t *testing.T) {
|
|
||||||
rendered := render(t, view{Unreachable: "the store did not answer"})
|
|
||||||
if !strings.Contains(rendered, "the store did not answer") {
|
|
||||||
t.Fatalf("the reason is not on the page:\n%s", rendered)
|
|
||||||
}
|
|
||||||
if strings.Contains(rendered, "Every machine is doing what it was told") {
|
|
||||||
t.Fatal("a board that could not read the mesh reported that the mesh is well")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Quiet is not broken, and the page says which it is.
|
|
||||||
func TestQuietIsNotReportedAsBroken(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{
|
|
||||||
nodes: []inventory.Node{{Name: "laptop"}},
|
|
||||||
quiet: []inventory.Node{{Name: "laptop"}},
|
|
||||||
}))
|
|
||||||
if !strings.Contains(rendered, "not the same as tried and could not") {
|
|
||||||
t.Fatalf("the page does not separate quiet from broken:\n%s", rendered)
|
|
||||||
}
|
|
||||||
if !strings.Contains(rendered, "Every machine is doing what it was told") {
|
|
||||||
t.Fatalf("a quiet machine was counted as broken:\n%s", rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The page renders what a machine said, and a hostile string in it is not markup.
|
|
||||||
//
|
|
||||||
// What is on this page comes from machines, and a machine's own words are the whole reason the
|
|
||||||
// page is useful. They are also the one thing here that nobody in this repository wrote.
|
|
||||||
func TestWhatAMachineSaidIsNotMarkup(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{
|
|
||||||
nodes: []inventory.Node{{Name: "anchor"}},
|
|
||||||
wrong: []inventory.Doing{{Node: "anchor", Outcome: "refused",
|
|
||||||
Refused: `<script>alert("from the machine")</script>`, At: time.Now()}},
|
|
||||||
}))
|
|
||||||
if strings.Contains(rendered, "<script>alert") {
|
|
||||||
t.Fatalf("a machine's words were rendered as markup:\n%s", rendered)
|
|
||||||
}
|
|
||||||
if !strings.Contains(rendered, "<script>") {
|
|
||||||
t.Fatalf("the words were not shown at all, so the reader cannot see what it said:\n%s", rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A module behind its source names the machines running the old one — the part with consequences.
|
|
||||||
func TestBeingBehindNamesTheMachinesRunningTheOldOne(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{
|
|
||||||
nodes: []inventory.Node{{Name: "laptop"}},
|
|
||||||
behind: map[string][]string{"web": {"laptop"}},
|
|
||||||
sources: map[string]inventory.Source{"web": {BuiltFrom: "aaaaaaaaaa", Head: "bbbbbbbbbb"}},
|
|
||||||
}))
|
|
||||||
for _, want := range []string{"web", "aaaaaaaa", "bbbbbbbb", "running on laptop"} {
|
|
||||||
if !strings.Contains(rendered, want) {
|
|
||||||
t.Fatalf("the page does not say %q:\n%s", want, rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func render(t *testing.T, v view) string {
|
|
||||||
t.Helper()
|
|
||||||
var out strings.Builder
|
|
||||||
if err := page.Execute(&out, v); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return out.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// The board and the command must answer the same question the same way.
|
|
||||||
//
|
|
||||||
// A machine not running what the mesh would send it was added to `status` and would have been
|
|
||||||
// missing here — which is the one thing this page's design forbids: two answers to the same
|
|
||||||
// question, disagreeing, with a person in front of each.
|
|
||||||
func TestTheBoardSaysWhichMachinesHaveNotBeenSentWhatTheyShouldBe(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{
|
|
||||||
nodes: []inventory.Node{{Name: "anchor"}, {Name: "laptop"}},
|
|
||||||
waiting: []inventory.Machine{
|
|
||||||
{Node: "anchor"},
|
|
||||||
{Node: "laptop", Never: true},
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
if !strings.Contains(rendered, "is not running what the mesh would send it") {
|
|
||||||
t.Fatalf("the page does not say a machine is out of date:\n%s", rendered)
|
|
||||||
}
|
|
||||||
// Never told is a different situation with the same remedy, and the page keeps them apart.
|
|
||||||
if !strings.Contains(rendered, "has never been sent anything") {
|
|
||||||
t.Fatalf("the page does not separate never told from out of date:\n%s", rendered)
|
|
||||||
}
|
|
||||||
if !strings.Contains(rendered, "push --behind") {
|
|
||||||
t.Fatalf("the page does not say what sends them:\n%s", rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// And says so when there is nothing waiting, rather than leaving the question unanswered.
|
|
||||||
func TestAMeshWithNothingWaitingSaysSo(t *testing.T) {
|
|
||||||
rendered := render(t, viewOf(answers{nodes: []inventory.Node{{Name: "anchor"}}}))
|
|
||||||
if !strings.Contains(rendered, "Every machine is running what the mesh would send it") {
|
|
||||||
t.Fatalf("the page leaves the question unanswered:\n%s", rendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,207 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **A filling bucket or log is said before it is full** (novox/hq ADR 0297 §6, issue 501).
|
|
||||||
//
|
|
||||||
// A module's bucket and its log each have a cap on the bus, and when either is full the bus refuses
|
|
||||||
// the next write: the module stops doing what it writes for, and nothing said so before. On
|
|
||||||
// 2026-10-11 the issue tracker's bucket held a sixth of its cap after two days, growing toward a stop
|
|
||||||
// nobody would have heard coming. So the self-check reads every module's bucket and log, and one at
|
|
||||||
// fillRaiseAt of its cap or more raises a condition naming the module, the bucket or log, and how full
|
|
||||||
// it is.
|
|
||||||
//
|
|
||||||
// **Cleared by observation below fillClearBelow, not below fillRaiseAt**: a bucket or log hovering at
|
|
||||||
// its threshold would otherwise be raised and cleared on every run. Between the two, an open condition
|
|
||||||
// is kept and none is raised.
|
|
||||||
//
|
|
||||||
// **One that cannot be read is said, and the rest are still judged.** An unanswered question about one
|
|
||||||
// stream is no reason to know nothing of the others: it is a finding of its own, naming the bucket or
|
|
||||||
// log and why, and a fill condition already open for it is kept, because not knowing is not a pass.
|
|
||||||
|
|
||||||
// The fill thresholds, in percent of a bucket's or log's cap.
|
|
||||||
const (
|
|
||||||
fillRaiseAt = 75
|
|
||||||
fillClearBelow = 70
|
|
||||||
)
|
|
||||||
|
|
||||||
// The conditions the fill probe raises: one filling, and one that could not be read.
|
|
||||||
const (
|
|
||||||
kindBucketOrLogFilling = "bucket-or-log-filling"
|
|
||||||
kindBucketOrLogUnread = "bucket-or-log-unread"
|
|
||||||
)
|
|
||||||
|
|
||||||
// probeFillID is the probe's id in the registry.
|
|
||||||
const probeFillID = "D-fill"
|
|
||||||
|
|
||||||
// bucketOrLog is one module's bucket or log as the fill probe reads it.
|
|
||||||
type bucketOrLog struct {
|
|
||||||
Module string
|
|
||||||
// Kind is `bucket` or `log`; Name its local name; Stream the stream it is on the bus.
|
|
||||||
Kind, Name, Stream string
|
|
||||||
}
|
|
||||||
|
|
||||||
// filled is how full one bucket or log is, read from the bus.
|
|
||||||
type filled struct {
|
|
||||||
Bytes, Max uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// percent is how full, in whole percent, rounded down.
|
|
||||||
func (f filled) percent() uint64 {
|
|
||||||
if f.Max == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return f.Bytes * 100 / f.Max
|
|
||||||
}
|
|
||||||
|
|
||||||
// fillKey is where a bucket's or log's fill condition is kept.
|
|
||||||
func fillKey(s bucketOrLog) string { return conditions.Key(conditions.ScopeBus, s.Stream, "filling") }
|
|
||||||
|
|
||||||
// fillObservation is what one bucket's or log's fill says: a finding at fillRaiseAt or above, and,
|
|
||||||
// while its condition is open, at fillClearBelow or above too; nothing otherwise, which is what clears
|
|
||||||
// it. The operator's words are the kind's (plain_words.go); the summary and the evidence name the
|
|
||||||
// module, the bucket or log, and the fill.
|
|
||||||
func fillObservation(s bucketOrLog, f filled, open bool) (conditions.Observation, bool) {
|
|
||||||
if f.Max == 0 {
|
|
||||||
return conditions.Observation{}, false // one with no cap cannot fill
|
|
||||||
}
|
|
||||||
// Compared in bytes, not rounded percent: 74.9% is not 75%.
|
|
||||||
atRaise := f.Bytes*100 >= f.Max*fillRaiseAt
|
|
||||||
aboveClear := f.Bytes*100 >= f.Max*fillClearBelow
|
|
||||||
if !atRaise && !(open && aboveClear) {
|
|
||||||
return conditions.Observation{}, false
|
|
||||||
}
|
|
||||||
pct := f.percent()
|
|
||||||
return conditions.Observation{
|
|
||||||
Scope: conditions.ScopeBus, ID: s.Stream, Token: "filling", Kind: kindBucketOrLogFilling,
|
|
||||||
Severity: conditions.Warning,
|
|
||||||
Summary: fmt.Sprintf("%s's %s %s holds %s of %s, %d%%: at its cap the bus refuses the module's next write",
|
|
||||||
s.Module, s.Kind, s.Name, mibWords(f.Bytes), mibWords(f.Max), pct),
|
|
||||||
Said: fmt.Sprintf("module %s, %s %s (stream %s): %d of %d bytes, %d%%", s.Module, s.Kind, s.Name,
|
|
||||||
s.Stream, f.Bytes, f.Max, pct),
|
|
||||||
}, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// unreadObservation says one bucket or log could not be read, and why. Asked over the network, so one
|
|
||||||
// look can be wrong: raised on the second look in a row (confirm.go).
|
|
||||||
func unreadObservation(s bucketOrLog, why error) conditions.Observation {
|
|
||||||
return conditions.Observation{
|
|
||||||
Scope: conditions.ScopeBus, ID: s.Stream, Token: "unread", Kind: kindBucketOrLogUnread,
|
|
||||||
Severity: conditions.Warning, Confirm: true,
|
|
||||||
Summary: fmt.Sprintf("%s's %s %s could not be read, so how full it is is not known", s.Module, s.Kind, s.Name),
|
|
||||||
Said: fmt.Sprintf("module %s, %s %s (stream %s): %v", s.Module, s.Kind, s.Name, s.Stream, why),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// keptFilling is an open fill condition said again for a bucket or log that could not be read this
|
|
||||||
// time: not knowing how full it is now is no reason to say it has room. Only ever made from a condition
|
|
||||||
// read back as open — its own summary and severity, never words made up for it.
|
|
||||||
func keptFilling(s bucketOrLog, open conditions.Condition, why error) conditions.Observation {
|
|
||||||
return conditions.Observation{
|
|
||||||
Scope: conditions.ScopeBus, ID: s.Stream, Token: "filling", Kind: kindBucketOrLogFilling,
|
|
||||||
Severity: open.Severity, Summary: open.Summary,
|
|
||||||
Said: fmt.Sprintf("module %s, %s %s (stream %s): not read this time (%v); kept open as it was",
|
|
||||||
s.Module, s.Kind, s.Name, s.Stream, why),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// mibWords is a size as a person reads it, in MiB with one decimal.
|
|
||||||
func mibWords(b uint64) string {
|
|
||||||
return fmt.Sprintf("%.1f MiB", float64(b)/(1024*1024))
|
|
||||||
}
|
|
||||||
|
|
||||||
// readFill is how full one bucket or log is on the bus; found false when it is not on the bus.
|
|
||||||
type readFill func(ctx context.Context, s bucketOrLog) (f filled, found bool, err error)
|
|
||||||
|
|
||||||
// openFill is the fill condition open under a key, if one is, or why it could not be read.
|
|
||||||
type openFill func(ctx context.Context, key string) (conditions.Condition, bool, error)
|
|
||||||
|
|
||||||
// judgeFills judges every bucket and log on its own: one that cannot be read is said as unread, with
|
|
||||||
// its fill condition kept only when that condition was read back and is open — when it cannot be read
|
|
||||||
// either, nothing is said of its fill, which the unread finding already covers. Every other is judged
|
|
||||||
// by its fill; for one of those, a condition that cannot be read is taken as open, so an unknown never
|
|
||||||
// clears a fill measured between fillClearBelow and fillRaiseAt.
|
|
||||||
func judgeFills(ctx context.Context, all []bucketOrLog, read readFill, open openFill) []conditions.Observation {
|
|
||||||
var out []conditions.Observation
|
|
||||||
for _, s := range all {
|
|
||||||
f, found, err := read(ctx, s)
|
|
||||||
if err != nil {
|
|
||||||
out = append(out, unreadObservation(s, err))
|
|
||||||
if c, isOpen, cerr := open(ctx, fillKey(s)); cerr == nil && isOpen {
|
|
||||||
out = append(out, keptFilling(s, c, err))
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
continue // not on the bus: created on the controller's next raise, and nothing there can fill
|
|
||||||
}
|
|
||||||
_, isOpen, cerr := open(ctx, fillKey(s))
|
|
||||||
isOpen = isOpen || cerr != nil
|
|
||||||
if o, said := fillObservation(s, f, isOpen); said {
|
|
||||||
out = append(out, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.SliceStable(out, func(i, j int) bool { return out[i].Key() < out[j].Key() })
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// declaredBucketsAndLogs is every module's bucket and log the catalogue declares, by its stream.
|
|
||||||
func declaredBucketsAndLogs(ctx context.Context, d *doctor) ([]bucketOrLog, error) {
|
|
||||||
buckets, err := d.open.inventory.DeclaredBuckets(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
logs, err := d.open.inventory.DeclaredLogs(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var out []bucketOrLog
|
|
||||||
for _, b := range buckets {
|
|
||||||
out = append(out, bucketOrLog{Module: b.Module, Kind: "bucket", Name: b.Name, Stream: "KV_" + b.Bucket()})
|
|
||||||
}
|
|
||||||
for _, l := range logs {
|
|
||||||
out = append(out, bucketOrLog{Module: l.Module, Kind: "log", Name: l.Name, Stream: l.Stream()})
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// probeFill reads every module's bucket and log on the bus and says each one filling toward its cap,
|
|
||||||
// and each one that could not be read.
|
|
||||||
func probeFill(ctx context.Context, d *doctor) ([]conditions.Observation, error) {
|
|
||||||
all, err := declaredBucketsAndLogs(ctx, d)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
js := d.js.Context()
|
|
||||||
read := func(ctx context.Context, s bucketOrLog) (filled, bool, error) {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return filled{}, false, err
|
|
||||||
}
|
|
||||||
info, err := js.StreamInfo(s.Stream, nats.Context(ctx))
|
|
||||||
switch {
|
|
||||||
case errors.Is(err, nats.ErrStreamNotFound):
|
|
||||||
return filled{}, false, nil
|
|
||||||
case err != nil:
|
|
||||||
return filled{}, false, err
|
|
||||||
case info.Config.MaxBytes <= 0:
|
|
||||||
return filled{}, true, nil
|
|
||||||
}
|
|
||||||
return filled{Bytes: info.State.Bytes, Max: uint64(info.Config.MaxBytes)}, true, nil
|
|
||||||
}
|
|
||||||
open := func(ctx context.Context, key string) (conditions.Condition, bool, error) {
|
|
||||||
if d.keeper == nil {
|
|
||||||
return conditions.Condition{}, false, nil
|
|
||||||
}
|
|
||||||
return d.keeper.Get(ctx, key)
|
|
||||||
}
|
|
||||||
return judgeFills(ctx, all, read, open), nil
|
|
||||||
}
|
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **A filling log or bucket is said at three quarters of its cap and cleared below seven tenths**
|
|
||||||
// (novox/hq ADR 0297 §6): raised at 75% naming the module, the bucket or log and its fill; not at 74.9%;
|
|
||||||
// kept between 70% and 75% while it is open, and not raised there when it is not; and cleared — said no
|
|
||||||
// more — once it reads below 70%, open or not.
|
|
||||||
func TestAFillingBucketOrLogIsRaisedAtThreeQuartersAndClearedBelowSevenTenths(t *testing.T) {
|
|
||||||
const mib = 1024 * 1024
|
|
||||||
log := bucketOrLog{Module: "mesh-issues", Kind: "log", Name: "changes", Stream: "LOG_mesh-issues_changes"}
|
|
||||||
bucket := bucketOrLog{Module: "mesh-issues", Kind: "bucket", Name: "issues", Stream: "KV_mesh-issues_issues"}
|
|
||||||
for _, c := range []struct {
|
|
||||||
name string
|
|
||||||
of bucketOrLog
|
|
||||||
bytes uint64
|
|
||||||
max uint64
|
|
||||||
open bool
|
|
||||||
said bool
|
|
||||||
}{
|
|
||||||
{"a log at exactly 75%", log, 768 * mib, 1024 * mib, false, true},
|
|
||||||
{"a bucket at exactly 75%", bucket, 48 * mib, 64 * mib, false, true},
|
|
||||||
{"a bucket full", bucket, 64 * mib, 64 * mib, false, true},
|
|
||||||
{"a log just under 75%", log, 768*mib - 1, 1024 * mib, false, false},
|
|
||||||
{"a bucket at 72%, not open", bucket, 64 * mib * 72 / 100, 64 * mib, false, false},
|
|
||||||
{"a bucket at 72%, open: kept", bucket, 64 * mib * 72 / 100, 64 * mib, true, true},
|
|
||||||
{"a log at exactly 70%, open: kept", log, 700 * mib, 1000 * mib, true, true},
|
|
||||||
{"a log just under 70%, open: cleared", log, 700*mib - 1, 1000 * mib, true, false},
|
|
||||||
{"a bucket at 10%, open: cleared", bucket, 64 * mib / 10, 64 * mib, true, false},
|
|
||||||
{"a bucket with no cap", bucket, 100, 0, true, false},
|
|
||||||
} {
|
|
||||||
o, said := fillObservation(c.of, filled{Bytes: c.bytes, Max: c.max}, c.open)
|
|
||||||
if said != c.said {
|
|
||||||
t.Errorf("%s: said %v, want %v", c.name, said, c.said)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !said {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if o.Key() != fillKey(c.of) || o.Kind != kindBucketOrLogFilling || o.Severity != conditions.Warning {
|
|
||||||
t.Errorf("%s: raised as %s (%s, %s)", c.name, o.Key(), o.Kind, o.Severity)
|
|
||||||
}
|
|
||||||
for _, part := range []string{c.of.Module, c.of.Kind + " " + c.of.Name, "%", " of "} {
|
|
||||||
if !strings.Contains(o.Summary, part) {
|
|
||||||
t.Errorf("%s: does not name %q: %q", c.name, part, o.Summary)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !strings.Contains(o.Said, "bytes") {
|
|
||||||
t.Errorf("%s: the evidence does not say the fill in bytes: %q", c.name, o.Said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
o, _ := fillObservation(log, filled{Bytes: 800 * mib, Max: 1024 * mib}, false)
|
|
||||||
if !strings.Contains(o.Summary, "800.0 MiB of 1024.0 MiB, 78%") {
|
|
||||||
t.Errorf("the fill is said as %q", o.Summary)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **One bucket or log that cannot be read does not stop the others being judged** (review of #231): it is
|
|
||||||
// said as unread with its reason, a fill condition open for it is kept, and every other bucket and log
|
|
||||||
// is judged by its own fill.
|
|
||||||
func TestAnUnreadableBucketOrLogIsSaidAndTheRestAreStillJudged(t *testing.T) {
|
|
||||||
const mib = 1024 * 1024
|
|
||||||
broken := bucketOrLog{Module: "a", Kind: "bucket", Name: "broken", Stream: "KV_a_broken"}
|
|
||||||
brokenOpen := bucketOrLog{Module: "a", Kind: "log", Name: "was-filling", Stream: "LOG_a_was-filling"}
|
|
||||||
full := bucketOrLog{Module: "b", Kind: "log", Name: "changes", Stream: "LOG_b_changes"}
|
|
||||||
empty := bucketOrLog{Module: "c", Kind: "bucket", Name: "quiet", Stream: "KV_c_quiet"}
|
|
||||||
absent := bucketOrLog{Module: "d", Kind: "bucket", Name: "not-yet", Stream: "KV_d_not-yet"}
|
|
||||||
refusal := errors.New("the bus did not answer")
|
|
||||||
read := func(_ context.Context, s bucketOrLog) (filled, bool, error) {
|
|
||||||
switch s {
|
|
||||||
case broken, brokenOpen:
|
|
||||||
return filled{}, false, refusal
|
|
||||||
case full:
|
|
||||||
return filled{Bytes: 900 * mib, Max: 1000 * mib}, true, nil
|
|
||||||
case empty:
|
|
||||||
return filled{Bytes: 1, Max: 64 * mib}, true, nil
|
|
||||||
}
|
|
||||||
return filled{}, false, nil
|
|
||||||
}
|
|
||||||
open := func(_ context.Context, key string) (conditions.Condition, bool, error) {
|
|
||||||
if key == fillKey(brokenOpen) {
|
|
||||||
return conditions.Condition{Key: key, Severity: conditions.Warning,
|
|
||||||
Summary: "a's log was-filling holds 800.0 MiB of 1024.0 MiB, 78%"}, true, nil
|
|
||||||
}
|
|
||||||
return conditions.Condition{}, false, nil
|
|
||||||
}
|
|
||||||
got := map[string]conditions.Observation{}
|
|
||||||
for _, o := range judgeFills(context.Background(), []bucketOrLog{broken, brokenOpen, full, empty, absent}, read, open) {
|
|
||||||
got[o.Key()] = o
|
|
||||||
}
|
|
||||||
for _, s := range []bucketOrLog{broken, brokenOpen} {
|
|
||||||
o, said := got[conditions.Key(conditions.ScopeBus, s.Stream, "unread")]
|
|
||||||
if !said || o.Kind != kindBucketOrLogUnread || !o.Confirm || !strings.Contains(o.Said, refusal.Error()) {
|
|
||||||
t.Errorf("%s could not be read and was said as %+v", s.Stream, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if o, kept := got[fillKey(brokenOpen)]; !kept || !strings.Contains(o.Said, "kept open") {
|
|
||||||
t.Errorf("an open fill condition of a log not read this time was not kept: %+v", o)
|
|
||||||
}
|
|
||||||
if _, said := got[fillKey(broken)]; said {
|
|
||||||
t.Error("a bucket not read and not filling was said filling")
|
|
||||||
}
|
|
||||||
if o, said := got[fillKey(full)]; !said || o.Kind != kindBucketOrLogFilling {
|
|
||||||
t.Errorf("a log at 90%% beside an unreadable one was not judged: %+v", got)
|
|
||||||
}
|
|
||||||
if len(got) != 4 {
|
|
||||||
t.Errorf("said %d findings, want 4 (two unread, one kept, one filling): %v", len(got), got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Both kinds have plain words of their own, which hold to the plain rule (novox/hq ADR 0253).
|
|
||||||
func TestAFillingOrUnreadBucketOrLogIsSaidInPlainWords(t *testing.T) {
|
|
||||||
for _, kind := range []string{kindBucketOrLogFilling, kindBucketOrLogUnread} {
|
|
||||||
wording, has := plainWordings[kind]
|
|
||||||
if !has {
|
|
||||||
t.Errorf("%s has no plain words", kind)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if why, ok := conditions.PlainWords(wording(conditions.Observation{Kind: kind})); !ok {
|
|
||||||
t.Errorf("%s: %s", kind, why)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **When neither the bucket or log nor its condition can be read, nothing is said of its fill** (second
|
|
||||||
// review of #231): the unread finding covers it, and no fill is invented for it. For one that is read and
|
|
||||||
// measured between seven tenths and three quarters, a condition that cannot be read is taken as open, so
|
|
||||||
// an unknown never clears it.
|
|
||||||
func TestNothingIsSaidOfAFillWhenNeitherItNorItsConditionCanBeRead(t *testing.T) {
|
|
||||||
const mib = 1024 * 1024
|
|
||||||
broken := bucketOrLog{Module: "a", Kind: "log", Name: "changes", Stream: "LOG_a_changes"}
|
|
||||||
between := bucketOrLog{Module: "b", Kind: "bucket", Name: "issues", Stream: "KV_b_issues"}
|
|
||||||
read := func(_ context.Context, s bucketOrLog) (filled, bool, error) {
|
|
||||||
if s == broken {
|
|
||||||
return filled{}, false, errors.New("the bus did not answer")
|
|
||||||
}
|
|
||||||
return filled{Bytes: 72 * mib, Max: 100 * mib}, true, nil
|
|
||||||
}
|
|
||||||
open := func(context.Context, string) (conditions.Condition, bool, error) {
|
|
||||||
return conditions.Condition{}, false, errors.New("the conditions bucket did not answer")
|
|
||||||
}
|
|
||||||
got := map[string]conditions.Observation{}
|
|
||||||
for _, o := range judgeFills(context.Background(), []bucketOrLog{broken, between}, read, open) {
|
|
||||||
got[o.Key()] = o
|
|
||||||
}
|
|
||||||
if o, said := got[fillKey(broken)]; said {
|
|
||||||
t.Fatalf("a fill was said for a log whose fill and condition could not be read: %+v", o)
|
|
||||||
}
|
|
||||||
if _, said := got[conditions.Key(conditions.ScopeBus, broken.Stream, "unread")]; !said {
|
|
||||||
t.Error("the log that could not be read was not said as unread")
|
|
||||||
}
|
|
||||||
if _, kept := got[fillKey(between)]; !kept {
|
|
||||||
t.Error("a bucket at 72% whose condition could not be read was cleared")
|
|
||||||
}
|
|
||||||
if len(got) != 2 {
|
|
||||||
t.Errorf("said %d findings, want 2: %v", len(got), got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,131 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestABuildHeardIsRecordedAndRegistered(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
manifest, _ := json.Marshal(map[string]any{"module": "shop", "version": "3"})
|
|
||||||
m, _, err := takeIn(ctx, open.inventory, asTheOperator(t, open.inventory, link.BuildResult{
|
|
||||||
ID: "b-1", Repository: "http://forge.internal:20000/novox/shop.git", Path: "modules/shop",
|
|
||||||
Ref: "main", On: "anchor", Commit: "abcdef0123", Manifest: manifest,
|
|
||||||
Source: &link.SourceOnSeat{Seat: "git", Repository: "novox/shop"},
|
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if m.Module != "shop" {
|
|
||||||
t.Fatalf("registered %q", m.Module)
|
|
||||||
}
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, held := shelf["shop"]; !held {
|
|
||||||
t.Fatal("the module a heard build produced is not in the catalogue")
|
|
||||||
}
|
|
||||||
src, err := open.inventory.SourceOf(ctx, "shop")
|
|
||||||
if err != nil || src.Seat != "git" || src.Repository != "novox/shop" || src.BuiltFrom != "abcdef0123" {
|
|
||||||
t.Fatalf("the source is the seat and the path, never the URL: %+v %v", src, err)
|
|
||||||
}
|
|
||||||
builds, err := open.inventory.Builds(ctx, "shop", 5)
|
|
||||||
if err != nil || len(builds) != 1 || builds[0].ID != "b-1" {
|
|
||||||
t.Fatalf("the build is not recorded once: %v %v", builds, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
named, _ := json.Marshal(map[string]any{"module": "idp", "version": "1", "resources": []any{
|
|
||||||
map[string]any{"id": "server", "type": "container", "image": "x@sha256:aa",
|
|
||||||
"env": map[string]any{"KC_HOSTNAME": "https://login.mesh-one.be"}}}})
|
|
||||||
_, _, err = takeIn(ctx, open.inventory, link.BuildResult{
|
|
||||||
ID: "b-2", Repository: "/r", On: "anchor", Commit: "0123456789", Manifest: named})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "does not register it") {
|
|
||||||
t.Fatalf("a definition naming an installation was taken in: %v", err)
|
|
||||||
}
|
|
||||||
if shelf, _ := open.inventory.Catalogue(ctx); shelf["idp"].Module != "" {
|
|
||||||
t.Fatal("the refused module was registered anyway")
|
|
||||||
}
|
|
||||||
if builds, _ := open.inventory.Builds(ctx, "idp", 5); len(builds) != 1 {
|
|
||||||
t.Fatalf("the refused build was not recorded: %v", builds)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, err = takeIn(ctx, open.inventory, link.BuildResult{ID: "b-3", Repository: "/r", On: "anchor", Failed: "no compiler"})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "no compiler") {
|
|
||||||
t.Fatalf("a failure is said in the builder's words: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABuildAtACommitKeepsTheBranchTheModuleFollows(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
manifest, _ := json.Marshal(map[string]any{"module": "unifi", "version": "1"})
|
|
||||||
result := func(id, ref, commit string) link.BuildResult {
|
|
||||||
return link.BuildResult{ID: id, Repository: "http://forge.internal:20000/novox/mesh-catalog.git",
|
|
||||||
Path: "modules/unifi", Ref: ref, On: "anchor", Commit: commit, Manifest: manifest,
|
|
||||||
Source: &link.SourceOnSeat{Seat: "git", Repository: "novox/mesh-catalog"}}
|
|
||||||
}
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, asTheOperator(t, open.inventory, result("b-1", "main", "1111111aaaa"))); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, result("b-2", "9c97a8a", "9c97a8a1d2c3")); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
src, err := open.inventory.SourceOf(ctx, "unifi")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if src.Ref != "main" || src.BuiltFrom != "9c97a8a1d2c3" {
|
|
||||||
t.Errorf("after a build at a commit the module follows %q, built from %q; want main, 9c97a8a1d2c3", src.Ref, src.BuiltFrom)
|
|
||||||
}
|
|
||||||
|
|
||||||
// One new to the catalogue, first built at a commit, follows the default branch.
|
|
||||||
other, _ := json.Marshal(map[string]any{"module": "letta", "version": "1"})
|
|
||||||
r := result("b-3", "deadbeef", "deadbeefcafe")
|
|
||||||
r.Manifest, r.Path = other, "modules/letta"
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, r); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if src, _ := open.inventory.SourceOf(ctx, "letta"); src.Ref != "" {
|
|
||||||
t.Errorf("a module first built at a commit follows %q, want the default branch", src.Ref)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAnOlderBuildHeardLaterDoesNotReplaceTheNewer(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
older := time.Date(2026, 10, 3, 21, 33, 45, 0, time.UTC)
|
|
||||||
newer := time.Date(2026, 10, 3, 21, 51, 57, 0, time.UTC)
|
|
||||||
result := func(asked time.Time, image string) link.BuildResult {
|
|
||||||
manifest, _ := json.Marshal(map[string]any{"module": "postgres", "version": image})
|
|
||||||
return link.BuildResult{ID: link.NewBuildID(asked), Repository: "http://forge.internal:20000/novox/mesh-catalog.git",
|
|
||||||
Path: "modules/postgres", Ref: "main", On: "anchor", Commit: "efff5415", Manifest: manifest,
|
|
||||||
Source: &link.SourceOnSeat{Seat: "git", Repository: "novox/mesh-catalog"}}
|
|
||||||
}
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, asTheOperator(t, open.inventory, result(newer, "4bcd5f73"))); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
_, _, err := takeIn(ctx, open.inventory, result(older, "0ab07fa9"))
|
|
||||||
if !errors.Is(err, inventory.ErrSuperseded) {
|
|
||||||
t.Fatalf("the older request's outcome was taken in as current: %v", err)
|
|
||||||
}
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := shelf["postgres"].Version; got != "4bcd5f73" {
|
|
||||||
t.Errorf("postgres is %q; want the newer request's 4bcd5f73", got)
|
|
||||||
}
|
|
||||||
if builds, _ := open.inventory.Builds(ctx, "postgres", 5); len(builds) != 2 {
|
|
||||||
t.Errorf("the late build was not recorded: %v", builds)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
func claiming(module, seat string, on ...string) inventory.Entry {
|
|
||||||
return inventory.Entry{
|
|
||||||
Manifest: catalogue.Manifest{Module: module, Claims: []catalogue.Claim{{Name: seat}}},
|
|
||||||
On: on,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The controller asks the build role that has a holder (novox/hq ADR 0190 handover): the retired
|
|
||||||
// one while only the builder is assigned, the current one from the first build-agent on, and the
|
|
||||||
// current one when nothing holds either — where every ask goes once the handover is done.
|
|
||||||
func TestTheControllerAsksTheBuildRoleThatHasAHolder(t *testing.T) {
|
|
||||||
onlyTheBuilder := []inventory.Entry{
|
|
||||||
claiming("builder", link.TheBuildMachineBefore, "anchor"),
|
|
||||||
claiming("build-agent", link.TheBuildMachine), // registered, assigned nowhere yet
|
|
||||||
}
|
|
||||||
if got := buildSeatAmong(onlyTheBuilder); got != link.TheBuildMachineBefore {
|
|
||||||
t.Errorf("with only the builder assigned, asked %q", got)
|
|
||||||
}
|
|
||||||
bothHeld := []inventory.Entry{
|
|
||||||
claiming("builder", link.TheBuildMachineBefore, "anchor"),
|
|
||||||
claiming("build-agent", link.TheBuildMachine, "home-server"),
|
|
||||||
}
|
|
||||||
if got := buildSeatAmong(bothHeld); got != link.TheBuildMachine {
|
|
||||||
t.Errorf("with a build-agent assigned anywhere, asked %q", got)
|
|
||||||
}
|
|
||||||
neither := []inventory.Entry{claiming("builder", link.TheBuildMachineBefore)}
|
|
||||||
if got := buildSeatAmong(neither); got != link.TheBuildMachine {
|
|
||||||
t.Errorf("with no holder of either, asked %q, want the current role", got)
|
|
||||||
}
|
|
||||||
if got := buildSeatAmong(nil); got != link.TheBuildMachine {
|
|
||||||
t.Errorf("an empty catalogue asks %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,725 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Where a build may register a module from (novox/hq ADR 0266).
|
|
||||||
//
|
|
||||||
// A build's outcome registers its module, and a registered module is what the next push sends. Before this,
|
|
||||||
// an outcome registered whatever its manifest named, from whichever repository it was built from: an agent
|
|
||||||
// that made a repository of its own, committed `modules/sudo/module.json` granting itself a rule without a
|
|
||||||
// password, and asked the `build` verb for it had its repository registered as the module `sudo` — whose next
|
|
||||||
// push made the agent root on every node. A fork of the node-engine did the same everywhere. The trunk rule
|
|
||||||
// (ADR 0238) did not stop it: the trunk it checked was the trunk of the repository built, which was the
|
|
||||||
// agent's own.
|
|
||||||
//
|
|
||||||
// So **an outcome registers a module only from the repository the catalogue already builds that module
|
|
||||||
// from**; and a module new to the catalogue only from a repository the catalogue already builds another
|
|
||||||
// module from — whose trunk takes a reviewed, approved merge, which is how a merge adds a module (novox/hq
|
|
||||||
// issue 300). Anything else — a module moved to another repository, a module from a repository the
|
|
||||||
// catalogue has never built — is the operator's, at the controller's terminal: allowed only when the build
|
|
||||||
// request was kept as asked there. Judged at the take-in, which every outcome reaches whoever hears it and
|
|
||||||
// whichever verb asked it (`build`, `rebuild`, `replay --register`, `assign` with build), and before the ask
|
|
||||||
// for a call through a verb, so an agent cannot have a build node run a repository the catalogue does not
|
|
||||||
// build from at all.
|
|
||||||
|
|
||||||
// errNotItsSource is an outcome refused for where it was built from.
|
|
||||||
var errNotItsSource = errors.New("not built from the repository the catalogue builds it from")
|
|
||||||
|
|
||||||
// servedVar marks every process the serving controller starts — each verb's command, each child — and the serving
|
|
||||||
// process itself, so none of them can read as the operator at the terminal (novox/hq ADR 0266). Set by serve
|
|
||||||
// before it answers anything, inherited by every child through os.Environ.
|
|
||||||
const servedVar = "MESH_SERVED_BY_THE_CONTROLLER"
|
|
||||||
|
|
||||||
// startedAtTheTerminal says this process was started at the controller's terminal: not the serving controller,
|
|
||||||
// not anything it started, not a verb's command, not a seat call's. The serving controller marks its own
|
|
||||||
// environment (servedVar), so a build asked in it, or by any process it starts, never reads as the terminal's;
|
|
||||||
// runVerb also names the verb and the caller.
|
|
||||||
//
|
|
||||||
// **And a line mesh-cli asked as the controller's terminal** (novox/hq ADR 0272 §4): the serving controller runs it
|
|
||||||
// without the served mark and without a verb, names its caller, and marks it with cliTerminalVar — a mark only the
|
|
||||||
// mesh-cli path sets and every other command line the controller runs is stripped of (commandEnvironment).
|
|
||||||
func startedAtTheTerminal() bool {
|
|
||||||
if os.Getenv(servedVar) != "" || os.Getenv(verbVar) != "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return os.Getenv(link.CallerVar) == "" || os.Getenv(cliTerminalVar) != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// cliTerminalVar marks a command line the serving controller runs as the controller's terminal for mesh-cli.
|
|
||||||
const cliTerminalVar = "MESH_CLI_TERMINAL"
|
|
||||||
|
|
||||||
// markServed marks this process, and so everything it starts, as the serving controller's.
|
|
||||||
func markServed() {
|
|
||||||
if err := os.Setenv(servedVar, "1"); err != nil {
|
|
||||||
panic("the serving controller could not mark its environment: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sourceForms compares sources however each is spelled: a path on a seat's holder (ADR 0111) or a URL — a
|
|
||||||
// build asked of a seat's path is registered with the URL composed from it when its outcome does not echo
|
|
||||||
// the seat. A seat's path is composed into its URL where the seat's holder is known, so both spellings of
|
|
||||||
// one repository are one; where it is not, a seat's path matches only the same seat's same path.
|
|
||||||
type sourceForms struct {
|
|
||||||
bases map[string]string // the seat's clone base, `scheme://host:port`, by seat; "" where it is not known
|
|
||||||
base func(seat string) string
|
|
||||||
// read is base with the reason it is unknown; nil where only base is given. again forces a fresh read past a
|
|
||||||
// failure kept for failedKept.
|
|
||||||
read func(ctx context.Context, seat string, again bool) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// failedKept is how long an unreadable base is not read again for the callers that loop over every catalogue
|
|
||||||
// entry: one composition of the mesh serves them all (novox/hq issue 1277).
|
|
||||||
const failedKept = 5 * time.Second
|
|
||||||
|
|
||||||
// newSourceForms reads the seats' bases from the mesh when first needed. A base that could not be read is not
|
|
||||||
// kept as if it were known: it is read again at the next ask (novox/hq issue 1277).
|
|
||||||
func newSourceForms(ctx context.Context, inv *inventory.Inventory) *sourceForms {
|
|
||||||
return sourceFormsRead(ctx, time.Now, func(ctx context.Context) (catalogue.World, error) {
|
|
||||||
shelf, err := inv.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return catalogue.World{}, fmt.Errorf("the catalogue cannot be read: %w", err)
|
|
||||||
}
|
|
||||||
read, err := restOfTheMeshOf(ctx, inv, shelf, "")
|
|
||||||
if err != nil {
|
|
||||||
return catalogue.World{}, fmt.Errorf("the mesh cannot be read: %w", err)
|
|
||||||
}
|
|
||||||
return read, nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// sourceFormsRead is newSourceForms on how the mesh is read, handed in.
|
|
||||||
func sourceFormsRead(ctx context.Context, now func() time.Time,
|
|
||||||
readWorld func(context.Context) (catalogue.World, error)) *sourceForms {
|
|
||||||
f := &sourceForms{bases: map[string]string{}}
|
|
||||||
var failedAt time.Time // when the last read failed; zero when it did not
|
|
||||||
var failure error
|
|
||||||
f.read = func(ctx context.Context, seat string, again bool) (string, error) {
|
|
||||||
if b, known := f.bases[seat]; known {
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
if failure != nil && !again && now().Sub(failedAt) < failedKept {
|
|
||||||
return "", failure
|
|
||||||
}
|
|
||||||
world, err := readWorld(ctx)
|
|
||||||
if err == nil {
|
|
||||||
var b string
|
|
||||||
if b, err = seatBase(world, seat); err == nil {
|
|
||||||
failure = nil
|
|
||||||
f.bases[seat] = b
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
failedAt, failure = now(), err // the holder may be changing now: only an unread base is kept, for a moment
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
f.base = func(seat string) string {
|
|
||||||
b, _ := f.read(ctx, seat, false)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgeAddressWait is the registration's wait for the forge's address (novox/hq issue 1277): a fresh read every
|
|
||||||
// 5 s for at most 60 s, as the wait for the forge's answer (issue 486). A variable so a test waits in no real time.
|
|
||||||
var forgeAddressWait = waitOut{bound: 60 * time.Second, first: 5 * time.Second, most: 5 * time.Second,
|
|
||||||
now: time.Now, sleep: sleepOrDone}
|
|
||||||
|
|
||||||
// forgeAddress is the git seat's base, read again within w's bound while it cannot be read. Where the forms have
|
|
||||||
// no reader of their own (a test's), base says it.
|
|
||||||
func (f *sourceForms) forgeAddress(ctx context.Context, w waitOut) (string, error) {
|
|
||||||
if f.read == nil {
|
|
||||||
return f.base(gitSeat), nil
|
|
||||||
}
|
|
||||||
began := w.now()
|
|
||||||
for {
|
|
||||||
b, err := f.read(ctx, gitSeat, true)
|
|
||||||
if err == nil {
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if w.now().Sub(began)+w.first > w.bound {
|
|
||||||
return "", fmt.Errorf("after %s: %w", w.now().Sub(began).Round(time.Second), err)
|
|
||||||
}
|
|
||||||
fmt.Printf("the forge's address cannot be read now (%v); reading it again in %s, for at most %s in all "+
|
|
||||||
"(novox/hq issue 1277)\n", err, w.first, w.bound)
|
|
||||||
if serr := w.sleep(ctx, w.first); serr != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// canonical is one spelling of a repository: lower case, no `.git`, no trailing slash, and a seat's path as
|
|
||||||
// the URL its holder serves it at where that is known.
|
|
||||||
func (f *sourceForms) canonical(repository, seat string) string {
|
|
||||||
trim := func(s string) string {
|
|
||||||
s = strings.TrimSpace(strings.ToLower(s))
|
|
||||||
s = strings.TrimRight(s, "/")
|
|
||||||
return strings.TrimRight(strings.TrimSuffix(s, ".git"), "/")
|
|
||||||
}
|
|
||||||
if seat == "" {
|
|
||||||
return trim(repository)
|
|
||||||
}
|
|
||||||
if b := f.base(seat); b != "" {
|
|
||||||
return trim(b + "/" + strings.Trim(repository, "/"))
|
|
||||||
}
|
|
||||||
return "seat:" + seat + ":" + trim(strings.Trim(repository, "/"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// same says two sources are one repository.
|
|
||||||
func (f *sourceForms) same(aRepository, aSeat, bRepository, bSeat string) bool {
|
|
||||||
if aRepository == "" || bRepository == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return f.canonical(aRepository, aSeat) == f.canonical(bRepository, bSeat)
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildsFrom says the catalogue builds some module from this repository.
|
|
||||||
func (f *sourceForms) buildsFrom(entries []inventory.Entry, repository, seat string) bool {
|
|
||||||
for _, e := range entries {
|
|
||||||
if e.Provided || e.Source.Repository == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if f.same(e.Source.Repository, e.Source.Seat, repository, seat) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgeFacts is what the mesh's forge says of a repository a module is registered from (novox/hq ADR 0266).
|
|
||||||
type forgeFacts struct {
|
|
||||||
// ID is the forge's own id of the repository: what tells it from one deleted and made again by its name.
|
|
||||||
ID int64
|
|
||||||
// Guarded is whether the branch is protected as a module's trunk must be: no direct push, at least one
|
|
||||||
// required status, and no administrator merging past one. Why says what is missing when it is not.
|
|
||||||
Guarded bool
|
|
||||||
Why string
|
|
||||||
}
|
|
||||||
|
|
||||||
// askTheForge asks the forge, through its module's tools on the bus, for a repository's id and its branch's
|
|
||||||
// protection, waiting out a forge not served for a moment (novox/hq issue 486). A variable so a test needs no
|
|
||||||
// forge.
|
|
||||||
var askTheForge = func(ctx context.Context, owner, repo, branch string) (forgeFacts, error) {
|
|
||||||
return askedWithinBudget(ctx, owner+"/"+repo, func(ctx context.Context) (forgeFacts, error) {
|
|
||||||
return askTheForgeOnce(ctx, owner, repo, branch)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type forgeBudgetKey struct{}
|
|
||||||
|
|
||||||
// withForgeBudget says how much of the registration's one wait is left for the forge's answer: the wait for its
|
|
||||||
// address has used the rest (novox/hq issue 1277). The registration holds the controller's one receive loop, so the
|
|
||||||
// two waits together keep to one bound.
|
|
||||||
func withForgeBudget(ctx context.Context, left time.Duration) context.Context {
|
|
||||||
return context.WithValue(ctx, forgeBudgetKey{}, max(left, 0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// askedWithinBudget is forgeWaitedOut with forgeWait's bound, or what is left of it when the address was waited for.
|
|
||||||
func askedWithinBudget(ctx context.Context, repository string, ask func(context.Context) (forgeFacts, error)) (forgeFacts, error) {
|
|
||||||
w := forgeWait
|
|
||||||
if left, ok := ctx.Value(forgeBudgetKey{}).(time.Duration); ok {
|
|
||||||
w.bound = min(w.bound, left)
|
|
||||||
}
|
|
||||||
return forgeWaitedOut(ctx, w, repository, ask)
|
|
||||||
}
|
|
||||||
|
|
||||||
// waitOut bounds how long a question is asked again while its answer is "not now": a pause of first, doubling
|
|
||||||
// to most, for at most bound in all. now and sleep are the clock, so a test waits in no real time.
|
|
||||||
type waitOut struct {
|
|
||||||
bound, first, most time.Duration
|
|
||||||
now func() time.Time
|
|
||||||
sleep func(context.Context, time.Duration) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgeWait is the registration's wait for the forge (novox/hq issue 486). On 2026-10-11 a build's registration
|
|
||||||
// asked the forge while node-tools on the control-node re-served after a rollout of the tool runner, heard
|
|
||||||
// "nothing serves gitea.gitea_api", and failed its plan; the forge answered again seconds later. A re-serve takes
|
|
||||||
// about 10 s, so 60 s waits one out with room to spare. It is a cap on the whole wait, an ask in flight included:
|
|
||||||
// the registration holds the controller's one receive loop while it waits, and a loop held two minutes reads as
|
|
||||||
// deaf. The pauses are those the build machine waits the registry with (novox/hq issue 457).
|
|
||||||
var forgeWait = waitOut{bound: 60 * time.Second, first: time.Second, most: 10 * time.Second,
|
|
||||||
now: time.Now, sleep: sleepOrDone}
|
|
||||||
|
|
||||||
// sleepOrDone pauses for d, or less when ctx ends first.
|
|
||||||
func sleepOrDone(ctx context.Context, d time.Duration) error {
|
|
||||||
t := time.NewTimer(d)
|
|
||||||
defer t.Stop()
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
case <-t.C:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgeNotNow is an answer that says the forge's tool is not served now — the tool runner re-serving — not that
|
|
||||||
// the forge refused. Only that is asked again: a refusal, an unreadable answer or a repository with no id is the
|
|
||||||
// forge's word, and a tool that did not answer in time has already held the receive loop its whole wait.
|
|
||||||
func forgeNotNow(err error) bool { return errors.Is(err, link.ErrNothingServes) }
|
|
||||||
|
|
||||||
// forgeWaitedOut asks the forge about repository, asking again while it answers "not served now", all of it
|
|
||||||
// within w's bound, an ask in flight included (novox/hq issue 486). Past the bound it fails, saying it waited,
|
|
||||||
// for how long, and what the forge last said; when the caller's own context ends, it says that instead.
|
|
||||||
func forgeWaitedOut(ctx context.Context, w waitOut, repository string,
|
|
||||||
ask func(context.Context) (forgeFacts, error)) (forgeFacts, error) {
|
|
||||||
capped, cancel := context.WithTimeout(ctx, w.bound)
|
|
||||||
defer cancel()
|
|
||||||
began := w.now()
|
|
||||||
pause := w.first
|
|
||||||
for asked := 1; ; asked++ {
|
|
||||||
facts, err := ask(capped)
|
|
||||||
if err == nil {
|
|
||||||
if asked > 1 {
|
|
||||||
fmt.Printf("the forge answered about %s again, after %s (novox/hq issue 486)\n", repository,
|
|
||||||
w.now().Sub(began).Round(time.Second))
|
|
||||||
}
|
|
||||||
return facts, nil
|
|
||||||
}
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return forgeFacts{}, err // the caller's own context ended: no wait of ours to speak of
|
|
||||||
}
|
|
||||||
gaveUp := func() (forgeFacts, error) {
|
|
||||||
return forgeFacts{}, fmt.Errorf("waited %s for the forge to answer about %s, asking %d times, and it "+
|
|
||||||
"did not (novox/hq issue 486): %w", w.now().Sub(began).Round(time.Second), repository, asked, err)
|
|
||||||
}
|
|
||||||
if capped.Err() != nil {
|
|
||||||
return gaveUp() // the bound ended an ask in flight
|
|
||||||
}
|
|
||||||
if !forgeNotNow(err) {
|
|
||||||
return forgeFacts{}, err
|
|
||||||
}
|
|
||||||
if w.now().Sub(began)+pause > w.bound {
|
|
||||||
return gaveUp()
|
|
||||||
}
|
|
||||||
fmt.Printf("the forge cannot be asked about %s now (%v); asking again in %s, for at most %s in all "+
|
|
||||||
"(novox/hq issue 486)\n", repository, err, pause, w.bound)
|
|
||||||
if serr := w.sleep(capped, pause); serr != nil {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return forgeFacts{}, fmt.Errorf("stopped waiting for the forge to answer about %s: %w", repository,
|
|
||||||
ctx.Err())
|
|
||||||
}
|
|
||||||
return gaveUp()
|
|
||||||
}
|
|
||||||
pause = min(2*pause, w.most)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// askTheForgeOnce asks the forge once, through its module's tools on the bus.
|
|
||||||
func askTheForgeOnce(ctx context.Context, owner, repo, branch string) (forgeFacts, error) {
|
|
||||||
js, err := aBus()
|
|
||||||
if err != nil {
|
|
||||||
return forgeFacts{}, err
|
|
||||||
}
|
|
||||||
defer js.Close()
|
|
||||||
bus := link.OverNATS{Conn: js.Conn()}
|
|
||||||
ask := func(tool string, args map[string]any, into any) error {
|
|
||||||
raw, _ := json.Marshal(args)
|
|
||||||
answer, err := link.Ask(ctx, bus, "gitea", tool, raw, 30*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if answer.Error != "" {
|
|
||||||
return fmt.Errorf("gitea.%s: %s", tool, answer.Error)
|
|
||||||
}
|
|
||||||
return json.Unmarshal(answer.Result, into)
|
|
||||||
}
|
|
||||||
var found struct {
|
|
||||||
Result struct {
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
FullName string `json:"full_name"`
|
|
||||||
} `json:"result"`
|
|
||||||
}
|
|
||||||
if err := ask("gitea_api", map[string]any{"path": "/repos/" + owner + "/" + repo}, &found); err != nil {
|
|
||||||
return forgeFacts{}, err
|
|
||||||
}
|
|
||||||
if found.Result.ID == 0 {
|
|
||||||
return forgeFacts{}, fmt.Errorf("the forge named no id for %s/%s", owner, repo)
|
|
||||||
}
|
|
||||||
var rules struct {
|
|
||||||
Rules []protectionRule `json:"rules"`
|
|
||||||
}
|
|
||||||
if err := ask("gitea_branch_protection_get", map[string]any{"owner": owner, "repo": repo}, &rules); err != nil {
|
|
||||||
return forgeFacts{}, err
|
|
||||||
}
|
|
||||||
facts := judgedRule(rules.Rules, branch)
|
|
||||||
facts.ID = found.Result.ID
|
|
||||||
return facts, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// protectionRule is a branch protection rule as the forge's tool summarises it.
|
|
||||||
type protectionRule struct {
|
|
||||||
Rule string `json:"rule"`
|
|
||||||
Push bool `json:"push"`
|
|
||||||
RequiredStatuses []string `json:"required_statuses"`
|
|
||||||
AdminMayOverride bool `json:"admin_may_override"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// judgedRule judges the one rule the forge applies to a branch, as the forge picks it: the rule named for the
|
|
||||||
// branch, else the first glob rule, in the forge's order, that covers it (gitea's first matching rule). Never a
|
|
||||||
// later rule that happens to be stronger: the forge does not read it.
|
|
||||||
func judgedRule(rules []protectionRule, branch string) forgeFacts {
|
|
||||||
var applied *protectionRule
|
|
||||||
for i := range rules {
|
|
||||||
if rules[i].Rule == branch {
|
|
||||||
applied = &rules[i]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if applied == nil {
|
|
||||||
for i := range rules {
|
|
||||||
if ruleCovers(rules[i].Rule, branch) {
|
|
||||||
applied = &rules[i]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch r := applied; {
|
|
||||||
case r == nil:
|
|
||||||
return forgeFacts{Why: fmt.Sprintf("no protection rule covers %s", branch)}
|
|
||||||
case r.Push:
|
|
||||||
return forgeFacts{Why: fmt.Sprintf("the rule %s lets a person push to %s directly", r.Rule, branch)}
|
|
||||||
case len(r.RequiredStatuses) == 0:
|
|
||||||
return forgeFacts{Why: fmt.Sprintf("the rule %s requires no status before a merge into %s", r.Rule, branch)}
|
|
||||||
case r.AdminMayOverride:
|
|
||||||
return forgeFacts{Why: fmt.Sprintf("the rule %s lets an administrator merge into %s past a status", r.Rule, branch)}
|
|
||||||
}
|
|
||||||
return forgeFacts{Guarded: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ruleCovers says a protection rule's name — a branch, or a glob of them — covers a branch, as the forge reads it.
|
|
||||||
func ruleCovers(rule, branch string) bool {
|
|
||||||
if rule == branch {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if !strings.ContainsAny(rule, "*?[") {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
var re strings.Builder
|
|
||||||
re.WriteString("^")
|
|
||||||
for i := 0; i < len(rule); i++ {
|
|
||||||
switch c := rule[i]; {
|
|
||||||
case c == '*' && i+1 < len(rule) && rule[i+1] == '*':
|
|
||||||
re.WriteString(".*")
|
|
||||||
i++
|
|
||||||
case c == '*':
|
|
||||||
re.WriteString("[^/]*")
|
|
||||||
case c == '?':
|
|
||||||
re.WriteString("[^/]")
|
|
||||||
default:
|
|
||||||
re.WriteString(regexp.QuoteMeta(string(c)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
re.WriteString("$")
|
|
||||||
ok, _ := regexp.MatchString(re.String(), branch)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// onTheForge is a source's owner and name on the mesh's own forge (the git seat's holder), and whether it is
|
|
||||||
// there at all.
|
|
||||||
func (f *sourceForms) onTheForge(repository, seat string) (owner, name string, ok bool) {
|
|
||||||
var rest string
|
|
||||||
switch {
|
|
||||||
case seat == gitSeat:
|
|
||||||
rest = strings.Trim(repository, "/")
|
|
||||||
case seat == "":
|
|
||||||
base := f.base(gitSeat)
|
|
||||||
if base == "" {
|
|
||||||
return "", "", false
|
|
||||||
}
|
|
||||||
url, prefix := f.canonical(repository, ""), f.canonical(base, "")+"/"
|
|
||||||
if !strings.HasPrefix(url, prefix) {
|
|
||||||
return "", "", false
|
|
||||||
}
|
|
||||||
// The case the forge spells it with: the URL as given, past the base.
|
|
||||||
rest = strings.TrimSuffix(strings.Trim(repository[len(prefix):], "/"), ".git")
|
|
||||||
default:
|
|
||||||
return "", "", false
|
|
||||||
}
|
|
||||||
parts := strings.Split(rest, "/")
|
|
||||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
|
||||||
return "", "", false
|
|
||||||
}
|
|
||||||
return parts[0], parts[1], true
|
|
||||||
}
|
|
||||||
|
|
||||||
// mayRegisterFrom says whether a build's outcome may register module from the source it was built from, and the
|
|
||||||
// forge's id of that repository to record (novox/hq ADR 0266). Through any verb, only:
|
|
||||||
//
|
|
||||||
// - from the module's registered repository — the same repository by the forge's own id, not only its name; or,
|
|
||||||
// for a module new to the catalogue, from a repository the catalogue builds another module from;
|
|
||||||
// - and from a repository on the mesh's forge whose trunk is protected as a trunk must be: no direct push, at
|
|
||||||
// least one required status, no administrator merging past one.
|
|
||||||
//
|
|
||||||
// Anything else only when the build request was kept as asked at the controller's terminal, for this very
|
|
||||||
// repository and path; or, for the build of a merge, when mesh-delivery announced it on the operator's Approve of
|
|
||||||
// that head (novox/hq ADR 0420 §7 for a moved module, ADR 0432 for a new one), the forge's checks still holding. path is the module's directory as built; branch the trunk it is registered from.
|
|
||||||
func mayRegisterFrom(ctx context.Context, inv *inventory.Inventory, module string, built inventory.Source,
|
|
||||||
buildID, path, branch string) (int64, error) {
|
|
||||||
return mayRegisterIn(ctx, inv, newSourceForms(ctx, inv), module, built, buildID, path, branch)
|
|
||||||
}
|
|
||||||
|
|
||||||
// catalogueReader is the catalogue's manifests, as the router's records are found in them.
|
|
||||||
type catalogueReader interface {
|
|
||||||
Catalogue(ctx context.Context) (map[string]catalogue.Manifest, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// registrations is what a registration's decision reads of the mesh: *inventory.Inventory, or a test's stand-in.
|
|
||||||
type registrations interface {
|
|
||||||
catalogueReader
|
|
||||||
SourceOf(ctx context.Context, module string) (inventory.Source, error)
|
|
||||||
SourceIdentity(ctx context.Context, module string) (int64, error)
|
|
||||||
Catalogued(ctx context.Context) ([]inventory.Entry, error)
|
|
||||||
BuildRequestByID(ctx context.Context, id string) (inventory.BuildRequest, bool, error)
|
|
||||||
MergeOf(ctx context.Context, repository, commit string) (inventory.BatchedMerge, bool, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// mayRegisterIn is mayRegisterFrom on what it reads of the mesh handed in.
|
|
||||||
func mayRegisterIn(ctx context.Context, inv registrations, forms *sourceForms, module string, built inventory.Source,
|
|
||||||
buildID, path, branch string) (int64, error) {
|
|
||||||
was, err := inv.SourceOf(ctx, module)
|
|
||||||
isNew := errors.Is(err, inventory.ErrNoSuchModule)
|
|
||||||
if err != nil && !isNew {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
terminal, err := askedHereFor(ctx, inv, forms, buildID, built, path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
refuse := func(why string) (int64, error) {
|
|
||||||
return 0, fmt.Errorf("%w: %s. A module is registered from such a source only by a build asked at the "+
|
|
||||||
"controller's terminal, or by the build of a merge mesh-delivery announced on the operator's Approve of that "+
|
|
||||||
"head, never through a verb: a registered module is what the next push sends, and whoever "+
|
|
||||||
"may call a verb includes agents (novox/hq ADR 0266)", errNotItsSource, why)
|
|
||||||
}
|
|
||||||
|
|
||||||
var why string
|
|
||||||
var alongside []inventory.Entry // the modules a new one's repository already builds
|
|
||||||
switch {
|
|
||||||
case !isNew && forms.same(was.Repository, was.Seat, built.Repository, built.Seat):
|
|
||||||
case !isNew:
|
|
||||||
registered := was.Repository
|
|
||||||
if registered == "" {
|
|
||||||
registered = "no repository (it was handed over by hand)"
|
|
||||||
}
|
|
||||||
why = fmt.Sprintf("%s is built from %s, and this build is of %s", module, sourceWords(registered, was.Seat),
|
|
||||||
sourceWords(built.Repository, built.Seat))
|
|
||||||
default:
|
|
||||||
entries, err := inv.Catalogued(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
for _, e := range entries {
|
|
||||||
if !e.Provided && e.Source.Repository != "" &&
|
|
||||||
forms.same(e.Source.Repository, e.Source.Seat, built.Repository, built.Seat) {
|
|
||||||
alongside = append(alongside, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(alongside) == 0 {
|
|
||||||
why = fmt.Sprintf("%s is new to the catalogue, and %s is no repository the catalogue builds a module from",
|
|
||||||
module, sourceWords(built.Repository, built.Seat))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// **Or the move the operator approved** (novox/hq ADR 0420 §7, issue 1025): a known module built from another
|
|
||||||
// repository by a merge, whose pull request mesh-delivery merged on the operator's Approve of that head.
|
|
||||||
// A verb alone, or a merge with no such warrant, is refused as before.
|
|
||||||
// **Or a new module from a repository nothing registered is built from, on the same warrant** (novox/hq ADR 0432,
|
|
||||||
// issue 1146): the build of a merge mesh-delivery announced on the operator's Approve of that head registers it.
|
|
||||||
// The forge's protection and the repository's identity are checked below as for every registration.
|
|
||||||
movedWhy := ""
|
|
||||||
if why != "" && !terminal {
|
|
||||||
accepted, err := approvedMove(ctx, inv, forms, buildID, built)
|
|
||||||
switch {
|
|
||||||
case err == nil:
|
|
||||||
movedWhy, why = accepted, ""
|
|
||||||
case !errors.Is(err, errNotAMerge):
|
|
||||||
fmt.Printf("%s: %s is built from another repository, and no approved move covers this build: %v\n",
|
|
||||||
buildID, module, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if why != "" && !terminal {
|
|
||||||
return refuse(why)
|
|
||||||
}
|
|
||||||
|
|
||||||
if built.Seat == "" {
|
|
||||||
// A URL is judged against the forge's address: an address that cannot be read is no proof the source is
|
|
||||||
// elsewhere (novox/hq issue 1277).
|
|
||||||
began := forgeAddressWait.now()
|
|
||||||
addressCtx, cancel := context.WithTimeout(ctx, forgeAddressWait.bound)
|
|
||||||
_, err := forms.forgeAddress(addressCtx, forgeAddressWait)
|
|
||||||
cancel()
|
|
||||||
ctx = withForgeBudget(ctx, forgeAddressWait.bound-forgeAddressWait.now().Sub(began))
|
|
||||||
if err != nil {
|
|
||||||
if terminal {
|
|
||||||
fmt.Printf("%s: the forge's address could not be read (%v) — registered, as asked at the controller's "+
|
|
||||||
"terminal\n", buildID, err)
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return refuse(fmt.Sprintf("the forge's address could not be read (%v), so whether %s's trunk is "+
|
|
||||||
"protected cannot be read", err, sourceWords(built.Repository, built.Seat)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
owner, name, onForge := forms.onTheForge(built.Repository, built.Seat)
|
|
||||||
if !onForge {
|
|
||||||
if terminal {
|
|
||||||
fmt.Printf("%s: %s is not on the mesh's forge — registered, as asked at the controller's terminal\n",
|
|
||||||
buildID, sourceWords(built.Repository, built.Seat))
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return refuse(fmt.Sprintf("%s is not on the mesh's forge, so whether its trunk is protected cannot be read",
|
|
||||||
sourceWords(built.Repository, built.Seat)))
|
|
||||||
}
|
|
||||||
facts, err := askTheForge(ctx, owner, name, branch)
|
|
||||||
if err != nil {
|
|
||||||
if terminal {
|
|
||||||
fmt.Printf("%s: the forge could not be asked about %s/%s (%v) — registered, as asked at the controller's "+
|
|
||||||
"terminal\n", buildID, owner, name, err)
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return refuse(fmt.Sprintf("the forge could not say whether %s/%s's %s is protected: %v", owner, name, branch, err))
|
|
||||||
}
|
|
||||||
if terminal {
|
|
||||||
if why != "" || !facts.Guarded {
|
|
||||||
fmt.Printf("%s: %s — registered, as asked at the controller's terminal\n", buildID,
|
|
||||||
strings.Trim(why+"; "+facts.Why, "; "))
|
|
||||||
}
|
|
||||||
return facts.ID, nil
|
|
||||||
}
|
|
||||||
if !facts.Guarded {
|
|
||||||
return refuse(fmt.Sprintf("%s/%s's %s is not protected as a module's trunk must be: %s", owner, name, branch,
|
|
||||||
facts.Why))
|
|
||||||
}
|
|
||||||
// The same repository by the forge's id, not only its name: one deleted and made again is another.
|
|
||||||
recorded := map[string]int64{}
|
|
||||||
// A moved module is registered from the repository the operator approved, not the one it was registered from.
|
|
||||||
if !isNew && movedWhy == "" {
|
|
||||||
id, err := inv.SourceIdentity(ctx, module)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
recorded[module] = id
|
|
||||||
}
|
|
||||||
for _, e := range alongside {
|
|
||||||
id, err := inv.SourceIdentity(ctx, e.Manifest.Module)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
recorded[e.Manifest.Module] = id
|
|
||||||
}
|
|
||||||
for m, id := range recorded {
|
|
||||||
if id != 0 && id != facts.ID {
|
|
||||||
return refuse(fmt.Sprintf("%s/%s is not the repository %s was registered from: the forge knows it as "+
|
|
||||||
"repository %d, and %s was registered from repository %d — one of that name deleted and made again",
|
|
||||||
owner, name, m, facts.ID, m, id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if movedWhy != "" && isNew {
|
|
||||||
fmt.Printf("%s: %s is new, from %s/%s — registered at the operator's Approve: %s\n", buildID, module, owner, name,
|
|
||||||
movedWhy)
|
|
||||||
} else if movedWhy != "" {
|
|
||||||
fmt.Printf("%s: %s moved from %s to %s/%s — registered at the operator's Approve: %s\n", buildID, module,
|
|
||||||
sourceWords(was.Repository, was.Seat), owner, name, movedWhy)
|
|
||||||
}
|
|
||||||
return facts.ID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// askedHereFor says the build was asked at the controller's terminal, for this repository and this path: a kept
|
|
||||||
// request marked so, whose source is the outcome's (novox/hq ADR 0266).
|
|
||||||
func askedHereFor(ctx context.Context, inv registrations, forms *sourceForms, buildID string,
|
|
||||||
built inventory.Source, path string) (bool, error) {
|
|
||||||
r, found, err := inv.BuildRequestByID(ctx, buildID)
|
|
||||||
if err != nil || !found || !r.AtTerminal {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if !forms.same(r.Repository, r.Seat, built.Repository, built.Seat) ||
|
|
||||||
strings.Trim(r.Path, "/") != strings.Trim(path, "/") {
|
|
||||||
fmt.Printf("%s was asked at the terminal of %s at %q, and its outcome is of %s at %q: not the terminal's\n",
|
|
||||||
buildID, sourceWords(r.Repository, r.Seat), r.Path, sourceWords(built.Repository, built.Seat), path)
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// sourceWords is a source as a person reads it.
|
|
||||||
func sourceWords(repository, seat string) string {
|
|
||||||
if seat == "" {
|
|
||||||
return repository
|
|
||||||
}
|
|
||||||
return buildSource{Repository: repository, Seat: seat}.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// firstBuildWarrant is what the controller checked before it asks the first build of the modules a merge adds to a
|
|
||||||
// repository nothing is built from: the merge was made on the operator's Approve of its head (approvedAnnounced). It
|
|
||||||
// travels in the context of that ask alone, in this process, so no verb carries one.
|
|
||||||
type firstBuildWarrant struct {
|
|
||||||
repository string // owner/name on the git seat
|
|
||||||
paths map[string]bool
|
|
||||||
why string
|
|
||||||
}
|
|
||||||
|
|
||||||
type firstBuildWarrantKey struct{}
|
|
||||||
|
|
||||||
// withFirstBuildWarrant is ctx carrying the warrant for asking the first builds of paths in repository.
|
|
||||||
func withFirstBuildWarrant(ctx context.Context, repository string, paths []string, why string) context.Context {
|
|
||||||
w := &firstBuildWarrant{repository: repository, paths: map[string]bool{}, why: why}
|
|
||||||
for _, p := range paths {
|
|
||||||
w.paths[strings.Trim(p, "/")] = true
|
|
||||||
}
|
|
||||||
return context.WithValue(ctx, firstBuildWarrantKey{}, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func firstBuildWarrantIn(ctx context.Context) *firstBuildWarrant {
|
|
||||||
w, _ := ctx.Value(firstBuildWarrantKey{}).(*firstBuildWarrant)
|
|
||||||
return w
|
|
||||||
}
|
|
||||||
|
|
||||||
// covers says the warrant is for exactly this ask: the repository on the git seat, and a module path the merge holds.
|
|
||||||
func (w *firstBuildWarrant) covers(source buildSource, path string) bool {
|
|
||||||
return w != nil && source.Seat == gitSeat && strings.EqualFold(strings.Trim(source.Repository, "/"), w.repository) &&
|
|
||||||
w.paths[strings.Trim(path, "/")]
|
|
||||||
}
|
|
||||||
|
|
||||||
// verbMayAsk refuses, for a call through a verb, a build of a repository the catalogue builds no module from
|
|
||||||
// (novox/hq ADR 0266): the build node would run what an agent wrote, and its outcome could never be
|
|
||||||
// registered anyway. url is the repository as it is cloned. At the terminal anything may be asked.
|
|
||||||
func verbMayAsk(ctx context.Context, source buildSource, url, path string) error {
|
|
||||||
if startedAtTheTerminal() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// **Or the first build of a module a merge adds on the operator's Approve** (novox/hq ADR 0432, issue 1146): the
|
|
||||||
// controller's own ask, carrying the warrant it checked, for that repository and the module paths the merge holds.
|
|
||||||
if firstBuildWarrantIn(ctx).covers(source, path) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
entries, err := open.inventory.Catalogued(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
forms := newSourceForms(ctx, open.inventory)
|
|
||||||
if forms.buildsFrom(entries, source.Repository, source.Seat) || forms.buildsFrom(entries, url, "") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return terminalRefusal("%s is no repository the catalogue builds a module from, and a build of any other is "+
|
|
||||||
"asked at the controller's terminal only, never through a verb: a build node runs what the repository "+
|
|
||||||
"says, and its outcome would register a module the next push sends — whoever may call a verb includes "+
|
|
||||||
"agents (novox/hq ADR 0266). Nothing was asked", source)
|
|
||||||
}
|
|
||||||
@@ -1,353 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
func theCatalogue(t *testing.T) *stores {
|
|
||||||
t.Helper()
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
for _, b := range []link.BuildResult{
|
|
||||||
onTrunk("build-sudo", "novox/mesh-catalog", "git", "modules/sudo", map[string]any{"module": "sudo", "version": "1"}),
|
|
||||||
onTrunk("build-host", "novox/mesh-host", "git", "", map[string]any{"module": "mesh-host", "version": "1"}),
|
|
||||||
} {
|
|
||||||
keptAsked(t, open.inventory, b.ID, b.Source.Repository, b.Path, true)
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, b); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return open
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABuildFromAnAgentsRepositoryIsNotRegisteredAsAModuleTheMeshHolds(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
for _, c := range []struct {
|
|
||||||
name, repository, path, module string
|
|
||||||
}{
|
|
||||||
{"its own repository naming sudo", "agent/sudo", "modules/sudo", "sudo"},
|
|
||||||
{"a fork of the catalogue", "agent/mesh-catalog", "modules/sudo", "sudo"},
|
|
||||||
{"a fork of the node-engine", "agent/mesh-host", "", "mesh-host"},
|
|
||||||
} {
|
|
||||||
t.Run(c.name, func(t *testing.T) {
|
|
||||||
id := "build-" + strings.ReplaceAll(c.repository, "/", "-")
|
|
||||||
keptAsked(t, open.inventory, id, c.repository, c.path, false) // through the build verb
|
|
||||||
evil := onTrunk(id, c.repository, "git", c.path, map[string]any{"module": c.module, "version": "evil"})
|
|
||||||
_, _, err := takeIn(ctx, open.inventory, evil)
|
|
||||||
if !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("a build of %s was taken in as %s: %v", c.repository, c.module, err)
|
|
||||||
}
|
|
||||||
shelf, err := open.inventory.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := shelf[c.module].Version; got != "1" {
|
|
||||||
t.Fatalf("%s is now %q, from %s", c.module, got, c.repository)
|
|
||||||
}
|
|
||||||
if _, found, _ := open.inventory.BuildByID(ctx, id); !found {
|
|
||||||
t.Errorf("the refused build %s is not recorded", id)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestANewModuleFromARepositoryTheCatalogueDoesNotBuildFromIsNotRegistered(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
keptAsked(t, open.inventory, "build-new", "agent/tools", "", false)
|
|
||||||
_, _, err := takeIn(ctx, open.inventory, onTrunk("build-new", "agent/tools", "git", "",
|
|
||||||
map[string]any{"module": "agent-tools", "version": "1"}))
|
|
||||||
if !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("a new module from an agent's repository was taken in: %v", err)
|
|
||||||
}
|
|
||||||
// And one asked of nobody here — an outcome on the bus no request was kept for — the same.
|
|
||||||
_, _, err = takeIn(ctx, open.inventory, onTrunk("build-unasked", "agent/tools", "git", "",
|
|
||||||
map[string]any{"module": "agent-tools", "version": "1"}))
|
|
||||||
if !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("an outcome nobody asked for was taken in: %v", err)
|
|
||||||
}
|
|
||||||
if shelf, _ := open.inventory.Catalogue(ctx); shelf["agent-tools"].Module != "" {
|
|
||||||
t.Fatal("the refused module is in the catalogue")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAtTheTerminalAnotherRepositoryIsRegistered(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
keptAsked(t, open.inventory, "build-moved", "novox/sudo", "", true)
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, onTrunk("build-moved", "novox/sudo", "git", "",
|
|
||||||
map[string]any{"module": "sudo", "version": "2"})); err != nil {
|
|
||||||
t.Fatalf("a move the operator asked for at the terminal was refused: %v", err)
|
|
||||||
}
|
|
||||||
if src, _ := open.inventory.SourceOf(ctx, "sudo"); src.Repository != "novox/sudo" {
|
|
||||||
t.Fatalf("sudo is built from %q", src.Repository)
|
|
||||||
}
|
|
||||||
if err := open.inventory.RecordBuildRequest(ctx, inventory.BuildRequest{ID: "build-external",
|
|
||||||
Repository: "http://forge.internal:20000/someone/app.git", For: "build", AtTerminal: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, onTrunk("build-external", "someone/app", "", "",
|
|
||||||
map[string]any{"module": "app", "version": "1"})); err != nil {
|
|
||||||
t.Fatalf("a new module the operator asked for at the terminal was refused: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestADeliveryFromTheRegisteredRepositoryIsRegistered(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
rebuilt := onTrunk("build-plan", "novox/mesh-catalog", "git", "modules/sudo", map[string]any{"module": "sudo", "version": "2"})
|
|
||||||
if err := open.inventory.RecordBuildRequest(ctx, inventory.BuildRequest{ID: rebuilt.ID,
|
|
||||||
Repository: "novox/mesh-catalog", Seat: "git", Path: "modules/sudo", For: "plan"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, rebuilt); err != nil {
|
|
||||||
t.Fatalf("a plan's build of the module's own repository was refused: %v", err)
|
|
||||||
}
|
|
||||||
added := onTrunk("build-merge", "novox/mesh-catalog", "git", "modules/zram", map[string]any{"module": "zram", "version": "1"})
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, added); err != nil {
|
|
||||||
t.Fatalf("a module a merge added to the catalogue repository was refused: %v", err)
|
|
||||||
}
|
|
||||||
shelf, _ := open.inventory.Catalogue(ctx)
|
|
||||||
if shelf["sudo"].Version != "2" || shelf["zram"].Module == "" {
|
|
||||||
t.Fatalf("not registered: sudo %q, zram %q", shelf["sudo"].Version, shelf["zram"].Module)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAVerbAsksNoBuildOfARepositoryTheCatalogueDoesNotBuildFrom(t *testing.T) {
|
|
||||||
theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
t.Setenv(verbVar, "build")
|
|
||||||
t.Setenv(link.CallerVar, "node-tools.anchor, through the mesh-controller seat")
|
|
||||||
err := verbMayAsk(ctx, buildSource{Repository: "agent/sudo", Seat: "git"}, "http://forge.internal:20000/agent/sudo.git", "")
|
|
||||||
var policy *heldAtTheTerminal
|
|
||||||
if !errors.As(err, &policy) {
|
|
||||||
t.Fatalf("a verb's build of an agent's repository was asked: %v", err)
|
|
||||||
}
|
|
||||||
if err := verbMayAsk(ctx, buildSource{Repository: "novox/mesh-catalog", Seat: "git"},
|
|
||||||
"http://forge.internal:20000/novox/mesh-catalog.git", ""); err != nil {
|
|
||||||
t.Fatalf("a verb's build of the catalogue repository was refused: %v", err)
|
|
||||||
}
|
|
||||||
t.Setenv(verbVar, "")
|
|
||||||
t.Setenv(link.CallerVar, "")
|
|
||||||
if err := verbMayAsk(ctx, buildSource{Repository: "agent/sudo", Seat: "git"}, "", ""); err != nil {
|
|
||||||
t.Fatalf("the terminal was refused: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestARollbackNeverPutsBackABuildFromAnotherRepository(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
fork := onTrunk("build-1791500000000000000", "agent/mesh-catalog", "git", "modules/sudo",
|
|
||||||
map[string]any{"module": "sudo", "version": "evil"})
|
|
||||||
fork.Commit = "c0ffee0123456789" // the commit sudo was registered at
|
|
||||||
keptAsked(t, inv, fork.ID, "agent/mesh-catalog", "modules/sudo", false)
|
|
||||||
if _, _, err := takeIn(ctx, inv, fork); !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("the fork's build was taken in: %v", err)
|
|
||||||
}
|
|
||||||
// Asked after the registered build, whenever the test runs: an id naming a fixed moment read as older
|
|
||||||
// than the registered build once the clock passed it (2026-10-10 02:40 UTC), and the test failed on main.
|
|
||||||
failed := onTrunk(fmt.Sprintf("build-%d", time.Now().Add(time.Hour).UnixNano()), "novox/mesh-catalog", "git", "modules/sudo",
|
|
||||||
map[string]any{"module": "sudo", "version": "2"})
|
|
||||||
failed.Commit = "badbadbad0123456"
|
|
||||||
if _, _, err := takeIn(ctx, inv, failed); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
record, _, err := inv.BuildByID(ctx, failed.ID)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
previous, found, err := inv.PreviousBuild(ctx, "sudo", "c0ffee0123456789", record)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if found && previous.ID == fork.ID {
|
|
||||||
t.Fatalf("a rollback would put back the fork's build %s", previous.ID)
|
|
||||||
}
|
|
||||||
if !found || previous.ID != "build-sudo" {
|
|
||||||
t.Fatalf("a rollback puts back %q (found %v), want the registered build-sudo", previous.ID, found)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestATrunkTheForgeDoesNotProtectRegistersNothing(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
theForge.Store("novox/unguarded", forgeFacts{ID: 7, Why: "the rule main lets a person push to main directly"})
|
|
||||||
t.Cleanup(func() { theForge.Delete("novox/unguarded") })
|
|
||||||
first := onTrunk("build-unguarded-1", "novox/unguarded", "git", "", map[string]any{"module": "unguarded", "version": "1"})
|
|
||||||
keptAsked(t, open.inventory, first.ID, "novox/unguarded", "", true)
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, first); err != nil {
|
|
||||||
t.Fatalf("at the terminal: %v", err)
|
|
||||||
}
|
|
||||||
again := onTrunk("build-unguarded-2", "novox/unguarded", "git", "", map[string]any{"module": "unguarded", "version": "2"})
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, again); !errors.Is(err, errNotItsSource) ||
|
|
||||||
!strings.Contains(err.Error(), "push to main directly") {
|
|
||||||
t.Fatalf("a rebuild from an unprotected trunk was taken in: %v", err)
|
|
||||||
}
|
|
||||||
theForge.Store("novox/unguarded", errors.New("nothing serves gitea.gitea_api"))
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, onTrunk("build-unguarded-3", "novox/unguarded", "git", "",
|
|
||||||
map[string]any{"module": "unguarded", "version": "3"})); !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("a forge that could not say was read as a protected trunk: %v", err)
|
|
||||||
}
|
|
||||||
if shelf, _ := open.inventory.Catalogue(ctx); shelf["unguarded"].Version != "1" {
|
|
||||||
t.Fatalf("unguarded is %q", shelf["unguarded"].Version)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestARepositoryMadeAgainUnderItsNameIsNotTheModulesSource(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
theForge.Store("novox/remade", forgeFacts{ID: 100, Guarded: true})
|
|
||||||
t.Cleanup(func() { theForge.Delete("novox/remade") })
|
|
||||||
first := onTrunk("build-remade-1", "novox/remade", "git", "", map[string]any{"module": "remade", "version": "1"})
|
|
||||||
keptAsked(t, open.inventory, first.ID, "novox/remade", "", true)
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, first); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if id, _ := open.inventory.SourceIdentity(ctx, "remade"); id != 100 {
|
|
||||||
t.Fatalf("the forge's id was not recorded: %d", id)
|
|
||||||
}
|
|
||||||
theForge.Store("novox/remade", forgeFacts{ID: 101, Guarded: true}) // deleted, and made again by an agent
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, onTrunk("build-remade-2", "novox/remade", "git", "",
|
|
||||||
map[string]any{"module": "remade", "version": "evil"})); !errors.Is(err, errNotItsSource) ||
|
|
||||||
!strings.Contains(err.Error(), "made again") {
|
|
||||||
t.Fatalf("a repository made again under the name was taken in: %v", err)
|
|
||||||
}
|
|
||||||
// And a new module from it, beside the one registered from the first, the same.
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, onTrunk("build-remade-3", "novox/remade", "git", "modules/other",
|
|
||||||
map[string]any{"module": "other", "version": "1"})); !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("a new module from a repository made again was taken in: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestATerminalRequestCoversOnlyWhatItAsked(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if err := open.inventory.RecordBuildRequest(ctx, inventory.BuildRequest{ID: "build-asked", Repository: "novox/app",
|
|
||||||
Seat: "git", Path: "modules/app", For: "build", AtTerminal: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
other := onTrunk("build-asked", "agent/sudo", "git", "modules/app", map[string]any{"module": "sudo", "version": "evil"})
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, other); !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("an outcome of another repository under a terminal request's id was taken in: %v", err)
|
|
||||||
}
|
|
||||||
elsewhere := onTrunk("build-asked", "novox/app", "git", "modules/sudo", map[string]any{"module": "sudo", "version": "evil"})
|
|
||||||
if _, _, err := takeIn(ctx, open.inventory, elsewhere); !errors.Is(err, errNotItsSource) {
|
|
||||||
t.Fatalf("an outcome of another path under a terminal request's id was taken in: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheServingControllerIsNeverTheTerminal(t *testing.T) {
|
|
||||||
t.Setenv(verbVar, "")
|
|
||||||
t.Setenv(link.CallerVar, "")
|
|
||||||
t.Setenv(servedVar, "")
|
|
||||||
if !startedAtTheTerminal() {
|
|
||||||
t.Fatal("a process started by hand is not the terminal")
|
|
||||||
}
|
|
||||||
markServed()
|
|
||||||
if startedAtTheTerminal() {
|
|
||||||
t.Fatal("the serving controller reads as the terminal")
|
|
||||||
}
|
|
||||||
child := exec.Command(os.Args[0], "-test.run=^$")
|
|
||||||
child.Env = os.Environ()
|
|
||||||
if !slices.Contains(child.Env, servedVar+"=1") {
|
|
||||||
t.Fatal("what the serving controller starts does not carry its mark")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAnUnknownIdentityKeepsTheRecordedOne(t *testing.T) {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
if err := open.inventory.SetSourceIdentity(ctx, "sudo", 100); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := open.inventory.SetSourceIdentity(ctx, "sudo", 0); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if id, _ := open.inventory.SourceIdentity(ctx, "sudo"); id != 100 {
|
|
||||||
t.Fatalf("the recorded id became %d", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAModuleMovesOnTheOperatorsApproveOfTheMergeThatMovedIt(t *testing.T) {
|
|
||||||
const commit = "c0ffee0123456789"
|
|
||||||
for _, c := range []struct {
|
|
||||||
what string
|
|
||||||
change func(*anApprovedMerge)
|
|
||||||
unkept bool
|
|
||||||
request string // who asked: "merge", or "build" through a verb
|
|
||||||
moved bool
|
|
||||||
}{
|
|
||||||
{what: "the operator's Approve of the merged head", request: "merge", moved: true},
|
|
||||||
{what: "no merge mesh-delivery announced", unkept: true, request: "merge"},
|
|
||||||
{what: "a warrant for other heads", request: "merge", change: func(a *anApprovedMerge) {
|
|
||||||
a.merge.Heads = map[string]string{"novox/mesh-host@aaaaaaaaaaaa": "e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5"}
|
|
||||||
}},
|
|
||||||
{what: "a Decline", request: "merge", change: func(a *anApprovedMerge) {
|
|
||||||
a.warrant.Option, a.warrant.Label, a.merge.Option = "decline", "Decline", "decline"
|
|
||||||
}},
|
|
||||||
{what: "a person who is not the operator", request: "merge", change: func(a *anApprovedMerge) { a.warrant.By.Who = "agent" }},
|
|
||||||
{what: "a build asked through a verb", request: "build"},
|
|
||||||
} {
|
|
||||||
open := theCatalogue(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
a := approvedMerge(t)
|
|
||||||
a.merge.Owner, a.merge.Repo, a.merge.Number, a.merge.HeadSHA = "novox", "mesh-host", 7, "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1"
|
|
||||||
if c.change != nil {
|
|
||||||
c.change(&a)
|
|
||||||
}
|
|
||||||
wasKept, wasAsk := keptDeliveryMerge, routerAskOf
|
|
||||||
keptDeliveryMerge = func(_ context.Context, owner, repo string, number int, head string) (deliveryMerge, bool, error) {
|
|
||||||
return a.merge, !c.unkept && owner == "novox" && repo == "mesh-host" && number == 7 && head == a.merge.HeadSHA, nil
|
|
||||||
}
|
|
||||||
routerAskOf = func(_ context.Context, _ catalogueReader, _, id string) (*asks.Ask, *asks.Warrant, error) {
|
|
||||||
if id != a.merge.AskID {
|
|
||||||
t.Errorf("%s: the router was asked for %q", c.what, id)
|
|
||||||
}
|
|
||||||
return &a.ask, &a.warrant, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { keptDeliveryMerge, routerAskOf = wasKept, wasAsk })
|
|
||||||
event, _ := json.Marshal(map[string]any{"owner": "novox", "repo": "mesh-host", "base": "main", "number": 7,
|
|
||||||
"head_sha": a.merge.HeadSHA, "merge_commit_sha": commit})
|
|
||||||
if _, err := inv.AddMerge(ctx, inventory.BatchedMerge{Repository: "novox/mesh-host", Branch: "main", Commit: commit,
|
|
||||||
Merged: time.Now(), Heard: time.Now(), Event: event}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
id := fmt.Sprintf("build-%d", time.Now().Add(time.Hour).UnixNano())
|
|
||||||
if err := inv.RecordBuildRequest(ctx, inventory.BuildRequest{ID: id, Repository: "novox/mesh-host", Seat: "git",
|
|
||||||
Path: "modules/sudo", Ref: "main", Commit: commit, For: c.request}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
moved := onTrunk(id, "novox/mesh-host", "git", "modules/sudo", map[string]any{"module": "sudo", "version": "2"})
|
|
||||||
moved.Commit = commit
|
|
||||||
_, _, err := takeIn(ctx, inv, moved)
|
|
||||||
src, _ := inv.SourceOf(ctx, "sudo")
|
|
||||||
switch {
|
|
||||||
case c.moved && err != nil:
|
|
||||||
t.Errorf("%s: the move was refused: %v", c.what, err)
|
|
||||||
case c.moved && src.Repository != "novox/mesh-host":
|
|
||||||
t.Errorf("%s: sudo is built from %q", c.what, src.Repository)
|
|
||||||
case !c.moved && !errors.Is(err, errNotItsSource):
|
|
||||||
t.Errorf("%s: the move was not refused as ADR 0266 has it: %v", c.what, err)
|
|
||||||
case !c.moved && src.Repository != "novox/mesh-catalog":
|
|
||||||
t.Errorf("%s: a refused move changed sudo's source to %q", c.what, src.Repository)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"hash/fnv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The routes to root a review of ADR 0266 found (novox/hq ADR 0266 §7): an agent makes a repository of its
|
|
||||||
// own — or forks one the mesh builds from — commits a module.json naming a module the mesh runs everywhere
|
|
||||||
// (`sudo`, granting itself a rule without a password; `mesh-host`, the node-engine), and asks the `build` verb
|
|
||||||
// for it. Its outcome was registered under that name from the agent's repository, and the next push sent it.
|
|
||||||
|
|
||||||
// onTrunk is an outcome of a commit on its repository's trunk, as the build seat says it.
|
|
||||||
func onTrunk(id, repository, seat, path string, manifest map[string]any) link.BuildResult {
|
|
||||||
raw, _ := json.Marshal(manifest)
|
|
||||||
r := link.BuildResult{ID: id, Repository: "http://forge.internal:20000/" + repository + ".git", Path: path,
|
|
||||||
Ref: "main", On: "anchor", Commit: "c0ffee0123456789", Manifest: raw,
|
|
||||||
Trunk: "main", OnTrunk: true, Branches: []string{"main"}}
|
|
||||||
if seat != "" {
|
|
||||||
r.Source = &link.SourceOnSeat{Seat: seat, Repository: repository}
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// keptAsked keeps a build request as the asker would: through a verb, or at the terminal.
|
|
||||||
func keptAsked(t *testing.T, inv *inventory.Inventory, id, repository, path string, atTerminal bool) {
|
|
||||||
t.Helper()
|
|
||||||
if err := inv.RecordBuildRequest(t.Context(), inventory.BuildRequest{ID: id, Repository: repository, Seat: "git",
|
|
||||||
Path: path, For: "build", AtTerminal: atTerminal}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// theCatalogue is a mesh whose sudo is built from the catalogue repository and whose node-engine from its own.
|
|
||||||
|
|
||||||
// The operator at the terminal may still move a module, or add one from a new repository.
|
|
||||||
|
|
||||||
// The delivery's flow is untouched: a merge's rebuild of a module from its own repository, and a merge adding
|
|
||||||
// a module to a repository the catalogue builds from (novox/hq issue 300), are registered with no terminal.
|
|
||||||
|
|
||||||
// Through a verb, a build of a repository the catalogue builds nothing from is not even asked: the build node
|
|
||||||
// would run what the agent wrote.
|
|
||||||
|
|
||||||
// asTheOperator keeps a build as asked at the controller's terminal, as the operator's first build of a module
|
|
||||||
// from a repository the catalogue does not yet build from is (novox/hq ADR 0266), and hands it back.
|
|
||||||
func asTheOperator(t *testing.T, inv *inventory.Inventory, b link.BuildResult) link.BuildResult {
|
|
||||||
t.Helper()
|
|
||||||
repository, seat := b.Repository, ""
|
|
||||||
if b.Source != nil {
|
|
||||||
repository, seat = b.Source.Repository, b.Source.Seat
|
|
||||||
}
|
|
||||||
if err := inv.RecordBuildRequest(t.Context(), inventory.BuildRequest{ID: b.ID, Repository: repository, Seat: seat,
|
|
||||||
Path: b.Path, For: "build", AtTerminal: true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// A rollback puts back only a build of the module's own repository: an agent's build of the module's name,
|
|
||||||
// recorded and refused, at the very commit the machine ran before (a fork carries it), is never registered by
|
|
||||||
// the back door of a failed gate.
|
|
||||||
|
|
||||||
// theForge is what the forge says in a test, by owner/name: a repository not named here is protected as a
|
|
||||||
// trunk must be, with an id of its own.
|
|
||||||
var theForge sync.Map
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
askTheForge = func(_ context.Context, owner, repo, _ string) (forgeFacts, error) {
|
|
||||||
if said, ok := theForge.Load(owner + "/" + repo); ok {
|
|
||||||
switch f := said.(type) {
|
|
||||||
case error:
|
|
||||||
return forgeFacts{}, f
|
|
||||||
case forgeFacts:
|
|
||||||
return f, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h := fnv.New32a()
|
|
||||||
_, _ = h.Write([]byte(owner + "/" + repo))
|
|
||||||
return forgeFacts{ID: int64(h.Sum32()), Guarded: true}, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A module's trunk the forge does not protect — direct pushes, no required status — or a forge that cannot say,
|
|
||||||
// registers nothing through a verb: the trunk rule means nothing on a branch anyone pushes to.
|
|
||||||
|
|
||||||
// A repository deleted and made again under the module's repository's name is another repository: the forge's
|
|
||||||
// id, recorded at registration, tells them apart.
|
|
||||||
|
|
||||||
// The terminal's mark is the operator's for the repository and path they asked: an outcome of another, under that
|
|
||||||
// build's id, is not theirs.
|
|
||||||
|
|
||||||
// The serving controller, and everything it starts, is never the terminal: a build asked in it reads as asked
|
|
||||||
// through the mesh even when no verb and no caller is named.
|
|
||||||
|
|
||||||
// The forge applies one rule to a branch: the rule named for it, else the first glob that covers it. A stronger
|
|
||||||
// rule later in the list is not what guards the branch, and is not read as if it were (the confirmation review).
|
|
||||||
func TestTheRuleJudgedIsTheOneTheForgeApplies(t *testing.T) {
|
|
||||||
guarded := protectionRule{Rule: "*", RequiredStatuses: []string{"mesh/merge-gate"}}
|
|
||||||
open := protectionRule{Rule: "main", Push: true, RequiredStatuses: []string{"mesh/merge-gate"}}
|
|
||||||
if f := judgedRule([]protectionRule{guarded, open}, "main"); f.Guarded {
|
|
||||||
t.Error("an exact rule letting pushes was passed over for a glob that guards")
|
|
||||||
}
|
|
||||||
if f := judgedRule([]protectionRule{{Rule: "ma*", RequiredStatuses: nil}, {Rule: "*", RequiredStatuses: []string{"x"}}},
|
|
||||||
"main"); f.Guarded || !strings.Contains(f.Why, "ma*") {
|
|
||||||
t.Errorf("the first glob covering the branch was not the one judged: %+v", f)
|
|
||||||
}
|
|
||||||
if f := judgedRule([]protectionRule{{Rule: "release/*"}, {Rule: "main", RequiredStatuses: []string{"x"}}}, "main"); !f.Guarded {
|
|
||||||
t.Errorf("the rule named for the branch was not judged: %+v", f)
|
|
||||||
}
|
|
||||||
if f := judgedRule(nil, "main"); f.Guarded {
|
|
||||||
t.Error("no rule is no protection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// An id the forge could not give keeps the id already recorded.
|
|
||||||
|
|
||||||
// **The operator's Approve of a reviewed group registers a moved module from its new repository** (novox/hq ADR 0420 §7,
|
|
||||||
// issue 1025). sudo is built from the catalogue; a merge into the repository of node-engine's module, announced by
|
|
||||||
// mesh-delivery with the ask it merged on, brings its build from there. Accepted with the operator's Approve of the
|
|
||||||
// merged head; refused with no kept merge, a warrant for other heads, a Decline, a person who is not the operator, and
|
|
||||||
// through a verb.
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
sdkdelivery "git.novox.be/novox/mesh-sdk/go/delivery"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A build's outcome is taken in the same way whoever hears it (novox/hq issue 176): recorded, and
|
|
||||||
// the module registered with its source as the seat and path when the request said so — never the
|
|
||||||
// URL. A definition naming an installation is recorded and not registered; a failure is recorded
|
|
||||||
// and said.
|
|
||||||
|
|
||||||
// novox/hq 04-ISSUES/215: a build asked at a commit is recorded as built from that commit, and the
|
|
||||||
// module keeps following the branch it followed — a new one, the default branch.
|
|
||||||
|
|
||||||
// novox/hq 04-ISSUES/219: an older request heard after a newer one is recorded and not registered,
|
|
||||||
// so a push sends what the newer request built.
|
|
||||||
|
|
||||||
func TestABuildIDSaysWhenItWasAsked(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 3, 21, 51, 57, 392539762, time.UTC)
|
|
||||||
if got, ok := link.BuildAskedAt(link.NewBuildID(at)); !ok || !got.Equal(at) {
|
|
||||||
t.Errorf("read back %v %v; want %v", got, ok, at)
|
|
||||||
}
|
|
||||||
if got, ok := link.BuildAskedAt("build-1791064317392539762"); !ok || got.Format(time.TimeOnly) != "21:51:57" {
|
|
||||||
t.Errorf("the incident's id reads as %v %v", got, ok)
|
|
||||||
}
|
|
||||||
for _, id := range []string{"b-1", "build-2", "build-", "build-x", ""} {
|
|
||||||
if _, ok := link.BuildAskedAt(id); ok {
|
|
||||||
t.Errorf("%q read as a request time", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// novox/hq issue 1204: builds says the source fingerprint the builder recorded, the one tool that reads it.
|
|
||||||
func TestBuildsSaysTheSourceFingerprint(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 19, 10, 40, 0, 0, time.UTC)
|
|
||||||
built := inventory.Build{ID: "build-1", Module: "mailu", Commit: "b4527d9091e972f28cb365623d9280045e7741a3", On: "ace",
|
|
||||||
Repository: "novox/mesh-catalog", At: at, ReuseKey: "r1:abc",
|
|
||||||
SourceFingerprint: "src1:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}
|
|
||||||
|
|
||||||
// 1204/AC1
|
|
||||||
var out bytes.Buffer
|
|
||||||
printBuild(&out, built)
|
|
||||||
lines := strings.Split(strings.TrimRight(out.String(), "\n"), "\n")
|
|
||||||
if last := lines[len(lines)-1]; last != " source "+built.SourceFingerprint {
|
|
||||||
t.Errorf("last line %q; want the source line after the reuse key:\n%s", last, out.String())
|
|
||||||
}
|
|
||||||
if !strings.Contains(lines[len(lines)-2], "reuse key r1:abc") {
|
|
||||||
t.Errorf("the source line does not follow the reuse key:\n%s", out.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1204/AC2
|
|
||||||
without := built
|
|
||||||
without.SourceFingerprint = ""
|
|
||||||
var plain bytes.Buffer
|
|
||||||
printBuild(&plain, without)
|
|
||||||
if strings.Contains(plain.String(), "source") {
|
|
||||||
t.Errorf("a build with no fingerprint says a source line:\n%s", plain.String())
|
|
||||||
}
|
|
||||||
if want := strings.Join(lines[:len(lines)-1], "\n") + "\n"; plain.String() != want {
|
|
||||||
t.Errorf("the other lines changed:\n%s\nwant\n%s", plain.String(), want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1215/AC4: what the builder said of the inputs is what the controller keeps of the build.
|
|
||||||
func TestTheControllerKeepsTheInputsABuilderSaid(t *testing.T) {
|
|
||||||
in := &sdkdelivery.SourceInputs{Module: "mailu", Tree: "t", Toolchains: map[string]string{"code": "ts sha256:aa 01"}}
|
|
||||||
kept := buildFrom(link.BuildResult{ID: "build-1", Repository: "r", Commit: "c", SourceFingerprint: "src1:x", SourceInputs: in})
|
|
||||||
if !reflect.DeepEqual(kept.SourceInputs, in) {
|
|
||||||
t.Errorf("the controller keeps %+v, the builder said %+v", kept.SourceInputs, in)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,410 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The bus as a planned step (novox/hq to-be 45 §8, ADR 0227 rule 8, ADR 0236).
|
|
||||||
//
|
|
||||||
// **A bus upgrade is never rolled out.** The bus carries every declaration, every report and the
|
|
||||||
// controller's own lease; a new bus build that does not come up is a mesh nobody can tell anything,
|
|
||||||
// and a version whose data format moved (2.10 → 2.11) cannot be undone by putting the old one back.
|
|
||||||
// So nothing sends a new bus build on its own: its policy is `record` whatever anyone says (the
|
|
||||||
// catalogue's DerivedUpgrade, and `upgrade` refuses a roll-out), a plan builds it and sends nothing, a
|
|
||||||
// cascade holds the machine (ADR 0221), and a push naming that machine is refused while a new bus build
|
|
||||||
// waits for it (busHeld). The one way is this verb: a person, with why, the streams snapshotted first,
|
|
||||||
// whether it can be reverted said before it starts, the step said as `bus-maintenance` while it runs,
|
|
||||||
// and every stream, durable consumer and a round trip checked after (H-bus) — or the step said failed,
|
|
||||||
// with its snapshot as the way back.
|
|
||||||
|
|
||||||
// busStepProbe is the registry's row for the step; its kinds.
|
|
||||||
const (
|
|
||||||
busStepProbe = "DB"
|
|
||||||
kindBusMaintenance = "bus-maintenance"
|
|
||||||
kindBusUpgradeFailed = "bus-upgrade-failed"
|
|
||||||
)
|
|
||||||
|
|
||||||
// busStepBound is how long after its start a bus upgrade must be followed by a healthy bus.
|
|
||||||
var busStepBound = 15 * time.Minute
|
|
||||||
|
|
||||||
// takeBusSnapshot snapshots every stream before a bus upgrade and answers where the snapshot is: the bus
|
|
||||||
// machine's backup holder backs the bus module up now, whose dump is the streams' snapshot (novox/hq ADR
|
|
||||||
// 0235). A variable so a test takes none. A person who took one by hand says where with --snapshot-taken,
|
|
||||||
// and then none is taken — for a bus whose module does not yet carry the snapshot program.
|
|
||||||
var takeBusSnapshot = snapshotTheBusNow
|
|
||||||
|
|
||||||
// busPending is what a bus upgrade would do: the bus's module, the machines running it, and, per
|
|
||||||
// machine, the build it was last sent against the build the mesh holds. Empty machines: the mesh holds
|
|
||||||
// no bus module.
|
|
||||||
type busPending struct {
|
|
||||||
module string
|
|
||||||
machines []string
|
|
||||||
from map[string]string
|
|
||||||
to string
|
|
||||||
// same are the commits whose build was made from the same source as the build the mesh holds, or
|
|
||||||
// made the same artifacts and manifest (novox/hq issue 280).
|
|
||||||
same map[string]bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// moves is whether sending the machine would replace its bus: a build it was not last sent, unless the
|
|
||||||
// two builds were made from the same source, or made the same artifacts from the same manifest — a
|
|
||||||
// rebuild of the same source for another module's merge changes nothing the machine runs, whatever
|
|
||||||
// image digest it made (novox/hq issue 280).
|
|
||||||
func (b busPending) moves(machine string) bool {
|
|
||||||
from, known := b.from[machine]
|
|
||||||
if b.module == "" || b.to == "" || (known && sameCommit(from, b.to)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return !known || !b.same[from]
|
|
||||||
}
|
|
||||||
|
|
||||||
// pendingBus reads what a bus upgrade would do.
|
|
||||||
func pendingBus(ctx context.Context, inv *inventory.Inventory) (busPending, error) {
|
|
||||||
var b busPending
|
|
||||||
shelf, err := inv.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
for name, m := range shelf {
|
|
||||||
if catalogue.ProvidesBus(m) {
|
|
||||||
b.module = name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if b.module == "" {
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
current, err := inv.CurrentBuilds(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
b.to = current[b.module].Commit
|
|
||||||
if b.machines, err = inv.Running(ctx, b.module); err != nil {
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
b.from, b.same = map[string]string{}, map[string]bool{}
|
|
||||||
made, err := inv.BuildFingerprints(ctx, b.module)
|
|
||||||
if err != nil {
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
for commit, refs := range made {
|
|
||||||
b.same[commit] = refs != "" && refs == made[b.to]
|
|
||||||
}
|
|
||||||
sources, err := inv.BuildSourceFingerprints(ctx, b.module)
|
|
||||||
if err != nil {
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
for commit, src := range sources {
|
|
||||||
if src != "" && src == sources[b.to] {
|
|
||||||
b.same[commit] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, n := range b.machines {
|
|
||||||
sent, known, err := inv.SentBuilds(ctx, n)
|
|
||||||
if err != nil {
|
|
||||||
return b, err
|
|
||||||
}
|
|
||||||
if known {
|
|
||||||
if c, carried := sent[b.module]; carried {
|
|
||||||
b.from[n] = c
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// busHeld names the machines a push may not send because sending them would replace the bus: the
|
|
||||||
// planned step's, not a push's (ADR 0236). Said with the remedy.
|
|
||||||
func busHeld(ctx context.Context, inv *inventory.Inventory, machines []string) (map[string]string, error) {
|
|
||||||
b, err := pendingBus(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
out := map[string]string{}
|
|
||||||
for _, n := range machines {
|
|
||||||
for _, holder := range b.machines {
|
|
||||||
if n == holder && b.moves(n) {
|
|
||||||
out[n] = fmt.Sprintf("sending %s would replace the bus (%s %s → %s), which is a planned step: "+
|
|
||||||
"`bus upgrade --why …` snapshots its streams first and checks them after (novox/hq ADR 0236)",
|
|
||||||
n, b.module, short(orNotKnown(b.from[n])), short(b.to))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// busCommand is `bus` — what a bus upgrade would do and how the last went — and `bus upgrade`.
|
|
||||||
func busCommand(ctx context.Context, args []string) error {
|
|
||||||
sub := ""
|
|
||||||
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
||||||
sub, args = args[0], args[1:]
|
|
||||||
}
|
|
||||||
set := flag.NewFlagSet("bus", flag.ContinueOnError)
|
|
||||||
snapshot := set.String("snapshot-taken", "", "where the streams' snapshot a person took is, while the mesh takes none itself")
|
|
||||||
reversible := set.Bool("reversible", false, "the new version can be undone by putting the old one back")
|
|
||||||
irreversible := set.Bool("irreversible", false, "the new version cannot be undone by putting the old one back, "+
|
|
||||||
"and this is the person's explicit word that it runs anyway")
|
|
||||||
why := addHandActFlags(set)
|
|
||||||
if rest, err := parseAround(set, args); err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(rest) > 0 {
|
|
||||||
return errors.New("bus [upgrade --why … --reversible|--irreversible [--snapshot-taken <where>]]")
|
|
||||||
}
|
|
||||||
switch sub {
|
|
||||||
case "":
|
|
||||||
return busStatus(ctx)
|
|
||||||
case "upgrade":
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("bus says what a bus upgrade would do, or `bus upgrade` — not %q", sub)
|
|
||||||
}
|
|
||||||
// Everything refused before anything is done.
|
|
||||||
if err := why.require("bus upgrade"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if *reversible == *irreversible {
|
|
||||||
return errors.New("bus upgrade says, before it starts, whether the new version can be undone by putting the " +
|
|
||||||
"old one back: --reversible, or --irreversible as your explicit word that it runs anyway (to-be 45 §8). " +
|
|
||||||
"Nothing was done")
|
|
||||||
}
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
inv := open.inventory
|
|
||||||
b, err := pendingBus(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if b.module == "" {
|
|
||||||
return errors.New("the mesh holds no module that provides its bus: there is nothing to upgrade")
|
|
||||||
}
|
|
||||||
var moving []string
|
|
||||||
for _, n := range b.machines {
|
|
||||||
if b.moves(n) {
|
|
||||||
moving = append(moving, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(moving) == 0 {
|
|
||||||
fmt.Printf("every machine running %s runs the build the mesh holds (%s): nothing to upgrade\n", b.module, short(b.to))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
where := strings.TrimSpace(*snapshot)
|
|
||||||
if where == "" {
|
|
||||||
for _, n := range moving {
|
|
||||||
fmt.Printf("snapshotting the bus's streams on %s first (its backup holder, ADR 0235)…\n", n)
|
|
||||||
if where, err = takeBusSnapshot(ctx, b.module, n); err != nil {
|
|
||||||
return fmt.Errorf("the streams could not be snapshotted, so the bus is not replaced: %w — a snapshot "+
|
|
||||||
"taken by hand is said with --snapshot-taken <where>", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
from := map[string]bool{}
|
|
||||||
for _, n := range moving {
|
|
||||||
from[orNotKnown(b.from[n])] = true
|
|
||||||
}
|
|
||||||
step, err := inv.StartBusStep(ctx, inventory.BusStep{Module: b.module, Machines: moving,
|
|
||||||
From: strings.Join(sortedKeys(from), ", "), To: b.to, Snapshot: where, Reversible: *reversible,
|
|
||||||
By: link.Caller(), Why: strings.TrimSpace(*why.why)})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cause := "bus-upgrade"
|
|
||||||
if strings.TrimSpace(*why.cause) == "" {
|
|
||||||
why.cause = &cause
|
|
||||||
}
|
|
||||||
why.record(ctx, "bus upgrade", append([]string{b.module}, moving...))
|
|
||||||
fmt.Printf("bus upgrade %d: %s %s → %s on %s; streams snapshotted at %s; %s\n", step.ID, b.module, step.From,
|
|
||||||
short(b.to), strings.Join(moving, ", "), where, map[bool]string{true: "reversible: putting the old build back undoes it",
|
|
||||||
false: "NOT reversible: the snapshot is the only way back"}[*reversible])
|
|
||||||
sent, err := sendRollout(withBusStep(withScope(ctx, sendScope{person: true})), open, moving)
|
|
||||||
if err != nil {
|
|
||||||
_ = inv.EndBusStep(ctx, step.ID, "failed", "the send was refused: "+err.Error())
|
|
||||||
return fmt.Errorf("the bus's machine could not be sent its new build: %w — nothing was replaced", err)
|
|
||||||
}
|
|
||||||
fmt.Printf("sent %s; `bus-maintenance` is open until the bus answers healthy again — every stream, every durable "+
|
|
||||||
"consumer, a round trip to the machines (H-bus) — within %s, or the step is said failed with its snapshot "+
|
|
||||||
"as the way back. `bus` says how it went\n", strings.Join(sent, ", "), busStepBound)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// busStatus is `bus`: what an upgrade would do, and the last step.
|
|
||||||
func busStatus(ctx context.Context) error {
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
b, err := pendingBus(ctx, open.inventory)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if b.module == "" {
|
|
||||||
fmt.Println("the mesh holds no module that provides its bus")
|
|
||||||
} else {
|
|
||||||
fmt.Printf("the bus is %s, built %s, on %s; never rolled out — a planned step (`bus upgrade`)\n", b.module,
|
|
||||||
short(b.to), orNone(strings.Join(b.machines, ", ")))
|
|
||||||
for _, n := range b.machines {
|
|
||||||
state := "runs it"
|
|
||||||
if b.moves(n) {
|
|
||||||
state = "runs " + short(orNotKnown(b.from[n])) + ": `bus upgrade` replaces it"
|
|
||||||
}
|
|
||||||
fmt.Printf(" %-10s %s\n", n, state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println(" `bus upgrade` has the bus machine's backup holder snapshot the streams first (ADR 0235)")
|
|
||||||
s, found, err := open.inventory.LatestBusStep(ctx)
|
|
||||||
if err != nil || !found {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
state := "running since " + s.Started.Local().Format("2006-01-02 15:04")
|
|
||||||
if s.Ended != nil {
|
|
||||||
state = s.Outcome + " at " + s.Ended.Local().Format("2006-01-02 15:04")
|
|
||||||
}
|
|
||||||
fmt.Printf("last step %d: %s → %s on %s by %s (%s): %s; snapshot %s\n", s.ID, s.From, short(s.To),
|
|
||||||
strings.Join(s.Machines, ", "), orNone(s.By), s.Why, state, s.Snapshot)
|
|
||||||
if s.Found != "" {
|
|
||||||
fmt.Printf(" %s\n", s.Found)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// probeBusStep is DB: a bus upgrade running is said as `bus-maintenance`; the bus healthy again after
|
|
||||||
// the machines reported the new build ends it done; past its bound, unhealthy, it ends failed and is
|
|
||||||
// said — urgent, with its snapshot — while the bus is still not healthy.
|
|
||||||
func probeBusStep(ctx context.Context, d *doctor) ([]conditions.Observation, error) {
|
|
||||||
inv := d.open.inventory
|
|
||||||
s, found, err := inv.LatestBusStep(ctx)
|
|
||||||
if err != nil || !found {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if s.Ended != nil && s.Outcome != "failed" {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
problems, err := busHealth(ctx, d)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
reports, err := inv.LastReports(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
applied := true
|
|
||||||
for _, r := range reports {
|
|
||||||
for _, n := range s.Machines {
|
|
||||||
if r.Node == n && (!r.Current || r.Outcome != inventory.OutcomeApplied || r.At == nil || r.At.Before(s.Started)) {
|
|
||||||
applied = false
|
|
||||||
problems = append(problems, n+" has not reported the new bus applied")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
id := s.Module
|
|
||||||
if s.Ended == nil {
|
|
||||||
switch {
|
|
||||||
case applied && len(problems) == 0:
|
|
||||||
return nil, inv.EndBusStep(ctx, s.ID, "done", "the bus answered healthy after the upgrade")
|
|
||||||
case time.Since(s.Started) > busStepBound:
|
|
||||||
found := strings.Join(problems, "; ")
|
|
||||||
if err := inv.EndBusStep(ctx, s.ID, "failed", found); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
s.Found = found
|
|
||||||
default:
|
|
||||||
return []conditions.Observation{{Scope: conditions.ScopeBus, ID: id, Token: "maintenance",
|
|
||||||
Kind: kindBusMaintenance, Severity: conditions.Warning,
|
|
||||||
Summary: fmt.Sprintf("the bus is being upgraded (step %d, %s → %s on %s, by %s: %s); its snapshot is %s",
|
|
||||||
s.ID, s.From, short(s.To), strings.Join(s.Machines, ", "), orNone(s.By), s.Why, s.Snapshot),
|
|
||||||
Said: orNone(strings.Join(problems, "; "))}}, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(problems) == 0 {
|
|
||||||
return nil, nil // failed, and healthy since: nothing wrong now
|
|
||||||
}
|
|
||||||
way := "put the old build back"
|
|
||||||
if !s.Reversible {
|
|
||||||
way = "restore the snapshot"
|
|
||||||
}
|
|
||||||
return []conditions.Observation{{Scope: conditions.ScopeBus, ID: id, Token: "upgrade-failed",
|
|
||||||
Kind: kindBusUpgradeFailed, Severity: conditions.Urgent, Resolver: conditions.ResolverOperator,
|
|
||||||
Summary: fmt.Sprintf("the bus upgrade (step %d, %s → %s) did not end healthy within %s: %s — the way back is to %s (%s)",
|
|
||||||
s.ID, s.From, short(s.To), busStepBound, strings.Join(problems, "; "), way, s.Snapshot)}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedKeys(set map[string]bool) []string {
|
|
||||||
out := make([]string, 0, len(set))
|
|
||||||
for k := range set {
|
|
||||||
out = append(out, k)
|
|
||||||
}
|
|
||||||
sort.Strings(out)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// busSnapshotWithin is how long the bus machine's backup holder is given to take the bus's snapshot.
|
|
||||||
var busSnapshotWithin = 15 * time.Minute
|
|
||||||
|
|
||||||
// snapshotTheBusNow asks the bus machine's backup holder to back the bus module up now — its dump is
|
|
||||||
// the streams' snapshot (novox/hq ADR 0235) — and waits until it says a backup newer than the ask:
|
|
||||||
// where the snapshot is, as a person reads it. The serving controller's connection, or one of its own.
|
|
||||||
func snapshotTheBusNow(ctx context.Context, module, node string) (string, error) {
|
|
||||||
var where string
|
|
||||||
err := onTheBus(func(conn *nats.Conn) error {
|
|
||||||
asked := time.Now()
|
|
||||||
answer, err := link.AskSeatTool(ctx, conn, catalogue.BackupSeat, "now", node,
|
|
||||||
map[string]any{"module": module}, 30*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%s's backup holder was not asked to take the bus's snapshot: %w", node, err)
|
|
||||||
}
|
|
||||||
if answer.Error != "" {
|
|
||||||
return fmt.Errorf("%s's backup holder would not take the bus's snapshot: %s", node, answer.Error)
|
|
||||||
}
|
|
||||||
deadline := time.Now().Add(busSnapshotWithin)
|
|
||||||
for {
|
|
||||||
answer, err := link.AskSeatTool(ctx, conn, catalogue.BackupSeat, "backed-up", node, map[string]any{}, 10*time.Second)
|
|
||||||
if err == nil && answer.Error == "" {
|
|
||||||
if measured, err := readHolder(answer.Result); err == nil {
|
|
||||||
for item, m := range measured[module] {
|
|
||||||
if m.LastBackup != nil && m.LastBackup.After(asked) && m.Error == "" {
|
|
||||||
where = fmt.Sprintf("%s's restore point of %s (%s) taken %s", node, module, item,
|
|
||||||
m.LastBackup.UTC().Format(time.RFC3339))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if time.Now().After(deadline) {
|
|
||||||
return fmt.Errorf("%s's backup holder did not say the bus's snapshot was taken within %s; the bus is "+
|
|
||||||
"not replaced", node, busSnapshotWithin)
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
case <-time.After(10 * time.Second):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return where, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// errBusWaits is a send refused because it would replace the bus outside its planned step.
|
|
||||||
var errBusWaits = errors.New("a new bus build waits for its planned step")
|
|
||||||
|
|
||||||
type busStepKey struct{}
|
|
||||||
|
|
||||||
// withBusStep marks a send as the bus's planned step: the one send that may replace the bus.
|
|
||||||
func withBusStep(ctx context.Context) context.Context {
|
|
||||||
return context.WithValue(ctx, busStepKey{}, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func busStepSending(ctx context.Context) bool { on, _ := ctx.Value(busStepKey{}).(bool); return on }
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **Every user's slow consumer is S9's, not the controller's alone** (novox/hq ADR 0357, issue 270). On
|
|
||||||
// 2026-10-07 the bus closed two clients on the laptop as slow consumers and nothing raised a condition:
|
|
||||||
// the controller heard only its own connection. Said by the bus module and read back, a user the bus
|
|
||||||
// closes is one condition naming the module and its machine, raised at the first look and cleared an hour
|
|
||||||
// after the last closure.
|
|
||||||
func TestAUserTheBusClosedAsASlowConsumerIsOneConditionNamingItsModuleAndMachine(t *testing.T) {
|
|
||||||
closed := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
|
|
||||||
now := closed.Add(20 * time.Second)
|
|
||||||
store := conditions.NewInMemory()
|
|
||||||
k := conditions.NewKeeper(t.Context(), conditions.Options{Store: store, History: store,
|
|
||||||
Teller: &conditions.Told{}, Now: func() time.Time { return now }})
|
|
||||||
t.Cleanup(func() { k.Close(t.Context()) })
|
|
||||||
|
|
||||||
advisories := link.BusClosures([]link.BusClosure{
|
|
||||||
{User: "laptop.claude-code", CID: 10, Reason: "Slow Consumer (Write Deadline)", Opened: closed.Add(-time.Hour),
|
|
||||||
Closed: closed.Add(-10 * time.Minute)},
|
|
||||||
{User: "laptop.claude-code", CID: 44, Reason: "Slow Consumer (Write Deadline)", Opened: closed.Add(-time.Minute),
|
|
||||||
Closed: closed},
|
|
||||||
{User: "anchor.desk", CID: 7, Reason: "Maximum Message Payload Exceeded", Opened: closed.Add(-time.Hour),
|
|
||||||
Closed: closed},
|
|
||||||
}, []string{link.AdvisorySlowConsumer, link.AdvisorySlowConsumer, link.AdvisoryConnectionClosed}, closed.Add(-time.Hour))
|
|
||||||
look := func() {
|
|
||||||
t.Helper()
|
|
||||||
f := &signalFacts{now: now, host: "anchor", advisories: advisories}
|
|
||||||
if err := k.Reconcile(t.Context(), "S9", watchAdvisories(f)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
look()
|
|
||||||
|
|
||||||
slow, found, err := conditions.ReadOne(t.Context(), store, "bus.laptop.claude-code.slow-consumer")
|
|
||||||
if err != nil || !found {
|
|
||||||
t.Fatalf("no slow-consumer condition for the user: %v %v", found, err)
|
|
||||||
}
|
|
||||||
if slow.Subject.Machine != "laptop" || slow.Observations != 2 || !slow.LastObserved.Equal(closed) {
|
|
||||||
t.Fatalf("the slow consumer is said on %q, %d closure(s), last %s: %+v", slow.Subject.Machine, slow.Observations,
|
|
||||||
slow.LastObserved, slow)
|
|
||||||
}
|
|
||||||
if !strings.Contains(slow.Summary, "claude-code on laptop") || strings.Contains(slow.Summary, "laptop.claude-code") {
|
|
||||||
t.Fatalf("the summary does not name the module in words, or carries the dotted user: %q", slow.Summary)
|
|
||||||
}
|
|
||||||
if !strings.Contains(slow.Headline, "Claude-code on laptop") {
|
|
||||||
t.Fatalf("the headline is %q", slow.Headline)
|
|
||||||
}
|
|
||||||
cut, found, err := conditions.ReadOne(t.Context(), store, "bus.anchor.desk.connection-closed")
|
|
||||||
if err != nil || !found || cut.Subject.Machine != "anchor" || !strings.Contains(cut.Summary, "maximum message payload exceeded") {
|
|
||||||
t.Fatalf("the user cut off for breaking the bus's rules: %v %v %+v", found, err, cut)
|
|
||||||
}
|
|
||||||
|
|
||||||
// An hour after the newest closure without another, both clear.
|
|
||||||
now = closed.Add(time.Hour + time.Minute)
|
|
||||||
look()
|
|
||||||
for _, key := range []string{"bus.laptop.claude-code.slow-consumer", "bus.anchor.desk.connection-closed"} {
|
|
||||||
if _, found, _ := k.Get(t.Context(), key); found {
|
|
||||||
t.Fatalf("%s is still open an hour after the last closure", key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **A read of the bus module's events that fails does not blind S9** (review of #326): the controller's own
|
|
||||||
// signals are still said, and the failed read is a condition of its own.
|
|
||||||
func TestABusModuleReadThatFailsKeepsTheControllersOwnSignals(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
|
|
||||||
f := &signalFacts{now: at.Add(time.Minute), host: "anchor",
|
|
||||||
advisories: []link.Advisory{{Kind: link.AdvisorySlowConsumer, ID: "controller", Said: "the controller fell behind",
|
|
||||||
First: at, Last: at, Count: 1}},
|
|
||||||
busFaultsErr: errors.New("nats: timeout")}
|
|
||||||
keys := map[string]bool{}
|
|
||||||
for _, o := range watchAdvisories(f) {
|
|
||||||
keys[o.Key()] = true
|
|
||||||
}
|
|
||||||
if !keys["bus.controller.slow-consumer"] || !keys["bus.bus-module.bus-faults-unread"] {
|
|
||||||
t.Fatalf("observed %v: the controller's own slow consumer and the unread events, both", keys)
|
|
||||||
}
|
|
||||||
for _, row := range signalsTable {
|
|
||||||
if row.Row == "S9" && row.needs(f) != nil {
|
|
||||||
t.Fatal("the S9 row went blind for a read of the bus module's events")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,195 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A send held for the bus's planned step (novox/hq issue 336).
|
|
||||||
//
|
|
||||||
// **A wait only a person can end is said to that person at once.** No send replaces the bus but its planned
|
|
||||||
// step (sendToEach, ADR 0236), and the step is a person's `bus upgrade`. So while a new bus build waits, every
|
|
||||||
// walk whose tier sends to the bus's machine is refused, and tries again each tick. The refusal was kept only
|
|
||||||
// as the walk's note; nothing was raised, and the operator found the hold by asking why a walk did not move.
|
|
||||||
// Row S17 raises it on the first tick after the first refusal, in the bus's scope, for the operator: what
|
|
||||||
// waits, behind which bus build, since when, and the verb. A walk held only by it is not late, so S3 leaves it
|
|
||||||
// out, as it leaves out a walk under a paused build seat. It clears once the bus's machine has been sent the
|
|
||||||
// build the mesh holds — the step was taken, and no send is refused for the bus any more — whatever the walks'
|
|
||||||
// notes still say. Whether the new bus came up healthy is the step's own condition (probe DB).
|
|
||||||
|
|
||||||
// kindBusStepWaiting is S17's kind: bus.<module>.step-waiting.
|
|
||||||
const kindBusStepWaiting = "bus-step-waiting"
|
|
||||||
|
|
||||||
// busFacts is a new bus build waiting for its planned step, and the sends held for it.
|
|
||||||
type busFacts struct {
|
|
||||||
module, to string
|
|
||||||
// from is the build each machine of the bus runs; machines those whose bus the step would replace.
|
|
||||||
from map[string]string
|
|
||||||
machines []string
|
|
||||||
waits []busWaitFacts
|
|
||||||
}
|
|
||||||
|
|
||||||
// busWaitFacts is one walk whose send was refused because it would replace the bus.
|
|
||||||
type busWaitFacts struct {
|
|
||||||
plan, repository, commit string
|
|
||||||
// modules are what its tier sends: what waits.
|
|
||||||
modules []string
|
|
||||||
// since is the first refusal: as this controller saw it, or, read back, the save that kept the refusal.
|
|
||||||
since time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// busRefusedFirst is when this controller first saw each walk refused for the bus's step. The walk's note
|
|
||||||
// keeps the refusal across a restart; this keeps its moment more exactly than the walk's last save.
|
|
||||||
var busRefusedFirst = &firstSeen{at: map[string]time.Time{}}
|
|
||||||
|
|
||||||
type firstSeen struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
at map[string]time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// mark keeps the first moment an id was seen.
|
|
||||||
func (s *firstSeen) mark(id string, at time.Time) {
|
|
||||||
s.mu.Lock()
|
|
||||||
defer s.mu.Unlock()
|
|
||||||
if _, seen := s.at[id]; !seen {
|
|
||||||
s.at[id] = at
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// of is when an id was first seen; zero when it was not.
|
|
||||||
func (s *firstSeen) of(id string) time.Time {
|
|
||||||
s.mu.Lock()
|
|
||||||
defer s.mu.Unlock()
|
|
||||||
return s.at[id]
|
|
||||||
}
|
|
||||||
|
|
||||||
// keepOnly forgets every id not given: a walk no longer held is not held since then.
|
|
||||||
func (s *firstSeen) keepOnly(ids map[string]bool) {
|
|
||||||
s.mu.Lock()
|
|
||||||
defer s.mu.Unlock()
|
|
||||||
for id := range s.at {
|
|
||||||
if !ids[id] {
|
|
||||||
delete(s.at, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// refusedForTheBus is whether an error is the refusal of a send for the bus's planned step.
|
|
||||||
func refusedForTheBus(err error) bool {
|
|
||||||
return err != nil && strings.Contains(err.Error(), errBusWaits.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// busWaitsOf is the sends held for the bus's step, from the open walks: each walk not waiting for its
|
|
||||||
// delivery's word whose note keeps a refusal for this very bus build, while the build would still replace
|
|
||||||
// the bus on a machine. first is when this controller first saw a walk refused, zero when it did not.
|
|
||||||
func busWaitsOf(plans []inventory.Plan, b busPending, first func(string) time.Time) busFacts {
|
|
||||||
f := busFacts{module: b.module, to: b.to, from: b.from}
|
|
||||||
for _, n := range b.machines {
|
|
||||||
if b.moves(n) {
|
|
||||||
f.machines = append(f.machines, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(f.machines) == 0 {
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
for _, p := range plans {
|
|
||||||
if !p.Open() || p.Waiting() || !strings.Contains(p.Note, errBusWaits.Error()) || !strings.Contains(p.Note, short(b.to)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
since := p.Updated
|
|
||||||
if seen := first(p.ID); !seen.IsZero() && (since.IsZero() || seen.Before(since)) {
|
|
||||||
since = seen
|
|
||||||
}
|
|
||||||
var modules []string
|
|
||||||
if p.Tier >= 0 && p.Tier < len(p.Tiers) {
|
|
||||||
modules = append(modules, p.Tiers[p.Tier]...)
|
|
||||||
} else {
|
|
||||||
modules = planModules(p)
|
|
||||||
}
|
|
||||||
sort.Strings(modules)
|
|
||||||
f.waits = append(f.waits, busWaitFacts{plan: p.ID, repository: p.Repository, commit: p.Commit, modules: modules,
|
|
||||||
since: since})
|
|
||||||
}
|
|
||||||
sort.Slice(f.waits, func(i, j int) bool { return f.waits[i].since.Before(f.waits[j].since) })
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// heldByTheBus is the walks of a bus's facts, by id: what S3 leaves out.
|
|
||||||
func (b busFacts) heldByTheBus() map[string]bool {
|
|
||||||
out := map[string]bool{}
|
|
||||||
for _, w := range b.waits {
|
|
||||||
out[w.plan] = true
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// busUpgradeVerb is the call that ends the wait, as the summary names it: through the mesh MCP server, with
|
|
||||||
// why, saying whether the new version can be undone ("reversible": "true") or not ("irreversible": "true").
|
|
||||||
const busUpgradeVerb = "`mesh_call mesh-controller.bus {\"upgrade\": \"true\", \"why\": \"…\", \"reversible\" or \"irreversible\": \"true\"}`"
|
|
||||||
|
|
||||||
// watchBusWaits is S17: a send held for the bus's planned step, said at once to the operator.
|
|
||||||
func watchBusWaits(f *signalFacts) []conditions.Observation {
|
|
||||||
b := f.bus
|
|
||||||
if len(b.waits) == 0 || len(b.machines) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
since := b.waits[0].since
|
|
||||||
var walks, what []string
|
|
||||||
for _, w := range b.waits {
|
|
||||||
walks = append(walks, fmt.Sprintf("%s (%s %s, tier: %s)", w.plan, w.repository, short(w.commit),
|
|
||||||
strings.Join(w.modules, ", ")))
|
|
||||||
what = append(what, deliveryWhat(w.modules, w.repository))
|
|
||||||
}
|
|
||||||
var from []string
|
|
||||||
for _, n := range b.machines {
|
|
||||||
from = append(from, short(orNotKnown(b.from[n])))
|
|
||||||
}
|
|
||||||
machines := namesWords(b.machines, 3)
|
|
||||||
in := f.now.Sub(since)
|
|
||||||
return []conditions.Observation{{Scope: conditions.ScopeBus, ID: b.module, Token: "step-waiting",
|
|
||||||
Kind: kindBusStepWaiting, Severity: conditions.Warning, Resolver: conditions.ResolverOperator,
|
|
||||||
Machine: b.machines[0], Also: b.machines[1:],
|
|
||||||
Summary: fmt.Sprintf("sends to %s wait for the bus's planned step: a new bus build (%s %s → %s) would replace "+
|
|
||||||
"the bus there, which only a person's %s does; waiting since %s: %s", strings.Join(b.machines, ", "),
|
|
||||||
b.module, strings.Join(sortedUnique(from), ", "), short(b.to), busUpgradeVerb,
|
|
||||||
since.UTC().Format(time.RFC3339), strings.Join(walks, "; ")),
|
|
||||||
Said: fmt.Sprintf("%d walk(s) refused since %s", len(b.waits), since.UTC().Format(time.RFC3339)),
|
|
||||||
Headline: clipWords("Sends to "+machines+" wait for a bus upgrade", conditions.HeadlineMax),
|
|
||||||
Explanation: clipWords(fmt.Sprintf("A new version of the mesh's message system is built, and only a person "+
|
|
||||||
"installs it. Until then nothing else is sent to %s: %s waits, for %s so far.", machines,
|
|
||||||
namesWords(sortedUnique(what), 3), humanDuration(in)), conditions.ExplanationMax),
|
|
||||||
Needs: "start the bus upgrade " + FromMeshMCPServer,
|
|
||||||
Resolved: "Resolved: the bus upgrade started, and sends go on"}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sortedUnique is a list sorted, each once.
|
|
||||||
func sortedUnique(xs []string) []string {
|
|
||||||
seen := map[string]bool{}
|
|
||||||
var out []string
|
|
||||||
for _, x := range xs {
|
|
||||||
if !seen[x] {
|
|
||||||
seen[x] = true
|
|
||||||
out = append(out, x)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(out)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// clipWords keeps a plain sentence within a bound, at a word.
|
|
||||||
func clipWords(s string, n int) string {
|
|
||||||
if len(s) <= n {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
cut := strings.LastIndex(s[:n-1], " ")
|
|
||||||
if cut <= 0 {
|
|
||||||
cut = n - 1
|
|
||||||
}
|
|
||||||
return s[:cut] + "…"
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestASendHeldForTheBusStepIsFoundFromTheWalksItHolds(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 8, 18, 30, 0, 0, time.UTC)
|
|
||||||
b := aBusOnAnchor()
|
|
||||||
walk := func(id, repo, note string, updated time.Time) inventory.Plan {
|
|
||||||
return inventory.Plan{ID: id, Repository: repo, Commit: "c0ffee001122", State: inventory.PlanRolling, Tier: 0,
|
|
||||||
Tiers: [][]string{{"mesh-host"}}, Modules: map[string]*inventory.PlanModule{"mesh-host": {}}, Note: note,
|
|
||||||
Updated: updated}
|
|
||||||
}
|
|
||||||
for _, c := range []struct {
|
|
||||||
name string
|
|
||||||
bus busPending
|
|
||||||
plans []inventory.Plan
|
|
||||||
first map[string]time.Time
|
|
||||||
want []string
|
|
||||||
since time.Time
|
|
||||||
}{
|
|
||||||
{"a walk refused for the bus", b,
|
|
||||||
[]inventory.Plan{walk("plan-1", "novox/mesh-host", refusedNote(b, 0), now.Add(-27*time.Minute))}, nil,
|
|
||||||
[]string{"plan-1"}, now.Add(-27 * time.Minute)},
|
|
||||||
{"refused earlier than its last save, as this controller saw it", b,
|
|
||||||
[]inventory.Plan{walk("plan-1", "novox/mesh-host", refusedNote(b, 0), now.Add(-5*time.Minute))},
|
|
||||||
map[string]time.Time{"plan-1": now.Add(-28 * time.Minute)}, []string{"plan-1"}, now.Add(-28 * time.Minute)},
|
|
||||||
{"a walk refused for another reason", b,
|
|
||||||
[]inventory.Plan{walk("plan-1", "novox/mesh-host", "tier 0: the build seat is paused — tried again", now)}, nil,
|
|
||||||
nil, time.Time{}},
|
|
||||||
{"refused for an older bus build than the one held now", func() busPending { o := b; o.to = "99999999cccc"; return o }(),
|
|
||||||
[]inventory.Plan{walk("plan-1", "novox/mesh-host", refusedNote(b, 0), now)}, nil, nil, time.Time{}},
|
|
||||||
{"the bus already runs the build held: its step started", func() busPending {
|
|
||||||
o := aBusOnAnchor()
|
|
||||||
o.from = map[string]string{"anchor": o.to}
|
|
||||||
return o
|
|
||||||
}(), []inventory.Plan{walk("plan-1", "novox/mesh-host", refusedNote(b, 0), now)}, nil, nil, time.Time{}},
|
|
||||||
{"a walk waiting for its delivery's word asks no send", b, func() []inventory.Plan {
|
|
||||||
p := walk("plan-1", "novox/mesh-host", refusedNote(b, 0), now)
|
|
||||||
p.Delivery = &inventory.PlanDelivery{Awaits: "mesh-delivery"}
|
|
||||||
return []inventory.Plan{p}
|
|
||||||
}(), nil, nil, time.Time{}},
|
|
||||||
} {
|
|
||||||
t.Run(c.name, func(t *testing.T) {
|
|
||||||
got := busWaitsOf(c.plans, c.bus, func(id string) time.Time { return c.first[id] })
|
|
||||||
var ids []string
|
|
||||||
for _, w := range got.waits {
|
|
||||||
ids = append(ids, w.plan)
|
|
||||||
}
|
|
||||||
if strings.Join(ids, ",") != strings.Join(c.want, ",") {
|
|
||||||
t.Fatalf("held for the bus: %v, want %v", ids, c.want)
|
|
||||||
}
|
|
||||||
if len(c.want) > 0 {
|
|
||||||
if !got.waits[0].since.Equal(c.since) {
|
|
||||||
t.Errorf("waiting since %s, want %s", got.waits[0].since, c.since)
|
|
||||||
}
|
|
||||||
if strings.Join(got.machines, ",") != "anchor" || got.to != b.to || got.module != "nats" {
|
|
||||||
t.Errorf("behind %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
)
|
|
||||||
|
|
||||||
// novox/hq issue 336: a send held because it would replace the bus outside its planned step waits for a
|
|
||||||
// person, so a person is told at once — what waits, behind which bus build, since when, and the verb that
|
|
||||||
// ends it — and the wait is not read as a walk running late.
|
|
||||||
|
|
||||||
// aBusOnAnchor is a new bus build waiting for its step on anchor: nats 88135ad0 there, 32307bd1 held.
|
|
||||||
func aBusOnAnchor() busPending {
|
|
||||||
return busPending{module: "nats", machines: []string{"anchor"}, from: map[string]string{"anchor": "88135ad0aaaa"},
|
|
||||||
to: "32307bd1bbbb", same: map[string]bool{}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// refusedNote is the note a walk keeps when its send was refused for the bus, as advanceHeld writes it.
|
|
||||||
func refusedNote(b busPending, tier int) string {
|
|
||||||
held := "sending anchor would replace the bus (nats " + short(b.from["anchor"]) + " → " + short(b.to) +
|
|
||||||
"), which is a planned step: `bus upgrade --why …` snapshots its streams first and checks them after (novox/hq ADR 0236)"
|
|
||||||
return "tier " + string(rune('0'+tier)) + ": " + errBusWaits.Error() + ": " + held + " — tried again"
|
|
||||||
}
|
|
||||||
|
|
||||||
// **Said at once, not as lateness, and cleared when the step starts**: the walk held only for the bus raises
|
|
||||||
// the bus's condition on the first tick, before any tier bound, naming what waits, the bus build from and to,
|
|
||||||
// since when and `bus upgrade`, for the operator; S3 says nothing of it even past its bound; and the
|
|
||||||
// condition clears once the bus's machine runs the new build.
|
|
||||||
func TestASendHeldForTheBusStepIsSaidAtOnceAndNotAsLateness(t *testing.T) {
|
|
||||||
now := time.Date(2026, 10, 8, 18, 30, 0, 0, time.UTC)
|
|
||||||
b := aBusOnAnchor()
|
|
||||||
held := func(entered time.Duration) *signalFacts {
|
|
||||||
f := calm(now)
|
|
||||||
f.plans = []planFacts{{id: "plan-1", repository: "novox/mesh-host", commit: "c0ffee00", tier: 0, tiers: 1,
|
|
||||||
entered: now.Add(-entered), bound: 30 * time.Minute, waiting: refusedNote(b, 0), bus: true}}
|
|
||||||
f.bus = busFacts{module: "nats", to: b.to, from: b.from, machines: []string{"anchor"},
|
|
||||||
waits: []busWaitFacts{{plan: "plan-1", repository: "novox/mesh-host", commit: "c0ffee001122",
|
|
||||||
modules: []string{"mesh-host"}, since: now.Add(-time.Minute)}}}
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
f := held(time.Minute)
|
|
||||||
got := watchBusWaits(f)
|
|
||||||
if len(got) != 1 {
|
|
||||||
t.Fatalf("a send held for the bus a minute ago raised %+v", got)
|
|
||||||
}
|
|
||||||
o := got[0]
|
|
||||||
if o.Key() != "bus.nats.step-waiting" || o.Kind != kindBusStepWaiting || o.Resolver != conditions.ResolverOperator {
|
|
||||||
t.Fatalf("raised %s (%s), resolver %q", o.Key(), o.Kind, o.Resolver)
|
|
||||||
}
|
|
||||||
for _, want := range []string{"anchor", "mesh-host", "88135ad0", "32307bd1", "mesh_call mesh-controller.bus",
|
|
||||||
`"upgrade": "true"`, `"reversible"`, `"irreversible"`, "2026-10-08T18:29:00Z", "plan-1"} {
|
|
||||||
if !strings.Contains(o.Summary, want) {
|
|
||||||
t.Errorf("its summary does not say %q: %s", want, o.Summary)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !strings.Contains(o.Explanation, "mesh-host") || !strings.Contains(o.Headline, "bus upgrade") || o.Needs == "" {
|
|
||||||
t.Errorf("its words do not say what waits and what the operator does: %+v", o)
|
|
||||||
}
|
|
||||||
if why, ok := conditions.PlainWords(conditions.Words{Headline: o.Headline, Explanation: o.Explanation,
|
|
||||||
Resolved: o.Resolved, Needs: o.Needs}, "anchor"); !ok {
|
|
||||||
t.Errorf("its words are not plain: %s", why)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Past S3's bound: still the bus's wait, never a stalled walk.
|
|
||||||
late := held(45 * time.Minute)
|
|
||||||
if s3 := watchPlans(late); len(s3) != 0 {
|
|
||||||
t.Fatalf("a walk held only by the bus step was said stalled: %+v", s3)
|
|
||||||
}
|
|
||||||
// A walk held for something else past its bound is still stalled.
|
|
||||||
other := held(45 * time.Minute)
|
|
||||||
other.plans[0].bus = false
|
|
||||||
if s3 := watchPlans(other); len(s3) != 1 {
|
|
||||||
t.Fatalf("a walk held for something else past its bound raised %+v", s3)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Through the keeper: open at the first tick, cleared when the step started.
|
|
||||||
store := conditions.NewInMemory()
|
|
||||||
k := conditions.NewKeeper(t.Context(), conditions.Options{Store: store, History: store, Teller: &conditions.Told{},
|
|
||||||
Now: func() time.Time { return now }})
|
|
||||||
defer k.Close(context.Background())
|
|
||||||
w := &watchdogs{keeper: k, started: now.Add(-time.Hour)}
|
|
||||||
w.see(t.Context(), held(time.Minute))
|
|
||||||
open, err := k.Open(t.Context())
|
|
||||||
if err != nil || len(open) != 1 || open[0].Key != "bus.nats.step-waiting" {
|
|
||||||
t.Fatalf("after the first tick, open: %+v (%v)", open, err)
|
|
||||||
}
|
|
||||||
started := held(time.Minute)
|
|
||||||
started.bus = busFacts{module: "nats", to: b.to}
|
|
||||||
started.plans[0].bus = false
|
|
||||||
w.see(t.Context(), started)
|
|
||||||
if open, _ := k.Open(t.Context()); len(open) != 0 {
|
|
||||||
t.Fatalf("the step started, and open: %+v", open)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,258 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/rsa"
|
|
||||||
"crypto/x509"
|
|
||||||
"crypto/x509/pkix"
|
|
||||||
"encoding/pem"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The bus's own certificate, made by the mesh rather than borrowed from an image.
|
|
||||||
//
|
|
||||||
// **The foundation asked a third-party image for a tool it never said must be there** (novox/hq
|
|
||||||
// 04-ISSUES/146). The bootstrap made this certificate by running `openssl` inside the broker's
|
|
||||||
// image, which worked while the broker was one that happened to carry it and stopped the day the
|
|
||||||
// bus changed: the new one has a shell and no openssl, so the step exited 127 and no mesh could be
|
|
||||||
// raised. Substituting another image the bundle names does not help — none of them carry it
|
|
||||||
// either.
|
|
||||||
//
|
|
||||||
// So the program that needs a certificate makes one. It is the mesh's own binary, already on the
|
|
||||||
// machine at this point in the bootstrap (the schema step ran it), and it needs nothing from the
|
|
||||||
// image it writes into but a mounted directory.
|
|
||||||
//
|
|
||||||
// **Self-signed, and that is the design** — a host pins this server's exact certificate and
|
|
||||||
// authenticates with a password (novox/hq ADR 0004). There is no authority above it to ask, and at
|
|
||||||
// this moment in a bootstrap there is no mesh to ask one of.
|
|
||||||
//
|
|
||||||
// Idempotent, because the step is applied again on every reconcile and a second certificate would
|
|
||||||
// be one the hosts that pinned the first no longer believe.
|
|
||||||
|
|
||||||
// busCertificateNames is what the bus is reached by: the container name on a mesh network, and the
|
|
||||||
// loopback address the machine's own foundation dials.
|
|
||||||
var busCertificateNames = []string{"mesh-broker"}
|
|
||||||
|
|
||||||
const busCertificateLife = 10 * 365 * 24 * time.Hour
|
|
||||||
|
|
||||||
// busCertificate makes the bus's certificate in a directory, or says whether one is there.
|
|
||||||
//
|
|
||||||
// broker certificate --into /tls make it if it is not there
|
|
||||||
// broker certificate --check --into /tls exit non-zero unless a usable pair is
|
|
||||||
func busCertificate(args []string) error {
|
|
||||||
into, check := "", false
|
|
||||||
for i := 0; i < len(args); i++ {
|
|
||||||
switch args[i] {
|
|
||||||
case "--check":
|
|
||||||
check = true
|
|
||||||
case "--into":
|
|
||||||
if i+1 >= len(args) {
|
|
||||||
return errors.New("--into needs a directory")
|
|
||||||
}
|
|
||||||
into = args[i+1]
|
|
||||||
i++
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("broker certificate [--check] --into <directory>: %q", args[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if into == "" {
|
|
||||||
return errors.New("broker certificate [--check] --into <directory>")
|
|
||||||
}
|
|
||||||
crt, key := filepath.Join(into, "tls.crt"), filepath.Join(into, "tls.key")
|
|
||||||
|
|
||||||
if usable, err := busCertificateUsable(crt, key); err != nil {
|
|
||||||
return err
|
|
||||||
} else if usable {
|
|
||||||
fmt.Printf("the bus already has a certificate at %s, and it was left alone\n", crt)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if check {
|
|
||||||
// Said as a failure, because that is what the caller asked: a bootstrap's verify runs
|
|
||||||
// this and a false answer is what makes the step run.
|
|
||||||
return fmt.Errorf("no usable certificate and key at %s", into)
|
|
||||||
}
|
|
||||||
return writeBusCertificate(crt, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// busCertificateUsable says whether a certificate and its key are both there and parse.
|
|
||||||
//
|
|
||||||
// Both, and parsed rather than stat'ed: a half-written pair is the state a bootstrap interrupted
|
|
||||||
// between the two files leaves behind, and a step that treated it as done would hand the server a
|
|
||||||
// certificate with no key and report success.
|
|
||||||
func busCertificateUsable(crt, key string) (bool, error) {
|
|
||||||
certPEM, err := os.ReadFile(crt)
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
keyPEM, err := os.ReadFile(key)
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if _, err := tlsPairParses(certPEM, keyPEM); err != nil {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func tlsPairParses(certPEM, keyPEM []byte) (*x509.Certificate, error) {
|
|
||||||
block, _ := pem.Decode(certPEM)
|
|
||||||
if block == nil || block.Type != "CERTIFICATE" {
|
|
||||||
return nil, errors.New("not a certificate")
|
|
||||||
}
|
|
||||||
certificate, err := x509.ParseCertificate(block.Bytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
keyBlock, _ := pem.Decode(keyPEM)
|
|
||||||
if keyBlock == nil {
|
|
||||||
return nil, errors.New("not a key")
|
|
||||||
}
|
|
||||||
if _, err := x509.ParsePKCS8PrivateKey(keyBlock.Bytes); err != nil {
|
|
||||||
if _, err := x509.ParsePKCS1PrivateKey(keyBlock.Bytes); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return certificate, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeBusCertificate(crt, key string) error {
|
|
||||||
private, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
template := &x509.Certificate{
|
|
||||||
SerialNumber: serial,
|
|
||||||
Subject: pkix.Name{CommonName: busCertificateNames[0]},
|
|
||||||
DNSNames: busCertificateNames,
|
|
||||||
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
|
|
||||||
NotBefore: time.Now().Add(-time.Hour),
|
|
||||||
NotAfter: time.Now().Add(busCertificateLife),
|
|
||||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
|
||||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
||||||
BasicConstraintsValid: true,
|
|
||||||
}
|
|
||||||
der, err := x509.CreateCertificate(rand.Reader, template, template, &private.PublicKey, private)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pkcs8, err := x509.MarshalPKCS8PrivateKey(private)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// **The key first, and only then the certificate**, so the pair a reader finds is never a
|
|
||||||
// certificate whose key has not been written yet — the one order in which an interruption
|
|
||||||
// leaves something that looks finished (novox/hq 04-ISSUES/014, a key present and unusable).
|
|
||||||
if err := os.WriteFile(key, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8}), 0o600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(crt, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o644); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("made the bus a certificate for %v, valid until %s\n %s\n %s\n",
|
|
||||||
busCertificateNames, template.NotAfter.Format(time.RFC3339), crt, key)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// busAccounts writes the mesh's composed user list to a file.
|
|
||||||
//
|
|
||||||
// **For genesis, where no declaration can deliver it** (novox/hq 04-ISSUES/146). Everywhere else
|
|
||||||
// the list reaches the machine running the bus as a resource of the module that holds it — which
|
|
||||||
// requires that machine to be an enrolled node, and at genesis it is not: the first node cannot
|
|
||||||
// enrol because the account it would enrol with cannot be composed onto a bus it has no declaration
|
|
||||||
// for. The installer breaks that circle by placing the file itself, once, and the module takes the
|
|
||||||
// file over from its first push.
|
|
||||||
//
|
|
||||||
// The same composition, not a second one: this asks the store for the same records and renders them
|
|
||||||
// with the same composer the declaration uses. A genesis that hand-wrote an account would be a
|
|
||||||
// second statement of who may say what, able to disagree with the first.
|
|
||||||
//
|
|
||||||
// **It writes to standard output unless told a file**, and that is the point: the control plane
|
|
||||||
// composes and says what it composed, and whoever is raising the machine puts it where that
|
|
||||||
// machine's bus reads it. A control plane that wrote into the bus's own directory would have to
|
|
||||||
// know where that is and how to make the server re-read it — which is the module's knowledge, and
|
|
||||||
// the module is what takes this over on the first push.
|
|
||||||
//
|
|
||||||
// broker accounts > /var/lib/mesh-bus-conf/accounts.conf
|
|
||||||
func busAccounts(ctx context.Context, args []string) error {
|
|
||||||
into := ""
|
|
||||||
for i := 0; i < len(args); i++ {
|
|
||||||
switch args[i] {
|
|
||||||
case "--into":
|
|
||||||
if i+1 >= len(args) {
|
|
||||||
return errors.New("--into needs a file")
|
|
||||||
}
|
|
||||||
into = args[i+1]
|
|
||||||
i++
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("broker accounts --into <file>: %q", args[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
|
|
||||||
records, err := open.inventory.BusRecords(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
users, err := broker.Users(records)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
kept, err := open.inventory.BusUsers(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
hashes := make(map[string]string, len(kept))
|
|
||||||
for name, u := range kept {
|
|
||||||
hashes[name] = u.PasswordHash
|
|
||||||
}
|
|
||||||
filled, missing := broker.WithPasswords(users, hashes)
|
|
||||||
if len(missing) > 0 {
|
|
||||||
// To standard error, always: the composed file may be going to standard output, and a
|
|
||||||
// remark in the middle of it is a configuration the server refuses to parse.
|
|
||||||
fmt.Fprintf(os.Stderr, "leaving out %d user(s) the mesh has minted no credential for: %s\n",
|
|
||||||
len(missing), strings.Join(missing, ", "))
|
|
||||||
}
|
|
||||||
if len(filled) == 0 {
|
|
||||||
return errors.New("not one user has a credential, so this list would refuse every " +
|
|
||||||
"connection in the mesh")
|
|
||||||
}
|
|
||||||
accounts, err := broker.ComposeAccounts(filled)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if into == "" {
|
|
||||||
fmt.Print(accounts)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(into, []byte(accounts), 0o600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("wrote %d user(s) to %s\n", len(filled), into)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// novox/hq 04-ISSUES/146. The bootstrap could not make the bus a certificate: it asked an image for
|
|
||||||
// `openssl` and the image it asks has none. What replaces it is this command, so what is checked is
|
|
||||||
// what the bootstrap needs from it — a pair a TLS server can actually load, made once and only once.
|
|
||||||
|
|
||||||
func TestTheBusCertificateLoadsAsAServersWould(t *testing.T) {
|
|
||||||
into := t.TempDir()
|
|
||||||
if err := busCertificate([]string{"--into", into}); err != nil {
|
|
||||||
t.Fatalf("the bus could not be given a certificate: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The check a cheaper test would not make. The key was present and valid and the server could
|
|
||||||
// not start, once, because nothing loaded the pair the way a server loads it
|
|
||||||
// (novox/hq 04-ISSUES/014).
|
|
||||||
pair, err := tls.LoadX509KeyPair(filepath.Join(into, "tls.crt"), filepath.Join(into, "tls.key"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("a TLS server cannot load what was written: %v", err)
|
|
||||||
}
|
|
||||||
leaf := pair.Leaf
|
|
||||||
if leaf == nil {
|
|
||||||
if leaf, err = x509.ParseCertificate(pair.Certificate[0]); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := leaf.VerifyHostname("mesh-broker"); err != nil {
|
|
||||||
t.Errorf("the certificate is not for the name the bus is reached by: %v", err)
|
|
||||||
}
|
|
||||||
if len(leaf.IPAddresses) == 0 || leaf.IPAddresses[0].String() != "127.0.0.1" {
|
|
||||||
t.Errorf("the certificate does not cover the loopback address the foundation dials: %v", leaf.IPAddresses)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The key is not readable by anything else on the machine; the certificate is public and is.
|
|
||||||
key, err := os.Stat(filepath.Join(into, "tls.key"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if key.Mode().Perm() != 0o600 {
|
|
||||||
t.Errorf("the key is %v", key.Mode().Perm())
|
|
||||||
}
|
|
||||||
crt, err := os.Stat(filepath.Join(into, "tls.crt"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if crt.Mode().Perm() != 0o644 {
|
|
||||||
t.Errorf("the certificate is %v, which the server runs as another user cannot read", crt.Mode().Perm())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **Made once.** The step is applied again on every reconcile, and a second certificate is one the
|
|
||||||
// hosts that pinned the first no longer believe (novox/hq ADR 0004).
|
|
||||||
func TestTheBusCertificateIsMadeOnce(t *testing.T) {
|
|
||||||
into := t.TempDir()
|
|
||||||
if err := busCertificate([]string{"--into", into}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
first, err := os.ReadFile(filepath.Join(into, "tls.crt"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := busCertificate([]string{"--into", into}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
again, err := os.ReadFile(filepath.Join(into, "tls.crt"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(first) != string(again) {
|
|
||||||
t.Fatal("running it twice replaced the certificate every host had pinned")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The verify half: false before, true after, which is what makes the bootstrap run the step at all.
|
|
||||||
func TestTheCheckIsFalseUntilThereIsAPair(t *testing.T) {
|
|
||||||
into := t.TempDir()
|
|
||||||
if err := busCertificate([]string{"--check", "--into", into}); err == nil {
|
|
||||||
t.Fatal("an empty directory reported a usable certificate")
|
|
||||||
}
|
|
||||||
if err := busCertificate([]string{"--into", into}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := busCertificate([]string{"--check", "--into", into}); err != nil {
|
|
||||||
t.Fatalf("the certificate it just made does not satisfy its own check: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A half-written pair is not a pair. An interrupted bootstrap leaves exactly this, and a step that
|
|
||||||
// called it done would hand the server a certificate with no key and report success.
|
|
||||||
func TestACertificateWithoutItsKeyIsNotUsable(t *testing.T) {
|
|
||||||
into := t.TempDir()
|
|
||||||
if err := busCertificate([]string{"--into", into}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.Remove(filepath.Join(into, "tls.key")); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := busCertificate([]string{"--check", "--into", into}); err == nil {
|
|
||||||
t.Fatal("a certificate with no key passed the check")
|
|
||||||
}
|
|
||||||
if err := busCertificate([]string{"--into", into}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := tls.LoadX509KeyPair(filepath.Join(into, "tls.crt"), filepath.Join(into, "tls.key")); err != nil {
|
|
||||||
t.Fatalf("it did not replace the unusable pair: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWhereToWriteIsRequired(t *testing.T) {
|
|
||||||
if err := busCertificate(nil); err == nil || !strings.Contains(err.Error(), "--into") {
|
|
||||||
t.Fatalf("it did not ask where to write: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,254 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The streams and durable consumers the mesh's own traffic needs, derived once (novox/hq to-be 45 §4,
|
|
||||||
// D6 and D7).
|
|
||||||
//
|
|
||||||
// **What the controller asserts at its start and what the self-check expects to find are one
|
|
||||||
// derivation**, run against the bus to make them and against a recorder to list them. Two lists would
|
|
||||||
// drift, and a self-check comparing the bus with a second opinion of what should be there would find
|
|
||||||
// the drift rather than the fault.
|
|
||||||
|
|
||||||
// assertBusObjects brings every stream and consumer into being on r, and answers the machines that
|
|
||||||
// can now hear a declaration.
|
|
||||||
func assertBusObjects(ctx context.Context, inv *inventory.Inventory, r broker.Raiser) ([]string, error) {
|
|
||||||
nodes, err := inv.Nodes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
names := make([]string, 0, len(nodes))
|
|
||||||
for _, n := range nodes {
|
|
||||||
names = append(names, n.Name)
|
|
||||||
}
|
|
||||||
if err := broker.Raise(r, names); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// And each node's bucket for the files of its declaration that travel by digest (novox/hq ADR 0486 §4), so a node finds its own
|
|
||||||
// whenever the bus is up.
|
|
||||||
if files, ok := r.(interface{ EnsureDeclarationFiles(nodes []string) error }); ok {
|
|
||||||
if err := files.EnsureDeclarationFiles(names); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// The work queues of the mesh's own roles (novox/hq ADR 0121). The queue before the holder,
|
|
||||||
// deliberately: work queues until somebody arrives to do it, so assigning a build machine a week
|
|
||||||
// after something started asking for builds flushes the backlog instead of having lost it.
|
|
||||||
// With the seats' holders, so each role's work queue gets the consumer its holder takes
|
|
||||||
// work from. Passed as nil until the first live raise, which left the build machine bound to a
|
|
||||||
// consumer nothing had created (2026-09-28).
|
|
||||||
holders, err := seatHolders(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := broker.RaiseSeats(r, inventory.MeshSeats(), holders); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// And how every module hears what it consumes. Derived from the same records the user list is
|
|
||||||
// composed from, so a module the mesh grants a consumer's subjects has that consumer waiting.
|
|
||||||
// Done on every raise, not only when a credential is issued: every module moved onto this bus
|
|
||||||
// by the rollout was issued on the old one, and came up with nothing to bind to (2026-09-28).
|
|
||||||
consumers, records, err := moduleConsumersAndRecords(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// And a grant of a restricted stream that composes nothing, said on every raise and send (novox/hq ADR 0376 §2).
|
|
||||||
sayWithheld(records, " ")
|
|
||||||
// And the work queues of seats that name their caller or their kind, with each holder's worker
|
|
||||||
// (novox/hq ADR 0259 §3): an ask queues until the router takes it, a channel's work until that kind
|
|
||||||
// takes it.
|
|
||||||
trafficStreams, trafficWorkers, err := seatTrafficObjects(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// And every module's own stream (novox/hq ADR 0363), from the catalogue, before the consumers on it.
|
|
||||||
modStreams, err := inv.DeclaredStreams(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// Every one tried, and every failure named: one module's consumer the bus refuses is no reason
|
|
||||||
// the modules after it in the list hear nothing (novox/hq issue 208, where this runs on each send).
|
|
||||||
var failed []error
|
|
||||||
for _, s := range broker.ModuleStreams(modStreams) {
|
|
||||||
if err := r.EnsureStream(s); err != nil {
|
|
||||||
failed = append(failed, fmt.Errorf("the module stream %s: %w", s.Name, err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, s := range trafficStreams {
|
|
||||||
if err := r.EnsureStream(s); err != nil {
|
|
||||||
failed = append(failed, fmt.Errorf("the work queue %s: %w", s.Name, err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, c := range trafficWorkers {
|
|
||||||
if err := r.EnsureConsumer(c); err != nil {
|
|
||||||
failed = append(failed, fmt.Errorf("the worker %s on %s: %w", c.Name, c.Stream, err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
restricted := map[string]bool{}
|
|
||||||
for _, s := range modStreams {
|
|
||||||
restricted[s.Name] = s.Restricted
|
|
||||||
}
|
|
||||||
for _, c := range consumers {
|
|
||||||
if err := broker.EnsureHeard(r, c.Consumer, restricted[c.Consumer.Stream]); err != nil {
|
|
||||||
failed = append(failed, fmt.Errorf("how %s on %s hears what it consumes: %w", c.Module, c.Node, err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// **And a consumer no longer granted loses what it reads through** (novox/hq ADR 0363 §6): every consumer
|
|
||||||
// on a module's stream the composition no longer asks for is removed, on this very send.
|
|
||||||
if p, prunes := r.(broker.Pruner); prunes && len(modStreams) > 0 {
|
|
||||||
var wanted []broker.Consumer
|
|
||||||
for _, c := range consumers {
|
|
||||||
wanted = append(wanted, c.Consumer)
|
|
||||||
}
|
|
||||||
removed, err := broker.PruneHeard(p, modStreams, wanted)
|
|
||||||
if err != nil {
|
|
||||||
failed = append(failed, err)
|
|
||||||
}
|
|
||||||
for _, gone := range removed {
|
|
||||||
fmt.Printf(" removed the consumer %s: its module is no longer granted that stream (novox/hq ADR 0363)\n", gone)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(failed) > 0 {
|
|
||||||
return nil, errors.Join(failed...)
|
|
||||||
}
|
|
||||||
return names, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// What raises the condition a send says when the objects it implies could not be asserted, and its kind.
|
|
||||||
const (
|
|
||||||
sourceBusObjects = "bus-objects"
|
|
||||||
kindBusObjectsUnasserted = "bus-objects-unasserted"
|
|
||||||
)
|
|
||||||
|
|
||||||
// assertOnSend asserts, on the bus a send is about to use, every object assertBusObjects derives —
|
|
||||||
// **whenever a declaration is sent, not only when the controller starts** (novox/hq issue 208).
|
|
||||||
//
|
|
||||||
// A module assigned after the controller started was sent its declaration and found no consumer to
|
|
||||||
// bind (`consumer not found`, messenger on 2026-10-06), and a seat holder assigned after it found no
|
|
||||||
// worker: the objects a declaration implies were asserted at start and nowhere else, so they existed
|
|
||||||
// only for what was assigned before the last restart. The same derivation, not a second list of what
|
|
||||||
// a send needs: what start asserts, the self-check expects and a send asserts are one answer. Every
|
|
||||||
// part is idempotent, so asserting the whole of it again is the no-op a restart already relies on.
|
|
||||||
//
|
|
||||||
// **A failure is said and raised, and the send goes on.** The objects are the mesh's, not the
|
|
||||||
// machines' being sent: holding every machine back for one consumer that none of them may use would
|
|
||||||
// turn one fault into all of them, and the declarations are not what is wrong. It is never silent —
|
|
||||||
// said in the send's own output and raised as a condition, which the next send that asserts them
|
|
||||||
// clears — and the start-time raise still refuses to serve without them.
|
|
||||||
func assertOnSend(ctx context.Context, inv *inventory.Inventory, r broker.Raiser, indent string) error {
|
|
||||||
_, err := assertBusObjects(ctx, inv, r)
|
|
||||||
var observed []conditions.Observation
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%sTHE BUS DOES NOT HOLD WHAT THIS SEND IMPLIES: %v\n", indent, err)
|
|
||||||
fmt.Printf("%s a module may find no consumer to bind, or a holder no worker; sent anyway, raised as "+
|
|
||||||
"condition %s, and asserted again by the next send\n", indent, unassertedObservation(err).Key())
|
|
||||||
observed = append(observed, unassertedObservation(err))
|
|
||||||
}
|
|
||||||
// Observed when it failed, cleared when it did not: a send that asserted everything is the
|
|
||||||
// observation that the bus holds what it should.
|
|
||||||
if kerr := withKeeper(ctx, func(k *conditions.Keeper) error {
|
|
||||||
return k.Reconcile(ctx, sourceBusObjects, observed)
|
|
||||||
}); kerr != nil {
|
|
||||||
fmt.Printf("%sand whether the bus holds what this send implies could not be kept as a condition: %v\n",
|
|
||||||
indent, kerr)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// unassertedObservation is a send's failure to assert the bus's objects, as a condition.
|
|
||||||
func unassertedObservation(err error) conditions.Observation {
|
|
||||||
return conditions.Observation{Scope: conditions.ScopeBus, ID: "objects", Token: "unasserted",
|
|
||||||
Kind: kindBusObjectsUnasserted, Severity: conditions.Warning, Source: sourceBusObjects,
|
|
||||||
Summary: "the bus's streams and consumers could not be asserted when a declaration was sent: " +
|
|
||||||
"a module may find no consumer to bind, or a seat's holder no worker",
|
|
||||||
Said: err.Error()}
|
|
||||||
}
|
|
||||||
|
|
||||||
// moduleConsumers is every module's durable consumer, from the records the user list is composed from.
|
|
||||||
func moduleConsumers(ctx context.Context, inv *inventory.Inventory) ([]broker.ModuleConsumer, error) {
|
|
||||||
consumers, _, err := moduleConsumersAndRecords(ctx, inv)
|
|
||||||
return consumers, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// moduleConsumersAndRecords is moduleConsumers with the records they were composed from.
|
|
||||||
func moduleConsumersAndRecords(ctx context.Context, inv *inventory.Inventory) ([]broker.ModuleConsumer, broker.Records, error) {
|
|
||||||
records, err := inv.BusRecords(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, broker.Records{}, err
|
|
||||||
}
|
|
||||||
users, err := broker.Users(records)
|
|
||||||
if err != nil {
|
|
||||||
return nil, broker.Records{}, err
|
|
||||||
}
|
|
||||||
return broker.ConsumersOf(users), records, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// seatTrafficObjects is the work queues and workers of seats that name their caller or their kind, from
|
|
||||||
// the records the user list is composed from.
|
|
||||||
func seatTrafficObjects(ctx context.Context, inv *inventory.Inventory) ([]broker.Stream, []broker.Consumer, error) {
|
|
||||||
records, err := inv.BusRecords(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
users, err := broker.Users(records)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
streams, workers := broker.SeatTrafficObjects(users)
|
|
||||||
// And the queue of every such seat the catalogue declares, held or not: work queues from registration,
|
|
||||||
// so what is submitted before a holder is assigned waits for it (the correctness review of 2026-10-08).
|
|
||||||
declared, err := inv.DeclaredTrafficSeats(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
have := map[string]bool{}
|
|
||||||
for _, s := range streams {
|
|
||||||
have[s.Name] = true
|
|
||||||
}
|
|
||||||
for _, s := range broker.TrafficQueues(declared) {
|
|
||||||
if !have[s.Name] {
|
|
||||||
streams = append(streams, s)
|
|
||||||
have[s.Name] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return streams, workers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// moduleConsumerCount is how many modules hear what they consume, for the raise's one line.
|
|
||||||
func moduleConsumerCount(ctx context.Context, inv *inventory.Inventory) (int, error) {
|
|
||||||
consumers, err := moduleConsumers(ctx, inv)
|
|
||||||
return len(consumers), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// expectedBusObjects is every stream and consumer assertBusObjects would make, made nowhere.
|
|
||||||
func expectedBusObjects(ctx context.Context, inv *inventory.Inventory) ([]broker.Stream, []broker.Consumer, error) {
|
|
||||||
var rec recordingRaiser
|
|
||||||
if _, err := assertBusObjects(ctx, inv, &rec); err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("what the bus should hold cannot be worked out: %w", err)
|
|
||||||
}
|
|
||||||
return rec.streams, rec.consumers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// recordingRaiser keeps what it was asked to assert and asserts nothing.
|
|
||||||
type recordingRaiser struct {
|
|
||||||
streams []broker.Stream
|
|
||||||
consumers []broker.Consumer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *recordingRaiser) EnsureStream(s broker.Stream) error {
|
|
||||||
r.streams = append(r.streams, s)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *recordingRaiser) EnsureConsumer(c broker.Consumer) error {
|
|
||||||
r.consumers = append(r.consumers, c)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/testbus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// novox/hq issue 208: the bus's objects a declaration implies are asserted whenever one is sent, not
|
|
||||||
// only when the controller starts.
|
|
||||||
|
|
||||||
// aCarriedConsumer is the runtime on laptop and, carried by it, a module that consumes an event: the
|
|
||||||
// shape of messenger on 2026-10-06, assigned after the controller started.
|
|
||||||
func aCarriedConsumer(t *testing.T, open *stores) broker.Consumer {
|
|
||||||
t.Helper()
|
|
||||||
register(t, open, catalogue.Manifest{Module: catalogue.RuntimeModule, Version: "1",
|
|
||||||
OwnSecrets: catalogue.OwnSecrets{"broker": {Path: "/var/lib/mesh/node-tools/broker"}}})
|
|
||||||
register(t, open, catalogue.Manifest{Module: "messenger", Version: "1",
|
|
||||||
Consumes: []string{"billing.order.placed"}})
|
|
||||||
for _, m := range []string{catalogue.RuntimeModule, "messenger"} {
|
|
||||||
if _, err := open.inventory.Assign(t.Context(), "laptop", m); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
consumers, err := moduleConsumers(t.Context(), open.inventory)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, c := range consumers {
|
|
||||||
if c.Module == "messenger" && c.Node == "laptop" {
|
|
||||||
return c.Consumer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Fatalf("messenger on laptop is derived no consumer: %+v", consumers)
|
|
||||||
return broker.Consumer{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// aLateHolder is a module holding the build agent's seat on anchor, assigned after the controller
|
|
||||||
// started, and the worker its seat's queue should have for it.
|
|
||||||
func aLateHolder(t *testing.T, open *stores) broker.Consumer {
|
|
||||||
t.Helper()
|
|
||||||
register(t, open, catalogue.Manifest{Module: "late-builder", Version: "1",
|
|
||||||
Claims: []catalogue.Claim{{Name: "node-build-agent", Scope: catalogue.ScopeNode}}})
|
|
||||||
if _, err := open.inventory.Assign(t.Context(), "anchor", "late-builder"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, s := range inventory.MeshSeats() {
|
|
||||||
if s.Name == "node-build-agent" {
|
|
||||||
c, needed := broker.HolderConsumerFor("anchor", "late-builder", s)
|
|
||||||
if !needed {
|
|
||||||
t.Fatal("the build agent's seat needs no worker")
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Fatal("the mesh declares no build agent's seat")
|
|
||||||
return broker.Consumer{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// asserted says whether a recording holds a consumer by stream and name.
|
|
||||||
func asserted(rec *recordingRaiser, want broker.Consumer) bool {
|
|
||||||
for _, c := range rec.consumers {
|
|
||||||
if c.Stream == want.Stream && c.Name == want.Name {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// What a send asserts includes what was assigned after the start's assertion: a carried module's
|
|
||||||
// consumer and a late holder's worker. The derivation is the start's own, so this holds without a bus.
|
|
||||||
func TestASendAssertsWhatWasAssignedAfterStart(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
var atStart recordingRaiser
|
|
||||||
if _, err := assertBusObjects(t.Context(), open.inventory, &atStart); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
consumer := aCarriedConsumer(t, open)
|
|
||||||
worker := aLateHolder(t, open)
|
|
||||||
if asserted(&atStart, consumer) || asserted(&atStart, worker) {
|
|
||||||
t.Fatal("the start asserted what was not yet assigned; the test proves nothing")
|
|
||||||
}
|
|
||||||
var onSend recordingRaiser
|
|
||||||
if err := assertOnSend(t.Context(), open.inventory, &onSend, ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !asserted(&onSend, consumer) {
|
|
||||||
t.Fatalf("messenger's consumer %s on %s is not asserted by the send: %+v", consumer.Name, consumer.Stream, onSend.consumers)
|
|
||||||
}
|
|
||||||
if !asserted(&onSend, worker) {
|
|
||||||
t.Fatalf("the late holder's worker %s on %s is not asserted by the send: %+v", worker.Name, worker.Stream, onSend.consumers)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// failingRaiser refuses one consumer by name and records everything it was asked.
|
|
||||||
type failingRaiser struct {
|
|
||||||
recordingRaiser
|
|
||||||
refuse string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *failingRaiser) EnsureConsumer(c broker.Consumer) error {
|
|
||||||
f.consumers = append(f.consumers, c)
|
|
||||||
if c.Name == f.refuse {
|
|
||||||
return errors.New("nats: API error: code=503 description=insufficient resources")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// A send whose objects cannot be asserted says so in its output and raises a condition — and the next
|
|
||||||
// send that asserts them clears it. The consumers after the refused one are still asked for.
|
|
||||||
func TestASendThatCannotAssertTheBusSaysSoAndRaisesACondition(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
consumer := aCarriedConsumer(t, open)
|
|
||||||
worker := aLateHolder(t, open)
|
|
||||||
failing := &failingRaiser{refuse: consumer.Name}
|
|
||||||
|
|
||||||
var sendErr error
|
|
||||||
out := stdoutOf(t, func() error {
|
|
||||||
sendErr = assertOnSend(t.Context(), open.inventory, failing, " ")
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if sendErr == nil || !strings.Contains(sendErr.Error(), "how messenger on laptop hears what it consumes") {
|
|
||||||
t.Fatalf("the failure is not answered: %v", sendErr)
|
|
||||||
}
|
|
||||||
if !strings.Contains(out, "THE BUS DOES NOT HOLD WHAT THIS SEND IMPLIES") ||
|
|
||||||
!strings.Contains(out, "insufficient resources") || !strings.Contains(out, "bus.objects.unasserted") {
|
|
||||||
t.Fatalf("the failure is not said in the send's output:\n%s", out)
|
|
||||||
}
|
|
||||||
if !asserted(&failing.recordingRaiser, worker) {
|
|
||||||
t.Fatal("the seat's worker was not asked for")
|
|
||||||
}
|
|
||||||
c, open1, err := conditionsFrom.Get(t.Context(), "bus.objects.unasserted")
|
|
||||||
if err != nil || !open1 {
|
|
||||||
t.Fatalf("no condition raised: %v", err)
|
|
||||||
}
|
|
||||||
if c.Kind != kindBusObjectsUnasserted || c.Source != sourceBusObjects ||
|
|
||||||
len(c.Evidence) == 0 || !strings.Contains(c.Evidence[0].Said, "insufficient resources") {
|
|
||||||
t.Fatalf("the condition does not say what failed: %+v", c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The next send asserts them, and that observation clears it.
|
|
||||||
if err := assertOnSend(t.Context(), open.inventory, &recordingRaiser{}, ""); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, stillOpen, err := conditionsFrom.Get(t.Context(), "bus.objects.unasserted"); err != nil || stillOpen {
|
|
||||||
t.Fatalf("a send that asserted everything left the condition open: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Against a real bus, through the send's own grant: a module assigned after start has its consumer
|
|
||||||
// once a declaration is sent, and a holder assigned after start its seat's worker.
|
|
||||||
func TestNatsAModuleAssignedAfterStartGetsItsConsumerAtItsFirstSend(t *testing.T) {
|
|
||||||
url := testbus.URL(t)
|
|
||||||
open := aMesh(t)
|
|
||||||
js, err := broker.Dial(url)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Cleanup(js.Close)
|
|
||||||
for _, s := range []string{"CONTROL", "NODES", "ASSIGNMENTS", "EVENTS"} {
|
|
||||||
_ = js.Context().DeleteStream(s)
|
|
||||||
}
|
|
||||||
// The controller starting, before either was assigned.
|
|
||||||
if _, err := assertBusObjects(t.Context(), open.inventory, js); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
consumer := aCarriedConsumer(t, open)
|
|
||||||
worker := aLateHolder(t, open)
|
|
||||||
_ = js.Context().DeleteConsumer(worker.Stream, worker.Name)
|
|
||||||
for _, c := range []broker.Consumer{consumer, worker} {
|
|
||||||
if _, err := js.Context().ConsumerInfo(c.Stream, c.Name); !errors.Is(err, nats.ErrConsumerNotFound) {
|
|
||||||
t.Fatalf("%s on %s exists before any send; the test proves nothing: %v", c.Name, c.Stream, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
server := link.ConnectNats(js, nil, nil)
|
|
||||||
if err := (overTheBus{open: open, server: server}).grant(t.Context(), nil); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, c := range []broker.Consumer{consumer, worker} {
|
|
||||||
if _, err := js.Context().ConsumerInfo(c.Stream, c.Name); err != nil {
|
|
||||||
t.Fatalf("%s on %s is not on the bus after a send: %v", c.Name, c.Stream, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, raised, _ := conditionsFrom.Get(t.Context(), "bus.objects.unasserted"); raised {
|
|
||||||
t.Fatal("a send that asserted everything raised a condition")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A first-node gate waits for a busy container (novox/hq ADR 0335 §4).
|
|
||||||
//
|
|
||||||
// When a machine reports, on what the gate sent it, that a container of a judged module was left running
|
|
||||||
// because it said busy, the judging has not started: the build is not yet what runs there. The walk says it
|
|
||||||
// waits for a busy container on that machine, counts no pass and no bound, and starts the judging — its
|
|
||||||
// Since — at the report that no longer lists the container waiting. The wait is the node-engine's, bounded
|
|
||||||
// at 15 minutes; the controller's own bound (busyGateBound past the engine's) is for an engine that stopped
|
|
||||||
// reporting while it waited, so no walk waits for ever. An engine older than the field never lists one, and
|
|
||||||
// its gate starts as before.
|
|
||||||
|
|
||||||
// busyGateSlack is how long past the engine's bound the controller still waits for the report that says the
|
|
||||||
// container replaced: a pass of the engine (its asks every 20 seconds) and the report's way here.
|
|
||||||
var busyGateSlack = 5 * time.Minute
|
|
||||||
|
|
||||||
// busyEngineBound is the node-engine's bound on a wait (novox/hq ADR 0335 §3), counted again here on this
|
|
||||||
// controller's clock from when it first read the wait.
|
|
||||||
const busyEngineBound = 15 * time.Minute
|
|
||||||
|
|
||||||
// busyGateEnds is when the gate stops waiting: the engine's own word on its bound, or this controller's count of
|
|
||||||
// it from when it first read the wait, whichever is sooner, and the slack — so neither a clock ahead on the
|
|
||||||
// machine nor a wrong word from it holds a walk longer.
|
|
||||||
func busyGateEnds(b *inventory.GateBusy) time.Time {
|
|
||||||
until := b.Seen.Add(busyEngineBound)
|
|
||||||
if !b.Until.IsZero() && b.Until.Before(until) {
|
|
||||||
until = b.Until
|
|
||||||
}
|
|
||||||
return until.Add(busyGateSlack)
|
|
||||||
}
|
|
||||||
|
|
||||||
// busyWait decides, before a judging, whether the gate waits for a busy container on a machine it judges:
|
|
||||||
// true while it waits, the gate saying so. False when nothing waits, or no longer: a wait that ended moves the
|
|
||||||
// gate's Since to its end. Pure but for the gate it is handed.
|
|
||||||
func busyWait(g *inventory.PlanGate, pairs []judged, f gateFacts, now time.Time) bool {
|
|
||||||
if g.Busy != nil && g.Busy.Ended != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, j := range pairs {
|
|
||||||
b, waits := busyOn(j, f)
|
|
||||||
if !waits {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if g.Busy == nil {
|
|
||||||
g.Busy = &inventory.GateBusy{Machine: j.node, Module: j.module, Container: b.Container, Since: b.Since,
|
|
||||||
Seen: now}
|
|
||||||
}
|
|
||||||
if g.Busy.Seen.IsZero() {
|
|
||||||
g.Busy.Seen = now
|
|
||||||
}
|
|
||||||
g.Busy.Until, g.Busy.Said = b.Until, b.Said
|
|
||||||
if now.After(busyGateEnds(g.Busy)) {
|
|
||||||
// The engine said it would have replaced it by now: the judging starts, and finds the module not
|
|
||||||
// yet healthy while the machine still lists it waiting (busyStill), until the gate's own bound.
|
|
||||||
end := now
|
|
||||||
g.Busy.Ended, g.Busy.PastBound = &end, true
|
|
||||||
g.Since, g.Passes, g.LastPass, g.Healthy, g.HealthyAt, g.Applied = &end, 0, nil, nil, nil, nil
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
g.Passes, g.LastPass, g.Last, g.Failing = 0, nil, g.Busy.Words(), nil
|
|
||||||
g.Read(now, false, g.Busy.Words())
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if g.Busy == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// Replaced: the judging starts at the report that said so.
|
|
||||||
end := now
|
|
||||||
if r, said := f.reports[g.Busy.Machine]; said && r.At != nil && r.At.After(g.Busy.Since) && r.At.Before(now) {
|
|
||||||
end = r.At.UTC()
|
|
||||||
}
|
|
||||||
g.Busy.Ended = &end
|
|
||||||
g.Since, g.Passes, g.LastPass, g.Last, g.Healthy, g.HealthyAt, g.Applied = &end, 0, nil, "", nil, nil, nil
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// busyOn is the container of a judged module its machine last reported, on what the gate sent it, as left
|
|
||||||
// running because it said busy.
|
|
||||||
func busyOn(j judged, f gateFacts) (inventory.BusyWait, bool) {
|
|
||||||
r, said := f.reports[j.node]
|
|
||||||
if !said || r.At == nil || !f.reportedOn(j.node, r) {
|
|
||||||
return inventory.BusyWait{}, false
|
|
||||||
}
|
|
||||||
return inventory.WaitingFor(r.Busy, j.module)
|
|
||||||
}
|
|
||||||
|
|
||||||
// busyStill is a judged module whose container its machine still lists waiting once the gate stopped waiting
|
|
||||||
// for it: not yet what was sent, so not healthy, whatever its health says of the container still running.
|
|
||||||
func busyStill(j judged, f gateFacts) (string, bool) {
|
|
||||||
b, waits := busyOn(j, f)
|
|
||||||
if !waits {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s still runs its busy container %s, past its node-engine's bound of %s",
|
|
||||||
j.node, b.Container, b.Until.UTC().Format(time.RFC3339)), true
|
|
||||||
}
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A walk whose module's container waits as busy on its first machine says so, and starts that machine's
|
|
||||||
// first-machine gate only once the machine reports the container replaced (novox/hq ADR 0335 §4).
|
|
||||||
func TestAGateWaitsForABusyContainerAndStartsWhenItIsReplaced(t *testing.T) {
|
|
||||||
sent := time.Date(2026, 10, 13, 1, 0, 0, 0, time.UTC)
|
|
||||||
g := &inventory.PlanGate{Machines: []string{"novox"}, Since: &sent,
|
|
||||||
Sent: map[string]inventory.SentDeclaration{"novox": {Digest: "d1", Sequence: 7}}}
|
|
||||||
pairs := []judged{{module: "build-agent", node: "novox"}, {module: "mesh-controller", node: "novox"}}
|
|
||||||
reported := sent.Add(20 * time.Second)
|
|
||||||
waiting := []inventory.BusyWait{{ID: "build-agent.server", Container: "mesh-build-agent",
|
|
||||||
State: inventory.BusyWaiting, Since: reported, Until: reported.Add(15 * time.Minute),
|
|
||||||
Said: "busy: novox/mesh-controller (check of #280), in hand since 00:58:00Z"}}
|
|
||||||
facts := func(at time.Time, busy []inventory.BusyWait) gateFacts {
|
|
||||||
// As judgeMoves hands it: the report held against the gate's own send.
|
|
||||||
return gateFacts{sent: g.Sent, reports: map[string]inventory.Reported{"novox": {Node: "novox",
|
|
||||||
Outcome: inventory.OutcomeApplied, At: &at, Declared: "d1", ReportedSequence: 7, Busy: busy}}}
|
|
||||||
}
|
|
||||||
|
|
||||||
now := reported.Add(time.Minute)
|
|
||||||
if !busyWait(g, pairs, facts(reported, waiting), now) {
|
|
||||||
t.Fatal("a gate whose module's container waits as busy judged it")
|
|
||||||
}
|
|
||||||
if !g.Since.Equal(sent) || g.Busy == nil || g.Busy.Machine != "novox" || g.Busy.Ended != nil {
|
|
||||||
t.Fatalf("the gate does not keep the wait: %+v %+v", g.Since, g.Busy)
|
|
||||||
}
|
|
||||||
for _, said := range []string{gateLine(g), g.Last} {
|
|
||||||
if !strings.Contains(said, "waits for a busy container on novox") {
|
|
||||||
t.Errorf("the walk does not say it waits for a busy container there: %s", said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !strings.Contains(g.Last, "check of #280") {
|
|
||||||
t.Errorf("the wait does not say what the container holds: %s", g.Last)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ten minutes on, still busy, within the engine's bound: still waiting, nothing judged, no bound run.
|
|
||||||
now = reported.Add(10 * time.Minute)
|
|
||||||
if !busyWait(g, pairs, facts(reported.Add(9*time.Minute), waiting), now) || g.Passes != 0 {
|
|
||||||
t.Fatalf("the gate stopped waiting within the bound: %+v", g)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The machine reports it replaced: the judging starts at that report.
|
|
||||||
replacedAt := reported.Add(12 * time.Minute)
|
|
||||||
now = replacedAt.Add(5 * time.Second)
|
|
||||||
replaced := []inventory.BusyWait{{ID: "build-agent.server", Container: "mesh-build-agent",
|
|
||||||
State: "replaced-when-idle", Since: reported, Until: reported.Add(15 * time.Minute)}}
|
|
||||||
if busyWait(g, pairs, facts(replacedAt, replaced), now) {
|
|
||||||
t.Fatal("the gate still waits after the container was replaced")
|
|
||||||
}
|
|
||||||
if g.Since == nil || !g.Since.Equal(replacedAt) || g.Busy.Ended == nil || !g.Busy.Ended.Equal(replacedAt) {
|
|
||||||
t.Fatalf("the gate does not start at the report that said it replaced: since %v, %+v", g.Since, g.Busy)
|
|
||||||
}
|
|
||||||
if strings.Contains(gateLine(g), "waits for a busy container") {
|
|
||||||
t.Errorf("a gate judging still says it waits: %s", gateLine(g))
|
|
||||||
}
|
|
||||||
// And a later report listing it waiting again does not hold a gate that started.
|
|
||||||
if busyWait(g, pairs, facts(now, waiting), now.Add(time.Minute)) {
|
|
||||||
t.Error("a gate that started waits again")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// An engine that stops reporting while it waits holds no walk for ever: past the engine's bound and the slack,
|
|
||||||
// the judging starts, and finds the module not yet healthy while its container is still listed waiting.
|
|
||||||
func TestAGateWaitsNoLongerThanTheEnginesBound(t *testing.T) {
|
|
||||||
sent := time.Date(2026, 10, 13, 1, 0, 0, 0, time.UTC)
|
|
||||||
g := &inventory.PlanGate{Machines: []string{"novox"}, Since: &sent}
|
|
||||||
pairs := []judged{{module: "build-agent", node: "novox"}}
|
|
||||||
waiting := []inventory.BusyWait{{ID: "build-agent.server", Container: "mesh-build-agent",
|
|
||||||
State: inventory.BusyWaiting, Since: sent, Until: sent.Add(15 * time.Minute)}}
|
|
||||||
f := gateFacts{reports: map[string]inventory.Reported{"novox": {Node: "novox", Outcome: inventory.OutcomeApplied,
|
|
||||||
At: &sent, Current: true, Busy: waiting}}}
|
|
||||||
now := sent.Add(15*time.Minute + busyGateSlack + time.Second)
|
|
||||||
if busyWait(g, pairs, f, now) {
|
|
||||||
t.Fatal("a gate waited past the engine's bound and the slack")
|
|
||||||
}
|
|
||||||
if !g.Busy.PastBound || !g.Since.Equal(now) {
|
|
||||||
t.Fatalf("the gate does not start, said past the bound: %+v %v", g.Busy, g.Since)
|
|
||||||
}
|
|
||||||
if why, still := busyStill(pairs[0], f); !still || !strings.Contains(why, "past its node-engine's bound") {
|
|
||||||
t.Fatalf("a module still running its busy container reads as what was sent: %q", why)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A report from an engine older than the field lists nothing: the gate starts at the send, as before; and a
|
|
||||||
// report not about the gate's send says nothing of it.
|
|
||||||
func TestAGateOfAnEngineThatNeverWaitsStartsAsBefore(t *testing.T) {
|
|
||||||
sent := time.Date(2026, 10, 13, 1, 0, 0, 0, time.UTC)
|
|
||||||
g := &inventory.PlanGate{Machines: []string{"ace"}, Since: &sent,
|
|
||||||
Sent: map[string]inventory.SentDeclaration{"ace": {Digest: "d2", Sequence: 9}}}
|
|
||||||
pairs := []judged{{module: "build-agent", node: "ace"}}
|
|
||||||
at := sent.Add(time.Minute)
|
|
||||||
older := gateFacts{sent: g.Sent, reports: map[string]inventory.Reported{"ace": {Node: "ace", Outcome: inventory.OutcomeApplied,
|
|
||||||
At: &at, Declared: "d2", ReportedSequence: 9}}}
|
|
||||||
if busyWait(g, pairs, older, at) || g.Busy != nil || !g.Since.Equal(sent) {
|
|
||||||
t.Fatalf("a gate on an engine that never waits did not start at the send: %+v %v", g.Busy, g.Since)
|
|
||||||
}
|
|
||||||
elsewhere := gateFacts{sent: g.Sent, reports: map[string]inventory.Reported{"ace": {Node: "ace", Outcome: inventory.OutcomeApplied,
|
|
||||||
At: &at, Declared: "d1", ReportedSequence: 8, Busy: []inventory.BusyWait{{ID: "build-agent.server",
|
|
||||||
State: inventory.BusyWaiting, Until: at.Add(time.Hour)}}}}}
|
|
||||||
if busyWait(g, pairs, elsewhere, at) {
|
|
||||||
t.Fatal("a wait reported about an earlier declaration held the gate")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The wait is bounded on this controller's clock: an engine whose clock runs an hour ahead, saying it waits until
|
|
||||||
// then, holds the gate no longer than 15 minutes and the slack from when the controller first read the wait.
|
|
||||||
func TestAGateWaitIsBoundedOnTheControllersClock(t *testing.T) {
|
|
||||||
seen := time.Date(2026, 10, 13, 1, 0, 0, 0, time.UTC)
|
|
||||||
g := &inventory.PlanGate{Machines: []string{"novox"}, Since: &seen}
|
|
||||||
pairs := []judged{{module: "build-agent", node: "novox"}}
|
|
||||||
ahead := seen.Add(time.Hour)
|
|
||||||
f := gateFacts{reports: map[string]inventory.Reported{"novox": {Node: "novox", Outcome: inventory.OutcomeApplied,
|
|
||||||
At: &seen, Current: true, Busy: []inventory.BusyWait{{ID: "build-agent.server", Container: "mesh-build-agent",
|
|
||||||
State: inventory.BusyWaiting, Since: ahead, Until: ahead.Add(15 * time.Minute)}}}}}
|
|
||||||
if !busyWait(g, pairs, f, seen) || !g.Busy.Seen.Equal(seen) {
|
|
||||||
t.Fatalf("the gate did not start waiting, seen now: %+v", g.Busy)
|
|
||||||
}
|
|
||||||
if !busyWait(g, pairs, f, seen.Add(15*time.Minute+busyGateSlack-time.Second)) {
|
|
||||||
t.Fatal("the gate stopped waiting within the bound on its own clock")
|
|
||||||
}
|
|
||||||
if busyWait(g, pairs, f, seen.Add(15*time.Minute+busyGateSlack+time.Second)) || !g.Busy.PastBound {
|
|
||||||
t.Fatalf("an engine's later word held the gate past the controller's bound: %+v", g.Busy)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// `plan` says, for a machine whose node-engine is older than the field, each container whose busy is not sent.
|
|
||||||
func TestAPlanSaysABusyAnOlderEngineIsNotSent(t *testing.T) {
|
|
||||||
modules := []catalogue.Manifest{{Module: "build-agent", Resources: []map[string]any{
|
|
||||||
{"id": "server", "type": "container", "busy": map[string]any{"command": "mesh-builder busy"}},
|
|
||||||
{"id": "agent-env", "type": "file"}}}}
|
|
||||||
lines := busyNotSent(modules, false)
|
|
||||||
if len(lines) != 1 || !strings.Contains(lines[0], "build-agent.server declares busy") ||
|
|
||||||
!strings.Contains(lines[0], "replaced at once") {
|
|
||||||
t.Fatalf("the plan does not say the busy not sent: %v", lines)
|
|
||||||
}
|
|
||||||
if lines := busyNotSent(modules, true); len(lines) != 0 {
|
|
||||||
t.Fatalf("a busy sent is said as not sent: %v", lines)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCallsIsServedAndSaysWhatItKeeps(t *testing.T) {
|
|
||||||
handlers, behind, err := seatToolHandlers()
|
|
||||||
if err != nil || len(behind) != 0 {
|
|
||||||
t.Fatalf("%v %v", behind, err)
|
|
||||||
}
|
|
||||||
calls, ok := handlers["calls"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatal("calls is not served")
|
|
||||||
}
|
|
||||||
if _, err := calls(context.Background(), json.RawMessage(`{"node":"anchor"}`)); err == nil ||
|
|
||||||
!strings.Contains(err.Error(), `"node"`) {
|
|
||||||
t.Errorf("calls took an argument it does not declare: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := calls(context.Background(), json.RawMessage(`{"call":"call-0-0"}`)); err == nil ||
|
|
||||||
!strings.Contains(err.Error(), "not across a restart") {
|
|
||||||
t.Errorf("an unknown call was not said plainly: %v", err)
|
|
||||||
}
|
|
||||||
got, err := calls(context.Background(), json.RawMessage(`{}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, listed := got.(map[string]any)["calls"]; !listed {
|
|
||||||
t.Errorf("calls answered %v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/broker"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// consoleWaits is how long the console waits for an answer (mesh-tools node-tools/internal/bus
|
|
||||||
// RequestTimeout) — the shortest wait of a caller the mesh ships.
|
|
||||||
const consoleWaits = 30 * time.Second
|
|
||||||
|
|
||||||
// **A call's one answer is never later than its caller or the bus allow** (novox/hq issue 265): a
|
|
||||||
// holder answers within AnswerWithin, which must be inside both the console's wait and the window the
|
|
||||||
// bus gives an answer. Before, the console waited 30s, the bus 60s, and a push ran as long as it ran.
|
|
||||||
func TestAVerbAnswersInsideEveryWaitOnIt(t *testing.T) {
|
|
||||||
if link.AnswerWithin >= consoleWaits/2 {
|
|
||||||
t.Errorf("a call answers within %s: not well inside the console's %s", link.AnswerWithin, consoleWaits)
|
|
||||||
}
|
|
||||||
if link.AnswerWithin >= broker.ResponseTTL {
|
|
||||||
t.Errorf("a call answers within %s, after the bus stops permitting an answer at %s", link.AnswerWithin, broker.ResponseTTL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A push — named or through command — answers before it runs: it sends the machine holding the bus
|
|
||||||
// first, and the broker reloading its user list forgets the answer it was about to permit.
|
|
||||||
func TestAPushAnswersBeforeItSends(t *testing.T) {
|
|
||||||
for _, c := range []struct {
|
|
||||||
verb string
|
|
||||||
args map[string]any
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{"push", map[string]any{"node": "anchor", "why": "w"}, true},
|
|
||||||
{"push", map[string]any{"why": "w"}, true},
|
|
||||||
{"command", map[string]any{"command": "builds"}, false},
|
|
||||||
{"status", map[string]any{}, false},
|
|
||||||
{"assign", map[string]any{"node": "anchor", "module": "m"}, false},
|
|
||||||
} {
|
|
||||||
argv, err := argvFor(c.verb, c.args)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("%s %v: %v", c.verb, c.args, err)
|
|
||||||
}
|
|
||||||
if got := answersFirst(argv); got != c.want {
|
|
||||||
t.Errorf("%s %v answers first: %v, want %v", c.verb, c.args, got, c.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// `calls` is served, takes a call's id and nothing else, and says plainly when it holds no such call.
|
|
||||||
@@ -1,647 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"path"
|
|
||||||
"regexp"
|
|
||||||
"slices"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
"github.com/nats-io/nats.go/jetstream"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
||||||
sdkdelivery "git.novox.be/novox/mesh-sdk/go/delivery"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **The controller catches up on merges it did not hear** (novox/hq ADR 0457, issue 1336). A merge the forge announced while the
|
|
||||||
// controller was being replaced was never heard, and nothing asked for it again: the trunk held a merge no walk answered. At its
|
|
||||||
// start, and each time it hears a merge, the controller reads the last catchUpWindow merge commits of a trunk from the forge, and
|
|
||||||
// hears each one it holds no record of as if the forge had announced it then, on the warrant the router holds for it.
|
|
||||||
// It lasts until release 6 of design 47 deletes delivery plans and batches (ADR 0429 §7, §8).
|
|
||||||
|
|
||||||
// catchUpWindow is how many of a trunk's newest merge commits are read; an older merge is a person's to decide.
|
|
||||||
const catchUpWindow = 50
|
|
||||||
|
|
||||||
// kindMergeNotHeard is the condition of a merge the controller did not hear and holds no warrant to plan.
|
|
||||||
const kindMergeNotHeard = "merge-not-heard"
|
|
||||||
|
|
||||||
// trunkMerge is a merge commit of a trunk as the forge lists it.
|
|
||||||
type trunkMerge struct {
|
|
||||||
Commit string
|
|
||||||
// Head is the merge commit's second parent: the pull request's head that was merged.
|
|
||||||
Head string
|
|
||||||
Subject string
|
|
||||||
At time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// trunkCommit is a commit of a trunk as the forge lists it, newest first: a merge commit has a second parent.
|
|
||||||
type trunkCommit struct {
|
|
||||||
Commit, SecondParent, Subject string
|
|
||||||
At time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// trunkCommitsPage is one page (fifty) of a trunk's commits, newest first; empty past the last. A variable so a test needs no
|
|
||||||
// forge.
|
|
||||||
var trunkCommitsPage = func(ctx context.Context, owner, repo, branch string, page int) ([]trunkCommit, error) {
|
|
||||||
var found struct {
|
|
||||||
Result []struct {
|
|
||||||
SHA string `json:"sha"`
|
|
||||||
Parents []struct {
|
|
||||||
SHA string `json:"sha"`
|
|
||||||
} `json:"parents"`
|
|
||||||
Commit struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Committer struct {
|
|
||||||
Date string `json:"date"`
|
|
||||||
} `json:"committer"`
|
|
||||||
} `json:"commit"`
|
|
||||||
} `json:"result"`
|
|
||||||
}
|
|
||||||
apiPath := fmt.Sprintf("/repos/%s/%s/commits?sha=%s&limit=50&page=%d&stat=false&verification=false&files=false",
|
|
||||||
owner, repo, url.QueryEscape(branch), page)
|
|
||||||
missing, err := askForgeApi(ctx, apiPath, &found)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if missing {
|
|
||||||
return nil, fmt.Errorf("the forge holds no branch %s of %s/%s", branch, owner, repo)
|
|
||||||
}
|
|
||||||
var out []trunkCommit
|
|
||||||
for _, c := range found.Result {
|
|
||||||
at, _ := time.Parse(time.RFC3339, c.Commit.Committer.Date)
|
|
||||||
tc := trunkCommit{Commit: c.SHA, Subject: firstLine(c.Commit.Message), At: at.UTC()}
|
|
||||||
if len(c.Parents) >= 2 {
|
|
||||||
tc.SecondParent = c.Parents[1].SHA
|
|
||||||
}
|
|
||||||
out = append(out, tc)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// readTrunkMerges is the last catchUpWindow merge commits of a repository's trunk, oldest first: pages are read until that many
|
|
||||||
// merges are held or the trunk ends.
|
|
||||||
func readTrunkMerges(ctx context.Context, owner, repo, branch string) ([]trunkMerge, error) {
|
|
||||||
var merges []trunkMerge
|
|
||||||
for page := 1; page <= 8 && len(merges) < catchUpWindow; page++ {
|
|
||||||
commits, err := trunkCommitsPage(ctx, owner, repo, branch, page)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(commits) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
for _, c := range commits {
|
|
||||||
// A merge of the trunk is a pull request's merge into it. A commit with a second parent that is none is a merge made
|
|
||||||
// inside some branch ("Merge remote-tracking branch 'origin/main' into fix/..."), which the trunk's history lists too and
|
|
||||||
// which no Approve ever covered: read as a gap it would raise a condition for nothing a person decides.
|
|
||||||
if c.SecondParent != "" && mergesIntoTrunk(c.Subject, branch) {
|
|
||||||
merges = append(merges, trunkMerge{Commit: c.Commit, Head: c.SecondParent, Subject: c.Subject, At: c.At})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lastMerges(merges), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// sinceTheRecord are the merges that can be gaps (novox/hq ADR 0463 §2): those made since the controller's record of merges begins on
|
|
||||||
// the mesh (first, the earliest merge it holds of any repository and branch, less mergeGrace as owedLate judges). A merge before it is
|
|
||||||
// history on every trunk, and a trunk with no merge kept of its own still has the merges made since as gaps. With no merge kept
|
|
||||||
// anywhere there is no record to have a gap in.
|
|
||||||
func sinceTheRecord(merges []trunkMerge, kept bool, first time.Time) []trunkMerge {
|
|
||||||
if !kept {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var out []trunkMerge
|
|
||||||
for _, m := range merges {
|
|
||||||
if !m.At.Before(first.Add(-mergeGrace)) {
|
|
||||||
out = append(out, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// lastMerges is the newest catchUpWindow of merges, given newest first, oldest first.
|
|
||||||
func lastMerges(newestFirst []trunkMerge) []trunkMerge {
|
|
||||||
if len(newestFirst) > catchUpWindow {
|
|
||||||
newestFirst = newestFirst[:catchUpWindow]
|
|
||||||
}
|
|
||||||
out := slices.Clone(newestFirst)
|
|
||||||
slices.Reverse(out)
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
var mergeSubject = regexp.MustCompile(`^Merge pull request '(.*)' \(#(\d+)\) from (\S+) into (\S+)$`)
|
|
||||||
|
|
||||||
// pullOfSubject is the pull request a merge commit's subject names: its title and number.
|
|
||||||
func pullOfSubject(subject string) (title string, number int, ok bool) {
|
|
||||||
m := mergeSubject.FindStringSubmatch(strings.TrimSpace(subject))
|
|
||||||
if m == nil {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
n, err := strconv.Atoi(m[2])
|
|
||||||
if err != nil || n <= 0 {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
return m[1], n, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// mergesIntoTrunk says a commit's subject is a pull request's merge into this trunk.
|
|
||||||
func mergesIntoTrunk(subject, branch string) bool {
|
|
||||||
m := mergeSubject.FindStringSubmatch(strings.TrimSpace(subject))
|
|
||||||
return m != nil && m[4] == branch
|
|
||||||
}
|
|
||||||
|
|
||||||
// headBranchOfSubject is the branch a merge commit's subject says was merged.
|
|
||||||
func headBranchOfSubject(subject string) string {
|
|
||||||
if m := mergeSubject.FindStringSubmatch(strings.TrimSpace(subject)); m != nil {
|
|
||||||
return m[3]
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// readMergeEvent is the merge as the forge would have announced it: the commit, the pull request, the files it changed and the
|
|
||||||
// directories among them that hold a module.json. A variable so a test needs no forge.
|
|
||||||
var readMergeEvent = func(ctx context.Context, owner, repo, branch string, tm trunkMerge) (link.SourceMoved, error) {
|
|
||||||
m := link.SourceMoved{Owner: owner, Repo: repo, Base: branch, Head: headBranchOfSubject(tm.Subject), HeadSHA: tm.Head,
|
|
||||||
Commit: tm.Commit, MergedAt: tm.At.UTC().Format(time.RFC3339)}
|
|
||||||
if title, number, ok := pullOfSubject(tm.Subject); ok {
|
|
||||||
m.Number, m.Title = number, title
|
|
||||||
}
|
|
||||||
var found struct {
|
|
||||||
Result struct {
|
|
||||||
Files []struct {
|
|
||||||
Filename string `json:"filename"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
} `json:"files"`
|
|
||||||
} `json:"result"`
|
|
||||||
}
|
|
||||||
missing, err := askForgeApi(ctx, "/repos/"+owner+"/"+repo+"/git/commits/"+url.PathEscape(tm.Commit), &found)
|
|
||||||
if err != nil {
|
|
||||||
return m, err
|
|
||||||
}
|
|
||||||
if missing || len(found.Result.Files) == 0 {
|
|
||||||
return m, fmt.Errorf("the forge named no files for the merge %.8s of %s/%s", tm.Commit, owner, repo)
|
|
||||||
}
|
|
||||||
for _, f := range found.Result.Files {
|
|
||||||
m.Paths = append(m.Paths, f.Filename)
|
|
||||||
if f.Status == "removed" || f.Status == "deleted" {
|
|
||||||
m.Removed = append(m.Removed, f.Filename)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// The forge lists a merge's files by the page; a list as long as a page may be a beginning.
|
|
||||||
m.PathsTruncated = len(m.Paths) >= 300
|
|
||||||
dirs := map[string]bool{}
|
|
||||||
for _, p := range m.Paths {
|
|
||||||
for d := path.Dir(p); ; d = path.Dir(d) {
|
|
||||||
dirs[d] = true
|
|
||||||
if d == "." || d == "/" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(dirs) > 64 {
|
|
||||||
return m, nil // too many to look at: the old rule stands, a directory is a module when its manifest changed
|
|
||||||
}
|
|
||||||
var holding []string
|
|
||||||
for d := range dirs {
|
|
||||||
file := "module.json"
|
|
||||||
if d != "." {
|
|
||||||
file = d + "/module.json"
|
|
||||||
}
|
|
||||||
_, exists, err := manifestCached(ctx, owner, repo, file, tm.Commit)
|
|
||||||
if err != nil {
|
|
||||||
return m, err
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
holding = append(holding, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(holding)
|
|
||||||
m.ModuleDirs, m.ModuleDirsSaid = holding, true
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// mergeAskPrefix is what the id of a merge ask made by the retired module mesh-delivery begins with: the pull request's number
|
|
||||||
// and the head's first twelve digits (`merge-<number>-<head>-<try>`). The teller's ids name neither (see findMergeWarrant).
|
|
||||||
func mergeAskPrefix(number int, head string) string {
|
|
||||||
if len(head) > 12 {
|
|
||||||
head = head[:12]
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("merge-%d-%s-", number, head)
|
|
||||||
}
|
|
||||||
|
|
||||||
// askersOfMerges are the askers a merge's warrant is read under: the teller, and mesh-delivery for a merge before release 3e
|
|
||||||
// (ADR 0437 §6). Release 4's controller work stops accepting mesh-delivery (ADR 0450 §8); a merge asked under it earlier is
|
|
||||||
// still found here until then.
|
|
||||||
func askersOfMerges() []string { return []string{sdkdelivery.TellerModule, theDelivery} }
|
|
||||||
|
|
||||||
// caughtUpWarrant judges a chosen merge ask of the router as the warrant of a merge: the ask is the operator's, the warrant
|
|
||||||
// answers that very ask as it was published, and it is the operator's Approve proved by the channel it came through (ADR 0437
|
|
||||||
// §6, as movedOnApprove judges an announced one). The heads the Approve was bound to are checked by the caller, where it can
|
|
||||||
// name them.
|
|
||||||
func caughtUpWarrant(asker string, ask *asks.Ask, w *asks.Warrant) (string, error) {
|
|
||||||
if err := askerAccepted(asker); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case ask == nil || w == nil:
|
|
||||||
return "", errors.New("the router holds no closed record of the ask")
|
|
||||||
case ask.Who != asks.Operator:
|
|
||||||
return "", fmt.Errorf("the ask %s is for %q to answer, not the operator", ask.ID, ask.Who)
|
|
||||||
}
|
|
||||||
option, err := w.For(asker, *ask)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if option.ID != optionApprove || w.Option != optionApprove {
|
|
||||||
return "", fmt.Errorf("the operator chose %q on the ask %s, not an Approve", w.Option, w.Ask)
|
|
||||||
}
|
|
||||||
if w.By == nil || w.By.Who != asks.Operator || w.By.Verified == "" {
|
|
||||||
return "", fmt.Errorf("the ask %s was not answered by the operator on a channel that proves who answered", w.Ask)
|
|
||||||
}
|
|
||||||
return w.Says() + " (ask " + w.Ask + ")", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// routerAskIDs are the ids of the asks an asker has in the router's record. A variable so a test needs no bus.
|
|
||||||
var routerAskIDs = func(ctx context.Context, inv catalogueReader, asker string) ([]string, error) {
|
|
||||||
serving := servingBus.Load()
|
|
||||||
if serving == nil {
|
|
||||||
return nil, errors.New("only the serving controller reads the router's record")
|
|
||||||
}
|
|
||||||
bucket, err := asksRecords(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if bucket == "" {
|
|
||||||
return nil, errors.New("no module declares the operator channel's records")
|
|
||||||
}
|
|
||||||
api, err := jetstream.New(serving.Conn())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
kv, err := api.KeyValue(ctx, bucket)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return askIDsOf(ctx, kv, asker)
|
|
||||||
}
|
|
||||||
|
|
||||||
// askIDsOf are the ids of the asks an asker has in the router's bucket.
|
|
||||||
func askIDsOf(ctx context.Context, kv jetstream.KeyValue, asker string) ([]string, error) {
|
|
||||||
lister, err := kv.ListKeysFiltered(ctx, asker+".>")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var ids []string
|
|
||||||
for key := range lister.Keys() {
|
|
||||||
ids = append(ids, strings.TrimPrefix(key, asker+"."))
|
|
||||||
}
|
|
||||||
// The lister stops its own watch when it has sent the last key, so a stop of ours finds it stopped: nats answers that with
|
|
||||||
// "invalid subscription", which is no failure of the read (issue 1336). Any other error stays one.
|
|
||||||
if err := lister.Stop(); err != nil && !errors.Is(err, nats.ErrBadSubscription) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return ids, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// closedRecord is a router record of an ask that ended: it never changes, so it is read once.
|
|
||||||
type closedRecord struct {
|
|
||||||
ask *asks.Ask
|
|
||||||
w *asks.Warrant
|
|
||||||
}
|
|
||||||
|
|
||||||
var closedRecords sync.Map // asker + "." + id -> closedRecord
|
|
||||||
|
|
||||||
// closedRecordOf is the router's record of an ask, kept once it is closed. A nil ask is an ask still open, ended without a
|
|
||||||
// choice, or not there.
|
|
||||||
func closedRecordOf(ctx context.Context, inv catalogueReader, asker, id string) (*asks.Ask, *asks.Warrant, error) {
|
|
||||||
if v, ok := closedRecords.Load(asker + "." + id); ok {
|
|
||||||
r := v.(closedRecord)
|
|
||||||
return r.ask, r.w, nil
|
|
||||||
}
|
|
||||||
ask, w, err := routerAskOf(ctx, inv, asker, id)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
if ask != nil && w != nil {
|
|
||||||
closedRecords.Store(asker+"."+id, closedRecord{ask, w})
|
|
||||||
}
|
|
||||||
return ask, w, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// deliveryIDOf is the id the gate gives a delivery, and so the key of its head in the heads an Approve binds: the repository as
|
|
||||||
// owner/name, lower-cased, `@` and the first twelve characters of the head.
|
|
||||||
func deliveryIDOf(owner, repo, head string) string {
|
|
||||||
if len(head) > 12 {
|
|
||||||
head = head[:12]
|
|
||||||
}
|
|
||||||
return strings.ToLower(owner+"/"+repo) + "@" + head
|
|
||||||
}
|
|
||||||
|
|
||||||
// coversHead says an ask the operator approved is for this merge: its Approve is bound to the heads of the pull request alone
|
|
||||||
// (mergeAct over {delivery id: head}, which is how a pull request not in a group is asked), or, for a group's ask, whose other
|
|
||||||
// members' heads the router's record does not carry in full, the ask as published names this pull request at this head in its
|
|
||||||
// details ("<repo>#<number> ... at <head>:"), and the digest the warrant answers covers those words.
|
|
||||||
func coversHead(ask *asks.Ask, owner, repo string, number int, head string) (how string, ok bool) {
|
|
||||||
option, found := ask.Option(optionApprove)
|
|
||||||
if !found {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
heads := map[string]string{deliveryIDOf(owner, repo, head): head}
|
|
||||||
if option.Performs(mergeAct(ask.ID, optionApprove, heads)) == nil {
|
|
||||||
return "bound to the head of this pull request", true
|
|
||||||
}
|
|
||||||
short := head
|
|
||||||
if len(short) > 12 {
|
|
||||||
short = short[:12]
|
|
||||||
}
|
|
||||||
line := regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(repo) + `#` + strconv.Itoa(number) + ` .* at ` + regexp.QuoteMeta(short) + `:`)
|
|
||||||
if line.MatchString(ask.Details) {
|
|
||||||
return "a group's ask naming this pull request at this head", true
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
// findMergeWarrant finds the warrant of a merge, with no read the controller does not make today: the router's chosen merge
|
|
||||||
// ask for it. Under the teller (releases 3e on) the id is `merge-<group>-<heads hash>-<tail>`, which names neither the pull
|
|
||||||
// request nor the head, so the teller's closed asks are read and the one the operator approved whose heads cover the merge's
|
|
||||||
// second parent is the warrant (coversHead). Under mesh-delivery, which made the earlier merges, the id names the pull request
|
|
||||||
// and the head and is found by them. Why it holds; or found false with why not, for a person to read; or an error when the
|
|
||||||
// router could not be read, which is no answer. A variable so a test needs no bus.
|
|
||||||
var findMergeWarrant = func(ctx context.Context, inv catalogueReader, owner, repo string, number int, head string) (why string, found bool, reason string, err error) {
|
|
||||||
reason = "the router holds no chosen merge ask of " + strings.Join(askersOfMerges(), " or ") + " that covers the head " + head[:min(12, len(head))]
|
|
||||||
for _, asker := range askersOfMerges() {
|
|
||||||
ids, err := routerAskIDs(ctx, inv, asker)
|
|
||||||
if err != nil {
|
|
||||||
return "", false, "", err
|
|
||||||
}
|
|
||||||
for _, id := range ids {
|
|
||||||
named := number > 0 && strings.HasPrefix(id, mergeAskPrefix(number, head))
|
|
||||||
if asker == sdkdelivery.TellerModule && !strings.HasPrefix(id, sdkdelivery.AskKindMerge+"-") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if asker == theDelivery && !named {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ask, w, err := closedRecordOf(ctx, inv, asker, id)
|
|
||||||
if err != nil {
|
|
||||||
return "", false, "", err
|
|
||||||
}
|
|
||||||
if ask == nil || w == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
warrant, err := caughtUpWarrant(asker, ask, w)
|
|
||||||
if err != nil {
|
|
||||||
if named {
|
|
||||||
reason = fmt.Sprintf("%s's ask %s holds no operator's Approve: %v", asker, id, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
how, covers := coversHead(ask, owner, repo, number, head)
|
|
||||||
if !covers && !named {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !covers {
|
|
||||||
how = "the ask's id names this pull request and head"
|
|
||||||
}
|
|
||||||
return warrant + ", " + how, true, "", nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false, reason, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// caughtTrunk is one trunk the catalogue builds from.
|
|
||||||
type caughtTrunk struct{ Owner, Repo, Branch string }
|
|
||||||
|
|
||||||
func (t caughtTrunk) repository() string { return t.Owner + "/" + t.Repo }
|
|
||||||
|
|
||||||
// trunksOfCatalogue are the trunks of the repositories on the mesh's forge the catalogue builds a module from.
|
|
||||||
func trunksOfCatalogue(forms *sourceForms, entries []inventory.Entry) []caughtTrunk {
|
|
||||||
var out []caughtTrunk
|
|
||||||
for _, e := range entries {
|
|
||||||
if e.Provided || e.Source.Repository == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
owner, name, onForge := forms.onTheForge(e.Source.Repository, e.Source.Seat)
|
|
||||||
if !onForge {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
branch := followedBranch(e.Source.Ref)
|
|
||||||
if branch == "" {
|
|
||||||
branch = "main"
|
|
||||||
}
|
|
||||||
t := caughtTrunk{owner, name, branch}
|
|
||||||
if !slices.Contains(out, t) {
|
|
||||||
out = append(out, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// catchUpBeside runs a catch-up beside the hearing of the merge that asked for it, so the bus's acknowledgement of that merge
|
|
||||||
// waits for no forge. A variable so a test runs it where it stands.
|
|
||||||
var catchUpBeside = func(run func()) { go run() }
|
|
||||||
|
|
||||||
// catchingUp makes one catch-up at a time.
|
|
||||||
var catchingUp sync.Mutex
|
|
||||||
|
|
||||||
// settledMerges are the merges this process found nothing to plan for: heard, and no record kept, because nothing reads them.
|
|
||||||
// They are not read from the forge again.
|
|
||||||
var settledMerges sync.Map
|
|
||||||
|
|
||||||
// catchUp hears the merges of the catalogue's trunks (only one, when only names it) that the controller holds no record of.
|
|
||||||
// It says whether a trunk was left for later: the forge, the router or the store did not answer.
|
|
||||||
func (f following) catchUp(ctx context.Context, only *caughtTrunk) (later bool) {
|
|
||||||
catchingUp.Lock()
|
|
||||||
defer catchingUp.Unlock()
|
|
||||||
inv := f.open.inventory
|
|
||||||
entries, err := inv.Catalogued(ctx)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("catch-up: the catalogue cannot be read, so no merge is caught up now: %v\n", err)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
kept, first, err := inv.EarliestKept(ctx)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("catch-up: what the controller kept cannot be read, so no merge is caught up now: %v\n", err)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if kept {
|
|
||||||
fmt.Printf("catch-up: the controller's record of merges begins %s; a merge made before it, less %s, is history on every trunk\n",
|
|
||||||
first.UTC().Format(time.RFC3339), mergeGrace)
|
|
||||||
} else {
|
|
||||||
fmt.Println("catch-up: the controller holds no merge of any trunk yet, so none can have been missed")
|
|
||||||
}
|
|
||||||
trunks := trunksOfCatalogue(newSourceForms(ctx, inv), entries)
|
|
||||||
if only != nil {
|
|
||||||
trunks = slices.DeleteFunc(trunks, func(t caughtTrunk) bool {
|
|
||||||
return !strings.EqualFold(t.repository(), only.repository()) || t.Branch != only.Branch
|
|
||||||
})
|
|
||||||
}
|
|
||||||
for _, t := range trunks {
|
|
||||||
if f.catchUpTrunk(ctx, t, kept, first) {
|
|
||||||
later = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return later
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f following) catchUpTrunk(ctx context.Context, t caughtTrunk, kept bool, first time.Time) (later bool) {
|
|
||||||
inv := f.open.inventory
|
|
||||||
repository := t.repository()
|
|
||||||
merges, err := readTrunkMerges(ctx, t.Owner, t.Repo, t.Branch)
|
|
||||||
if err != nil {
|
|
||||||
// Not nothing to catch up: left for the next start or the next heard merge (ADR 0457 clause 3).
|
|
||||||
fmt.Printf("catch-up: the forge did not answer for the last merges of %s %s, so they are not caught up now; the next start "+
|
|
||||||
"or the next merge heard there does it: %v\n", repository, t.Branch, err)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// **A merge made before the controller's record of merges begins is history, never a gap** (novox/hq ADR 0463 §2).
|
|
||||||
read := len(merges)
|
|
||||||
merges = sinceTheRecord(merges, kept, first)
|
|
||||||
fmt.Printf("catch-up: %s %s: %d of the last %d pull request merges were made since the record began\n", repository, t.Branch,
|
|
||||||
len(merges), read)
|
|
||||||
unwarranted := map[string]bool{}
|
|
||||||
for _, tm := range merges {
|
|
||||||
id := fmt.Sprintf("%s_%.8s", repository, tm.Commit)
|
|
||||||
if _, done := settledMerges.Load(repository + "@" + tm.Commit); done {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, known, err := inv.MergeOf(ctx, repository, tm.Commit); err != nil {
|
|
||||||
fmt.Printf("catch-up: the record of %s cannot be read, so %s is not caught up now: %v\n", repository, tm.Commit[:8], err)
|
|
||||||
return true
|
|
||||||
} else if known {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m, err := readMergeEvent(ctx, t.Owner, t.Repo, t.Branch, tm)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("catch-up: the merge %.8s of %s cannot be read from the forge, so it is not caught up now: %v\n", tm.Commit, repository, err)
|
|
||||||
later = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
entries, err := inv.Catalogued(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
read, err := readForPlanning(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
facts, err := factsOfMerge(ctx, inv, m, time.Now().UTC(), entries, read)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("catch-up: %s %.8s cannot be judged now: %v\n", repository, tm.Commit, err)
|
|
||||||
later = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// The very test hearMerge makes: a merge it would plan, or take as a first build, is a gap; any other it would say
|
|
||||||
// nothing reads, and is no gap for a person to decide.
|
|
||||||
if !facts.plans() && !facts.addsModuleToBuilds {
|
|
||||||
settledMerges.Store(repository+"@"+tm.Commit, true)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
why, found, reason, err := findMergeWarrant(ctx, inv, t.Owner, t.Repo, m.Number, tm.Head)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("catch-up: the router cannot be read, so %s %.8s is not caught up now: %v\n", repository, tm.Commit, err)
|
|
||||||
later = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
unwarranted[id] = true
|
|
||||||
f.raiseMergeNotHeard(ctx, repository, t.Branch, tm, m, reason)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
now := time.Now().UTC()
|
|
||||||
said := fmt.Sprintf("caught up: merged %s, not heard, planned %s", tm.At.Format(time.RFC3339), now.Format(time.RFC3339))
|
|
||||||
fmt.Printf("catch-up: %s merged into %s (%.8s) was not heard; %s; on the warrant %s\n", repository, t.Branch, tm.Commit, said, why)
|
|
||||||
m.Title = strings.TrimSpace(m.Title + " (" + said + ")")
|
|
||||||
if err := f.hearMerge(ctx, m, now); err != nil {
|
|
||||||
fmt.Printf("catch-up: %s %.8s could not be heard now: %v\n", repository, tm.Commit, err)
|
|
||||||
later = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, known, err := inv.MergeOf(ctx, repository, tm.Commit); err == nil && !known {
|
|
||||||
settledMerges.Store(repository+"@"+tm.Commit, true) // heard, and nothing reads it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Cleared only when every gap was judged: one left for later may still be open.
|
|
||||||
if !later {
|
|
||||||
f.clearMergesHeard(ctx, repository, unwarranted)
|
|
||||||
}
|
|
||||||
return later
|
|
||||||
}
|
|
||||||
|
|
||||||
// raiseMergeNotHeard keeps open, and observes again, the condition of a merge with no warrant to plan it. One for each
|
|
||||||
// repository and commit, however many catch-ups find it.
|
|
||||||
func (f following) raiseMergeNotHeard(ctx context.Context, repository, branch string, tm trunkMerge, m link.SourceMoved, reason string) {
|
|
||||||
fmt.Printf("catch-up: %s merged into %s (%.8s) was not heard, and is not planned: %s\n", repository, branch, tm.Commit, reason)
|
|
||||||
if conditionsFrom == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
o := conditions.Observation{Scope: conditions.ScopeMerge, ID: fmt.Sprintf("%s_%.8s", repository, tm.Commit), Kind: kindMergeNotHeard,
|
|
||||||
Severity: conditions.Warning, Source: "catch-up", Resolver: conditions.ResolverOperator,
|
|
||||||
Summary: fmt.Sprintf("the merge %.8s of %s into %s was not heard by the controller, and no operator's Approve of it is on "+
|
|
||||||
"record to plan it on: a person decides", tm.Commit, repository, branch),
|
|
||||||
Said: fmt.Sprintf("%s %.8s (pull request %d, head %.8s, merged %s): %s", repository, tm.Commit, m.Number, tm.Head,
|
|
||||||
tm.At.Format(time.RFC3339), reason),
|
|
||||||
Headline: fmt.Sprintf("A merge into %s was never delivered", repository),
|
|
||||||
Explanation: fmt.Sprintf("A merge into %s was made while the controller did not hear it, and no Approve of yours for it is on "+
|
|
||||||
"record that the controller can plan it on. It stays open while the merge is among the latest %d merges of that repository. "+
|
|
||||||
"The details say what was looked for.", repository, catchUpWindow),
|
|
||||||
Needs: "decide whether that merge should be delivered, and say so if it should.",
|
|
||||||
Resolved: "the merge is planned, or is no longer among the latest merges of its repository"}
|
|
||||||
if _, err := conditionsFrom.Observe(ctx, o); err != nil {
|
|
||||||
fmt.Printf("catch-up: the condition of %s %.8s could not be kept: %v\n", repository, tm.Commit, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// clearMergesHeard clears the open conditions of a repository's merges not heard that this catch-up did not find again: the
|
|
||||||
// gap was planned, or left the window.
|
|
||||||
func (f following) clearMergesHeard(ctx context.Context, repository string, still map[string]bool) {
|
|
||||||
if conditionsFrom == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
open, err := conditionsFrom.Open(ctx)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("catch-up: the open conditions cannot be read, so none is cleared now: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, c := range open {
|
|
||||||
if c.Kind != kindMergeNotHeard || c.Subject.Scope != conditions.ScopeMerge ||
|
|
||||||
!strings.HasPrefix(c.Subject.ID, repository+"_") || still[c.Subject.ID] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, err := conditionsFrom.Clear(ctx, c.Key, "the merge is planned, or left the last merges of its trunk"); err != nil {
|
|
||||||
fmt.Printf("catch-up: the condition %s could not be cleared: %v\n", c.Key, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// catchUpAtStart catches up every trunk once the controller serves, and again every minute while a trunk was left for later.
|
|
||||||
func catchUpAtStart(ctx context.Context, open *stores) {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-time.After(30 * time.Second):
|
|
||||||
}
|
|
||||||
for try := 0; try < 20; try++ {
|
|
||||||
if !(following{open: open}).catchUp(ctx, nil) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-time.After(time.Minute):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println("catch-up: a trunk is still left for later after twenty tries; the next merge heard there does it")
|
|
||||||
}
|
|
||||||
@@ -1,352 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The checks of ADR 0457 (issue 1336): what the controller does about merges it did not hear.
|
|
||||||
|
|
||||||
// aTrunk is a forge's trunk as the catch-up reads it: its merges oldest first, the module each moves, and which of them the
|
|
||||||
// router holds an operator's Approve for.
|
|
||||||
type aTrunk struct {
|
|
||||||
merges []trunkMerge
|
|
||||||
module map[string]string
|
|
||||||
approved map[string]bool
|
|
||||||
read []string // the trunks asked of the forge, as owner/repo
|
|
||||||
events int // merges read in full from the forge
|
|
||||||
silent bool // the forge does not answer
|
|
||||||
}
|
|
||||||
|
|
||||||
func aTrunkOf(t *testing.T, repo string, modules ...string) *aTrunk {
|
|
||||||
t.Helper()
|
|
||||||
tr := &aTrunk{module: map[string]string{}, approved: map[string]bool{}}
|
|
||||||
for i, m := range modules {
|
|
||||||
c := fmt.Sprintf("%s-c%02d", repo, i+1)
|
|
||||||
tr.merges = append(tr.merges, trunkMerge{Commit: c, Head: fmt.Sprintf("%shead%02d", repo, i+1),
|
|
||||||
Subject: fmt.Sprintf("Merge pull request 'change %d' (#%d) from pr/%s into main", i+1, 100+i, c),
|
|
||||||
At: t0.Add(time.Duration(i) * time.Minute)})
|
|
||||||
tr.module[c] = m
|
|
||||||
tr.approved[c] = true
|
|
||||||
}
|
|
||||||
was, wasEvent, wasWarrant := trunkCommitsPage, readMergeEvent, findMergeWarrant
|
|
||||||
trunkCommitsPage = func(_ context.Context, owner, name, _ string, page int) ([]trunkCommit, error) {
|
|
||||||
tr.read = append(tr.read, owner+"/"+name)
|
|
||||||
if tr.silent {
|
|
||||||
return nil, errors.New("the forge did not answer")
|
|
||||||
}
|
|
||||||
if page > 1 || owner+"/"+name != "novox/"+repo {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
var newest []trunkCommit
|
|
||||||
for i := len(tr.merges) - 1; i >= 0; i-- {
|
|
||||||
m := tr.merges[i]
|
|
||||||
newest = append(newest, trunkCommit{Commit: m.Commit, SecondParent: m.Head, Subject: m.Subject, At: m.At})
|
|
||||||
}
|
|
||||||
return newest, nil
|
|
||||||
}
|
|
||||||
readMergeEvent = func(_ context.Context, owner, name, branch string, tm trunkMerge) (link.SourceMoved, error) {
|
|
||||||
tr.events++
|
|
||||||
m := catalogueMerge(tm.Commit, tr.module[tm.Commit], tm.At)
|
|
||||||
m.Owner, m.Repo, m.Base, m.HeadSHA = owner, name, branch, tm.Head
|
|
||||||
_, m.Number, _ = pullOfSubject(tm.Subject)
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
findMergeWarrant = func(_ context.Context, _ catalogueReader, _, _ string, number int, head string) (string, bool, string, error) {
|
|
||||||
for _, m := range tr.merges {
|
|
||||||
if strings.Contains(m.Subject, fmt.Sprintf("(#%d)", number)) && m.Head == head && tr.approved[m.Commit] {
|
|
||||||
return "the operator chose Approve (ask merge-" + fmt.Sprint(number) + ")", true, "", nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false, "the router holds no chosen merge ask for the pull request and head", nil
|
|
||||||
}
|
|
||||||
wasBeside := catchUpBeside
|
|
||||||
catchUpBeside = func(run func()) { run() }
|
|
||||||
t.Cleanup(func() {
|
|
||||||
trunkCommitsPage, readMergeEvent, findMergeWarrant, catchUpBeside = was, wasEvent, wasWarrant, wasBeside
|
|
||||||
settledMerges.Clear()
|
|
||||||
})
|
|
||||||
return tr
|
|
||||||
}
|
|
||||||
|
|
||||||
// aKeptMergeBefore is a merge the controller heard before the merges of a test: where its record begins, so what a trunk holds from
|
|
||||||
// then on can be a gap (novox/hq ADR 0463 §2).
|
|
||||||
func aKeptMergeBefore(t *testing.T, open *stores) {
|
|
||||||
t.Helper()
|
|
||||||
hear(t, open, catalogueMerge("mesh-catalog-seed", "app", t0.Add(-time.Hour)), t0.Add(-time.Hour))
|
|
||||||
}
|
|
||||||
|
|
||||||
func mergeKnown(t *testing.T, open *stores, commit string) bool {
|
|
||||||
t.Helper()
|
|
||||||
_, known, err := open.inventory.MergeOf(t.Context(), "novox/mesh-catalog", commit)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return known
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clause 1: the trunk holds merges 1, 2 and 3 and the controller holds records of 1 and 3 only; at its start it plans 2, said
|
|
||||||
// as caught up; a second start plans nothing new.
|
|
||||||
func TestAGapBeforeANewerHeardMergeIsPlannedAtTheControllersStart(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "app", "notes", "app")
|
|
||||||
hear(t, open, catalogueMerge("mesh-catalog-c01", "app", t0), t0.Add(time.Second))
|
|
||||||
hear(t, open, catalogueMerge("mesh-catalog-c03", "app", t0.Add(2*time.Minute)), t0.Add(2*time.Minute+time.Second))
|
|
||||||
if mergeKnown(t, open, "mesh-catalog-c02") {
|
|
||||||
t.Fatal("the merge to be caught up is known before the catch-up")
|
|
||||||
}
|
|
||||||
if (following{open: open}).catchUp(t.Context(), nil) {
|
|
||||||
t.Fatal("the catch-up says a trunk was left for later")
|
|
||||||
}
|
|
||||||
if !mergeKnown(t, open, "mesh-catalog-c02") {
|
|
||||||
t.Fatal("the merge the controller did not hear was not planned")
|
|
||||||
}
|
|
||||||
kept, _, _ := open.inventory.MergeOf(t.Context(), "novox/mesh-catalog", "mesh-catalog-c02")
|
|
||||||
if said := announcedOf(kept).Title; !strings.Contains(said, "caught up: merged ") || !strings.Contains(said, "not heard, planned ") {
|
|
||||||
t.Fatalf("the merge does not say it was caught up: %q", said)
|
|
||||||
}
|
|
||||||
cutAt(t, open, t0.Add(10*time.Minute))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) == 0 {
|
|
||||||
t.Fatal("no walk answers the caught-up merge")
|
|
||||||
}
|
|
||||||
if _, in := ws[0].Modules["notes"]; !in {
|
|
||||||
t.Fatalf("the walk does not build notes, which the caught-up merge moved: %v", ws[0].Modules)
|
|
||||||
}
|
|
||||||
before := tr.events
|
|
||||||
if (following{open: open}).catchUp(t.Context(), nil) || tr.events != before {
|
|
||||||
t.Fatalf("a second start read %d merge(s) in full again", tr.events-before)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clause 1: a heard merge of a repository whose trunk holds earlier merges the controller has no record of plans them all, the
|
|
||||||
// earlier first, and the forge is read for that repository's trunk only.
|
|
||||||
func TestAHeardMergeCatchesUpTheEarlierOnesOfItsOwnTrunkOnly(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "app", "notes", "app")
|
|
||||||
var order []string
|
|
||||||
inner := readMergeEvent
|
|
||||||
readMergeEvent = func(ctx context.Context, owner, name, branch string, tm trunkMerge) (link.SourceMoved, error) {
|
|
||||||
order = append(order, tm.Commit)
|
|
||||||
return inner(ctx, owner, name, branch, tm)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { readMergeEvent = inner })
|
|
||||||
tr.read = nil
|
|
||||||
heard := catalogueMerge("mesh-catalog-c03", "app", t0.Add(2*time.Minute))
|
|
||||||
if err := (following{open: open}).SourceMoved(t.Context(), heard); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, c := range []string{"mesh-catalog-c01", "mesh-catalog-c02", "mesh-catalog-c03"} {
|
|
||||||
if !mergeKnown(t, open, c) {
|
|
||||||
t.Fatalf("%s is not planned: the heard merge and the earlier ones of its trunk are all wanted", c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(order) != 2 || order[0] != "mesh-catalog-c01" || order[1] != "mesh-catalog-c02" {
|
|
||||||
t.Fatalf("the earlier merges were heard in the order %v, not the trunk's", order)
|
|
||||||
}
|
|
||||||
if len(tr.read) == 0 {
|
|
||||||
t.Fatal("the forge was not read for the trunk of the merge heard")
|
|
||||||
}
|
|
||||||
for _, r := range tr.read {
|
|
||||||
if r != "novox/mesh-catalog" {
|
|
||||||
t.Fatalf("the forge was read for %s, not only for the trunk of the merge heard: %v", r, tr.read)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clause 1: a trunk of fifty-one merges whose oldest is unheard plans nothing: the oldest is outside the window.
|
|
||||||
func TestAMergeOlderThanTheLastFiftyIsNotRead(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
modules := make([]string, 51)
|
|
||||||
for i := range modules {
|
|
||||||
modules[i] = "app"
|
|
||||||
}
|
|
||||||
aTrunkOf(t, "mesh-catalog", modules...)
|
|
||||||
// Fifty are known; the oldest is not.
|
|
||||||
for i := 2; i <= 51; i++ {
|
|
||||||
c := fmt.Sprintf("mesh-catalog-c%02d", i)
|
|
||||||
hear(t, open, catalogueMerge(c, "app", t0.Add(time.Duration(i)*time.Minute)), t0.Add(time.Duration(i)*time.Minute+time.Second))
|
|
||||||
}
|
|
||||||
// Paged as the forge pages: fifty a page, and a trunk of another repository is empty (a short page, or none).
|
|
||||||
pages := trunkCommitsPage
|
|
||||||
trunkCommitsPage = func(ctx context.Context, owner, name, branch string, page int) ([]trunkCommit, error) {
|
|
||||||
all, err := pages(ctx, owner, name, branch, 1)
|
|
||||||
if err != nil || page < 1 || (page-1)*50 >= len(all) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return all[(page-1)*50 : min(page*50, len(all))], nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { trunkCommitsPage = pages })
|
|
||||||
(following{open: open}).catchUp(t.Context(), nil)
|
|
||||||
if mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("the oldest of fifty-one merges was read and planned, outside the window")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clause 2: a gap with no chosen Approve in the router is not planned and raises one condition naming its repository and
|
|
||||||
// commit; a second start leaves that one condition open, observed twice; planning the gap clears it.
|
|
||||||
func TestAGapWithNoApproveRaisesOneConditionKeptOpenAndClearedWhenPlanned(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
aKeptMergeBefore(t, open)
|
|
||||||
keeper, _ := withConditionsInMemory(t)
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "notes")
|
|
||||||
tr.approved["mesh-catalog-c01"] = false
|
|
||||||
catch := func() { (following{open: open}).catchUp(t.Context(), nil) }
|
|
||||||
catch()
|
|
||||||
if mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("a merge with no Approve on record was planned")
|
|
||||||
}
|
|
||||||
raised := func() []conditions.Condition {
|
|
||||||
t.Helper()
|
|
||||||
all, err := keeper.Open(t.Context())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var out []conditions.Condition
|
|
||||||
for _, c := range all {
|
|
||||||
if c.Kind == kindMergeNotHeard {
|
|
||||||
out = append(out, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
one := raised()
|
|
||||||
if len(one) != 1 || !strings.Contains(one[0].Subject.ID, "novox/mesh-catalog") || !strings.Contains(one[0].Subject.ID, "mesh-cata") {
|
|
||||||
t.Fatalf("the gap raised %+v, not one condition naming its repository and commit", one)
|
|
||||||
}
|
|
||||||
catch()
|
|
||||||
if again := raised(); len(again) != 1 || again[0].Observations < 2 {
|
|
||||||
t.Fatalf("a second catch-up left %+v: one condition, observed twice, was wanted", again)
|
|
||||||
}
|
|
||||||
tr.approved["mesh-catalog-c01"] = true
|
|
||||||
catch()
|
|
||||||
if !mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("the gap was not planned once its Approve was on record")
|
|
||||||
}
|
|
||||||
if left := raised(); len(left) != 0 {
|
|
||||||
t.Fatalf("planning the gap left its condition open: %+v", left)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clause 3: a forge that does not answer leaves the catch-up undone and says so, and the next start does it.
|
|
||||||
func TestAForgeThatDoesNotAnswerLeavesTheCatchUpForTheNextStart(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
aKeptMergeBefore(t, open)
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "notes")
|
|
||||||
tr.silent = true
|
|
||||||
if later := (following{open: open}).catchUp(t.Context(), nil); !later {
|
|
||||||
t.Fatal("a forge that did not answer counted as nothing to catch up")
|
|
||||||
}
|
|
||||||
if mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("a merge was planned without the forge's word")
|
|
||||||
}
|
|
||||||
tr.silent = false
|
|
||||||
if later := (following{open: open}).catchUp(t.Context(), nil); later || !mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatalf("the next start did not catch up (left for later: %v)", later)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finding 2 of the review: a gap that adds a module is a gap, judged as hearMerge judges it. With the operator's Approve on record it
|
|
||||||
// is heard (hearMerge asks the new module in its walk); without, it raises the condition and is not lost in silence.
|
|
||||||
func TestAGapThatAddsAModuleIsHeardOnItsWarrantAndRaisedWithout(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
aKeptMergeBefore(t, open)
|
|
||||||
keeper, _ := withConditionsInMemory(t)
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "newmodule")
|
|
||||||
tr.approved["mesh-catalog-c01"] = false
|
|
||||||
catch := func() { (following{open: open}).catchUp(t.Context(), nil) }
|
|
||||||
catch()
|
|
||||||
if mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("a merge adding a module was heard with no Approve on record")
|
|
||||||
}
|
|
||||||
all, err := keeper.Open(t.Context())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var raised int
|
|
||||||
for _, c := range all {
|
|
||||||
if c.Kind == kindMergeNotHeard {
|
|
||||||
raised++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if raised != 1 {
|
|
||||||
t.Fatalf("a merge adding a module with no Approve raised %d condition(s), not one: it was lost in silence", raised)
|
|
||||||
}
|
|
||||||
tr.approved["mesh-catalog-c01"] = true
|
|
||||||
catch()
|
|
||||||
if !mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("a merge adding a module was not heard once its Approve was on record")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A trunk with merges from before the controller began keeping merges, and merges since: only the later ones are gaps.
|
|
||||||
func TestAMergeFromBeforeTheRecordBeganIsHistoryAndNoCondition(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
keeper, _ := withConditionsInMemory(t)
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "notes", "notes", "notes")
|
|
||||||
tr.approved["mesh-catalog-c01"], tr.approved["mesh-catalog-c02"], tr.approved["mesh-catalog-c03"] = false, false, false
|
|
||||||
// The record begins at the second merge: the first is two days older.
|
|
||||||
tr.merges[0].At = t0.Add(-48 * time.Hour)
|
|
||||||
hear(t, open, catalogueMerge("mesh-catalog-c02", "notes", t0.Add(time.Minute)), t0.Add(time.Minute))
|
|
||||||
delete(tr.approved, "mesh-catalog-c02")
|
|
||||||
(following{open: open}).catchUp(t.Context(), nil)
|
|
||||||
// The first merge (two days before the record) was not even read from the forge: only the third, which is unknown, was.
|
|
||||||
if tr.events != 1 {
|
|
||||||
t.Fatalf("%d merge(s) were read from the forge: the one from before the record is history and is not read", tr.events)
|
|
||||||
}
|
|
||||||
all, err := keeper.Open(t.Context())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var raised int
|
|
||||||
for _, c := range all {
|
|
||||||
if c.Kind == kindMergeNotHeard {
|
|
||||||
raised++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if raised != 1 {
|
|
||||||
t.Fatalf("%d condition(s) raised: only the merge since the record began (the third) is a gap", raised)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// novox/hq ADR 0463 §2: the record begins at the earliest merge kept on ANY trunk. A trunk the controller kept no merge of has the
|
|
||||||
// merges made since as gaps (mesh-delivery #1 on novox), and the merges made before as history.
|
|
||||||
func TestATrunkWithNoKeptMergeHasItsLaterMergesAsGapsAndItsEarlierOnesAsHistory(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asksRecorded(t)
|
|
||||||
// The record begins on another trunk, at t0 less an hour.
|
|
||||||
hear(t, open, repoMerge("one", "c-one", t0.Add(-time.Hour)), t0.Add(-time.Hour))
|
|
||||||
tr := aTrunkOf(t, "mesh-catalog", "notes", "notes")
|
|
||||||
tr.merges[0].At = t0.Add(-72 * time.Hour) // before the record: history
|
|
||||||
(following{open: open}).catchUp(t.Context(), nil)
|
|
||||||
if mergeKnown(t, open, "mesh-catalog-c01") {
|
|
||||||
t.Fatal("a merge from before the record began was heard")
|
|
||||||
}
|
|
||||||
if !mergeKnown(t, open, "mesh-catalog-c02") {
|
|
||||||
t.Fatal("a merge since the record began, on a trunk with no merge kept, was not caught up")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,346 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
||||||
sdkdelivery "git.novox.be/novox/mesh-sdk/go/delivery"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The pieces of the catch-up that need no store (novox/hq ADR 0457, issue 1336).
|
|
||||||
|
|
||||||
func TestAMergeCommitsSubjectNamesItsPullRequest(t *testing.T) {
|
|
||||||
title, number, ok := pullOfSubject("Merge pull request 'Fix keycloak's sign-in (issue 1332)' (#378) from fix/1332-sign-in into main")
|
|
||||||
if !ok || number != 378 || title != "Fix keycloak's sign-in (issue 1332)" {
|
|
||||||
t.Fatalf("read %q %d %v", title, number, ok)
|
|
||||||
}
|
|
||||||
if b := headBranchOfSubject("Merge pull request 'x' (#378) from fix/1332-sign-in into main"); b != "fix/1332-sign-in" {
|
|
||||||
t.Fatalf("the head branch read as %q", b)
|
|
||||||
}
|
|
||||||
if _, _, ok := pullOfSubject("Merge branch 'main' of somewhere"); ok {
|
|
||||||
t.Fatal("a merge that names no pull request read as one")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOnlyTheLastFiftyMergesOfATrunkAreRead(t *testing.T) {
|
|
||||||
pages := map[int][]trunkCommit{}
|
|
||||||
var all []trunkCommit // newest first: 51 merges, a plain commit between each pair
|
|
||||||
for i := 51; i >= 1; i-- {
|
|
||||||
all = append(all, trunkCommit{Commit: fmt.Sprintf("merge-%02d", i), SecondParent: fmt.Sprintf("head-%02d", i),
|
|
||||||
Subject: fmt.Sprintf("Merge pull request 'change %d' (#%d) from fix/x into main", i, i)},
|
|
||||||
trunkCommit{Commit: fmt.Sprintf("plain-%02d", i)})
|
|
||||||
}
|
|
||||||
for i := 0; i < len(all); i += 50 {
|
|
||||||
pages[i/50+1] = all[i:min(i+50, len(all))]
|
|
||||||
}
|
|
||||||
was := trunkCommitsPage
|
|
||||||
trunkCommitsPage = func(_ context.Context, _, _, _ string, page int) ([]trunkCommit, error) { return pages[page], nil }
|
|
||||||
t.Cleanup(func() { trunkCommitsPage = was })
|
|
||||||
got, err := readTrunkMerges(t.Context(), "novox", "mesh-catalog", "main")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(got) != catchUpWindow || got[0].Commit != "merge-02" || got[len(got)-1].Commit != "merge-51" {
|
|
||||||
t.Fatalf("read %d merges from %s to %s: the oldest of 51 is outside the window", len(got), got[0].Commit, got[len(got)-1].Commit)
|
|
||||||
}
|
|
||||||
if got[0].Head != "head-02" {
|
|
||||||
t.Fatalf("a merge's head is its second parent, not %q", got[0].Head)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAForgeThatDoesNotAnswerIsAnErrorNotAnEmptyTrunk(t *testing.T) {
|
|
||||||
was := trunkCommitsPage
|
|
||||||
trunkCommitsPage = func(context.Context, string, string, string, int) ([]trunkCommit, error) {
|
|
||||||
return nil, errors.New("the forge did not answer")
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { trunkCommitsPage = was })
|
|
||||||
if got, err := readTrunkMerges(t.Context(), "novox", "mesh-catalog", "main"); err == nil {
|
|
||||||
t.Fatalf("a forge that did not answer read as %d merges: nothing to catch up", len(got))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestACaughtUpMergeIsAcceptedOnTheSameWarrantAsAnAnnouncedOne(t *testing.T) {
|
|
||||||
ok := approvedMerge(t)
|
|
||||||
for _, asker := range []string{sdkdelivery.TellerModule, theDelivery} {
|
|
||||||
w := ok.warrant
|
|
||||||
w.Asker = asker
|
|
||||||
if _, err := caughtUpWarrant(asker, &ok.ask, &w); err != nil {
|
|
||||||
t.Errorf("the operator's Approve under %s was refused: %v", asker, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err := caughtUpWarrant("keycloak", &ok.ask, &ok.warrant); err == nil {
|
|
||||||
t.Error("a warrant of an asker the controller takes none from was accepted")
|
|
||||||
}
|
|
||||||
declined := ok.warrant
|
|
||||||
declined.Option, declined.Label = "decline", "Decline"
|
|
||||||
if _, err := caughtUpWarrant(theDelivery, &ok.ask, &declined); err == nil {
|
|
||||||
t.Error("a Decline was accepted as a warrant")
|
|
||||||
}
|
|
||||||
unproved := ok.warrant
|
|
||||||
by := *unproved.By
|
|
||||||
by.Verified = ""
|
|
||||||
unproved.By = &by
|
|
||||||
if _, err := caughtUpWarrant(theDelivery, &ok.ask, &unproved); err == nil {
|
|
||||||
t.Error("an answer whose channel proves nobody was accepted")
|
|
||||||
}
|
|
||||||
if _, err := caughtUpWarrant(theDelivery, nil, nil); err == nil {
|
|
||||||
t.Error("a router that holds nothing was accepted")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// aTellersAsk is the ask the delivery gate composes for a pull request, as the router holds it once the operator chose Approve
|
|
||||||
// (the gate's composeMergeAsk: its id from sdkdelivery.AskingID of the group and the heads, its Approve bound to mergeAct over the
|
|
||||||
// heads, the pull request and its head's first twelve digits in the details), with the warrant the router wrote.
|
|
||||||
func aTellersAsk(t *testing.T, group string, members ...sdkdelivery.Head) (asks.Ask, asks.Warrant) {
|
|
||||||
t.Helper()
|
|
||||||
key, err := sdkdelivery.MergeAskKey(group, members)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
id := sdkdelivery.AskingID(key, 1)
|
|
||||||
heads := map[string]string{}
|
|
||||||
var details []string
|
|
||||||
for _, m := range members {
|
|
||||||
owner, repo, _ := strings.Cut(m.Repository, "/")
|
|
||||||
heads[deliveryIDOf(owner, repo, m.Head)] = m.Head
|
|
||||||
details = append(details, fmt.Sprintf("%s#%d %q at %s: issues: none named; review: approved; reaches: no delivery plan yet", repo, m.Number, "title", m.Head[:12]))
|
|
||||||
}
|
|
||||||
bind := func(option string) string {
|
|
||||||
d, err := asks.ActDigest(mergeAct(id, option, heads))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
at := time.Date(2026, 10, 20, 12, 2, 1, 0, time.UTC)
|
|
||||||
ask := asks.Ask{ID: id, Headline: "Merge into main", Explanation: "passed its checks.", Who: asks.Operator, Expires: at.Add(24 * time.Hour),
|
|
||||||
OnExpiry: "nothing is merged; the ask is made again", Details: strings.Join(details, "\n"),
|
|
||||||
Options: []asks.Option{
|
|
||||||
{ID: optionApprove, Label: "Approve", Does: "merges", Level: asks.Approve, Binds: bind(optionApprove)},
|
|
||||||
{ID: "decline", Label: "Decline", Does: "nothing is merged", Level: asks.Approve, Binds: bind("decline")}}}
|
|
||||||
w := asks.Warrant{Ask: id, Asker: sdkdelivery.TellerModule, Outcome: asks.OutcomeChosen, Option: optionApprove, Label: "Approve",
|
|
||||||
Level: asks.Approve, By: &asks.Person{Who: asks.Operator, Kind: "telegram", Identity: "1", Verified: "user id"},
|
|
||||||
At: at.Add(time.Minute), AskDigest: ask.Digest()}
|
|
||||||
return ask, w
|
|
||||||
}
|
|
||||||
|
|
||||||
// withRouter hands findMergeWarrant these asks as the router's records, by asker.
|
|
||||||
func withRouter(t *testing.T, records map[string]map[string]struct {
|
|
||||||
ask asks.Ask
|
|
||||||
w asks.Warrant
|
|
||||||
}) {
|
|
||||||
t.Helper()
|
|
||||||
wasIDs, wasAsk := routerAskIDs, routerAskOf
|
|
||||||
closedRecords.Range(func(k, _ any) bool { closedRecords.Delete(k); return true })
|
|
||||||
routerAskIDs = func(_ context.Context, _ catalogueReader, asker string) ([]string, error) {
|
|
||||||
var ids []string
|
|
||||||
for id := range records[asker] {
|
|
||||||
ids = append(ids, id)
|
|
||||||
}
|
|
||||||
return ids, nil
|
|
||||||
}
|
|
||||||
routerAskOf = func(_ context.Context, _ catalogueReader, asker, id string) (*asks.Ask, *asks.Warrant, error) {
|
|
||||||
r, ok := records[asker][id]
|
|
||||||
if !ok {
|
|
||||||
return nil, nil, nil
|
|
||||||
}
|
|
||||||
return &r.ask, &r.w, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
routerAskIDs, routerAskOf = wasIDs, wasAsk
|
|
||||||
closedRecords.Range(func(k, _ any) bool { closedRecords.Delete(k); return true })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type aRouterRecord = struct {
|
|
||||||
ask asks.Ask
|
|
||||||
w asks.Warrant
|
|
||||||
}
|
|
||||||
|
|
||||||
// The teller's asks, as the router held them on 2026-10-20: the id is the router's own key, rebuilt here from the group and the
|
|
||||||
// head the gate composed it for (mesh-controller#405 at 8a330afd, mesh-catalog#381 at 1c702422, mesh-host#124 at 52600ec7). It names
|
|
||||||
// neither the pull request nor the head, so the warrant is found by the heads the Approve is bound to.
|
|
||||||
func TestATellersWarrantIsFoundByTheHeadItsApproveIsBoundTo(t *testing.T) {
|
|
||||||
real := []struct {
|
|
||||||
group, key, owner, repo string
|
|
||||||
number int
|
|
||||||
head string
|
|
||||||
}{
|
|
||||||
{"feat/r4b-sdk-users", "merge-feat_r4b_sdk_users-ba59b03220f8-989afe0a3bcf", "novox", "mesh-controller", 405, "8a330afd44ba852873339cf1210e90989dca46e1"},
|
|
||||||
{"fix/1351-remove-todays-delivery-module", "merge-fix_1351_remove_todays_delivery_module-64a353-d93b97b9697b", "novox", "mesh-catalog", 381, "1c7024224dd77c5b603299378883e0993b2e2fdd"},
|
|
||||||
{"feat/r4b-sdk-users", "merge-feat_r4b_sdk_users-6a59ba48da14-424e053ebc9c", "novox", "mesh-host", 124, "52600ec7bd61ee271a5cc7f4136bcf0a2a8f00fc"},
|
|
||||||
}
|
|
||||||
teller := map[string]aRouterRecord{}
|
|
||||||
for _, r := range real {
|
|
||||||
ask, w := aTellersAsk(t, r.group, sdkdelivery.Head{Repository: r.owner + "/" + r.repo, Number: r.number, Head: r.head})
|
|
||||||
if ask.ID != r.key {
|
|
||||||
t.Fatalf("the id of %s#%d is %q, and the router held it as %q: the fixture is not the router's", r.repo, r.number, ask.ID, r.key)
|
|
||||||
}
|
|
||||||
teller[ask.ID] = aRouterRecord{ask, w}
|
|
||||||
}
|
|
||||||
withRouter(t, map[string]map[string]aRouterRecord{sdkdelivery.TellerModule: teller})
|
|
||||||
for _, r := range real {
|
|
||||||
why, found, reason, err := findMergeWarrant(t.Context(), nil, r.owner, r.repo, r.number, r.head)
|
|
||||||
if err != nil || !found || !strings.Contains(why, r.key) || !strings.Contains(why, "bound to the head of this pull request") {
|
|
||||||
t.Errorf("%s#%d: the warrant was not found: %q %v %q %v", r.repo, r.number, why, found, reason, err)
|
|
||||||
}
|
|
||||||
// Another head of the same pull request is another merge: the Approve was not given for it.
|
|
||||||
if _, found, _, _ := findMergeWarrant(t.Context(), nil, r.owner, r.repo, r.number, "ffffffffffffffffffffffffffffffffffffffff"); found {
|
|
||||||
t.Errorf("%s#%d: another head found the warrant of this one", r.repo, r.number)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// One pull request's Approve is none for another repository's merge at the same head digits.
|
|
||||||
if _, found, _, _ := findMergeWarrant(t.Context(), nil, "novox", "mesh-tools", 405, real[0].head); found {
|
|
||||||
t.Error("a merge of another repository found mesh-controller#405's warrant")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A group's ask binds several heads, and the router's record carries only twelve digits of the other members': the pull request is
|
|
||||||
// found in the ask as it was published, which the warrant's digest covers.
|
|
||||||
func TestAGroupsWarrantIsFoundByTheAsksWordsForThePullRequestAndItsHead(t *testing.T) {
|
|
||||||
a := sdkdelivery.Head{Repository: "novox/mesh-host", Number: 124, Head: "52600ec7bd61ee271a5cc7f4136bcf0a2a8f00fc"}
|
|
||||||
b := sdkdelivery.Head{Repository: "novox/mesh-controller", Number: 405, Head: "8a330afd44ba852873339cf1210e90989dca46e1"}
|
|
||||||
ask, w := aTellersAsk(t, "feat/r4b-sdk-users", a, b)
|
|
||||||
withRouter(t, map[string]map[string]aRouterRecord{sdkdelivery.TellerModule: {ask.ID: {ask, w}}})
|
|
||||||
for _, h := range []sdkdelivery.Head{a, b} {
|
|
||||||
owner, repo, _ := strings.Cut(h.Repository, "/")
|
|
||||||
why, found, reason, err := findMergeWarrant(t.Context(), nil, owner, repo, h.Number, h.Head)
|
|
||||||
if err != nil || !found || !strings.Contains(why, "a group's ask naming this pull request at this head") {
|
|
||||||
t.Errorf("%s#%d: not found in the group's ask: %q %v %q %v", repo, h.Number, why, found, reason, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, found, _, _ := findMergeWarrant(t.Context(), nil, "novox", "mesh-catalog", 381, a.Head); found {
|
|
||||||
t.Error("a pull request the group's ask does not name found its warrant")
|
|
||||||
}
|
|
||||||
// A Decline is no warrant.
|
|
||||||
declined := w
|
|
||||||
declined.Option, declined.Label = "decline", "Decline"
|
|
||||||
withRouter(t, map[string]map[string]aRouterRecord{sdkdelivery.TellerModule: {ask.ID: {ask, declined}}})
|
|
||||||
if _, found, _, _ := findMergeWarrant(t.Context(), nil, "novox", "mesh-host", 124, a.Head); found {
|
|
||||||
t.Error("a Decline found a warrant")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keycloak's merge was asked as mesh-delivery.merge-378-b9104454f9a5-1, before release 3e: the id names the pull request and
|
|
||||||
// the first twelve digits of its head, and the warrant is found by them under mesh-delivery.
|
|
||||||
func TestAWarrantAskedUnderMeshDeliveryIsFoundByThePullRequestAndItsHead(t *testing.T) {
|
|
||||||
ok := approvedMerge(t)
|
|
||||||
head := "b9104454f9a5e0c4d3b2a1f0e9d8c7b6a5948372"
|
|
||||||
askID := "merge-378-b9104454f9a5-1"
|
|
||||||
ask, w := ok.ask, ok.warrant
|
|
||||||
ask.ID, w.Ask, w.Asker = askID, askID, theDelivery
|
|
||||||
w.AskDigest = ask.Digest()
|
|
||||||
withRouter(t, map[string]map[string]aRouterRecord{theDelivery: {
|
|
||||||
"merge-377-aaaaaaaaaaaa-1": {},
|
|
||||||
askID: {ask, w},
|
|
||||||
}})
|
|
||||||
why, found, reason, err := findMergeWarrant(t.Context(), nil, "novox", "mesh-catalog", 378, head)
|
|
||||||
if err != nil || !found || !strings.Contains(why, askID) {
|
|
||||||
t.Fatalf("the warrant was not found: %q %v %q %v", why, found, reason, err)
|
|
||||||
}
|
|
||||||
// Another head names another ask, and none is found.
|
|
||||||
if _, found, _, _ := findMergeWarrant(t.Context(), nil, "novox", "mesh-catalog", 378, "ffffffffffff0000"); found {
|
|
||||||
t.Fatal("another head found a warrant")
|
|
||||||
}
|
|
||||||
// A router that cannot be read is an error, never "no warrant".
|
|
||||||
routerAskIDs = func(context.Context, catalogueReader, string) ([]string, error) { return nil, errors.New("no answer") }
|
|
||||||
if _, found, _, err := findMergeWarrant(t.Context(), nil, "novox", "mesh-catalog", 378, head); found || err == nil {
|
|
||||||
t.Fatalf("an unreadable router read as no warrant: %v %v", found, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A trunk shorter than a page, or with none, is read without a fault: the pages end when the forge's do.
|
|
||||||
func TestAShortTrunkIsReadWholeAndAnEmptyOneIsNoMerges(t *testing.T) {
|
|
||||||
was := trunkCommitsPage
|
|
||||||
t.Cleanup(func() { trunkCommitsPage = was })
|
|
||||||
trunkCommitsPage = func(_ context.Context, _, _, _ string, page int) ([]trunkCommit, error) {
|
|
||||||
if page > 1 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return []trunkCommit{{Commit: "m2", SecondParent: "h2", Subject: "Merge pull request 'two' (#2) from fix/b into main"},
|
|
||||||
{Commit: "p"}, {Commit: "m1", SecondParent: "h1", Subject: "Merge pull request 'one' (#1) from fix/a into main"}}, nil
|
|
||||||
}
|
|
||||||
got, err := readTrunkMerges(t.Context(), "novox", "x", "main")
|
|
||||||
if err != nil || len(got) != 2 || got[0].Commit != "m1" || got[1].Commit != "m2" {
|
|
||||||
t.Fatalf("a short trunk read as %+v %v", got, err)
|
|
||||||
}
|
|
||||||
trunkCommitsPage = func(context.Context, string, string, string, int) ([]trunkCommit, error) { return nil, nil }
|
|
||||||
if got, err := readTrunkMerges(t.Context(), "novox", "x", "main"); err != nil || len(got) != 0 {
|
|
||||||
t.Fatalf("an empty trunk read as %+v %v", got, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ADR 0253: what the operator reads of a merge not heard is plain: no hash, no identifier, nothing to do "by hand".
|
|
||||||
func TestTheConditionOfAMergeNotHeardIsInPlainWords(t *testing.T) {
|
|
||||||
keeper, _ := withConditionsInMemory(t)
|
|
||||||
tm := trunkMerge{Commit: "c0880c4d1f2e3a4b5c6d7e8f9a0b1c2d3e4f5a6b", Head: "b9104454f9a5e0c4d3b2a1f0e9d8c7b6a5948372"}
|
|
||||||
(following{}).raiseMergeNotHeard(t.Context(), "novox/mesh-catalog", "main", tm, link.SourceMoved{Number: 378},
|
|
||||||
"the router holds no chosen merge ask of mesh-delivery-teller or mesh-delivery that covers the head b9104454f9a5")
|
|
||||||
open, err := keeper.Open(t.Context())
|
|
||||||
if err != nil || len(open) != 1 {
|
|
||||||
t.Fatalf("opened %d conditions, %v", len(open), err)
|
|
||||||
}
|
|
||||||
c := open[0]
|
|
||||||
if why, ok := conditions.PlainWords(conditions.Words{Headline: c.Headline, Explanation: c.Explanation, Needs: c.Needs,
|
|
||||||
Resolved: c.Resolved}); !ok {
|
|
||||||
t.Fatalf("the condition's words are not plain: %s\n%q\n%q\n%q", why, c.Headline, c.Explanation, c.Needs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The trunk's history lists the merges made inside a pull request's branch too. On novox (2026-10-20) 15 of the 15 non-pull-request
|
|
||||||
// "gaps" of mesh-catalog and mesh-controller were such merges; only a pull request's merge into this trunk is a merge to catch up.
|
|
||||||
func TestAMergeMadeInsideABranchIsNoMergeOfTheTrunk(t *testing.T) {
|
|
||||||
was := trunkCommitsPage
|
|
||||||
t.Cleanup(func() { trunkCommitsPage = was })
|
|
||||||
trunkCommitsPage = func(_ context.Context, _, _, _ string, page int) ([]trunkCommit, error) {
|
|
||||||
if page > 1 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return []trunkCommit{
|
|
||||||
{Commit: "c0880c4d", SecondParent: "b9104454", Subject: "Merge pull request 'Read the grant file' (#378) from fix/1332 into main"},
|
|
||||||
{Commit: "6394a62a", SecondParent: "x1", Subject: "Merge remote-tracking branch 'origin/main' into fix/1081-withdraw"},
|
|
||||||
{Commit: "896f1d28", SecondParent: "x2", Subject: "Merge feat/agents-write-git-ssh into feat/agents-write-git-ssh-f (issue 955 F)"},
|
|
||||||
{Commit: "4d03755c", SecondParent: "x3", Subject: "Bring the branch up to main after #354 (issue 1047)"},
|
|
||||||
{Commit: "other", SecondParent: "x4", Subject: "Merge pull request 'Elsewhere' (#5) from fix/y into nox-mesh"},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
got, err := readTrunkMerges(t.Context(), "novox", "mesh-catalog", "main")
|
|
||||||
if err != nil || len(got) != 1 || got[0].Commit != "c0880c4d" {
|
|
||||||
t.Fatalf("read %+v %v: only keycloak's pull request merge into main is a merge to catch up", got, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// novox/hq ADR 0463 §2: the record of merges begins at the earliest merge the controller holds on any trunk. A merge before it is
|
|
||||||
// history on every trunk; a trunk with no merge kept of its own still has the merges made since as gaps (mesh-delivery #1 on novox).
|
|
||||||
func TestOnlyTheMergesSinceTheRecordBeganAreGapsOnEveryTrunk(t *testing.T) {
|
|
||||||
first := time.Date(2026, 10, 17, 12, 0, 0, 0, time.UTC) // the first merge it heard, on some trunk of the mesh
|
|
||||||
trunk := func(prefix string) []trunkMerge {
|
|
||||||
return []trunkMerge{
|
|
||||||
{Commit: prefix + "-old-1", At: first.Add(-30 * 24 * time.Hour)},
|
|
||||||
{Commit: prefix + "-old-2", At: first.Add(-2 * time.Hour)},
|
|
||||||
{Commit: prefix + "-within-grace", At: first.Add(-mergeGrace / 2)},
|
|
||||||
{Commit: prefix + "-since", At: first.Add(24 * time.Hour)},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Two trunks, one of which the controller has kept no merge of: both read by the one starting point.
|
|
||||||
for _, prefix := range []string{"kept-trunk", "trunk-with-no-kept-merge"} {
|
|
||||||
var commits []string
|
|
||||||
for _, m := range sinceTheRecord(trunk(prefix), true, first) {
|
|
||||||
commits = append(commits, m.Commit)
|
|
||||||
}
|
|
||||||
if want := prefix + "-within-grace " + prefix + "-since"; strings.Join(commits, " ") != want {
|
|
||||||
t.Errorf("%s: the gaps are %v, want %q: earlier ones are history on every trunk, later ones are gaps", prefix, commits, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if none := sinceTheRecord(trunk("x"), false, time.Time{}); len(none) != 0 {
|
|
||||||
t.Errorf("with no merge kept anywhere there are %d gap(s), but no record to have one in", len(none))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/chain"
|
|
||||||
"github.com/novox/mesh-controller/internal/conditions"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The chain on the bus (novox/hq ADR 0408, design 47 milestone 5): builds, rollouts and assignments are keys of the
|
|
||||||
// bus's key-value store, written by the controller that holds the lease, and the controller's store keeps none of them.
|
|
||||||
|
|
||||||
// chainHolds is whether this process may write the chain now: the lease's gate, the one every act passes.
|
|
||||||
func chainHolds(ctx context.Context) error {
|
|
||||||
_, err := theLease.epoch(ctx)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// openTheChain opens the chain's buckets on the bus: the serving controller's connection, or one dialled for a command.
|
|
||||||
func openTheChain(ctx context.Context) (*chain.Chain, error) {
|
|
||||||
js, err := aBus()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// Not closed: a borrowed connection closes nothing, and a dialled one lasts as long as the command.
|
|
||||||
return chain.OnTheBus(ctx, js.Conn(), chainHolds)
|
|
||||||
}
|
|
||||||
|
|
||||||
// attachTheChain gives the inventory of the serving controller the chain on its connection.
|
|
||||||
func attachTheChain(ctx context.Context, inv *inventory.Inventory, server *link.Server) error {
|
|
||||||
bus, ok := server.Bus().(link.OverNATS)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("the chain lives on the bus, and this control plane is not on it")
|
|
||||||
}
|
|
||||||
c, err := chain.OnTheBus(ctx, bus.Conn, chainHolds)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
inv.UseChain(c)
|
|
||||||
inv.RequireLayout()
|
|
||||||
// And the builds laid out under their modules, once, before they are read (issue 1121) — **before this returns**, so
|
|
||||||
// nothing the start goes on to read meets a chain not laid out yet: on 2026-10-18 the layout ran beside the start, a
|
|
||||||
// read of the start was refused, and the controller exited and was started again every five seconds. The layout of
|
|
||||||
// keys laid out before skips them, so this takes seconds; one that fails is tried again beside the serving, loudly.
|
|
||||||
if err := inv.LayBuildsOut(ctx, func(format string, args ...any) { fmt.Printf("chain: "+format+"\n", args...) }); err != nil {
|
|
||||||
fmt.Printf("chain: the builds are not laid out under their modules yet, and none is read until they are: %v\n", err)
|
|
||||||
go layBuildsOutSoon(ctx, inv)
|
|
||||||
}
|
|
||||||
// And the requests the move of delivery 1 found never handed over, asked again once (askAgain), once it is serving.
|
|
||||||
go askAgainSoon(ctx, inv)
|
|
||||||
// And what delivery 1 recorded of what each machine was sent, copied onto the keys of the assignments (CopySentBuilds).
|
|
||||||
go copySentBuildsSoon(ctx, inv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// askAgain asks once more for each build request the move found never handed over (novox/hq issue 981 note 7 Q5): the
|
|
||||||
// requests a merge could not ask for, which a person's hand `build` would otherwise have to repeat. **At most once**: a
|
|
||||||
// request is claimed before it is asked and settled after, so a crash between loses the ask, which is where it stood.
|
|
||||||
func askAgain(ctx context.Context, inv *inventory.Inventory) {
|
|
||||||
pending, err := inv.RequestsToAskAgain(ctx)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("chain: the requests never handed over cannot be read: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, r := range pending {
|
|
||||||
if claimed, err := inv.ClaimToAskAgain(ctx, r.ID); err != nil || !claimed {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("chain: %s cannot be claimed to be asked again: %v\n", r.ID, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
id, err := askABuild(ctx, buildSource{Repository: r.Repository, Seat: r.Seat}, r.Path, r.Ref)
|
|
||||||
if err != nil {
|
|
||||||
if serr := inv.AskedAgainAs(ctx, r.ID, "not asked again: "+err.Error()); serr != nil {
|
|
||||||
fmt.Printf("chain: %s was not asked again, and that could not be kept: %v\n", r.ID, serr)
|
|
||||||
}
|
|
||||||
fmt.Printf("chain: %s was never handed over and cannot be asked again: %v\n", r.ID, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
recordBuildRequest(ctx, inv, inventory.BuildRequest{ID: id, Repository: r.Repository, Seat: r.Seat, Path: r.Path,
|
|
||||||
Ref: r.Ref, Commit: r.Commit, For: r.For})
|
|
||||||
if err := inv.AskedAgainAs(ctx, r.ID, id); err != nil {
|
|
||||||
fmt.Printf("chain: %s was asked again as %s, and that could not be kept: %v\n", r.ID, id, err)
|
|
||||||
}
|
|
||||||
fmt.Printf("chain: %s, never handed over, is asked again as %s\n", r.ID, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// askAgainSoon waits for the controller to be serving, which an ask needs, and then asks again.
|
|
||||||
func askAgainSoon(ctx context.Context, inv *inventory.Inventory) {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
case <-time.After(time.Minute):
|
|
||||||
askAgain(ctx, inv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// kindSentBuildsNotCopied is the condition of the one-time copy of node.sent_builds onto the assignments' keys that has not
|
|
||||||
// succeeded: until it has, a module an assignment pulled in may be missing from what a machine is read to run.
|
|
||||||
const kindSentBuildsNotCopied = "sent-builds-not-copied"
|
|
||||||
|
|
||||||
// copySentBuildsSoon copies what the store recorded of what each machine was sent onto the keys of the assignments, as soon
|
|
||||||
// as this controller holds the lease (the chain refuses a write before), and tries again every half minute while it fails,
|
|
||||||
// each time it fails saying so as an urgent condition until it has worked. It is idempotent: CopySentBuilds overwrites no key
|
|
||||||
// that names a commit and empties each column it has copied.
|
|
||||||
func copySentBuildsSoon(ctx context.Context, inv *inventory.Inventory) {
|
|
||||||
failed := false
|
|
||||||
for {
|
|
||||||
_, err := inv.CopySentBuilds(ctx)
|
|
||||||
o := conditions.Observation{Scope: conditions.ScopeBuild, ID: kindSentBuildsNotCopied, Token: kindSentBuildsNotCopied,
|
|
||||||
Kind: kindSentBuildsNotCopied, Severity: conditions.Urgent, Resolver: conditions.ResolverOperator,
|
|
||||||
Source: "chain", Summary: "what each machine was sent is not copied onto its assignments: "}
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("chain: what the machines were sent is not copied onto the assignments: %v\n", err)
|
|
||||||
if keeper := conditionsFrom; keeper != nil && !failed {
|
|
||||||
o.Summary += err.Error()
|
|
||||||
if _, oerr := keeper.Observe(ctx, o); oerr == nil {
|
|
||||||
failed = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if keeper := conditionsFrom; keeper != nil && failed {
|
|
||||||
_, _ = keeper.Clear(ctx, o.Key(), "copied")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-time.After(chainMoveRetry):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// chainMoveRetry is how long the copy waits before it tries again.
|
|
||||||
const chainMoveRetry = 30 * time.Second
|
|
||||||
|
|
||||||
// kindBuildsNotLaidOut is the condition of the layout of the builds under their modules that has not succeeded: until it
|
|
||||||
// has, no build is read, and a send that needs one is refused with the reason.
|
|
||||||
const kindBuildsNotLaidOut = "builds-not-laid-out"
|
|
||||||
|
|
||||||
// layBuildsOutSoon lays the builds out under their modules as soon as this controller holds the lease (the chain refuses a
|
|
||||||
// write before), and tries again every half minute while it fails, loudly: each failure is printed, and the first raises an
|
|
||||||
// urgent condition until it has worked. It is idempotent (LayBuildsOut).
|
|
||||||
func layBuildsOutSoon(ctx context.Context, inv *inventory.Inventory) {
|
|
||||||
failed := false
|
|
||||||
for {
|
|
||||||
err := inv.LayBuildsOut(ctx, func(format string, args ...any) { fmt.Printf("chain: "+format+"\n", args...) })
|
|
||||||
o := conditions.Observation{Scope: conditions.ScopeBuild, ID: kindBuildsNotLaidOut, Token: kindBuildsNotLaidOut,
|
|
||||||
Kind: kindBuildsNotLaidOut, Severity: conditions.Urgent, Resolver: conditions.ResolverOperator,
|
|
||||||
Source: "chain", Summary: "the builds are not laid out under their modules, so none is read: "}
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("chain: the builds are not laid out under their modules, and none is read until they are: %v\n", err)
|
|
||||||
if keeper := conditionsFrom; keeper != nil && !failed {
|
|
||||||
o.Summary += err.Error()
|
|
||||||
if _, oerr := keeper.Observe(ctx, o); oerr == nil {
|
|
||||||
failed = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if keeper := conditionsFrom; keeper != nil && failed {
|
|
||||||
_, _ = keeper.Clear(ctx, o.Key(), "laid out")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-time.After(chainMoveRetry):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,245 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/chain"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/lease"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **Acceptance 10 of design 47: one read of an assignment names the commit, the build and the rollout that put it there,
|
|
||||||
// and one read of the rollout lists the assignments it changed** (novox/hq ADR 0408, issue 1095). It enters by the live
|
|
||||||
// trigger — a merge heard, its batch cut into a plan when the window closed, its build answered through the build seat's
|
|
||||||
// path, the plan advanced — and writes no key and no row by hand. Only the machine's end of the send is the test's: the send
|
|
||||||
// is recorded as the controller records one it made, and the gate's facts are the store's.
|
|
||||||
func TestOneReadOfAnAssignmentNamesItsCommitBuildAndRolloutAndOneReadOfTheRolloutListsItsAssignments(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asked := asksRecorded(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
if _, err := assign(ctx, open, "anchor", "notes"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var sent [][]string
|
|
||||||
wasSend := sendRollout
|
|
||||||
sendRollout = func(ctx context.Context, open *stores, names []string) ([]string, error) {
|
|
||||||
sent = append(sent, append([]string(nil), names...))
|
|
||||||
current, err := open.inventory.CurrentBuilds(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, node := range names {
|
|
||||||
digest := fmt.Sprintf("d-%s-%d", node, len(sent))
|
|
||||||
if err := open.inventory.RecordSent(ctx, nodeID(t, open, node), digest,
|
|
||||||
map[string]string{"notes": current["notes"].Commit}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return names, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { sendRollout = wasSend })
|
|
||||||
wasGather := gatherGateFacts
|
|
||||||
gatherGateFacts = func(ctx context.Context, open *stores, component string) (gateFacts, error) {
|
|
||||||
f := gateFacts{now: time.Now(), reports: map[string]inventory.Reported{}, engines: map[string]string{},
|
|
||||||
rolledBack: map[string][]lease.Rollback{}, served: map[string]served{}}
|
|
||||||
reports, err := open.inventory.LastReports(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return f, err
|
|
||||||
}
|
|
||||||
for _, r := range reports {
|
|
||||||
f.reports[r.Node] = r
|
|
||||||
}
|
|
||||||
return f, nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { gatherGateFacts = wasGather })
|
|
||||||
|
|
||||||
// A merge of notes is heard, and its window closes: the batch is cut into the walk.
|
|
||||||
hear(t, open, catalogueMerge("c-notes", "notes", t0), t0.Add(time.Second))
|
|
||||||
cutAt(t, open, t0.Add(time.Second+90*time.Second))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 {
|
|
||||||
t.Fatalf("%d walk(s) after the cut", len(ws))
|
|
||||||
}
|
|
||||||
rollout := ws[0].ID
|
|
||||||
|
|
||||||
// The walk asks its first tier (at the cut, or at its next step); the build seat answers, and the controller takes the
|
|
||||||
// outcome in as it does.
|
|
||||||
asking, err := inv.PlanByID(ctx, rollout)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for try := 0; try < 3 && (asking.Modules["notes"] == nil || asking.Modules["notes"].Build == ""); try++ {
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if asking, err = inv.PlanByID(ctx, rollout); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buildID := asking.Modules["notes"].Build
|
|
||||||
if buildID == "" || len(*asked) != 1 || !strings.HasSuffix((*asked)[0], "#"+buildID) {
|
|
||||||
t.Fatalf("the walk asked %v, and notes' build is %q", *asked, buildID)
|
|
||||||
}
|
|
||||||
digest := "sha256:" + strings.Repeat("a", 64)
|
|
||||||
result := outcome(buildID, "modules/notes", "c-notes", "notes", "")
|
|
||||||
result.ReuseKey = "rk-notes"
|
|
||||||
result.Made = []link.MadeArtifact{{Name: "notes", Kind: "image", Reference: "artifact-store://notes/notes@" + digest}}
|
|
||||||
if err := (builds{inv: inv, open: open}).Built(ctx, result); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The walk sends the tier to its first machine.
|
|
||||||
for try := 0; try < 3 && len(sent) == 0; try++ {
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
}
|
|
||||||
if !slices.Equal(slices.Concat(sent...), []string{"anchor"}) {
|
|
||||||
t.Fatalf("sent %v", sent)
|
|
||||||
}
|
|
||||||
|
|
||||||
// One read of the assignment.
|
|
||||||
on, err := inv.AssignmentsOn(ctx, "anchor")
|
|
||||||
a, ok := assignmentOf(on, "notes")
|
|
||||||
if err != nil || !ok {
|
|
||||||
t.Fatalf("the assignments of anchor: %+v %v", on, err)
|
|
||||||
}
|
|
||||||
// Beside it a module pulled in by the assignment, which no send of this walk put anywhere.
|
|
||||||
if wireguard, pulled := assignmentOf(on, "mesh-wireguard"); pulled && (wireguard.Build != "" || wireguard.Rollout != "") {
|
|
||||||
t.Fatalf("a module this rollout did not send names a build or a rollout: %+v", wireguard)
|
|
||||||
}
|
|
||||||
if a.Module != "notes" || a.Commit != "c-notes" || a.Build != buildID || a.Rollout != rollout || a.Artifact != digest ||
|
|
||||||
a.Since.IsZero() || a.Assigned.IsZero() || !a.Stands() {
|
|
||||||
t.Fatalf("the assignment names %+v, not commit c-notes, build %s, artifact %s, rollout %s", a, buildID, digest, rollout)
|
|
||||||
}
|
|
||||||
// One read of the rollout.
|
|
||||||
p, err := inv.PlanByID(ctx, rollout)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
wantChange := chain.Change{Node: "anchor", Module: "notes", After: chain.Ref{Build: buildID, Commit: "c-notes"}}
|
|
||||||
if !slices.Contains(p.Changed, wantChange) || !slices.Contains(p.Builds, buildID) || len(p.Steps) != 1 ||
|
|
||||||
p.Steps[0].Node != "anchor" || p.Steps[0].Sent == nil {
|
|
||||||
t.Fatalf("the rollout lists changed %+v, builds %v, steps %+v; want %+v", p.Changed, p.Builds, p.Steps, wantChange)
|
|
||||||
}
|
|
||||||
// And one read of the build names the same commit, with what it published and its reuse key.
|
|
||||||
built, found, err := inv.BuildByID(ctx, buildID)
|
|
||||||
if err != nil || !found || built.Commit != "c-notes" || built.Module != "notes" {
|
|
||||||
t.Fatalf("the build: %+v %v %v", built, found, err)
|
|
||||||
}
|
|
||||||
lately, err := inv.Builds(ctx, "notes", 1)
|
|
||||||
if err != nil || len(lately) != 1 || lately[0].ReuseKey != "rk-notes" || lately[0].Published == nil ||
|
|
||||||
lately[0].Published.Artifact != digest {
|
|
||||||
t.Fatalf("what the build published: %+v %v", lately, err)
|
|
||||||
}
|
|
||||||
// The verbs say the links.
|
|
||||||
if lines := changedLines(p); len(lines) != 2 || !strings.Contains(lines[1], "anchor/notes") ||
|
|
||||||
!strings.Contains(lines[1], buildID) {
|
|
||||||
t.Fatalf("`plans` says %v", lines)
|
|
||||||
}
|
|
||||||
if lines := runLines(on); len(lines) != 2 || !strings.Contains(lines[1], buildID) || !strings.Contains(lines[1], rollout) {
|
|
||||||
t.Fatalf("`node show` says %v", lines)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A machine sent again what it runs says nothing new, and a push by hand with no rollout fills what is missing and blanks
|
|
||||||
// nothing (issue 1095).
|
|
||||||
func TestASendOfWhatAMachineRunsAlreadyChangesNoLinkOfItsAssignment(t *testing.T) {
|
|
||||||
open := aMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
register(t, open, catalogue.Manifest{Module: "notes", Version: "1"})
|
|
||||||
if _, err := assign(ctx, open, "anchor", "notes"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := inv.RecordBuild(ctx, inventory.Build{ID: "build-1", Module: "notes", Commit: "c1", Repository: "novox/notes"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
node := nodeID(t, open, "anchor")
|
|
||||||
inRollout := inventory.WithRollout(ctx, "plan-1", map[string]inventory.RolloutModule{"notes": {Commit: "c1", Build: "build-1"}})
|
|
||||||
if err := inv.RecordSent(inRollout, node, "d1", map[string]string{"notes": "c1"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// Sent again by hand, with no rollout: the assignment still names the rollout that put the build there.
|
|
||||||
if err := inv.RecordSent(ctx, node, "d2", map[string]string{"notes": "c1"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
on, _ := inv.AssignmentsOn(ctx, "anchor")
|
|
||||||
if a, _ := assignmentOf(on, "notes"); a.Rollout != "plan-1" || a.Build != "build-1" || a.Commit != "c1" {
|
|
||||||
t.Fatalf("the assignment names %+v", on)
|
|
||||||
}
|
|
||||||
// A declaration sent by hand with no builds known makes what the machine runs unknown: a time on the key, no commit.
|
|
||||||
if err := inv.RecordSent(ctx, node, "d3", nil); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if on, _ = inv.AssignmentsOn(ctx, "anchor"); runOf(on, "notes").Commit != "" || runOf(on, "notes").SentUnknown.IsZero() {
|
|
||||||
t.Fatalf("a send of unknown builds left %+v", on)
|
|
||||||
}
|
|
||||||
// A module sent a commit no build is kept of is named by its commit alone.
|
|
||||||
if err := inv.RecordSent(ctx, node, "d4", map[string]string{"notes": "c2"}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if on, _ = inv.AssignmentsOn(ctx, "anchor"); runOf(on, "notes").Commit != "c2" || runOf(on, "notes").Build != "" ||
|
|
||||||
runOf(on, "notes").Rollout != "" {
|
|
||||||
t.Fatalf("the assignment after a push of c2 by hand names %+v", on)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// assignmentOf is one module's assignment among those of a machine: assigning a module assigns what it requires beside it.
|
|
||||||
func assignmentOf(on []chain.Assignment, module string) (chain.Assignment, bool) {
|
|
||||||
for _, a := range on {
|
|
||||||
if a.Module == module {
|
|
||||||
return a, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return chain.Assignment{}, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func runOf(on []chain.Assignment, module string) chain.Assignment {
|
|
||||||
a, _ := assignmentOf(on, module)
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
// **An assignment waiting for its build is one key of the assignments bucket** (ADR 0261, ADR 0408, issue 1094): entered
|
|
||||||
// through the `assign` verb for a module whose build is in flight, one read of `<node>.<module>` says state pending with the
|
|
||||||
// build it waits for, the build's outcome through the build seat's path makes the key stand with the wait carried as applied,
|
|
||||||
// and the table of pending assignments is read by nobody. No key is written by hand.
|
|
||||||
func TestAnAssignmentWaitingForItsBuildIsOneKeyOfTheAssignmentsBucket(t *testing.T) {
|
|
||||||
open := aCatalogueMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
asksWithPaths(t)
|
|
||||||
mergeAdding(t, open, "modules/sensors", "3da80a4b00aa")
|
|
||||||
if _, err := assign(ctx, open, "laptop", "sensors"); err != nil {
|
|
||||||
t.Fatalf("an assignment while its build runs was refused: %v", err)
|
|
||||||
}
|
|
||||||
waiting, found, err := inv.AssignmentValue(ctx, "laptop", "sensors")
|
|
||||||
if err != nil || !found || waiting.State != chain.StatePending || waiting.Stands() || !waiting.Assigned.IsZero() ||
|
|
||||||
waiting.Pending == nil || waiting.Pending.State != chain.PendingWaiting || waiting.Pending.Build != "b-modules/sensors" ||
|
|
||||||
!strings.HasPrefix(waiting.Pending.ID, "p-") {
|
|
||||||
t.Fatalf("one read of the key says %+v (found %v, %v)", waiting, found, err)
|
|
||||||
}
|
|
||||||
if got := assignedTo(t, open, "laptop"); slices.Contains(got, "sensors") {
|
|
||||||
t.Fatalf("a module only waiting is assigned: %v", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := (builds{inv: inv, open: open}).Built(ctx, outcome("b-modules/sensors", "modules/sensors", "3da80a4b00aa", "sensors", "")); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
made, found, err := inv.AssignmentValue(ctx, "laptop", "sensors")
|
|
||||||
if err != nil || !found || !made.Stands() || made.State != "" || made.Assigned.IsZero() || made.Pending == nil ||
|
|
||||||
made.Pending.State != chain.PendingApplied || made.Pending.ID != waiting.Pending.ID {
|
|
||||||
t.Fatalf("one read of the key after the build says %+v (found %v, %v)", made, found, err)
|
|
||||||
}
|
|
||||||
if !slices.Contains(assignedTo(t, open, "laptop"), "sensors") {
|
|
||||||
t.Fatal("the build registered sensors and it was not assigned")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"slices"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/chain"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The links a rollout makes (novox/hq ADR 0408 §1, design 47 acceptance 10, issue 1095): the send of a plan's tier puts a
|
|
||||||
// build on a machine; the assignment of the module there names the build, its commit and the rollout (written by the record of
|
|
||||||
// the send, inventory.RecordSent, from the context a send is made in), and the rollout lists the assignments it changed with the
|
|
||||||
// build before and after.
|
|
||||||
|
|
||||||
// rolloutModules is what a plan sends of each module: the commit and the build of it.
|
|
||||||
func rolloutModules(p *inventory.Plan) map[string]inventory.RolloutModule {
|
|
||||||
out := make(map[string]inventory.RolloutModule, len(p.Modules))
|
|
||||||
for m, s := range p.Modules {
|
|
||||||
if s != nil && s.Commit != "" {
|
|
||||||
out[m] = inventory.RolloutModule{Commit: s.Commit, Build: s.Build}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// sendingAs is the context a plan's send is made in: what is recorded of the send names this rollout.
|
|
||||||
func sendingAs(ctx context.Context, p *inventory.Plan) context.Context {
|
|
||||||
return inventory.WithRollout(ctx, p.ID, rolloutModules(p))
|
|
||||||
}
|
|
||||||
|
|
||||||
// ranBefore is what each module on a machine ran before a send, as the chain names it: its assignment's build and commit,
|
|
||||||
// else the commit the machine was last sent for it. Read before the send, which writes the new ones.
|
|
||||||
func ranBefore(ctx context.Context, inv *inventory.Inventory, node string) map[string]chain.Ref {
|
|
||||||
was := map[string]chain.Ref{}
|
|
||||||
if sent, known, err := inv.SentBuilds(ctx, node); err == nil && known {
|
|
||||||
for m, commit := range sent {
|
|
||||||
was[m] = chain.Ref{Commit: commit}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ran, err := inv.AssignmentRuns(ctx, node); err == nil {
|
|
||||||
for m, r := range ran {
|
|
||||||
was[m] = r
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return was
|
|
||||||
}
|
|
||||||
|
|
||||||
// noteChanges writes into a plan that a send to a machine changed the assignments of the modules it carried: the build and
|
|
||||||
// commit before and after, the builds the rollout carried, and the step taken. A change noted already for the machine and the
|
|
||||||
// module is replaced, so a send made again says it once.
|
|
||||||
func noteChanges(p *inventory.Plan, node string, modules []string, was map[string]chain.Ref, at time.Time) {
|
|
||||||
for _, m := range modules {
|
|
||||||
s := p.Modules[m]
|
|
||||||
if s == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
change := chain.Change{Node: node, Module: m, Before: was[m], After: chain.Ref{Build: s.Build, Commit: s.Commit}}
|
|
||||||
if at := slices.IndexFunc(p.Changed, func(c chain.Change) bool { return c.Node == node && c.Module == m }); at >= 0 {
|
|
||||||
change.Before = p.Changed[at].Before
|
|
||||||
p.Changed[at] = change
|
|
||||||
} else {
|
|
||||||
p.Changed = append(p.Changed, change)
|
|
||||||
}
|
|
||||||
if s.Build != "" && !slices.Contains(p.Builds, s.Build) {
|
|
||||||
p.Builds = append(p.Builds, s.Build)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !slices.ContainsFunc(p.Steps, func(s chain.Step) bool { return s.Tier == p.Tier && s.Node == node }) {
|
|
||||||
sent := at.UTC()
|
|
||||||
p.Steps = append(p.Steps, chain.Step{Tier: p.Tier, Node: node, Sent: &sent})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// refSaid is a build and the commit it was made from as a person reads them; "nothing" for a module that ran none.
|
|
||||||
func refSaid(r chain.Ref) string {
|
|
||||||
switch {
|
|
||||||
case r.Build == "" && r.Commit == "":
|
|
||||||
return "nothing"
|
|
||||||
case r.Build == "":
|
|
||||||
return "commit " + short(r.Commit)
|
|
||||||
}
|
|
||||||
return r.Build + " (" + short(r.Commit) + ")"
|
|
||||||
}
|
|
||||||
|
|
||||||
// changedLines say the assignments a rollout changed: the machine, the module, and the build before and after.
|
|
||||||
func changedLines(p inventory.Plan) []string {
|
|
||||||
if len(p.Changed) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
out := []string{" changed:"}
|
|
||||||
for _, c := range p.Changed {
|
|
||||||
out = append(out, fmt.Sprintf(" %s/%s %s → %s", c.Node, c.Module, refSaid(c.Before), refSaid(c.After)))
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// runLines say what each module assigned to a machine runs there, as its assignment names it: the build, the commit, the
|
|
||||||
// rollout that put it there and since when.
|
|
||||||
func runLines(assigned []chain.Assignment) []string {
|
|
||||||
var out []string
|
|
||||||
for _, a := range assigned {
|
|
||||||
if a.Build == "" && a.Commit == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
line := fmt.Sprintf(" %-22s %s", a.Module, refSaid(chain.Ref{Build: a.Build, Commit: a.Commit}))
|
|
||||||
if a.Rollout != "" {
|
|
||||||
line += ", sent by " + a.Rollout
|
|
||||||
}
|
|
||||||
if !a.Since.IsZero() {
|
|
||||||
line += " at " + a.Since.Local().Format("2006-01-02 15:04")
|
|
||||||
}
|
|
||||||
out = append(out, line)
|
|
||||||
}
|
|
||||||
if len(out) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return append([]string{" what they run, as the chain names it:"}, out...)
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/chain"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **A rollout lists the assignments it changed, each with the build before and after** (ADR 0408 §1, issue 1095): one
|
|
||||||
// change for a machine and a module however often the send is noted, the build before kept from the first, the builds the
|
|
||||||
// rollout carried once each, and one step for a machine and a tier.
|
|
||||||
func TestARolloutListsTheAssignmentsItChangedWithTheBuildBeforeAndAfter(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 18, 3, 0, 0, 0, time.UTC)
|
|
||||||
p := inventory.Plan{ID: "plan-1", Tier: 1, Modules: map[string]*inventory.PlanModule{
|
|
||||||
"notes": {Commit: "c2", Build: "b2"}, "wiki": {Commit: "c2w", Build: "b2w"}, "nothing": nil}}
|
|
||||||
was := map[string]chain.Ref{"notes": {Build: "b1", Commit: "c1"}, "wiki": {Commit: "c1w"}}
|
|
||||||
|
|
||||||
noteChanges(&p, "anchor", []string{"notes", "wiki", "nothing"}, was, at)
|
|
||||||
noteChanges(&p, "laptop", []string{"notes"}, nil, at)
|
|
||||||
// The send noted again, after what was sent had become what the machine runs: the first before stands.
|
|
||||||
noteChanges(&p, "anchor", []string{"notes"}, map[string]chain.Ref{"notes": {Build: "b2", Commit: "c2"}}, at.Add(time.Minute))
|
|
||||||
|
|
||||||
want := []chain.Change{
|
|
||||||
{Node: "anchor", Module: "notes", Before: chain.Ref{Build: "b1", Commit: "c1"}, After: chain.Ref{Build: "b2", Commit: "c2"}},
|
|
||||||
{Node: "anchor", Module: "wiki", Before: chain.Ref{Commit: "c1w"}, After: chain.Ref{Build: "b2w", Commit: "c2w"}},
|
|
||||||
{Node: "laptop", Module: "notes", After: chain.Ref{Build: "b2", Commit: "c2"}},
|
|
||||||
}
|
|
||||||
if len(p.Changed) != len(want) {
|
|
||||||
t.Fatalf("changed %+v", p.Changed)
|
|
||||||
}
|
|
||||||
for k := range want {
|
|
||||||
if p.Changed[k] != want[k] {
|
|
||||||
t.Errorf("change %d is %+v, want %+v", k, p.Changed[k], want[k])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(p.Builds) != 2 || p.Builds[0] != "b2" || p.Builds[1] != "b2w" {
|
|
||||||
t.Errorf("builds %v", p.Builds)
|
|
||||||
}
|
|
||||||
if len(p.Steps) != 2 || p.Steps[0].Node != "anchor" || p.Steps[0].Tier != 1 || p.Steps[0].Sent == nil || p.Steps[1].Node != "laptop" {
|
|
||||||
t.Errorf("steps %+v", p.Steps)
|
|
||||||
}
|
|
||||||
lines := changedLines(p)
|
|
||||||
if len(lines) != 4 || !strings.Contains(lines[1], "anchor/notes b1 (c1) → b2 (c2)") ||
|
|
||||||
!strings.Contains(lines[3], "laptop/notes nothing → b2 (c2)") {
|
|
||||||
t.Errorf("changed lines %q", lines)
|
|
||||||
}
|
|
||||||
if got := changedLines(inventory.Plan{}); got != nil {
|
|
||||||
t.Errorf("a plan that changed nothing says %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWhatAMachineRunsIsSaidAsItsAssignmentNamesIt(t *testing.T) {
|
|
||||||
at := time.Date(2026, 10, 18, 3, 0, 0, 0, time.UTC)
|
|
||||||
lines := runLines([]chain.Assignment{
|
|
||||||
{Node: "anchor", Module: "notes", Build: "b2", Commit: "c2c2c2c2c2c2", Rollout: "plan-1", Since: at},
|
|
||||||
{Node: "anchor", Module: "web", Commit: "c3"},
|
|
||||||
{Node: "anchor", Module: "never-sent"},
|
|
||||||
})
|
|
||||||
if len(lines) != 3 || !strings.Contains(lines[1], "notes") || !strings.Contains(lines[1], "b2 (c2c2c2c2)") ||
|
|
||||||
!strings.Contains(lines[1], "sent by plan-1") || !strings.Contains(lines[2], "web") || !strings.Contains(lines[2], "commit c3") {
|
|
||||||
t.Fatalf("lines %q", lines)
|
|
||||||
}
|
|
||||||
if runLines([]chain.Assignment{{Node: "a", Module: "m"}}) != nil {
|
|
||||||
t.Fatal("a module never sent is said to run something")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **Acceptance 12 of design 47: no table in the controller's store holds builds, rollouts or assignments** (novox/hq ADR
|
|
||||||
// 0408, issue 1111). A delivery is driven through the live trigger — a merge heard, its batch cut into a plan, the plan's
|
|
||||||
// build asked and answered, the assignment made — until a build, a rollout and an assignment exist; they are read from the
|
|
||||||
// chain, and then the store's own catalog is read: none of the tables of the chain is in it, nor the function that raised the generation from the assignment table; the table that stays, stays.
|
|
||||||
func TestNoTableOfTheControllersStoreHoldsBuildsRolloutsOrAssignments(t *testing.T) {
|
|
||||||
open := windowed(t)
|
|
||||||
everyHeadWaits(t)
|
|
||||||
asked := asksRecorded(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := open.inventory
|
|
||||||
if _, err := assign(ctx, open, "anchor", "notes"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
hear(t, open, catalogueMerge("c-notes", "notes", t0), t0.Add(time.Second))
|
|
||||||
cutAt(t, open, t0.Add(91*time.Second))
|
|
||||||
ws, _ := walks(t, open)
|
|
||||||
if len(ws) != 1 {
|
|
||||||
t.Fatalf("%d walk(s) after the cut", len(ws))
|
|
||||||
}
|
|
||||||
rollout := ws[0].ID
|
|
||||||
asking, err := inv.PlanByID(ctx, rollout)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for try := 0; try < 3 && (asking.Modules["notes"] == nil || asking.Modules["notes"].Build == ""); try++ {
|
|
||||||
advancePlans(ctx, open)
|
|
||||||
if asking, err = inv.PlanByID(ctx, rollout); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buildID := asking.Modules["notes"].Build
|
|
||||||
if buildID == "" || len(*asked) != 1 {
|
|
||||||
t.Fatalf("the walk asked %v, and notes' build is %q", *asked, buildID)
|
|
||||||
}
|
|
||||||
digest := "sha256:" + strings.Repeat("b", 64)
|
|
||||||
result := outcome(buildID, "modules/notes", "c-notes", "notes", "")
|
|
||||||
result.Made = []link.MadeArtifact{{Name: "notes", Kind: "image", Reference: "artifact-store://notes/notes@" + digest}}
|
|
||||||
if err := (builds{inv: inv, open: open}).Built(ctx, result); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// What the live path made is in the chain.
|
|
||||||
if built, found, err := inv.BuildByID(ctx, buildID); err != nil || !found || built.Commit != "c-notes" {
|
|
||||||
t.Fatalf("the build is not read from the chain: %+v %v %v", built, found, err)
|
|
||||||
}
|
|
||||||
if p, err := inv.PlanByID(ctx, rollout); err != nil || p.ID != rollout {
|
|
||||||
t.Fatalf("the rollout is not read from the chain: %+v %v", p, err)
|
|
||||||
}
|
|
||||||
if on, err := inv.AssignmentsOn(ctx, "anchor"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
} else if _, ok := assignmentOf(on, "notes"); !ok {
|
|
||||||
t.Fatalf("the assignment is not read from the chain: %+v", on)
|
|
||||||
}
|
|
||||||
|
|
||||||
// And the catalog of the store holds none of them.
|
|
||||||
have := map[string]bool{}
|
|
||||||
for _, row := range inv.QueryForTest(t, `select table_name::text from information_schema.tables where table_schema = 'public'`) {
|
|
||||||
have[strings.Trim(row[0], `"`)] = true
|
|
||||||
}
|
|
||||||
for _, table := range []string{"build", "build_request", "release_plan", "batched_merge", "assignment", "pending_assignment", "chain_moved"} {
|
|
||||||
if have[table] {
|
|
||||||
t.Errorf("the table %s is in the controller's store", table)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, table := range []string{"build_gate", "assignment_generation"} {
|
|
||||||
if !have[table] {
|
|
||||||
t.Errorf("the table %s, which stays, is gone", table)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if rows := inv.QueryForTest(t, `select count(*) from pg_proc where proname = 'raise_assignment_generation'`); rows[0][0] != "0" {
|
|
||||||
t.Errorf("the function raise_assignment_generation is in the store: %v", rows)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,221 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"path"
|
|
||||||
"slices"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The change plan (novox/hq ADR 0238): **one commit, one plan** — what a change does to the mesh, computed
|
|
||||||
// from its diffset (a repository, the branch it merges into, the commit at hand) by the planner, never by
|
|
||||||
// a mapping of its own. reachOfMerge answers what moves and what follows it; this adds where each module
|
|
||||||
// goes — the deploy plan, machine by machine in the build plan's order — and what is not an ordinary
|
|
||||||
// send. A pull request's check posts it with its verdict; the release a merge makes follows the same
|
|
||||||
// planner, so the two can be compared.
|
|
||||||
|
|
||||||
// policyOf is a module's upgrade policy, as the controller holds it; false when it cannot be read.
|
|
||||||
type policyOf func(module string) (inventory.Upgrade, bool)
|
|
||||||
|
|
||||||
// changePlanOf is the change plan of a reach: pure, so it is tested without a store.
|
|
||||||
func changePlanOf(repository, base, head string, r mergeReach, entries []inventory.Entry, policy policyOf) link.ChangePlan {
|
|
||||||
p := link.ChangePlan{Repository: repository, Base: base, Head: head, Moved: r.Moved(), Dependents: r.Dependents(),
|
|
||||||
New: r.Added, Unread: r.Unread, Tiers: r.Plan.Tiers}
|
|
||||||
byName := map[string]inventory.Entry{}
|
|
||||||
for _, e := range entries {
|
|
||||||
byName[e.Manifest.Module] = e
|
|
||||||
}
|
|
||||||
// The machine holding the bus, whose declaration carries every module's grants (novox/hq issue 490).
|
|
||||||
holder := ""
|
|
||||||
for _, e := range entries {
|
|
||||||
if e.Manifest.BusUsers != "" && e.Manifest.ClaimsSeat(catalogue.BrokerSeat) && len(e.On) > 0 {
|
|
||||||
holder = e.On[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var granting []string
|
|
||||||
machines := map[string]*link.MachinePlan{}
|
|
||||||
machine := func(name string) *link.MachinePlan {
|
|
||||||
if machines[name] == nil {
|
|
||||||
machines[name] = &link.MachinePlan{Machine: name}
|
|
||||||
}
|
|
||||||
return machines[name]
|
|
||||||
}
|
|
||||||
for _, tier := range r.Plan.Tiers {
|
|
||||||
for _, name := range tier {
|
|
||||||
e, held := byName[name]
|
|
||||||
if !held {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
u, known := policy(name)
|
|
||||||
waits := known && !u.RollOut
|
|
||||||
for _, on := range e.On {
|
|
||||||
if waits {
|
|
||||||
machine(on).Waits = append(machine(on).Waits, name)
|
|
||||||
} else {
|
|
||||||
machine(on).Receives = append(machine(on).Receives, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !waits && holder != "" && !(len(e.On) == 1 && e.On[0] == holder) && len(e.On) > 0 &&
|
|
||||||
!slices.Contains(granting, name) {
|
|
||||||
granting = append(granting, name)
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case (name == "nats" || catalogue.ProvidesBus(e.Manifest)) && len(e.On) > 0:
|
|
||||||
// Said before the merge (novox/hq issue 336): a new bus build holds every send to the bus's machine
|
|
||||||
// until a person takes the step, so merging it is a promise to take it.
|
|
||||||
p.Steps = append(p.Steps, fmt.Sprintf("%s: %s is never sent by an ordinary send, and until %s runs, "+
|
|
||||||
"nothing else is sent to %s either — unless the new build turns out the same as the one running there "+
|
|
||||||
"(issue 280)", busUpgradeNeeded, name, busUpgradeVerb, strings.Join(e.On, ", ")))
|
|
||||||
case name == "nats" || catalogue.ProvidesBus(e.Manifest):
|
|
||||||
p.Steps = append(p.Steps, "a planned bus step: the bus is upgraded by `bus upgrade`, never by an ordinary send")
|
|
||||||
case waits && len(e.On) > 0 && (u.From == catalogue.FromModule || u.From == catalogue.FromData):
|
|
||||||
// Judged per machine by the walk, against the build each was last sent (novox/hq ADR 0377).
|
|
||||||
p.Steps = append(p.Steps, fmt.Sprintf("%s waits for a person: its policy records (%s) — unless on a machine "+
|
|
||||||
"its move recreates none of its containers and moves none of its data, which the walk judges and then sends",
|
|
||||||
name, orNone(u.From)))
|
|
||||||
case waits && len(e.On) > 0:
|
|
||||||
p.Steps = append(p.Steps, fmt.Sprintf("%s waits for a person: its policy records (%s)", name,
|
|
||||||
orNone(u.From)))
|
|
||||||
}
|
|
||||||
if len(e.Manifest.Provides) > 0 && len(e.On) > 0 {
|
|
||||||
var offers []string
|
|
||||||
for _, o := range e.Manifest.Provides {
|
|
||||||
offers = append(offers, o.Name)
|
|
||||||
}
|
|
||||||
p.Steps = append(p.Steps, fmt.Sprintf("%s provides %s: its consumers are sent again after it",
|
|
||||||
name, strings.Join(offers, ", ")))
|
|
||||||
}
|
|
||||||
if e.Manifest.Data != nil && len(e.On) > 0 {
|
|
||||||
p.Steps = append(p.Steps, fmt.Sprintf("%s keeps data (ADR 0233): what it holds is backed up before it moves", name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(granting) > 0 {
|
|
||||||
// Said before the merge (novox/hq issue 490): what the walk does when the change alters the bus's
|
|
||||||
// user list. Whether it does is known only once the builds are registered, so it is said as a rule.
|
|
||||||
p.Steps = append(p.Steps, fmt.Sprintf("%s: if this changes what the bus's user list says (a new tool, "+
|
|
||||||
"subject or user), %s is sent that list alone, every build there kept, before %s is judged on its first "+
|
|
||||||
"machine", grantsStepWord, holder, strings.Join(granting, ", ")))
|
|
||||||
}
|
|
||||||
var names []string
|
|
||||||
for name := range machines {
|
|
||||||
names = append(names, name)
|
|
||||||
}
|
|
||||||
sort.Strings(names)
|
|
||||||
for _, name := range names {
|
|
||||||
p.Machines = append(p.Machines, *machines[name])
|
|
||||||
}
|
|
||||||
p.Summary = summaryOf(p)
|
|
||||||
p.Class = changeClassOf(p, entries)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// classNone is the class of a change that moves no module: nothing for a walk to move (novox/hq ADR 0313 §2).
|
|
||||||
const classNone = "none"
|
|
||||||
|
|
||||||
// changeClassOf is a change plan's class (novox/hq ADR 0313 §2): that of the slowest-budgeted module it moves,
|
|
||||||
// itself or as a dependent — core when one is on the controller's own path or holds the mesh's resolver, leaf
|
|
||||||
// otherwise — and none when it moves nothing. A module it adds a directory for counts by its own class: the
|
|
||||||
// catalogue names a module's directory by the module, so the directory's last element is its name. The graph holds
|
|
||||||
// no manifest of it yet, so a new module is core by its name alone (on the controller's own path), never by a
|
|
||||||
// resolver claim its manifest makes.
|
|
||||||
func changeClassOf(p link.ChangePlan, entries []inventory.Entry) string {
|
|
||||||
if len(p.Moved) == 0 && len(p.Dependents) == 0 && len(p.New) == 0 {
|
|
||||||
return classNone
|
|
||||||
}
|
|
||||||
modules := append(append([]string{}, p.Moved...), p.Dependents...)
|
|
||||||
for _, dir := range p.New {
|
|
||||||
if name := path.Base(strings.Trim(dir, "/")); name != "." && name != "/" && name != "" {
|
|
||||||
modules = append(modules, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return classOfModules(modules, entries)
|
|
||||||
}
|
|
||||||
|
|
||||||
// grantsStepWord names the grants step (novox/hq issue 490): the bus's user list sent alone to the machine
|
|
||||||
// holding the bus ahead of a walk's gate. Not the bus step, which replaces the bus itself.
|
|
||||||
const grantsStepWord = "grants step"
|
|
||||||
|
|
||||||
// busUpgradeNeeded is how a change plan says that merging it holds the bus's machine for a person's step.
|
|
||||||
const busUpgradeNeeded = "merging this needs a person's bus upgrade"
|
|
||||||
|
|
||||||
// summaryOf is a change plan in one line: what it builds, where it goes, and whether the bus moves.
|
|
||||||
func summaryOf(p link.ChangePlan) string {
|
|
||||||
if len(p.Moved) == 0 && len(p.New) == 0 {
|
|
||||||
return "builds nothing: the change touches no module of the mesh's graph"
|
|
||||||
}
|
|
||||||
var parts []string
|
|
||||||
what := strings.Join(p.Moved, ", ")
|
|
||||||
if len(p.Dependents) > 0 {
|
|
||||||
what += fmt.Sprintf(" (+%d dependent(s))", len(p.Dependents))
|
|
||||||
}
|
|
||||||
if len(p.New) > 0 {
|
|
||||||
if what != "" {
|
|
||||||
what += ", "
|
|
||||||
}
|
|
||||||
what += "new: " + strings.Join(p.New, ", ")
|
|
||||||
}
|
|
||||||
var to []string
|
|
||||||
for _, m := range p.Machines {
|
|
||||||
if len(m.Receives) > 0 {
|
|
||||||
to = append(to, m.Machine)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(to) > 0 {
|
|
||||||
parts = append(parts, "builds "+what+" → "+strings.Join(to, ", "))
|
|
||||||
} else {
|
|
||||||
parts = append(parts, "builds "+what+", sent nowhere")
|
|
||||||
}
|
|
||||||
bus := "no bus step"
|
|
||||||
for _, s := range p.Steps {
|
|
||||||
switch {
|
|
||||||
case strings.HasPrefix(s, busUpgradeNeeded):
|
|
||||||
bus = busUpgradeNeeded
|
|
||||||
case strings.HasPrefix(s, "a planned bus step") && bus == "no bus step":
|
|
||||||
bus = "a bus step"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
parts = append(parts, bus)
|
|
||||||
waiting := 0
|
|
||||||
for _, m := range p.Machines {
|
|
||||||
waiting += len(m.Waits)
|
|
||||||
}
|
|
||||||
if waiting > 0 {
|
|
||||||
parts = append(parts, fmt.Sprintf("%d wait(s) for a person", waiting))
|
|
||||||
}
|
|
||||||
return strings.Join(parts, "; ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// planText is a change plan as a person reads it, for the pull request's comment.
|
|
||||||
func planText(p link.ChangePlan) string {
|
|
||||||
var b strings.Builder
|
|
||||||
fmt.Fprintf(&b, "change plan of %s at %.8s into %s: %s\n", p.Repository, p.Head, p.Base, p.Summary)
|
|
||||||
for i, tier := range p.Tiers {
|
|
||||||
fmt.Fprintf(&b, " tier %d: %s\n", i, strings.Join(tier, ", "))
|
|
||||||
}
|
|
||||||
for _, m := range p.Machines {
|
|
||||||
line := " " + m.Machine + ": "
|
|
||||||
if len(m.Receives) > 0 {
|
|
||||||
line += "receives " + strings.Join(m.Receives, ", ")
|
|
||||||
}
|
|
||||||
if len(m.Waits) > 0 {
|
|
||||||
if len(m.Receives) > 0 {
|
|
||||||
line += "; "
|
|
||||||
}
|
|
||||||
line += "waits for a person: " + strings.Join(m.Waits, ", ")
|
|
||||||
}
|
|
||||||
b.WriteString(line + "\n")
|
|
||||||
}
|
|
||||||
for _, s := range p.Steps {
|
|
||||||
b.WriteString(" - " + s + "\n")
|
|
||||||
}
|
|
||||||
if len(p.Unread) > 0 {
|
|
||||||
fmt.Fprintf(&b, " read by no module's build: %s\n", strings.Join(p.Unread, ", "))
|
|
||||||
}
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// **One commit, one change plan** (novox/hq ADR 0238): the planner's reach, laid out machine by machine in
|
|
||||||
// the build plan's order, with what is not an ordinary send said — the bus step, a module that waits for a
|
|
||||||
// person, a provider whose consumers follow it.
|
|
||||||
func TestAChangePlanSaysWhatEachMachineReceives(t *testing.T) {
|
|
||||||
const catalogue_ = "http://forge.internal:20000/novox/mesh-catalog.git"
|
|
||||||
entry := func(name, path string, on ...string) inventory.Entry {
|
|
||||||
e := fromRepo(name, catalogue_, path)
|
|
||||||
e.Source.BuiltFrom = "old"
|
|
||||||
e.On = on
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
nats := entry("nats", "modules/nats", "anchor")
|
|
||||||
nats.Manifest.Provides = []catalogue.Offer{{Name: "mesh-bus"}}
|
|
||||||
gitea := entry("gitea", "modules/gitea", "anchor")
|
|
||||||
held := entry("photos", "modules/photos", "anchor", "laptop")
|
|
||||||
tools := fromRepo("node-tools", "http://forge.internal:20000/novox/mesh-tools.git", "node-tools")
|
|
||||||
tools.On = []string{"anchor", "laptop"}
|
|
||||||
entries := []inventory.Entry{nats, gitea, held, tools}
|
|
||||||
edges := []inventory.Edge{{From: "node-tools", To: "nats", Kind: inventory.EdgeStandsOn}}
|
|
||||||
policy := func(module string) (inventory.Upgrade, bool) {
|
|
||||||
if module == "photos" {
|
|
||||||
return inventory.Upgrade{RollOut: false, From: "a person"}, true
|
|
||||||
}
|
|
||||||
return inventory.Upgrade{RollOut: true}, true
|
|
||||||
}
|
|
||||||
plan := func(paths ...string) link.ChangePlan {
|
|
||||||
m := link.SourceMoved{Owner: "novox", Repo: "mesh-catalog", Base: "main", Commit: "head", Paths: paths}
|
|
||||||
return changePlanOf("novox/mesh-catalog", "main", "head", reachOfMerge(m, entries, nil, edges), entries, policy)
|
|
||||||
}
|
|
||||||
|
|
||||||
p := plan("modules/gitea/index.ts")
|
|
||||||
if p.Summary != "builds gitea → anchor; no bus step" {
|
|
||||||
t.Errorf("one module's change reads %q", p.Summary)
|
|
||||||
}
|
|
||||||
|
|
||||||
p = plan("modules/nats/Dockerfile", "modules/photos/x.js")
|
|
||||||
if !strings.Contains(p.Summary, "needs a person's bus upgrade") || !strings.Contains(p.Summary, "2 wait(s) for a person") ||
|
|
||||||
!strings.Contains(p.Summary, "(+1 dependent(s))") {
|
|
||||||
t.Errorf("the bus and a held module read %q", p.Summary)
|
|
||||||
}
|
|
||||||
got := map[string]string{}
|
|
||||||
for _, m := range p.Machines {
|
|
||||||
got[m.Machine] = strings.Join(m.Receives, ",") + "|" + strings.Join(m.Waits, ",")
|
|
||||||
}
|
|
||||||
// The bus first, what stands on it after: the build plan's order, per machine.
|
|
||||||
if got["anchor"] != "nats,node-tools|photos" || got["laptop"] != "node-tools|photos" {
|
|
||||||
t.Errorf("the deploy plan reads %v", got)
|
|
||||||
}
|
|
||||||
text := strings.Join(p.Steps, "\n")
|
|
||||||
// Said before the merge (novox/hq issue 336): merging it holds every send to the bus's machine until a
|
|
||||||
// person runs the bus's step, and the verb that does.
|
|
||||||
for _, want := range []string{"merging this needs a person's bus upgrade", "nothing else is sent to anchor",
|
|
||||||
"mesh_call mesh-controller.bus", "the same as the one running there", "photos waits for a person",
|
|
||||||
"nats provides mesh-bus"} {
|
|
||||||
if !strings.Contains(text, want) {
|
|
||||||
t.Errorf("the steps do not say %q:\n%s", want, text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
p = plan("merge-check.sh")
|
|
||||||
if !strings.HasPrefix(p.Summary, "builds nothing") || len(p.Machines) != 0 || strings.Join(p.Unread, ",") != "merge-check.sh" {
|
|
||||||
t.Errorf("a root file's plan reads %+v", p)
|
|
||||||
}
|
|
||||||
if !strings.Contains(planText(p), "read by no module's build: merge-check.sh") {
|
|
||||||
t.Errorf("the plan's text does not say why nothing is built:\n%s", planText(p))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,337 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// moduleCheck judges manifests where they are written, with no mesh (novox/hq ADR 0037, issue 148).
|
|
||||||
//
|
|
||||||
// **The same functions registration runs, and nothing the command line adds** (ADR 0035): the strict
|
|
||||||
// parse with every per-manifest problem, then the rules no single manifest can be judged against,
|
|
||||||
// over exactly the manifests given. Somebody describing their own application in their own
|
|
||||||
// repository runs this before pushing and finds out there, rather than when a running mesh refuses
|
|
||||||
// the registration or, later, when a machine applies something that resolved and should not have.
|
|
||||||
//
|
|
||||||
// **What it cannot know without a store, it says.** The mesh's own seat set is the store's (ADR
|
|
||||||
// 0122); this binary carries a compiled copy that the store overrides when loaded, so a claim on a
|
|
||||||
// mesh seat is judged fully only at registration. A seat another module declares is unknown unless
|
|
||||||
// that module's manifest is passed too. Both are printed as a note, not as a problem — a check that
|
|
||||||
// refused what it could not see would teach people to ignore it.
|
|
||||||
//
|
|
||||||
// **And every identity against every bound it meets** (novox/hq ADR 0225, issue 263): each module's
|
|
||||||
// identity, on a machine whose name is `longestMachine` characters, against the bound of every
|
|
||||||
// provision it wants that a manifest given here offers. An overflow is refused in the pull request
|
|
||||||
// that introduces it — a new requirement, a lowered bound, a longer slug — instead of on the
|
|
||||||
// provider's machine when a real machine's name first meets the module's.
|
|
||||||
// SomeManifestsVar, set by the merge gate, says the manifests given are only some of their repository's.
|
|
||||||
const SomeManifestsVar = "MESH_MODULE_CHECK_SOME"
|
|
||||||
|
|
||||||
func moduleCheck(paths []string, out io.Writer) error {
|
|
||||||
return moduleCheckFor(paths, catalogue.DefaultLongestMachine, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func moduleCheckFor(paths []string, longestMachine int, out io.Writer) error {
|
|
||||||
if len(paths) == 0 {
|
|
||||||
return errors.New("module check <manifest.json>... — one file per module; pass every " +
|
|
||||||
"manifest of a repository together so the rules between them are checked too")
|
|
||||||
}
|
|
||||||
shelf := catalogue.Shelf{}
|
|
||||||
faulted := map[string]bool{}
|
|
||||||
failed := 0
|
|
||||||
// The list of predefined keys at the root of the repository above each manifest (novox/hq ADR 0348 §7), read
|
|
||||||
// once per repository: a manifest naming a key is judged against the list its own tree holds.
|
|
||||||
lists := map[string]catalogue.PredefinedList{}
|
|
||||||
listOf := func(path string) (catalogue.PredefinedList, error) {
|
|
||||||
root := catalogue.RepositoryRoot(filepath.Dir(path))
|
|
||||||
if root == "" {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if list, read := lists[root]; read {
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
list, err := catalogue.ReadPredefined(root)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
lists[root] = list
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
for _, path := range paths {
|
|
||||||
raw, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(out, "%s: %v\n", path, err)
|
|
||||||
failed++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
list, err := listOf(path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(out, "%s: %v\n", path, err)
|
|
||||||
failed++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m, err := catalogue.ParseManifestWith(raw, list)
|
|
||||||
if err == nil {
|
|
||||||
// Each template's text read from the tree beside the manifest, and judged as inline content is
|
|
||||||
// (novox/hq ADR 0349 §2): a missing template is refused by name.
|
|
||||||
m, err = catalogue.WithTemplatesFrom(m, filepath.Dir(path))
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(out, "%s: %v\n", path, err)
|
|
||||||
failed++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if first, twice := shelf[m.Module]; twice {
|
|
||||||
_ = first
|
|
||||||
fmt.Fprintf(out, "%s: %s was already given; two manifests name one module\n", path, m.Module)
|
|
||||||
failed++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// A definition names no installation (novox/hq ADR 0112, ADR 0155): judged here, in the
|
|
||||||
// catalogue-wide test, and at registration, which refuses in the same words.
|
|
||||||
if wrong := catalogue.TrustProblems(m); len(wrong) > 0 {
|
|
||||||
for _, p := range wrong {
|
|
||||||
fmt.Fprintf(out, "%s: %s\n", path, p)
|
|
||||||
}
|
|
||||||
failed += len(wrong)
|
|
||||||
faulted[m.Module] = true
|
|
||||||
}
|
|
||||||
// A module that declares a setting names the keys of its mergeable files (novox/hq ADR 0326 §3,
|
|
||||||
// issue 574): judged where a definition is written, as at registration, never where a stored
|
|
||||||
// build is read again.
|
|
||||||
if wrong := catalogue.WrittenSettingsProblems(m); len(wrong) > 0 {
|
|
||||||
for _, p := range wrong {
|
|
||||||
fmt.Fprintf(out, "%s: %s\n", path, p)
|
|
||||||
}
|
|
||||||
failed += len(wrong)
|
|
||||||
faulted[m.Module] = true
|
|
||||||
}
|
|
||||||
// A module lives whole in one repository (novox/hq ADR 0420 §1, issue 1019): when the check is told the
|
|
||||||
// repository it runs for (MESH_CHECK_REPOSITORY, set by the build's check), an artifact building from
|
|
||||||
// another is refused, as the merge gate refuses it. A run by hand names no repository and judges none.
|
|
||||||
if repo := os.Getenv("MESH_CHECK_REPOSITORY"); repo != "" && m.Build != nil {
|
|
||||||
for _, a := range m.Build.Artifacts {
|
|
||||||
if a.Context != nil && a.Context.Repository != "" && !sameRepository(a.Context.Repository,
|
|
||||||
link.SourceMoved{Owner: ownerOf(repo), Repo: repoOf(repo)}) {
|
|
||||||
fmt.Fprintf(out, "%s: %s builds artifact %s from %s: a module lives whole in one repository "+
|
|
||||||
"(novox/hq ADR 0420 §1)\n", path, m.Module, a.Name, a.Context.Repository)
|
|
||||||
failed++
|
|
||||||
faulted[m.Module] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if named := catalogue.InstallationProblems(m); len(named) > 0 {
|
|
||||||
for _, p := range named {
|
|
||||||
fmt.Fprintf(out, "%s: %s\n", path, p)
|
|
||||||
}
|
|
||||||
failed += len(named)
|
|
||||||
faulted[m.Module] = true
|
|
||||||
}
|
|
||||||
shelf[m.Module] = m
|
|
||||||
}
|
|
||||||
|
|
||||||
// Between the manifests: a seat declared twice, a use of a seat nothing declares, a claim on
|
|
||||||
// a seat that does not exist. Run only over what parsed, because a problem inside one manifest
|
|
||||||
// has already been said and would be said again here in a worse form.
|
|
||||||
//
|
|
||||||
// **Over some of a repository's manifests, a seat none of them declares is a note** (novox/hq issue 364), as
|
|
||||||
// this command's own word says above: the merge gate passes only the manifests a change touches, and says so
|
|
||||||
// with SomeManifestsVar, so a module that uses or claims a seat another module declares (the operator
|
|
||||||
// channel's, a channel bench) was refused there for a manifest it was not given. Given every manifest — the
|
|
||||||
// catalogue's own check, and registration — it stays a refusal.
|
|
||||||
some := os.Getenv(SomeManifestsVar) != ""
|
|
||||||
problems := catalogue.CatalogueProblems(shelf)
|
|
||||||
sort.Strings(problems)
|
|
||||||
for _, p := range problems {
|
|
||||||
if some && catalogue.IsUndeclaredSeat(p) {
|
|
||||||
fmt.Fprintf(out, "note: %s among the manifests given; registration judges it against the whole catalogue, "+
|
|
||||||
"and passing the declaring module's manifest too judges it here\n", p)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Fprintln(out, p)
|
|
||||||
failed++
|
|
||||||
}
|
|
||||||
|
|
||||||
// Between the manifests too: an identity against the bounds of the provisions it wants, which
|
|
||||||
// only the provider's manifest states.
|
|
||||||
identities := catalogue.IdentityProblems(shelf, longestMachine)
|
|
||||||
sort.Strings(identities)
|
|
||||||
for _, p := range identities {
|
|
||||||
fmt.Fprintln(out, p)
|
|
||||||
}
|
|
||||||
failed += len(identities)
|
|
||||||
|
|
||||||
// And the data each module keeps (novox/hq ADR 0233): a provider that grants says what it keeps for
|
|
||||||
// its consumers, a directory a container writes is declared, and no backup line is written by hand.
|
|
||||||
data := catalogue.DataProblems(shelf)
|
|
||||||
sort.Strings(data)
|
|
||||||
for _, p := range data {
|
|
||||||
fmt.Fprintln(out, p)
|
|
||||||
}
|
|
||||||
failed += len(data)
|
|
||||||
|
|
||||||
var names []string
|
|
||||||
for name := range shelf {
|
|
||||||
names = append(names, name)
|
|
||||||
}
|
|
||||||
sort.Strings(names)
|
|
||||||
|
|
||||||
// **Every long-running resource says how it is ready** (novox/hq ADR 0240 rule 8): warned until the
|
|
||||||
// date, refused from it. The count is the catalogue's: its merge check keeps the number and lets a
|
|
||||||
// change lower it, never raise it.
|
|
||||||
undeclared := 0
|
|
||||||
required := !checkNow().Before(catalogue.HealthRequiredFrom)
|
|
||||||
for _, name := range names {
|
|
||||||
missing := catalogue.Undeclared(shelf[name])
|
|
||||||
undeclared += len(missing)
|
|
||||||
if len(missing) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if required {
|
|
||||||
for _, id := range missing {
|
|
||||||
fmt.Fprintf(out, "%s: %s stays up and does not say how it is ready: a long-running resource declares "+
|
|
||||||
"health since %s (novox/hq ADR 0240 rule 8)\n", name, id, catalogue.HealthRequiredFrom.Format("2006-01-02"))
|
|
||||||
}
|
|
||||||
failed += len(missing)
|
|
||||||
faulted[name] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// **A file that asks for a setting and does not say whether it is trusted counts as trusted** (novox/hq issue
|
|
||||||
// 339): listed and counted, never refused, so an author can opt out a file nothing trusts.
|
|
||||||
unsaid := 0
|
|
||||||
for _, name := range names {
|
|
||||||
unsaid += len(catalogue.UnsaidTrust(shelf[name]))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, name := range names {
|
|
||||||
m := shelf[name]
|
|
||||||
if faulted[name] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Fprintf(out, "%s: ok", name)
|
|
||||||
if n := len(m.Tools); n > 0 {
|
|
||||||
fmt.Fprintf(out, ", %d tool(s)", n)
|
|
||||||
}
|
|
||||||
if len(m.Invokes) > 0 {
|
|
||||||
fmt.Fprintf(out, ", invokes %s", joinInvokes(m.Invokes))
|
|
||||||
}
|
|
||||||
// The state it keeps and reads (novox/hq ADR 0201), so a reviewer sees what lands on the bus.
|
|
||||||
if len(m.State) > 0 {
|
|
||||||
kept := make([]string, 0, len(m.State))
|
|
||||||
for _, s := range m.State {
|
|
||||||
kept = append(kept, s.Name)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(out, ", keeps state %s", strings.Join(kept, ", "))
|
|
||||||
}
|
|
||||||
// And the logs it keeps, with their caps (novox/hq ADR 0297).
|
|
||||||
if len(m.Logs) > 0 {
|
|
||||||
kept := make([]string, 0, len(m.Logs))
|
|
||||||
for _, l := range m.Logs {
|
|
||||||
kept = append(kept, fmt.Sprintf("%s (%d MiB)", l.Name, l.Cap()))
|
|
||||||
}
|
|
||||||
fmt.Fprintf(out, ", keeps log %s", strings.Join(kept, ", "))
|
|
||||||
}
|
|
||||||
if len(m.Reads) > 0 {
|
|
||||||
fmt.Fprintf(out, ", reads %s", strings.Join(m.Reads, ", "))
|
|
||||||
}
|
|
||||||
// The data it keeps, by class, so a reviewer sees what the mesh will protect and how.
|
|
||||||
if items := m.DataItems(); len(items) > 0 {
|
|
||||||
kept := make([]string, 0, len(items))
|
|
||||||
for _, it := range items {
|
|
||||||
kept = append(kept, it.ID+" ("+it.Class+")")
|
|
||||||
}
|
|
||||||
fmt.Fprintf(out, ", keeps %s", strings.Join(kept, ", "))
|
|
||||||
}
|
|
||||||
// How what it runs is ready (ADR 0240): each declared check, and what is judged by liveness alone.
|
|
||||||
var checks []string
|
|
||||||
for _, r := range m.Resources {
|
|
||||||
if h, has, _ := catalogue.ReadHealth(r); has {
|
|
||||||
checks = append(checks, fmt.Sprintf("%v by %s", r["id"], catalogue.HealthWords(h)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// And its own runtime's, where it runs as its own account (novox/hq ADR 0366).
|
|
||||||
if own, has := catalogue.OwnRuntimeHealth(m); has {
|
|
||||||
if h, _, _ := catalogue.ReadHealth(own); h.Kind != "" {
|
|
||||||
checks = append(checks, fmt.Sprintf("%v by %s", own["id"], catalogue.HealthWords(h)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(checks) > 0 {
|
|
||||||
fmt.Fprintf(out, ", ready: %s", strings.Join(checks, "; "))
|
|
||||||
}
|
|
||||||
if missing := catalogue.UnsaidTrust(m); len(missing) > 0 {
|
|
||||||
fmt.Fprintf(out, "; WARNING: %s ask(s) for a setting and do(es) not say whether it is trusted, so it counts as "+
|
|
||||||
"trusted: set at the terminal alone; say %q false where nothing trusts it (novox/hq issue 339)",
|
|
||||||
strings.Join(missing, ", "), catalogue.TrustedField)
|
|
||||||
}
|
|
||||||
if missing := catalogue.Undeclared(m); len(missing) > 0 {
|
|
||||||
fmt.Fprintf(out, "; WARNING: %s stay(s) up and say(s) not how it is ready — judged by liveness alone, "+
|
|
||||||
"refused from %s (ADR 0240 rule 8)", strings.Join(missing, ", "), catalogue.HealthRequiredFrom.Format("2006-01-02"))
|
|
||||||
}
|
|
||||||
fmt.Fprintln(out)
|
|
||||||
}
|
|
||||||
// The count the catalogue keeps (ADR 0240 rule 8), in a line its merge check reads.
|
|
||||||
fmt.Fprintf(out, "%s %d\n", UndeclaredHealthLine, undeclared)
|
|
||||||
fmt.Fprintf(out, "%s %d\n", UnsaidTrustLine, unsaid)
|
|
||||||
if failed > 0 {
|
|
||||||
return fmt.Errorf("%d problem(s) in %d manifest(s)", failed, len(paths))
|
|
||||||
}
|
|
||||||
fmt.Fprintf(out, "%d manifest(s) checked. Judged against the seats this binary carries; a claim on "+
|
|
||||||
"one of the mesh's own seats is judged fully at registration, and a seat declared by a "+
|
|
||||||
"module not given here reads as unknown. Identities judged on a %d-character machine name, "+
|
|
||||||
"against the bounds of the providers given here\n", len(paths), longestMachine)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UndeclaredHealthLine starts the line `module check` says the count of long-running resources without
|
|
||||||
// `health` in, over the manifests given: the catalogue's merge check compares it with the number it keeps.
|
|
||||||
const UndeclaredHealthLine = "long-running resources without health:"
|
|
||||||
|
|
||||||
// UnsaidTrustLine starts the line `module check` says the count of files that ask for a setting and do not say
|
|
||||||
// whether it is trusted, and so count as trusted (novox/hq issue 339).
|
|
||||||
const UnsaidTrustLine = "files asking for a setting without saying whether it is trusted:"
|
|
||||||
|
|
||||||
// checkNow is the clock `module check` judges the date by; a test sets it.
|
|
||||||
var checkNow = time.Now
|
|
||||||
|
|
||||||
func joinInvokes(invokes []string) string {
|
|
||||||
if len(invokes) == 1 && invokes[0] == "*" {
|
|
||||||
return "every tool"
|
|
||||||
}
|
|
||||||
s := ""
|
|
||||||
for i, t := range invokes {
|
|
||||||
if i > 0 {
|
|
||||||
s += ", "
|
|
||||||
}
|
|
||||||
s += t
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// manifestsUnder lists every module.json below a directory, for `module check <dir>`.
|
|
||||||
func manifestsUnder(dir string) ([]string, error) {
|
|
||||||
var found []string
|
|
||||||
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if d.IsDir() && (d.Name() == "node_modules" || d.Name() == ".git" || d.Name() == "dist") {
|
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
if !d.IsDir() && d.Name() == "module.json" {
|
|
||||||
found = append(found, path)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
sort.Strings(found)
|
|
||||||
return found, err
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A pull request's check builds go when it closes (novox/hq ADR 0350 §5): the build seat keeps them in the artifact
|
|
||||||
// store's scratch namespace for the walk of its merge to take. **Let go of where the moment is heard**: the
|
|
||||||
// controller ends the walk answering a merge — after that walk took what it reuses — and the delivery's owner, which
|
|
||||||
// hears the forge say a pull request closed unmerged, asks the `forget-checks` verb; both let go through the store,
|
|
||||||
// as the controller's own collection does. The build seat's holders let go of anything a week old on their own,
|
|
||||||
// so a word lost here leaves a week's builds, never more.
|
|
||||||
|
|
||||||
// checkBuildsIn is the scratch namespace as the controller reaches the artifact store.
|
|
||||||
func checkBuildsIn(ctx context.Context, inv *inventory.Inventory) (builder.Scratch, error) {
|
|
||||||
shelf, err := inv.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
address, err := artifactStoreAddress(ctx, inv, shelf, "")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if address == "" {
|
|
||||||
return nil, errors.New("the artifact store is not on the mesh's network")
|
|
||||||
}
|
|
||||||
return builder.Registry{Address: address}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgetChecks lets go of a pull request's check builds and answers what it did.
|
|
||||||
var forgetChecks = func(ctx context.Context, inv *inventory.Inventory, pull builder.PullRef) (string, error) {
|
|
||||||
scratch, err := checkBuildsIn(ctx, inv)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
n, err := scratch.Forget(ctx, pull)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("%s: %d check(s)' builds let go of, and not all: %w", pull, n, err)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s: %d check(s)' builds let go of", pull, n), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgetChecksOfWalk lets go, on the save that ends a walk, of the check builds of every pull request whose merge
|
|
||||||
// it answers: the walk took what it reuses. Said, never fatal.
|
|
||||||
func forgetChecksOfWalk(ctx context.Context, inv *inventory.Inventory, p inventory.Plan) {
|
|
||||||
if p.WalkSaid != inventory.WalkEnded || p.Delivery == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, pull := range reuseAskOf(&p).Pulls {
|
|
||||||
said, err := forgetChecks(ctx, inv, builder.PullRef{Owner: pull.Owner, Repo: pull.Repo, Number: pull.Number})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "%s: its check builds stay until the week's sweep: %v\n", p.ID, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Printf("%s: %s\n", p.ID, said)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// forgetChecksCommand is `forget-checks <owner> <repo> <number>`: the verb the delivery's owner asks when the forge
|
|
||||||
// says a pull request closed unmerged (novox/hq ADR 0350 §5).
|
|
||||||
func forgetChecksCommand(ctx context.Context, args []string) error {
|
|
||||||
if len(args) != 3 {
|
|
||||||
return errors.New("forget-checks <owner> <repo> <number>")
|
|
||||||
}
|
|
||||||
number, err := strconv.Atoi(args[2])
|
|
||||||
if err != nil || number <= 0 {
|
|
||||||
return fmt.Errorf("%q is not a pull request's number", args[2])
|
|
||||||
}
|
|
||||||
open, err := openStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
said, err := forgetChecks(ctx, open.inventory, builder.PullRef{Owner: args[0], Repo: args[1], Number: number})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println(said)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
//go:build integration
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestOnlyAWalksAskTakesAChecksBuildAndADryRunNever(t *testing.T) {
|
|
||||||
p := &inventory.Plan{Delivery: &inventory.PlanDelivery{Merges: []inventory.PlanMerge{
|
|
||||||
{Repository: "novox/shop", Number: 7}, {Repository: "http://forge.invalid/novox/web.git", Number: 3},
|
|
||||||
{Repository: "novox/shop", Number: 7}, {Repository: "novox/shop"}}}}
|
|
||||||
asked := reuseAskOf(p)
|
|
||||||
if want := []link.PullRef{{Owner: "novox", Repo: "shop", Number: 7}, {Owner: "novox", Repo: "web", Number: 3}}; !reflect.DeepEqual(asked.Pulls, want) {
|
|
||||||
t.Fatalf("the walk names %+v", asked.Pulls)
|
|
||||||
}
|
|
||||||
walk := withReuse(t.Context(), asked)
|
|
||||||
if reuseAsked(walk, false) != asked {
|
|
||||||
t.Fatal("a walk's ask does not carry its leave")
|
|
||||||
}
|
|
||||||
if reuseAsked(walk, true) != nil {
|
|
||||||
t.Fatal("a dry run carries a leave to take a check's build")
|
|
||||||
}
|
|
||||||
if reuseAsked(t.Context(), false) != nil {
|
|
||||||
t.Fatal("an ask that is not a walk's carries a leave")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAReusedCheckBuildIsRegisteredByItsDigestAndGatedAsABuiltOne(t *testing.T) {
|
|
||||||
withTheGate(t) // the first-machine gate's own path (novox/hq ADR 0360 keeps it for earlier walks)
|
|
||||||
g := aGateMesh(t)
|
|
||||||
ctx := t.Context()
|
|
||||||
inv := g.open.inventory
|
|
||||||
gateEvery = 0
|
|
||||||
|
|
||||||
digest := "sha256:" + strings.Repeat("c", 64)
|
|
||||||
reused := func(id, reference string) link.BuildResult {
|
|
||||||
manifest := mustJSON(t, catalogue.Manifest{Module: "app", Version: "c3", Resources: []map[string]any{
|
|
||||||
{"id": "svc", "type": "container", "name": "app", "image": reference}}})
|
|
||||||
return link.BuildResult{ID: id, Repository: "novox/mesh-catalog", Path: "modules/app", Ref: "main", On: "anchor",
|
|
||||||
Commit: "c3", Manifest: manifest, Trunk: "main", OnTrunk: true, Branches: []string{"main"},
|
|
||||||
Made: []link.MadeArtifact{{Name: "server", Kind: "image", Reference: reference}},
|
|
||||||
Source: &link.SourceOnSeat{Seat: "git", Repository: "novox/mesh-catalog"},
|
|
||||||
Reused: "reused from the check of novox/mesh-catalog#9 at head abcdefabcdef (merged into trunk 123456123456): server " + digest,
|
|
||||||
ReusedFrom: &link.ReusedFrom{Pull: link.PullRef{Owner: "novox", Repo: "mesh-catalog", Number: 9},
|
|
||||||
Head: "abcdefabcdef", Trunk: "123456123456", Key: "r1:" + strings.Repeat("1", 64)}}
|
|
||||||
}
|
|
||||||
if _, _, err := takeIn(ctx, inv, reused(link.NewBuildID(time.Now()), "store.internal:5000/scratch/app/server@"+digest)); err == nil ||
|
|
||||||
!strings.Contains(err.Error(), "scratch") {
|
|
||||||
t.Fatalf("a build in the scratch namespace was registered: %v", err)
|
|
||||||
}
|
|
||||||
id := link.NewBuildID(time.Now().Add(time.Second))
|
|
||||||
result := reused(id, "store.internal:5000/app/server@"+digest)
|
|
||||||
if _, _, err := takeIn(ctx, inv, result); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
current, err := inv.CurrentBuilds(ctx)
|
|
||||||
if err != nil || current["app"].Commit != "c3" {
|
|
||||||
t.Fatalf("the reused build is not what app is registered at: %+v %v", current["app"], err)
|
|
||||||
}
|
|
||||||
shelf, err := inv.Catalogue(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
raw, _ := json.Marshal(shelf["app"].Resources)
|
|
||||||
if !strings.Contains(string(raw), "app/server@"+digest) {
|
|
||||||
t.Fatalf("app is registered as %s, not by the check's digest in its own namespace", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The walk asked it: the walk keeps what its build did, and its gate judges the registered build.
|
|
||||||
asked := time.Now().UTC()
|
|
||||||
p := g.plan(t)
|
|
||||||
p.Commit = "c3"
|
|
||||||
p.Modules["app"] = &inventory.PlanModule{State: "asked", AskedAt: &asked, Build: id}
|
|
||||||
if err := inv.SavePlan(ctx, &p); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
planBuiltSaying(ctx, g.open, "app", "c3", "", asked, id, result.MadeWords())
|
|
||||||
p = g.plan(t)
|
|
||||||
if m := p.Modules["app"]; m.State != "built" || !strings.HasPrefix(m.Made, "reused from the check of novox/mesh-catalog#9") {
|
|
||||||
t.Fatalf("the walk keeps %+v", m)
|
|
||||||
}
|
|
||||||
for i := 0; i < 6; i++ {
|
|
||||||
advancePlans(ctx, g.open)
|
|
||||||
}
|
|
||||||
p = g.plan(t)
|
|
||||||
gate := p.Modules["app"].Gate
|
|
||||||
if gate == nil || gate.To != "c3" || gate.Verdict != inventory.GatePassed {
|
|
||||||
t.Fatalf("the gate judged %+v, not the reused build", gate)
|
|
||||||
}
|
|
||||||
if sent, _, _ := inv.SentBuilds(ctx, "anchor"); sent["app"] != "c3" {
|
|
||||||
t.Fatalf("the first machine was sent %v, not the reused build", sent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,193 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// One build per change (novox/hq ADR 0350), as the controller asks it and takes it in.
|
|
||||||
|
|
||||||
// The check of a change is asked to build what a merge of it would move, in the walk's order: a module of the
|
|
||||||
// repository checked from the merge, one standing on it from its own repository as the walk builds it, and one
|
|
||||||
// that reads the repository through a context not at all. A change that moves no module builds nothing.
|
|
||||||
func TestACheckIsAskedToBuildWhatTheMergeWouldMove(t *testing.T) {
|
|
||||||
entries := []inventory.Entry{
|
|
||||||
{Manifest: catalogue.Manifest{Module: "app"}, Source: inventory.Source{Repository: "novox/shop", Seat: "git", Path: "modules/app"}},
|
|
||||||
{Manifest: catalogue.Manifest{Module: "web"}, Source: inventory.Source{Repository: "novox/web", Seat: "git", Ref: "main"}},
|
|
||||||
{Manifest: catalogue.Manifest{Module: "docs"}, Source: inventory.Source{Repository: "novox/docs", Seat: "git"}},
|
|
||||||
}
|
|
||||||
p := link.PullUpdated{Owner: "novox", Repo: "shop", Number: 7, Base: "main", Commit: "abc"}
|
|
||||||
scope := checkScope{Reach: mergeReach{Packaging: entries[2:], Plan: inventory.Plan{Tiers: [][]string{{"app", "docs"}, {"web"}}}}}
|
|
||||||
clone := func(s inventory.Source) (string, error) { return "http://forge.invalid/" + s.Repository + ".git", nil }
|
|
||||||
|
|
||||||
got := checkBuildsOf(p, scope, entries, clone)
|
|
||||||
want := []link.CheckBuild{
|
|
||||||
{Module: "app", Path: "modules/app", Merged: true, Tier: 0},
|
|
||||||
{Module: "docs", Tier: 0, Skip: "its build reads novox/shop through a context, which a check cannot hand the merge"},
|
|
||||||
{Module: "web", Repository: "http://forge.invalid/novox/web.git", Ref: "main", Tier: 1},
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(got, want) {
|
|
||||||
t.Fatalf("the check is asked to build\n%+v\nwant\n%+v", got, want)
|
|
||||||
}
|
|
||||||
if got := checkBuildsOf(p, checkScope{}, entries, clone); got != nil {
|
|
||||||
t.Fatalf("a change that moves no module asks for builds: %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A walk's ask carries its leave to take a check's build, naming the pull requests of the merges it answers; a dry
|
|
||||||
// run never does, and neither does an ask that is not a walk's.
|
|
||||||
|
|
||||||
// A walk ended lets go of its merges' check builds — only once it ended, after it took what it reuses.
|
|
||||||
func TestAWalkEndedLetsGoOfItsMergesCheckBuilds(t *testing.T) {
|
|
||||||
var forgot []builder.PullRef
|
|
||||||
was := forgetChecks
|
|
||||||
forgetChecks = func(_ context.Context, _ *inventory.Inventory, pull builder.PullRef) (string, error) {
|
|
||||||
forgot = append(forgot, pull)
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
t.Cleanup(func() { forgetChecks = was })
|
|
||||||
p := inventory.Plan{ID: "plan-1", Delivery: &inventory.PlanDelivery{Merges: []inventory.PlanMerge{
|
|
||||||
{Repository: "novox/shop", Number: 7}}}}
|
|
||||||
|
|
||||||
p.WalkSaid = inventory.WalkStarted
|
|
||||||
forgetChecksOfWalk(t.Context(), nil, p)
|
|
||||||
p.WalkSaid = ""
|
|
||||||
forgetChecksOfWalk(t.Context(), nil, p)
|
|
||||||
if len(forgot) != 0 {
|
|
||||||
t.Fatalf("a walk not ended let go of %v, which it may still take", forgot)
|
|
||||||
}
|
|
||||||
p.WalkSaid = inventory.WalkEnded
|
|
||||||
forgetChecksOfWalk(t.Context(), nil, p)
|
|
||||||
if want := []builder.PullRef{{Owner: "novox", Repo: "shop", Number: 7}}; !reflect.DeepEqual(forgot, want) {
|
|
||||||
t.Fatalf("the walk ended let go of %v", forgot)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A build in the scratch namespace is never registered: only the walk's copy in the module's own namespace is.
|
|
||||||
func TestABuildInTheScratchNamespaceIsNeverRegistered(t *testing.T) {
|
|
||||||
if got := inScratch([]link.MadeArtifact{{Name: "server", Reference: "store.internal:5000/app/server@sha256:aa"}}); got != "" {
|
|
||||||
t.Fatalf("a module's own build reads as a scratch build: %s", got)
|
|
||||||
}
|
|
||||||
scratch := "store.internal:5000/scratch/app/server@sha256:aa"
|
|
||||||
if got := inScratch([]link.MadeArtifact{{Name: "server", Reference: scratch}}); got != scratch {
|
|
||||||
t.Fatalf("a scratch build was not found: %q", got)
|
|
||||||
}
|
|
||||||
if got := inScratch([]link.MadeArtifact{{Name: "look", Reference: "http://store.internal:5000/v2/scratch/app/look/blobs/sha256:bb"}}); got == "" {
|
|
||||||
t.Fatal("a scratch archive was not found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The walk says per module what it did: "reused from …" or "built: <why>" — and a build seat from before says
|
|
||||||
// neither, which is said.
|
|
||||||
func TestAWalksBuildSaysWhetherItReusedOrBuilt(t *testing.T) {
|
|
||||||
if got := (link.BuildResult{Reused: "reused from the check of novox/shop#7"}).MadeWords(); got != "reused from the check of novox/shop#7" {
|
|
||||||
t.Fatalf("said %q", got)
|
|
||||||
}
|
|
||||||
if got := (link.BuildResult{BuiltWhy: "no check built it"}).MadeWords(); got != "built: no check built it" {
|
|
||||||
t.Fatalf("said %q", got)
|
|
||||||
}
|
|
||||||
if got := (link.BuildResult{}).MadeWords(); !strings.Contains(got, "predates ADR 0350") {
|
|
||||||
t.Fatalf("said %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A walk's module the check's build stood in for is registered by the digest the check built — the copy in the
|
|
||||||
// module's namespace — and the walk keeps "reused from …" for it; its first-node gate judges that registered
|
|
||||||
// build as it judges one the walk built. A scratch reference is refused. Needs the store (MESH_TEST_POSTGRES), and
|
|
||||||
// is named in testdata/store-tests.
|
|
||||||
|
|
||||||
// A walk's leave names the builds its own pull requests' verdicts made at the head each merge merged — never an
|
|
||||||
// earlier head's — read once per walk, and none when they cannot be read: the builder takes nothing a verdict does
|
|
||||||
// not name (novox/hq ADR 0350 §2, issue 703).
|
|
||||||
func TestAWalksLeaveNamesTheBuildsItsPullRequestsVerdictsMade(t *testing.T) {
|
|
||||||
was := checkedBuildsOf
|
|
||||||
forgetWalkVerdicts()
|
|
||||||
t.Cleanup(func() { checkedBuildsOf = was; forgetWalkVerdicts() })
|
|
||||||
var asked [][]link.PullRef
|
|
||||||
pull := link.PullRef{Owner: "novox", Repo: "shop", Number: 7}
|
|
||||||
atHead := link.CheckedBuild{Pull: pull, Check: "build-2", Head: "bbbbbbbbbbbb", Built: link.CheckBuilt{Module: "app", Key: "r1:2"}}
|
|
||||||
earlier := link.CheckedBuild{Pull: pull, Check: "build-1", Head: "aaaaaaaaaaaa", Built: link.CheckBuilt{Module: "app", Key: "r1:1"}}
|
|
||||||
checkedBuildsOf = func(_ context.Context, pulls []link.PullRef) ([]link.CheckedBuild, error) {
|
|
||||||
asked = append(asked, pulls)
|
|
||||||
return []link.CheckedBuild{atHead, earlier}, nil
|
|
||||||
}
|
|
||||||
pulls := []link.PullRef{pull}
|
|
||||||
heads := map[link.PullRef]string{pull: "bbbbbbbbbbbb0123456789"}
|
|
||||||
for range 3 {
|
|
||||||
if got := reuseWithVerdicts(t.Context(), "plan-1", &link.ReuseAsk{Pulls: pulls}, heads); !reflect.DeepEqual(got.Builds,
|
|
||||||
[]link.CheckedBuild{atHead}) {
|
|
||||||
t.Fatalf("the leave names %+v, not only the merged head's build", got.Builds)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(asked, [][]link.PullRef{pulls}) {
|
|
||||||
t.Fatalf("the verdicts were read %d times for one walk: %v", len(asked), asked)
|
|
||||||
}
|
|
||||||
// A merge whose head was not said takes nothing.
|
|
||||||
if got := reuseWithVerdicts(t.Context(), "plan-1", &link.ReuseAsk{Pulls: pulls}, nil); got.Builds != nil {
|
|
||||||
t.Fatalf("a merge of no said head names %+v", got.Builds)
|
|
||||||
}
|
|
||||||
checkedBuildsOf = func(context.Context, []link.PullRef) ([]link.CheckedBuild, error) {
|
|
||||||
return nil, errors.New("no bus")
|
|
||||||
}
|
|
||||||
if got := reuseWithVerdicts(t.Context(), "plan-2", &link.ReuseAsk{Pulls: pulls}, heads); got.Builds != nil {
|
|
||||||
t.Fatalf("verdicts that could not be read named %+v", got.Builds)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A check is handed only the builds earlier checks of its own pull request at its own head made: another pull
|
|
||||||
// request's build, or an earlier head's, is never kept (novox/hq issue 703). None when they cannot be read.
|
|
||||||
func TestACheckIsHandedOnlyItsOwnHeadsBuilds(t *testing.T) {
|
|
||||||
was := checkedBuildsOf
|
|
||||||
t.Cleanup(func() { checkedBuildsOf = was })
|
|
||||||
pull := link.PullRef{Owner: "novox", Repo: "shop", Number: 7}
|
|
||||||
mine := link.CheckedBuild{Pull: pull, Check: "build-1", Head: "bbbbbbbbbbbb", Built: link.CheckBuilt{Module: "app", Key: "r1:1"}}
|
|
||||||
earlier := link.CheckedBuild{Pull: pull, Check: "build-0", Head: "aaaaaaaaaaaa", Built: link.CheckBuilt{Module: "app", Key: "r1:1"}}
|
|
||||||
another := link.CheckedBuild{Pull: link.PullRef{Owner: "novox", Repo: "shop", Number: 8}, Check: "build-9",
|
|
||||||
Head: "bbbbbbbbbbbb", Built: link.CheckBuilt{Module: "app", Key: "r1:1"}}
|
|
||||||
var asked [][]link.PullRef
|
|
||||||
checkedBuildsOf = func(_ context.Context, pulls []link.PullRef) ([]link.CheckedBuild, error) {
|
|
||||||
asked = append(asked, pulls)
|
|
||||||
return []link.CheckedBuild{mine, earlier, another}, nil
|
|
||||||
}
|
|
||||||
p := link.PullUpdated{Owner: "novox", Repo: "shop", Number: 7, Commit: "bbbbbbbbbbbb0123456789"}
|
|
||||||
builds := []link.CheckBuild{{Module: "app"}}
|
|
||||||
if got := namedForTheCheck(t.Context(), p, builds); !reflect.DeepEqual(got, []link.CheckedBuild{mine}) ||
|
|
||||||
!reflect.DeepEqual(asked, [][]link.PullRef{{pull}}) {
|
|
||||||
t.Fatalf("the check is handed %+v, read for %v", got, asked)
|
|
||||||
}
|
|
||||||
checkedBuildsOf = func(context.Context, []link.PullRef) ([]link.CheckedBuild, error) {
|
|
||||||
return nil, errors.New("no bus")
|
|
||||||
}
|
|
||||||
if got := namedForTheCheck(t.Context(), p, builds); got != nil {
|
|
||||||
t.Fatalf("verdicts that could not be read named %+v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func forgetWalkVerdicts() {
|
|
||||||
walkVerdicts.Lock()
|
|
||||||
defer walkVerdicts.Unlock()
|
|
||||||
walkVerdicts.read = map[string]verdictsRead{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A walk's merged heads are the forge's word for each merge; a merge announced without its head, or a pull request
|
|
||||||
// merged at two heads in one walk, has none, and its walk takes no check's build (novox/hq issue 703).
|
|
||||||
func TestAWalksMergedHeadsAreTheForgesWord(t *testing.T) {
|
|
||||||
merge := func(number int, head string) inventory.PlanMerge {
|
|
||||||
return inventory.PlanMerge{Repository: "novox/shop", Number: number, Head: head}
|
|
||||||
}
|
|
||||||
p := &inventory.Plan{Delivery: &inventory.PlanDelivery{Merges: []inventory.PlanMerge{
|
|
||||||
merge(7, "aaaaaaaaaaaa1"), merge(8, ""), merge(9, "bbbbbbbbbbbb"), merge(9, "cccccccccccc")}}}
|
|
||||||
heads := mergedHeadsOf(p)
|
|
||||||
shop := func(n int) link.PullRef { return link.PullRef{Owner: "novox", Repo: "shop", Number: n} }
|
|
||||||
if heads[shop(7)] != "aaaaaaaaaaaa1" || heads[shop(8)] != "" || heads[shop(9)] != "" {
|
|
||||||
t.Fatalf("merged heads %v", heads)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,203 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
||||||
"github.com/novox/mesh-controller/internal/builder"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// `check-here` is a pull request's merge check run on the machine at hand **exactly as the build seat
|
|
||||||
// runs it** (novox/hq issue 286): the same code (builder.Check), the same ask the controller would make of
|
|
||||||
// the seat — the gate's modules and judge by the planner's own answer over the facts snapshot, the
|
|
||||||
// repositories beside it at the refs the snapshot says the seat clones them at — in the toolchain image
|
|
||||||
// the mesh holds, as the user the build seat runs as, against a throwaway store and bus of the versions
|
|
||||||
// the mesh runs.
|
|
||||||
//
|
|
||||||
// **The build seat is the reference.** A merge-check.sh that passed on an agent's machine failed on the
|
|
||||||
// seat for three reasons that were each the agent's environment, never the change: a newer Go whose gofmt
|
|
||||||
// lays a file out differently, a sibling checkout at the agent's feature branch where the seat had the
|
|
||||||
// commit the mesh runs, and a different user. Run here, a check sees what the seat will.
|
|
||||||
//
|
|
||||||
// check-here [--tree <checkout>] [--base main] [--registry <artifact store>] [--forge <url of the owner>]
|
|
||||||
// [--number <n>] [--user uid:gid] [--facts store|<file>] [--keep]
|
|
||||||
//
|
|
||||||
// The checkout's HEAD is what is checked, and it must be committed: the seat checks a commit, never a
|
|
||||||
// working tree.
|
|
||||||
func checkHereCommand(ctx context.Context, args []string) error {
|
|
||||||
set := flag.NewFlagSet("check-here", flag.ContinueOnError)
|
|
||||||
tree := set.String("tree", ".", "the change's checkout; its HEAD is checked")
|
|
||||||
base := set.String("base", "main", "the branch the change would merge into")
|
|
||||||
registry := set.String("registry", os.Getenv("MESH_REGISTRY"), "the artifact store holding the facts and the toolchains")
|
|
||||||
forge := set.String("forge", "", "where the repositories beside it are cloned from, as <forge>/<repository>.git; "+
|
|
||||||
"the checkout's origin without its own name when not given")
|
|
||||||
number := set.Int("number", 0, "the pull request's number, when there is one")
|
|
||||||
// The build seat's service runs as root, and its check containers run as the builder does.
|
|
||||||
user := set.String("user", "0:0", "the user the check's containers run as: the build seat's")
|
|
||||||
keep := set.Bool("keep", false, "keep the workspace afterwards")
|
|
||||||
factsFrom := set.String("facts", "store", "the facts snapshot: `store`, the one the artifact store holds — "+
|
|
||||||
"what the seat reads — or a file")
|
|
||||||
if _, err := parseAround(set, args); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if *registry == "" {
|
|
||||||
return errors.New("check-here reads the facts and the toolchains from the artifact store: --registry <host:port> or MESH_REGISTRY")
|
|
||||||
}
|
|
||||||
dir, err := filepath.Abs(*tree)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
git := func(args ...string) (string, error) {
|
|
||||||
cmd := exec.CommandContext(ctx, "git", args...)
|
|
||||||
cmd.Dir = dir
|
|
||||||
out, err := cmd.Output()
|
|
||||||
return strings.TrimSpace(string(out)), err
|
|
||||||
}
|
|
||||||
if dirty, err := git("status", "--porcelain", "--untracked-files=no"); err != nil {
|
|
||||||
return fmt.Errorf("%s is not a checkout: %w", dir, err)
|
|
||||||
} else if dirty != "" {
|
|
||||||
return errors.New("the checkout has changes not committed: the build seat checks a commit, so commit first")
|
|
||||||
}
|
|
||||||
head, err := git("rev-parse", "HEAD")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
origin, err := git("remote", "get-url", "origin")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("the checkout has no origin to say which repository it is: %w", err)
|
|
||||||
}
|
|
||||||
owner, repo, prefix := ownerRepoOf(origin)
|
|
||||||
if *forge == "" {
|
|
||||||
*forge = prefix
|
|
||||||
}
|
|
||||||
if _, err := git("fetch", "--quiet", "origin", *base); err != nil {
|
|
||||||
return fmt.Errorf("cannot fetch %s to say what the change touches: %w", *base, err)
|
|
||||||
}
|
|
||||||
// Without rename detection, so a file moved out of a build source is said under its old name as well:
|
|
||||||
// its going is a change to the build that held it (novox/hq ADR 0267).
|
|
||||||
changedText, err := git("diff", "--name-only", "--no-renames", "origin/"+*base+"...HEAD")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var paths, removed []string
|
|
||||||
for _, p := range strings.Split(changedText, "\n") {
|
|
||||||
if p = strings.TrimSpace(p); p == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
paths = append(paths, p)
|
|
||||||
if _, err := os.Stat(filepath.Join(dir, p)); os.IsNotExist(err) {
|
|
||||||
removed = append(removed, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = os.Setenv("MESH_REGISTRY", *registry)
|
|
||||||
f, err := readFacts(ctx, *factsFrom)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("the facts snapshot cannot be read: %w", err)
|
|
||||||
}
|
|
||||||
entries, read, edges, err := graphOfFacts(f)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p := link.PullUpdated{Owner: owner, Repo: repo, Number: *number, Base: *base, Commit: head, Paths: paths,
|
|
||||||
Removed: removed, ModuleDirs: moduleDirsIn(dir, paths), ModuleDirsSaid: true}
|
|
||||||
scope := pullScope(p, entries, read, edges)
|
|
||||||
_, scriptErr := os.Stat(filepath.Join(dir, builder.CheckScript))
|
|
||||||
if !scope.gated() && !scope.Mesh {
|
|
||||||
fmt.Printf("%s: %s, and the repository is not the mesh's: the build seat runs nothing for it\n",
|
|
||||||
owner+"/"+repo, noModuleTouched)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if !scope.gated() && scriptErr != nil {
|
|
||||||
fmt.Printf("%s: %s; %s\n", owner+"/"+repo, noModuleTouched, noMergeCheck)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
toolchains := map[string]string{}
|
|
||||||
for language, reference := range f.Versions.Toolchains {
|
|
||||||
toolchains[language] = catalogue.Rerouted(reference, *registry)
|
|
||||||
}
|
|
||||||
if len(toolchains) == 0 {
|
|
||||||
return errors.New("the facts snapshot names no toolchain: it was taken by a controller from before " +
|
|
||||||
"issue 286, and the seat's toolchain cannot be known here")
|
|
||||||
}
|
|
||||||
beside := map[string]builder.Beside{}
|
|
||||||
for d, ref := range f.Beside {
|
|
||||||
from := d
|
|
||||||
if d == "mesh-controller-main" {
|
|
||||||
from = "mesh-controller"
|
|
||||||
}
|
|
||||||
beside[d] = builder.Beside{Repository: strings.TrimSuffix(*forge, "/") + "/" + from + ".git", Ref: ref}
|
|
||||||
}
|
|
||||||
id := fmt.Sprintf("check-here-%d", time.Now().UnixNano())
|
|
||||||
spec := builder.CheckSpec{ID: id, Repository: dir, Ref: head, Owner: owner, Repo: repo, Number: *number,
|
|
||||||
Paths: paths, Beside: beside, Modules: scope.Modules, New: scope.New, Manifests: scope.Manifests,
|
|
||||||
Base: *base, Judge: scope.Judge, Toolchain: toolchains["go"], Toolchains: toolchains, User: *user}
|
|
||||||
|
|
||||||
workspace, err := os.MkdirTemp("", "mesh-check-here-")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !*keep {
|
|
||||||
defer removeWorkspace(spec.Toolchain, workspace, *user)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "checking %s/%s at %.8s as the build seat would, against the facts of %s, in %s\n",
|
|
||||||
owner, repo, head, f.Taken.Format(time.RFC3339), workspace)
|
|
||||||
v, err := builder.Check(ctx, builder.Command, spec, workspace, *registry, builder.GitCredential{},
|
|
||||||
func(step, message string) {
|
|
||||||
if step != "output" {
|
|
||||||
fmt.Fprintf(os.Stderr, " [%s] %s\n", step, message)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("the check could not run — on the seat an error, never a pass: %w", err)
|
|
||||||
}
|
|
||||||
fmt.Println(v.Report)
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Printf("mesh/merge-gate: %s — %s\n", strings.ToUpper(v.Gate.Verdict), v.Gate.Summary)
|
|
||||||
if v.Repo != nil {
|
|
||||||
fmt.Printf("mesh/repo-check: %s — %s\n", strings.ToUpper(v.Repo.Verdict), v.Repo.Summary)
|
|
||||||
}
|
|
||||||
for _, l := range []*builder.Layer{v.Gate, v.Repo} {
|
|
||||||
if l != nil && l.Verdict != "pass" && l.Verdict != "warning" {
|
|
||||||
return errors.New("the build seat would not pass this change")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ownerRepoOf reads owner, repository and the owner's URL from a remote: ssh://git@host:222/novox/mesh-host.git
|
|
||||||
// → novox, mesh-host, ssh://git@host:222/novox.
|
|
||||||
func ownerRepoOf(remote string) (string, string, string) {
|
|
||||||
trimmed := strings.TrimSuffix(strings.TrimSuffix(remote, "/"), ".git")
|
|
||||||
cut := strings.LastIndexAny(trimmed, "/:")
|
|
||||||
if cut < 0 {
|
|
||||||
return "", trimmed, ""
|
|
||||||
}
|
|
||||||
repo, prefix := trimmed[cut+1:], trimmed[:cut]
|
|
||||||
owner := prefix
|
|
||||||
if at := strings.LastIndexAny(prefix, "/:"); at >= 0 {
|
|
||||||
owner = prefix[at+1:]
|
|
||||||
}
|
|
||||||
return owner, repo, prefix
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeWorkspace removes what the check left, written as the seat's user: by a container of that user
|
|
||||||
// when it is not this one.
|
|
||||||
func removeWorkspace(image, workspace, user string) {
|
|
||||||
if image != "" && user != fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid()) {
|
|
||||||
// Everything in it — the check's HOME is the workspace, so the toolchain's own files are there too.
|
|
||||||
_ = exec.Command("docker", "run", "--rm", "--user", user, "--volume", workspace+":/workspace", image,
|
|
||||||
"sh", "-c", "rm -rf /workspace/* /workspace/.[!.]*").Run()
|
|
||||||
}
|
|
||||||
_ = os.RemoveAll(workspace)
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/novox/mesh-controller/internal/inventory"
|
|
||||||
"github.com/novox/mesh-controller/internal/link"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 1212/AC1 and AC2: the check request names the pulls bucket of each module claiming the delivery seat, in the order
|
|
||||||
// of the claimants' names.
|
|
||||||
func TestACheckNamesThePullsBucketsOfTheSeatsClaimants(t *testing.T) {
|
|
||||||
for name, c := range map[string]struct {
|
|
||||||
entries []inventory.Entry
|
|
||||||
want []string
|
|
||||||
}{
|
|
||||||
"only the gate": {[]inventory.Entry{claimingTheSeat("mesh-delivery-gate"), namedOnly("mesh-delivery")}, []string{"mesh-delivery-gate_pulls"}},
|
|
||||||
"only today's": {[]inventory.Entry{claimingTheSeat("mesh-delivery"), namedOnly("gitea")}, []string{"mesh-delivery_pulls"}},
|
|
||||||
"both, name order": {[]inventory.Entry{claimingTheSeat("mesh-delivery-gate"), claimingTheSeat("mesh-delivery")}, []string{"mesh-delivery_pulls", "mesh-delivery-gate_pulls"}},
|
|
||||||
"none": {[]inventory.Entry{namedOnly("gitea")}, nil},
|
|
||||||
} {
|
|
||||||
if got := pullsBucketsOf(c.entries); !reflect.DeepEqual(got, c.want) {
|
|
||||||
t.Errorf("%s: %v, want %v", name, got, c.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1212/AC1 and AC2: the check a request carries names those buckets (the wiring, not only the helper).
|
|
||||||
func TestTheCheckRequestCarriesThePullsBuckets(t *testing.T) {
|
|
||||||
for name, c := range map[string]struct {
|
|
||||||
entries []inventory.Entry
|
|
||||||
want []string
|
|
||||||
}{
|
|
||||||
"only the gate": {[]inventory.Entry{claimingTheSeat("mesh-delivery-gate")}, []string{"mesh-delivery-gate_pulls"}},
|
|
||||||
"only today's": {[]inventory.Entry{claimingTheSeat("mesh-delivery")}, []string{"mesh-delivery_pulls"}},
|
|
||||||
"both, name order": {[]inventory.Entry{claimingTheSeat("mesh-delivery-gate"), claimingTheSeat("mesh-delivery")}, []string{"mesh-delivery_pulls", "mesh-delivery-gate_pulls"}},
|
|
||||||
} {
|
|
||||||
got := checkOf(link.PullUpdated{Owner: "novox", Repo: "a", Number: 7}, checkScope{}, c.entries, nil, nil, nil, nil)
|
|
||||||
if !reflect.DeepEqual(got.Pulls, c.want) || got.Owner != "novox" || got.Number != 7 {
|
|
||||||
t.Errorf("%s: %+v, want pulls %v", name, got, c.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user