/
test_utils.go
187 lines (160 loc) · 4.6 KB
/
test_utils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package tests
import (
"context"
"database/sql"
"fmt"
"math/big"
"strconv"
"strings"
"testing"
"time"
"cloud.google.com/go/storage"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
"github.com/tablelandnetwork/basin-storage/pkg/ethereum"
"github.com/textileio/go-tableland/pkg/wallet"
"golang.org/x/crypto/sha3"
)
const functionsPort = "8293"
func uploadRandomBytesToGCS(t *testing.T, data []byte, bucketName, objectName string) {
ctx := context.Background()
// Create a client
client, err := storage.NewClient(ctx)
require.NoError(t, err)
defer func() {
require.NoError(t, client.Close())
}()
// Get a handle to the bucket and the object
bucket := client.Bucket(bucketName)
object := bucket.Object(objectName)
// Create a writer and write the random bytes
wc := object.NewWriter(ctx)
_, err = wc.Write(data)
require.NoError(t, err)
require.NoError(t, wc.Close())
// Set the metadata
metadata := map[string]string{
"signature": "25ee57b44817278828f3ad3f47dfe440cf2f729524b7ae445a933cf78e22d8583084048b47676f8c64daae85b937dda79ee2596b924710eebbff94652e5e2f9500", // nolint
"hash": "f00a989b4f86fd3bd6d347b03c59bba377bcaac57f3b43addfad9da1bca51938", // nolint
}
attrs := storage.ObjectAttrsToUpdate{
Metadata: metadata,
}
_, err = object.Update(ctx, attrs)
require.NoError(t, err)
}
func deleteObjectFromGCS(t *testing.T, bucketName, objectName string) {
ctx := context.Background()
// Create a client
client, err := storage.NewClient(ctx)
require.NoError(t, err)
defer func() {
require.NoError(t, client.Close())
}()
// Get a handle to the bucket and the object
bucket := client.Bucket(bucketName)
object := bucket.Object(objectName)
// Delete the object
err = object.Delete(ctx)
require.NoError(t, err)
}
func setupDB(t *testing.T, db *sql.DB) {
_, err := db.Exec("CREATE DATABASE IF NOT EXISTS basin_test")
require.NoError(t, err)
_, err = db.Exec(
`CREATE TABLE IF NOT EXISTS namespaces
(
id BIGSERIAL PRIMARY KEY,
name VARCHAR(32) UNIQUE NOT NULL,
owner BYTEA NOT NULL,
last_export TIMESTAMPTZ
)`)
require.NoError(t, err)
_, err = db.Exec("INSERT INTO namespaces (name, owner) VALUES ('esfbmltndstj', 'test_owner')")
require.NoError(t, err)
_, err = db.Exec(
`CREATE TABLE IF NOT EXISTS jobs
(
id BIGSERIAL PRIMARY KEY,
ns_id BIGINT,
cid BYTEA NOT NULL,
relation TEXT NOT NULL,
activated TIMESTAMP,
timestamp BIGINT,
cache_path TEXT,
expires_at TIMESTAMP,
signature bytea,
hash bytea,
CONSTRAINT fk_namespace
FOREIGN KEY(ns_id)
REFERENCES namespaces(id)
)`)
require.NoError(t, err)
_, err = db.Exec(
`CREATE TABLE IF NOT EXISTS cache_config
(
ns_id BIGINT NOT NULL,
relation TEXT NOT NULL,
duration BIGINT,
CONSTRAINT fk_namespace
FOREIGN KEY(ns_id)
REFERENCES namespaces(id)
);`)
require.NoError(t, err)
}
func insertProcessedJob(t *testing.T, db *sql.DB) cid.Cid {
// already processed job with 2 deals
cidStr := "bafybeie62jygi4bi5m3yd43ttppkn5velvck5yutckggp6iwdmkkowyhxy"
cid, err := cid.Decode(cidStr)
require.NoError(t, err)
_, err = db.Exec(
`INSERT INTO jobs (ns_id, cid, relation)
VALUES (
(SELECT id FROM namespaces WHERE name = 'esfbmltndstj'),
$1,
'ksvraapqfiyf'
)`, cid.Bytes())
require.NoError(t, err)
return cid
}
func createPub(t *testing.T, pk, chainIDStr, backendURL, basinStorageAddr string) {
wallet, err := wallet.NewWallet(pk)
require.NoError(t, err)
backend, err := ethclient.DialContext(context.Background(), backendURL)
require.NoError(t, err)
addr, err := common.NewMixedcaseAddressFromString(basinStorageAddr)
require.NoError(t, err)
chainID, err := strconv.ParseUint(chainIDStr, int(10), 64)
require.NoError(t, err)
contract, err := ethereum.NewContract(
addr.Address(),
backend,
)
require.NoError(t, err)
txOpts, err := bind.NewKeyedTransactorWithChainID(
wallet.PrivateKey(),
big.NewInt(int64(chainID)),
)
require.NoError(t, err)
res, err := contract.CreatePub(
txOpts,
wallet.Address(),
"esfbmltndstj.ksvraapqfiyf",
)
if err != nil {
hasher := sha3.NewLegacyKeccak256()
hasher.Write([]byte("PubAlreadyExists(string)"))
hash := hasher.Sum(nil)
if !strings.Contains(err.Error(), fmt.Sprintf("0x%x", hash[:4])) {
// ignores error if pub already exists error
require.NoError(t, err)
}
}
if res != nil {
// wait for the transaction to be included in a block
time.Sleep(150 * time.Second)
}
}