//go:build integration package main import ( "errors" "strings" "testing" "time" "github.com/novox/mesh-controller/internal/catalogue" "github.com/novox/mesh-controller/internal/conditions" "github.com/novox/mesh-controller/internal/overlay" ) 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. plan, settings, err := planFor(ctx, open, "laptop") if err != nil { t.Fatal(err) } gens, err := generators(ctx, open) if err != nil { t.Fatal(err) } _, err = declarationWith(ctx, open, "laptop", plan, settings, gens, Reading) 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 := declarationWith(ctx, open, "laptop", plan, settings, gens, Foreseeing) 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() register(t, open, aKeeper()) pushedBy(t, open, "laptop") 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) } }