Skip to content

Commit

Permalink
unit test for cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindu.dodanduwa@elastic.co>
  • Loading branch information
Kavindu-Dodan committed Nov 19, 2024
1 parent 383cb25 commit c129de6
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions x-pack/filebeat/input/awss3/states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package awss3

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -123,3 +124,73 @@ func TestStatesAddStateAndIsProcessed(t *testing.T) {
})
}
}

func TestStatesCleanUp(t *testing.T) {
bucketName := "test-bucket"
lModifiedTime := time.Unix(0, 0)
stateA := newState(bucketName, "a", "a-etag", lModifiedTime)
stateB := newState(bucketName, "b", "b-etag", lModifiedTime)
stateC := newState(bucketName, "c", "c-etag", lModifiedTime)

tests := []struct {
name string
initStates []state
knownIDs []string
expectIDs []string
}{
{
name: "No cleanup if not missing from known list",
initStates: []state{stateA, stateB, stateC},
knownIDs: []string{stateA.ID(), stateB.ID(), stateC.ID()},
expectIDs: []string{stateA.ID(), stateB.ID(), stateC.ID()},
},
{
name: "Clean up if missing from known list",
initStates: []state{stateA, stateB, stateC},
knownIDs: []string{stateA.ID()},
expectIDs: []string{stateA.ID()},
},
{
name: "Clean up everything",
initStates: []state{stateA, stateC}, // given A, C
knownIDs: []string{stateB.ID()}, // but known B
expectIDs: []string{}, // empty state & store
},
{
name: "Empty known IDs are valid",
initStates: []state{stateA}, // given A
knownIDs: []string{}, // Known nothing
expectIDs: []string{}, // empty state & store
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
store := openTestStatestore()
statesInstance, err := newStates(nil, store)
require.NoError(t, err, "states creation must succeed")

for _, s := range test.initStates {
err := statesInstance.AddState(s)
require.NoError(t, err, "state initialization must succeed")
}

// perform cleanup
err = statesInstance.CleanUp(test.knownIDs)
require.NoError(t, err, "state cleanup must succeed")

// validate
for _, id := range test.expectIDs {
// must be in local state
_, ok := statesInstance.states[id]
require.True(t, ok, fmt.Errorf("expected id %s in state, but got missing", id))

// must be in store
ok, err := statesInstance.store.Has(getStoreKey(id))
require.NoError(t, err, "state has must succeed")
require.True(t, ok, fmt.Errorf("expected id %s in store, but got missing", id))
}
})
}

}

0 comments on commit c129de6

Please sign in to comment.