Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ func AddCommands(app *cli.App, ikwid bool) *cli.App {
app.Commands = append(app.Commands, NewReplicateLogsCmd())
app.Commands = append(app.Commands, NewReceiptCmd())

app.Commands = append(app.Commands, NewFindTrieEntriesCmd())
app.Commands = append(app.Commands, NewFindMMREntriesCmd())

if ikwid {
app.Commands = append(app.Commands, NewMassifsCmd())
app.Commands = append(app.Commands, NewLogTailCmd())
app.Commands = append(app.Commands, NewEventDiagCmd())
app.Commands = append(app.Commands, NewDiagCmd())
app.Commands = append(app.Commands, NewNodeScanCmd())
app.Commands = append(app.Commands, NewFindTrieEntriesCmd())
app.Commands = append(app.Commands, NewFindMMREntriesCmd())
}
return app
}
6 changes: 3 additions & 3 deletions findmmrentries.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func findMMREntries(
// NOTE: the leaf index and trie index are equivilent.
mmrLeafEntries := massifContext.MassifLeafCount()

log.Debugf("checking %v trie entries in massif %v for matches", mmrLeafEntries, massifIndex)
log.Debugf("checking %v mmr entries in massif %v for matches", mmrLeafEntries, massifIndex)

// check each mmr leaf entry for matching mmr entry
for range mmrLeafEntries {
Expand Down Expand Up @@ -201,12 +201,12 @@ func NewFindMMREntriesCmd() *cli.Command {
},
&cli.Int64Flag{
Name: massifRangeStartFlagName,
Usage: "if set, start the search for matching trie entries at the massif at this given massif index. if omitted will start search at massif 0.",
Usage: "if set, start the search for matching mmr entries at the massif at this given massif index. if omitted will start search at massif 0.",
Value: 0,
},
&cli.Int64Flag{
Name: massifRangeEndFlagName,
Usage: "if set, end the search for matching trie entries at the massif at this given massif index. if omitted will end search at the last massif.",
Usage: "if set, end the search for matching mmr entries at the massif at this given massif index. if omitted will end search at the last massif.",
Value: -1,
},
},
Expand Down
226 changes: 226 additions & 0 deletions tests/findmmrentries/findmmrentries_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package findmmrentries

import (
"io"
"os"
"strings"

"github.com/datatrails/veracity"
"github.com/datatrails/veracity/tests/katdata"
)

const (
prodPublicTenant = "tenant/6ea5cd00-c711-3649-6914-7b125928bbb4"
prodEventsv1Tenant = "tenant/97e90a09-8c56-40df-a4de-42fde462ef6f"
)

// TestAssetsV2EventStdIn tests we can find
// the correct PROD public assetsv2 event mmr entry match
func (s *FindMMREntriesSuite) TestAssetsV2EventStdIn() {
assert := s.Assert()
require := s.Require()

app := veracity.NewApp("version", true)
veracity.AddCommands(app, true)

// note: the suite does a before & after pipe for Stdin
s.StdinWriteAndClose(katdata.KnownGoodPublicAssetsV2EventLaterMassif)

// redirect std out to a known pipe so we can capture it
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }() // ensure we redirect std out back after

reader, writer, _ := os.Pipe()
os.Stdout = writer

err := app.Run([]string{
"veracity",
"find-mmr-entries",
"--log-tenant", prodPublicTenant,
})
assert.NoErrorf(err, "the event is a known good event from the public production tenant, yet we have errored trying to find the mmr entries")

writer.Close()
actualBytes, err := io.ReadAll(reader)
require.NoError(err)

// convert the stdout to a string and strip the newlines
actual := strings.ReplaceAll(string(actualBytes), "\n", "")

assert.Equal("matches: [27899]", actual)
}

// TestAssetsV2EventAsLeafIndexStdIn tests we can find
// the correct PROD public assetsv2 event mmr entry match as
// a leaf index.
func (s *FindMMREntriesSuite) TestAssetsV2EventAsLeafIndexStdIn() {
assert := s.Assert()
require := s.Require()

app := veracity.NewApp("version", true)
veracity.AddCommands(app, true)

// note: the suite does a before & after pipe for Stdin
s.StdinWriteAndClose(katdata.KnownGoodPublicAssetsV2EventLaterMassif)

// redirect std out to a known pipe so we can capture it
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }() // ensure we redirect std out back after

reader, writer, _ := os.Pipe()
os.Stdout = writer

err := app.Run([]string{
"veracity",
"find-mmr-entries",
"--log-tenant", prodPublicTenant,
"--massif-start", "1",
"--massif-end", "1", // the event is in massif 1
"--as-leafindexes", "true",
})
assert.NoErrorf(err, "the event is a known good event from the public production tenant, yet we have errored trying to find the mmr entries")

writer.Close()
actualBytes, err := io.ReadAll(reader)
require.NoError(err)

// convert the stdout to a string and strip the newlines
actual := strings.ReplaceAll(string(actualBytes), "\n", "")

assert.Equal("matches: [13952]", actual)
}

// TestAssetsV2EventWrongMassifStdIn tests we CANNOT find
// the correct PROD public assetsv2 event mmr entry match
// if we set the range of massifs to not include the massif the event is in.
func (s *FindMMREntriesSuite) TestAssetsV2EventWrongMassifStdIn() {
assert := s.Assert()
require := s.Require()

app := veracity.NewApp("version", true)
veracity.AddCommands(app, true)

// note: the suite does a before & after pipe for Stdin
s.StdinWriteAndClose(katdata.KnownGoodPublicAssetsV2EventLaterMassif)

// redirect std out to a known pipe so we can capture it
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }() // ensure we redirect std out back after

reader, writer, _ := os.Pipe()
os.Stdout = writer

err := app.Run([]string{
"veracity",
"find-mmr-entries",
"--log-tenant", prodPublicTenant,
"--massif-start", "0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this test, But setting the range to be zero length will likely excersise the short circute do nothing path. Just take this as a minor comment, I think these tests are great. Its hard to be sure we have enough massifs to do a non zero scan that ommits the massif containing the event

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we need to ramp up the public events :P I will revist the test when we have a later massifs in the prod public tenant.

It doesn't short circuit, it considers the first massif only (index 0) and there are two massifs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although i agree that this test won't catch a short circuit if its introduced at a later date, so its not really a regression test for short circuiting at the 0 massif

"--massif-end", "0", // the actual event is in massif 1
})
assert.NoErrorf(err, "the event is a known good event from the public production tenant, yet we have errored trying to find the mmr entries")

writer.Close()
actualBytes, err := io.ReadAll(reader)
require.NoError(err)

// convert the stdout to a string and strip the newlines
actual := strings.ReplaceAll(string(actualBytes), "\n", "")

assert.Equal("matches: []", actual)
}

// TestAssetsV2EventCorrectMassifStdIn tests we CAN find
// the correct PROD public assetsv2 event mmr entry match
// if we set the range of massifs to include ONLY the massif the event is in.
func (s *FindMMREntriesSuite) TestAssetsV2EventCorrectMassifStdIn() {
assert := s.Assert()
require := s.Require()

app := veracity.NewApp("version", true)
veracity.AddCommands(app, true)

// note: the suite does a before & after pipe for Stdin
s.StdinWriteAndClose(katdata.KnownGoodPublicAssetsV2EventLaterMassif)

// redirect std out to a known pipe so we can capture it
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }() // ensure we redirect std out back after

rescueStderr := os.Stderr
defer func() { os.Stderr = rescueStderr }() // ensure we redirect std err back after

readerStdOut, writerStdOut, _ := os.Pipe()
os.Stdout = writerStdOut

readerStdErr, writerStdErr, _ := os.Pipe()
os.Stderr = writerStdErr

err := app.Run([]string{
"veracity",
"--loglevel", "DEBUG",
"find-mmr-entries",
"--log-tenant", prodPublicTenant,
"--massif-start", "1",
"--massif-end", "1", // the actual event is in massif 1
})
assert.NoErrorf(err, "the event is a known good event from the public production tenant, yet we have errored trying to find the mmr entries")

writerStdOut.Close()
actualStdOutBytes, err := io.ReadAll(readerStdOut)
require.NoError(err)

writerStdErr.Close()
actualStdErrBytes, err := io.ReadAll(readerStdErr)
require.NoError(err)

// convert the stdout to string and string new lines and convert stderr to string
actualStdOut := strings.ReplaceAll(string(actualStdOutBytes), "\n", "")
actualStdErr := string(actualStdErrBytes)

// assert we are checking the correct massif
assert.Contains(actualStdErr, "mmr entries in massif 1 for matches")

// assert we are not checking the neighbouring massifs
assert.NotContains(actualStdErr, "mmr entries in massif 0 for matches")
assert.NotContains(actualStdErr, "mmr entries in massif 2 for matches")

assert.Equal("matches: [27899]", actualStdOut)
}

// TestEventsV1EventRepeatedAppDataStdIn tests we can find
// the correct PROD eventsv1 event mmr entry matches for app data used
// for 2 events on the same log tenant.
func (s *FindMMREntriesSuite) TestEventsV1EventRepeatedAppDataStdIn() {
assert := s.Assert()
require := s.Require()

app := veracity.NewApp("version", true)
veracity.AddCommands(app, true)

// note: the suite does a before & after pipe for Stdin
s.StdinWriteAndClose(katdata.KnownGoodEventsv1RepeatedAppData)

// redirect std out to a known pipe so we can capture it
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }() // ensure we redirect std out back after

reader, writer, _ := os.Pipe()
os.Stdout = writer

err := app.Run([]string{
"veracity",
"find-mmr-entries",
"--log-tenant", prodEventsv1Tenant,
})
assert.NoErrorf(err, "the event is a known good event from the production tenant we are using for test eventsv1 events, yet we have errored trying to find the mmr entries")

writer.Close()
actualBytes, err := io.ReadAll(reader)
require.NoError(err)

// convert the stdout to a string and strip the newlines
actual := strings.ReplaceAll(string(actualBytes), "\n", "")

// check we get back matches mmr indexes 26 and 31
assert.Equal("matches: [26 31]", actual)
}
18 changes: 18 additions & 0 deletions tests/findmmrentries/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package findmmrentries

import (
"testing"

"github.com/datatrails/veracity/tests"
"github.com/stretchr/testify/suite"
)

// FindMMREntriesSuite deals with tests around finding mmr entries
type FindMMREntriesSuite struct {
tests.IntegrationTestSuite
}

func TestFindMMREntriesSuite(t *testing.T) {

suite.Run(t, new(FindMMREntriesSuite))
}
Loading
Loading