Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add coverage for verifying inclusion against massifs after the… #31

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
42 changes: 26 additions & 16 deletions tests/integrationsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ func (s *IntegrationTestSuite) EnsureAzuriteEnv() {
}
}

func (s *IntegrationTestSuite) ReplaceStdIO() {
var err error
require := s.Require()
require.NotNil(s.origStdin)
s.restoreStdIO()
s.stdinReader, s.stdinWriter, err = os.Pipe()
require.NoError(err)
os.Stdin = s.stdinReader
// Note, we don't mess with stdout
}

func (s *IntegrationTestSuite) restoreStdIO() {
os.Stdin = s.origStdin

if s.stdinWriter != nil {
s.stdinWriter.Close()
}
if s.stdinReader != nil {
s.stdinReader.Close()
}
s.stdinWriter = nil
s.stdinReader = nil
}

// BeforeTest is run before the test
//
// It gets the correct suite wide test environment
Expand All @@ -89,12 +113,7 @@ func (s *IntegrationTestSuite) BeforeTest(suiteName, testName string) {

var err error
require := s.Require()

s.stdinReader, s.stdinWriter, err = os.Pipe()
require.NoError(err)
require.NotNil(s.origStdin)
os.Stdin = s.stdinReader
// Note, we don't mess with stdout
s.ReplaceStdIO()

logger.New("NOOP")
defer logger.OnExit()
Expand All @@ -109,14 +128,5 @@ func (s *IntegrationTestSuite) BeforeTest(suiteName, testName string) {
// Currently used to print useful information for failing tests
func (s *IntegrationTestSuite) AfterTest(suiteName, testName string) {

os.Stdin = s.origStdin

if s.stdinWriter != nil {
s.stdinWriter.Close()
}
if s.stdinReader != nil {
s.stdinReader.Close()
}
s.stdinWriter = nil
s.stdinReader = nil
s.restoreStdIO()
}
3 changes: 1 addition & 2 deletions tests/replicatelogs/replicatelogs_prod_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build integration && azurite
//go:build integration

package verifyconsistency

Expand All @@ -14,7 +14,6 @@ func (s *ReplicateLogsCmdSuite) TestReplicateFirstPublicMassif() {

replicaDir := s.T().TempDir()

// NOTE: These will fail in the CI until the prod APIM principal gets the new custom role
app := veracity.NewApp("tests", false)
veracity.AddCommands(app, false)

Expand Down
100 changes: 100 additions & 0 deletions tests/verifyincluded/verifyevents_azurite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//go:build integration && azurite

package verifyevents

import (
"fmt"
"strings"

"github.com/datatrails/go-datatrails-common/logger"
"github.com/datatrails/go-datatrails-logverification/integrationsupport"
"github.com/datatrails/go-datatrails-merklelog/mmr"
"github.com/datatrails/go-datatrails-merklelog/mmrtesting"
"github.com/datatrails/go-datatrails-simplehash/simplehash"
"github.com/datatrails/veracity"
"github.com/stretchr/testify/require"
)

func (s *VerifyEventsSuite) newMMRTestingConfig(labelPrefix, tenantIdentity string) mmrtesting.TestConfig {
return mmrtesting.TestConfig{
StartTimeMS: (1698342521) * 1000, EventRate: 500,
TestLabelPrefix: labelPrefix,
TenantIdentity: tenantIdentity,
Container: strings.ReplaceAll(strings.ToLower(labelPrefix), "_", ""),
}
}

// TestVerifyIncludedMultiMassif tests that the veracity sub command verify-included
// works for massifs beyond the first one and covers some obvious edge cases.
func (s *VerifyEventsSuite) TestVerifyIncludedMultiMassif() {
logger.New("TestVerifyIncludedMultiMassif")
defer logger.OnExit()

cfg := s.newMMRTestingConfig("TestVerifyIncludedMultiMassif", "")
azurite := mmrtesting.NewTestContext(s.T(), cfg)

massifHeight := uint8(8)
leavesPerMassif := mmr.HeightIndexLeafCount(uint64(massifHeight) - 1)

tests := []struct {
name string
massifCount uint32
// leaf indices to verify the inclusion of.
leaves []uint64
}{
// make sure we cover the obvious edge cases
{name: "single massif first few and last few", massifCount: 1, leaves: []uint64{0, 1, 2, leavesPerMassif - 2, leavesPerMassif - 1}},
{name: "2 massifs, last of first and first of last", massifCount: 2, leaves: []uint64{leavesPerMassif - 1, leavesPerMassif}},
{name: "5 massifs, first and last of each", massifCount: 5, leaves: []uint64{
0, leavesPerMassif - 1,
1 * leavesPerMassif, 2*leavesPerMassif - 1,
2 * leavesPerMassif, 3*leavesPerMassif - 1,
3 * leavesPerMassif, 4*leavesPerMassif - 1,
}},
}

// note: VERACITY_IKWID is set in main, we need it to enable --envauth so we force it here
app := veracity.NewApp("tests", true)
veracity.AddCommands(app, true)

for _, tt := range tests {

massifCount := tt.massifCount
s.Run(fmt.Sprintf("massifCount:%d", massifCount), func() {

leafHasher := integrationsupport.NewLeafHasher()
g := integrationsupport.NewTestGenerator(
s.T(), cfg.StartTimeMS/1000, &leafHasher, mmrtesting.TestGeneratorConfig{
StartTimeMS: cfg.StartTimeMS,
EventRate: cfg.EventRate,
TenantIdentity: cfg.TenantIdentity,
TestLabelPrefix: cfg.TestLabelPrefix,
})

tenantId0 := g.NewTenantIdentity()
events := integrationsupport.GenerateTenantLog(
&azurite, g, int(tt.massifCount)*int(leavesPerMassif), tenantId0, true,
massifHeight,
)

for _, iLeaf := range tt.leaves {
marshaler := simplehash.NewEventMarshaler()
eventJson, err := marshaler.Marshal(events[iLeaf])
require.NoError(s.T(), err)
s.StdinWriteAndClose(eventJson)

err = app.Run([]string{
"veracity",
"--envauth", // uses the emulator
"--container", cfg.Container,
"--data-url", s.Env.AzuriteVerifiableDataURL,
"--tenant", tenantId0,
"--height", fmt.Sprintf("%d", massifHeight),
"verify-included",
})
s.NoError(err)
s.ReplaceStdIO() // reset stdin for write & close
}
})
}
}
Loading