Files
mesh-delivery/modules/mesh-delivery-checker/internal/check/kept_test.go
T

103 lines
3.7 KiB
Go

package check
import (
"context"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/delivery"
)
var at0 = time.Date(2026, 10, 21, 19, 0, 0, 0, time.UTC)
const (
digA = "aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11"
digB = "bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22"
)
func keptValue(pkg string, tests int) delivery.Kept {
return delivery.Kept{State: delivery.VerdictPass, Repository: "novox/mesh-delivery", Package: pkg, Tests: tests, Took: 120, Toolchain: "go1.26.0", At: at0}
}
func TestAPassIsKeptByACreateUnderItsLevelAndDigest(t *testing.T) { // 1287/AC1
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
made, err := k.Keep(ctx, delivery.LevelUnit, digA, keptValue("m/a", 3))
if err != nil || !made {
t.Fatalf("Keep = %v, %v, want made", made, err)
}
key, _ := delivery.KeptKey(delivery.LevelUnit, digA)
if st.creates[key] != 1 {
t.Fatalf("the key %s was created %d times, want once", key, st.creates[key])
}
got, found, err := k.Lookup(ctx, delivery.LevelUnit, digA)
if err != nil || !found || got.Package != "m/a" || got.Tests != 3 || got.Level != delivery.LevelUnit || got.Toolchain != "go1.26.0" {
t.Fatalf("Lookup = %+v, %v, %v", got, found, err)
}
}
func TestASecondKeepUnderTheSameDigestIsRefusedAndTheFirstStands(t *testing.T) { // 1287/AC2
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
if made, err := k.Keep(ctx, delivery.LevelUnit, digA, keptValue("m/a", 3)); err != nil || !made {
t.Fatal(made, err)
}
made, err := k.Keep(ctx, delivery.LevelUnit, digA, keptValue("m/other", 9))
if err != nil || made {
t.Fatalf("the second Keep = %v, %v, want refused without an error", made, err)
}
got, _, _ := k.Lookup(ctx, delivery.LevelUnit, digA)
if got.Package != "m/a" || got.Tests != 3 {
t.Errorf("the first value did not stand: %+v", got)
}
}
func TestOnlyPassingPackagesWithADigestAreKept(t *testing.T) { // 1287/AC3, AC4
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
res := Tested{Level: "unit", Passed: false, Packages: []GoPackage{
{Package: "m/a", Passed: true, Tests: 4, Elapsed: 0.2},
{Package: "m/b", Passed: false, Tests: 2},
{Package: "m/c", Passed: true, Tests: 1}, // no digest named
{Package: "m/d", Passed: true, NoTests: true}, // no tests
}, Failed: []Failure{{Package: "m/b", Test: "TestB"}}}
kept, err := k.KeepPasses(ctx, "novox/mesh-delivery", "unit", "go1.26.0", res, map[string]string{"m/a": digA, "m/b": digB, "m/d": digB}, at0)
if err != nil {
t.Fatal(err)
}
if strings.Join(kept, ",") != "m/a" {
t.Fatalf("kept %v, want only m/a", kept)
}
if keys, _ := st.Keys(ctx); len(keys) != 1 {
t.Errorf("the bucket holds %v, want one key", keys)
}
}
func TestAKeptValueOfAnotherDigestIsNotRead(t *testing.T) { // 1287/AC7
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
if _, err := k.Keep(ctx, delivery.LevelUnit, digA, keptValue("m/a", 3)); err != nil {
t.Fatal(err)
}
// A toolchain change changes the digest, so the new digest finds nothing and the old key is not deleted.
if _, found, _ := k.Lookup(ctx, delivery.LevelUnit, digB); found {
t.Error("a digest that was not kept was found")
}
if _, found, _ := k.Lookup(ctx, delivery.LevelIntegration, digA); found {
t.Error("a kept unit verdict was found at the integration level")
}
if keys, _ := st.Keys(ctx); len(keys) != 1 {
t.Errorf("the old key was removed: %v", keys)
}
}
func TestAKeptVerdictThatIsNotAPassIsRefused(t *testing.T) {
k := Kept{Store: newMemKV()}
v := keptValue("m/a", 1)
v.State = delivery.VerdictFail
if _, err := k.Keep(context.Background(), delivery.LevelUnit, digA, v); err == nil {
t.Error("a failure was kept")
}
}