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

131 lines
5.1 KiB
Go

package check
import (
"context"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/contracts/go/clients/meshdeliverycheckerclient"
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker"
"git.novox.be/novox/mesh-delivery/sdk/buildstep"
)
var at0 = time.Date(2026, 10, 21, 19, 0, 0, 0, time.UTC)
const (
digA = "aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11"
digB = "bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22bb22"
)
func keptValue(pkg string, tests int) meshdeliverychecker.KeptByLevelDigestValue {
return meshdeliverychecker.KeptByLevelDigestValue{State: meshdeliverycheckerclient.VerdictPass, Repository: "novox/mesh-delivery", Package: pkg, Tests: int64(tests), Took: 120, Toolchain: "go1.26.0", At: at0.UTC().Format(time.RFC3339)}
}
func TestAPassIsKeptByACreateUnderItsLevelAndDigest(t *testing.T) { // 1287/AC1
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
made, err := k.Keep(ctx, buildstep.LevelUnit, digA, keptValue("m/a", 3))
if err != nil || !made {
t.Fatalf("Keep = %v, %v, want made", made, err)
}
key, _ := meshdeliverycheckerclient.KeptKey(buildstep.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, buildstep.LevelUnit, digA)
if err != nil || !found || got.Package != "m/a" || got.Tests != 3 || got.Level != buildstep.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, buildstep.LevelUnit, digA, keptValue("m/a", 3)); err != nil || !made {
t.Fatal(made, err)
}
made, err := k.Keep(ctx, buildstep.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, buildstep.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 TestAResultWithSkippedTestsKeepsNothing(t *testing.T) { // issue 890: a pass that skipped proved nothing
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
res := Tested{Level: "integration", Passed: true, Skipped: 2, Packages: []GoPackage{{Package: "m/a", Passed: true, Tests: 4}}}
kept, err := k.KeepPasses(ctx, "novox/mesh-catalog", "integration", "go1.26.0", res, map[string]string{"m/a": digA}, at0)
if err != nil || len(kept) != 0 {
t.Fatalf("kept %v, %v, want nothing", kept, err)
}
if keys, _ := st.Keys(ctx); len(keys) != 0 {
t.Errorf("the bucket holds %v, want none", keys)
}
}
func TestOnlyTheSkippingPackageOfAResultIsWithheld(t *testing.T) { // issue 890
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
res := Tested{Level: "integration", Passed: true, Skipped: 2, Packages: []GoPackage{
{Package: "m/a", Passed: true, Tests: 4, Skipped: 2},
{Package: "m/b", Passed: true, Tests: 3},
}}
kept, err := k.KeepPasses(ctx, "novox/mesh-catalog", "integration", "go1.26.0", res, map[string]string{"m/a": digA, "m/b": digB}, at0)
if err != nil || strings.Join(kept, ",") != "m/b" {
t.Fatalf("kept %v, %v, want only m/b", kept, err)
}
}
func TestAKeptValueOfAnotherDigestIsNotRead(t *testing.T) { // 1287/AC7
ctx, st := context.Background(), newMemKV()
k := Kept{Store: st}
if _, err := k.Keep(ctx, buildstep.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, buildstep.LevelUnit, digB); found {
t.Error("a digest that was not kept was found")
}
if _, found, _ := k.Lookup(ctx, buildstep.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 = meshdeliverycheckerclient.VerdictFail
if _, err := k.Keep(context.Background(), buildstep.LevelUnit, digA, v); err == nil {
t.Error("a failure was kept")
}
}