-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodescan_test.go
101 lines (84 loc) · 3.42 KB
/
nodescan_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//go:build integration && azurite
package veracity
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"
"github.com/datatrails/go-datatrails-common/logger"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"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/veracitytesting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestConfigReaderForEmulator tests that we can run the veracity tool against
// emulator urls.
func TestNodeScanCmd(t *testing.T) {
logger.New("TestVerifyList")
defer logger.OnExit()
url := os.Getenv("TEST_INTEGRATION_FORESTRIE_BLOBSTORE_URL")
logger.Sugar.Infof("url: '%s'", url)
// Create a single massif in the emulator
tenantID := mmrtesting.DefaultGeneratorTenantIdentity
testContext, testGenerator, cfg := veracitytesting.NewAzuriteTestContext(t, "TestNodeScanCmd")
eventsResponse := veracitytesting.GenerateTenantLog(&testContext, testGenerator, 10, tenantID, true, massifHeight, LeafTypePlain)
marshaledEvents, err := marshalEventsList(eventsResponse)
require.NoError(t, err)
// Arbitrarily chose to look for leaf 7
leafIndex := 7
// We could try an capture the output from the tool and check this, but that
// sort of thing is extensively checked elsewhere. Here we are just
// confirming the derived leaf hash is matched in the blob. WHich is the
// purpose of the diagnostic tool. But we log the expected mmrIndex for development ease.
fmt.Printf("MMRIndex(%d) == %d\n", leafIndex, mmr.MMRIndex(uint64(leafIndex)))
// Get the idtimestamp that ensures every entry is unique
idTimestamp, _, err := massifs.SplitIDTimestampHex(
eventsResponse[leafIndex].MerklelogEntry.Commit.Idtimestamp)
require.NoError(t, err)
// Produce the expected leaf hash for the tree entry for this event
simplehashv3Hasher := simplehash.NewHasherV3()
err = simplehashv3Hasher.HashEventFromJSON(
marshaledEvents[leafIndex],
simplehash.WithPrefix([]byte{LeafTypePlain}),
simplehash.WithIDCommitted(idTimestamp))
require.NoError(t, err)
expectedLeafNodeValue := fmt.Sprintf("%x", simplehashv3Hasher.Sum(nil))
tests := []struct {
testArgs []string
}{
// match the expected leaf hash by scanning massif 0. typically this is
// useful only as a diagnostic tool or triage tool. typically the
// precise node is located by the mmrIndex and the leafIndex derived
// from that.
{testArgs: []string{
"<progname>", "-u", "-", "-s", "devstoreaccount1", "-c", cfg.Container, "-t", tenantID,
"nodescan", "-m", "0", "-v", expectedLeafNodeValue}},
}
for _, tc := range tests {
t.Run(strings.Join(tc.testArgs, " "), func(t *testing.T) {
app := AddCommands(NewApp("version", true), true)
ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond)
err := app.RunContext(ctx, tc.testArgs)
cancel()
assert.NoError(t, err)
})
}
}
func marshalEventsList(eventsResponse []*v2assets.EventResponse) ([][]byte, error) {
marshaller := v2assets.NewFlatMarshalerForEvents()
eventJsonList := make([][]byte, 0)
for _, event := range eventsResponse {
eventJson, err := marshaller.Marshal(event)
if err != nil {
return nil, err
}
eventJsonList = append(eventJsonList, eventJson)
}
return eventJsonList, nil
}