diff --git a/cmd/ecs-agent/runtime/runtime_test.go b/cmd/ecs-agent/runtime/runtime_test.go index b4e34c52d..1de212700 100644 --- a/cmd/ecs-agent/runtime/runtime_test.go +++ b/cmd/ecs-agent/runtime/runtime_test.go @@ -1,71 +1,9 @@ package runtime import ( - "context" - "errors" "testing" ) -// staticResolver is a test Resolver returning fixed creds or an error. -type staticResolver struct { - user, pass, endpoint string - err error -} - -func (s staticResolver) Authorize(_ context.Context, _ string) (string, string, string, error) { - return s.user, s.pass, s.endpoint, s.err -} - -func TestFakePuller_RecordsAndDefaultsRef(t *testing.T) { - f := &FakePuller{} - img, err := f.Pull(context.Background(), PullSpec{Ref: "repo/app:1"}, staticResolver{user: "AWS"}) - if err != nil { - t.Fatalf("Pull: %v", err) - } - if img.Ref != "repo/app:1" { - t.Errorf("Ref defaulted wrong: %q", img.Ref) - } - if len(f.Pulls) != 1 || f.Pulls[0].Ref != "repo/app:1" { - t.Errorf("Pulls = %+v", f.Pulls) - } - if len(f.Authzd) != 1 || f.Authzd[0] != "AWS" { - t.Errorf("Authzd = %+v", f.Authzd) - } -} - -func TestFakePuller_ResolverError(t *testing.T) { - f := &FakePuller{} - _, err := f.Pull(context.Background(), PullSpec{Ref: "r"}, staticResolver{err: errors.New("boom")}) - if err == nil { - t.Fatal("expected resolver error to propagate") - } -} - -func TestFakePuller_ProgrammedError(t *testing.T) { - f := &FakePuller{Err: errors.New("pull failed")} - if _, err := f.Pull(context.Background(), PullSpec{Ref: "r"}, nil); err == nil { - t.Fatal("expected programmed error") - } -} - -func TestFakePuller_OnPullOverrides(t *testing.T) { - want := Image{Ref: "x", Digest: "sha256:abc", Size: 42} - f := &FakePuller{Err: errors.New("ignored"), OnPull: func(PullSpec) (Image, error) { - return want, nil - }} - got, err := f.Pull(context.Background(), PullSpec{Ref: "r"}, nil) - if err != nil || got != want { - t.Fatalf("got %+v, %v; want %+v", got, err, want) - } -} - -func TestFakePuller_Close(t *testing.T) { - f := &FakePuller{} - if err := f.Close(); err != nil || !f.Closed { - t.Fatalf("Close: err=%v closed=%v", err, f.Closed) - } -} - func TestNormalizeRef(t *testing.T) { cases := map[string]string{ "nginx:alpine": "docker.io/library/nginx:alpine", diff --git a/spinifex/handlers/ochrevector/query_test.go b/spinifex/handlers/ochrevector/query_test.go index a9379081e..c7ba626e3 100644 --- a/spinifex/handlers/ochrevector/query_test.go +++ b/spinifex/handlers/ochrevector/query_test.go @@ -5,11 +5,9 @@ package handlers_ochrevector import ( - "context" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) // TestClampQueryK proves Query's k default/clamp rule directly (D8/D10): @@ -33,76 +31,3 @@ func TestClampQueryK(t *testing.T) { }) } } - -// TestFakeBackend_Query_DefaultsAndClampsK proves the backend seam itself -// applies the same default/clamp rule Query documents, using the in-memory -// fake so this is a pure-Go test with no Postgres dependency. -func TestFakeBackend_Query_DefaultsAndClampsK(t *testing.T) { - backend := newFakeBackend() - ctx := context.Background() - - canned := make([]QueryResult, 150) - for i := range canned { - canned[i] = QueryResult{Chunk: "chunk", SourceKey: "docs/one.txt", Score: 1.0} - } - backend.queryResults = canned - - // k<=0 defaults to 10. - got, err := backend.Query(ctx, "111111111111", "idx-one", []float32{1, 0}, 0, nil) - require.NoError(t, err) - assert.Len(t, got, defaultQueryK) - assert.Equal(t, defaultQueryK, backend.lastQueryK) - - // A k above the hard cap is clamped, not rejected. - got, err = backend.Query(ctx, "111111111111", "idx-one", []float32{1, 0}, 1000, nil) - require.NoError(t, err) - assert.Len(t, got, maxQueryK) - assert.Equal(t, maxQueryK, backend.lastQueryK) - - // An in-range k passes straight through. - got, err = backend.Query(ctx, "111111111111", "idx-one", []float32{1, 0}, 30, nil) - require.NoError(t, err) - assert.Len(t, got, 30) -} - -// TestFakeBackend_Query_ResultsPlumbedThrough proves the exact QueryResult -// values set on the backend come back unchanged through the Query call -- -// the plumbing this stage adds, independent of any real ANN ranking. -func TestFakeBackend_Query_ResultsPlumbedThrough(t *testing.T) { - backend := newFakeBackend() - ctx := context.Background() - - want := []QueryResult{ - {Chunk: "first chunk", Metadata: map[string]any{"category": "handbook"}, SourceKey: "docs/a.txt", SourceOffset: 0, Score: 0.95}, - {Chunk: "second chunk", Metadata: map[string]any{"category": "faq"}, SourceKey: "docs/b.txt", SourceOffset: 128, Score: 0.42}, - } - backend.queryResults = want - - got, err := backend.Query(ctx, "111111111111", "idx-one", []float32{0.1, 0.2, 0.3}, 5, nil) - require.NoError(t, err) - assert.Equal(t, want, got) -} - -// TestFakeBackend_Query_PropagatesBackendError proves a backend failure -// surfaces to the caller rather than being swallowed. -func TestFakeBackend_Query_PropagatesBackendError(t *testing.T) { - backend := newFakeBackend() - backend.failQuery = errFakeBackend - - _, err := backend.Query(context.Background(), "111111111111", "idx-one", []float32{1}, 5, nil) - require.Error(t, err) - assert.ErrorIs(t, err, errFakeBackend) -} - -// TestFakeBackend_Query_RecordsFilterForAssertions proves the filter a -// caller passes reaches the backend call, so an orchestration-level test -// can assert the right filter was requested even though the fake does not -// evaluate it. -func TestFakeBackend_Query_RecordsFilterForAssertions(t *testing.T) { - backend := newFakeBackend() - f := Equals("category", "handbook") - - _, err := backend.Query(context.Background(), "111111111111", "idx-one", []float32{1}, 5, f) - require.NoError(t, err) - assert.Same(t, f, backend.lastQueryFilter) -} diff --git a/spinifex/vm/mock/mock_test.go b/spinifex/vm/mock/mock_test.go deleted file mode 100644 index f86a4fd59..000000000 --- a/spinifex/vm/mock/mock_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package mock_test - -import ( - "errors" - "testing" - - handlers_ec2_instance "github.com/mulgadc/spinifex/spinifex/handlers/ec2/instance" - "github.com/mulgadc/spinifex/spinifex/vm" - "github.com/mulgadc/spinifex/spinifex/vm/mock" - "github.com/nats-io/nats.go/jetstream" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -// This check lives here rather than in mock.go so mock.go's own import set -// stays minimal (vm only): handlers/ec2/instance imports vm, and pulling it -// into mock.go would make its own internal tests importing this package a -// cycle. This external test package imports both, so it can assert the -// same thing without either production package importing the other. -var _ handlers_ec2_instance.StoppedInstanceStore = (*mock.StateStore)(nil) - -func TestSaveAndLoadRunningState_RoundTrips(t *testing.T) { - s := mock.New() - require.NoError(t, s.SaveRunningState("node-1", map[string]*vm.VM{"i-1": {ID: "i-1"}})) - - got, err := s.LoadRunningState("node-1") - require.NoError(t, err) - require.Contains(t, got, "i-1") - - empty, err := s.LoadRunningState("node-2") - require.NoError(t, err) - assert.Empty(t, empty) -} - -func TestClaimStoppedInstance_MissingReturnsErrStoppedInstanceClaimed(t *testing.T) { - s := mock.New() - _, err := s.ClaimStoppedInstance("i-missing") - assert.ErrorIs(t, err, vm.ErrStoppedInstanceClaimed) - assert.Equal(t, 1, s.ClaimAttempts) -} - -func TestClaimAfterLoad_RemovesEntryOnLoad(t *testing.T) { - s := mock.New() - s.Stopped["i-1"] = &vm.VM{ID: "i-1"} - s.ClaimAfterLoad = true - - got, err := s.LoadStoppedInstance("i-1") - require.NoError(t, err) - require.NotNil(t, got) - assert.Equal(t, []string{"i-1"}, s.ClaimedStopped) - - _, err = s.ClaimStoppedInstance("i-1") - assert.ErrorIs(t, err, vm.ErrStoppedInstanceClaimed) -} - -func TestDeleteStoppedInstance_FailFirstThenSucceeds(t *testing.T) { - s := mock.New() - s.Stopped["i-1"] = &vm.VM{ID: "i-1"} - s.DeleteFailFirst = true - - require.Error(t, s.DeleteStoppedInstance("i-1")) - require.NoError(t, s.DeleteStoppedInstance("i-1")) - assert.Equal(t, 2, s.DeleteAttempts) - assert.Contains(t, s.DeletedStopped, "i-1") -} - -func TestUpdateStoppedInstance_MissingReturnsErrKeyNotFound(t *testing.T) { - s := mock.New() - _, err := s.UpdateStoppedInstance("i-missing", func(*vm.VM) {}) - assert.ErrorIs(t, err, jetstream.ErrKeyNotFound) - assert.Equal(t, 1, s.UpdateStoppedCalls) -} - -func TestWriteStoppedInstance_ErrInjectionAndHook(t *testing.T) { - s := mock.New() - var hookCalls int - s.OnWriteStoppedInstance = func(id string, v *vm.VM) { hookCalls++ } - - require.NoError(t, s.WriteStoppedInstance("i-1", &vm.VM{ID: "i-1"})) - assert.Equal(t, 1, hookCalls) - assert.NotNil(t, s.WroteStopped["i-1"]) - // A write records only; it must not add to the claimable pool, or a - // rollback would let a second caller claim the same instance. - assert.Nil(t, s.Stopped["i-1"]) - _, err := s.ClaimStoppedInstance("i-1") - assert.ErrorIs(t, err, vm.ErrStoppedInstanceClaimed) - - s.WriteStoppedErr = errors.New("kv down") - err = s.WriteStoppedInstance("i-2", &vm.VM{ID: "i-2"}) - assert.EqualError(t, err, "kv down") -} - -func TestUpdateTerminatedInstance_MissingErrors(t *testing.T) { - s := mock.New() - _, err := s.UpdateTerminatedInstance("i-missing", func(*vm.VM) {}) - require.Error(t, err) -}